-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JSON properties get camelCased on serialization #1719
Comments
You need to override the serializer options like client.UseSystemTextJson(new JsonSerialializerOptions(JsonSerializerDefaults.General)); It's mentioned in the docs, and for Newtonsoft.Json defaults are directly listed, so you should understand what to change if you need to. RestSharp 107 doesn't have any wicked rules, it just calls the serializer. The fact that v107 uses System.Text.Json serializer by default is documented. |
@alexeyzimarev Thanks for the quick reply. I'm a bit confused, on several points:
Have things always behaved this way? I've used RestSharp in just about all of my projects for many years and have never come across this, but maybe I just never had to deal with a case-sensitive API before? (I'm talking to Okta, FWIW.) Here is some working code that restores what I would consider 'classic' RestSharp / Newtonsoft behavior:
This was tested with RestSharp 107.1.1 and Newtonsoft.Json 13.0.1 running under .NET 4.8. If nothing has changed on your end, maybe something changed in Newtonsoft and System.Text.Json? The |
Don't get me wrong, but I don't think you have read the docs following the link I provided. The default serializer for RestSharp is using I posted a direct link to JsonSerializerSettings DefaultSettings = new JsonSerializerSettings {
ContractResolver = new CamelCasePropertyNamesContractResolver(),
DefaultValueHandling = DefaultValueHandling.Include,
TypeNameHandling = TypeNameHandling.None,
NullValueHandling = NullValueHandling.Ignore,
Formatting = Formatting.None,
ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor
}; It also says:
Explicitly setting the As you can see there, the RestSharp default is camel case. As NewtonsoftJson itself is properly documented, it should not be an issue to reconfigure it in a way you want. Both System.Text.Json and Newtonsoft.Json serializer exist for almost two years with exactly these defaults, there was no change at all during the v107 release. The only thing that really changed is that SimpleJson was removed for good. |
Ah, I see the RestSharp default ContractResolver now. Sorry, I somehow overlooked that. And looking at the GIT history, it's been there for at least two years. My bad. What's the thinking on forcing an explicit casing on outbound calls? I would think it would be better to use attributes like I'm not looking for any kind of standard casing, camel or otherwise. Okta uses camel on default properties but custom properties can be anything, and they have to match. I can now initialize Newtonsoft with I would think that calling PS, thanks for all your hard work on this project! I don't mean to come across as unappreciative. |
Well, it's because Newtonsoft.Json is not the default serializer. The decision to choose the camel case is, again, because most JSON APIs today use camel case as it is a de-facto standard for JavaScript. That's why the Concerning |
Got it. Thanks for your time and attention! |
When adding a Json body to a request using request.AddJsonBody(), it seems that properties are converted to camelCase on serialization. This is happening on both the 106.x and 107.x branches. On the 106.x branch, it happens with both SimpleJson and Newtonsoft.Json sterilizers. With Newtonsoft, [JsonProperty("Name")] attributes are correctly read, but the camelCasing takes place after that.
The rules seem to be: if the first character is uppercase, downcase that character and all subsequent uppercase character, and stop when you come to a uppercase character followed by a lowercase or non-word character. For example: XXXProfile --> xxxProfile
When using
client.UseNewtonsoftJson()
, this casing change happens even though calling the serializer directly ex:Newtonsoft.Json.JsonConvert.SerializeObject(obj)
produces the correct results.To Reproduce
With RestSharp 107.1.1 and
client.UseNewtonsoftJson()
, this is what is serialized and sent:With RestSharp 107.1.1 and NOT explicitly calling
UseNewtonsoftJson()
, we get this:These are the results from calling
Newtonsoft.Json.JsonConvert.SerializeObject(obj)
:Testing is a little harder in 107.x since there is no longer a request.Body property with the post-serialization text. I am getting the serialization by capturing actual traffic with Fiddler.
Expected behavior
Objects are serialized exactly as defined in the [JsonProperty], or lacking that, the property name.
Desktop (please complete the following information):
The text was updated successfully, but these errors were encountered: