Skip to content
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

CamelCase serialization doesn't work on System.Object after upgrading to: 110.2.0 #2150

Open
SebastianJust opened this issue Oct 18, 2023 · 4 comments

Comments

@SebastianJust
Copy link

SebastianJust commented Oct 18, 2023

Hi,

I've newly upgraded to version: 110.2.0.
After the upgrade, it will not serialize the data as camelCase, when using:

RestResponse response = await _restClient.ExecuteGetAsync (new RestRequest("/api/users")
.AddHeader("Authorization", string.Format("Bearer {0}", token)));
object result = response.Data;
image

The object is still PascalCase.

I've tried to override the existing configuration by using the 'UseNewtonsoftJson' without any progress.

RestClient client = new RestClient(url, configureSerialization: s => s.UseNewtonsoftJson());

In addtion: I've also tried:
string json = JsonConvert.SerializeObject(response.Data, new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
Formatting = Formatting.Indented
});

Secondly i've also tried to use the 'UseSystemTextJson'. Here is the response:
{
"valueKind": 2
}

Can you please help me how to fix it? Are there anyway I can configure it as it was in release: 106.15?

@alexeyzimarev
Copy link
Member

alexeyzimarev commented Oct 22, 2023

I don't see any serialisation and deserialisation being linked in one execution path. The sample code is deserialising something, and I can't see where the serialisation issue occurs. As your deserialisation result isn't typed, you are basically parsing JSON, and it works as expected. The server returns pascal case, and that's what you get.

@SebastianJust
Copy link
Author

SebastianJust commented Oct 23, 2023

@alexeyzimarev yes exactly I agree. But I think it might be the way it's deserialized that has been changed, when you returning: 'Data'. It looks like it was some kind of KeyValue before (In your previous versions).

I've made a work around, that works.

private static object DeserializeJson(this string json)
 {
     if(string.IsNullOrEmpty(json))
         return null;

     JToken jToken = JToken.Parse(json);

     return jToken.Type switch
     {
         JTokenType.Object => JsonConvert.DeserializeObject<ExpandoObject>(json),
         JTokenType.Array => JsonConvert.DeserializeObject<List<ExpandoObject>>(json),
         JTokenType.String => JsonConvert.DeserializeObject<string>(json),
         JTokenType.Boolean => JsonConvert.DeserializeObject<bool>(json),
         JTokenType.Bytes => JsonConvert.DeserializeObject<byte[]>(json),
         JTokenType.Date => JsonConvert.DeserializeObject<DateTime>(json),
         JTokenType.Guid => JsonConvert.DeserializeObject<Guid>(json),
         JTokenType.Integer => JsonConvert.DeserializeObject<int>(json),
         JTokenType.Uri => JsonConvert.DeserializeObject<string>(json),
         JTokenType.Float => JsonConvert.DeserializeObject<float>(json),
         _ => throw new NotImplementedException()
     };
 }

But that's not the proper way of wanna do it. So I was wondering if you have any extensions or anything, so I can get the same data as before?

@alexeyzimarev
Copy link
Member

Well, yes, before v107 the default serialiser was a fork of SimpleJson, and it created all sort of problems. It's been moved to System.Text.Json a while ago. You can find it mentioned in the docs.

@SebastianJust
Copy link
Author

Oh okay! Cool :) That makes sense. So do you have any extensions or something build in or, should I do something else?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants