Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON.NET Case Insensitive Deserialization not working

I need to deserialize some JSON into my object where the casing of the JSON is unknown/inconsistent. JSON.NET is supposed to be case insensitive but it not working for me.

My class definition:

public class MyRootNode
{
    public string Action {get;set;}
    public MyData Data {get;set;}
}

public class MyData
{
    public string Name {get;set;}
}

The JSON I receive has Action & Data in lowercase and has the correct casing for MyRootNode.

I'm using this to deserialize:

MyRootNode ResponseObject = JsonConvert.DeserializeObject<MyRootnode>(JsonString);

It returns to be an initialised MyRootNode but the Action and Data properties are null.

Any ideas?

EDIT: Added JSON

{
   "MyRootNode":{
      "action":"PACT",
      "myData":{
         "name":"jimmy"
      }
   }
}
like image 530
CathalMF Avatar asked Aug 20 '17 12:08

CathalMF


People also ask

Is Newtonsoft JSON deserialize case sensitive?

By default Newtonsoft does case insensitive JSON deserialization and System. Text. Json does case sensitive JSON deserialization. Case sensitivity comes into play when a JSON string is being deserialized into an object.

Are JSON case sensitive?

JSON is case-sensitive. You must refer to SQL names in JSON code using the correct case: uppercase SQL names must be written as uppercase.

Is StringEnumConverter case sensitive?

Jericho commented on Nov 25, 2017 Up until Json.net v9, the StringEnumConverter class was case insensitive and it was changed in v10. Here's the discussion explaining why it was changed and here's the commit released in v10 that changed the behavior to case-sensitive.

Is Jobject case sensitive?

Refer to this answer that this is wanted according the JSON-RPC spec (keys are case sensitive!).


2 Answers

This is the .NET Core built-in JSON library.

I found another way of doing it.. just in case, somebody is still looking for a cleaner way of doing it. Assume there exists a Movie class

    using System.Text.Json;

. . .

    var movies = await JsonSerializer.DeserializeAsync
        <IEnumerable<Movie>>(responseStream,
        new JsonSerializerOptions
        {
            PropertyNameCaseInsensitive = true
        });

Startup Options:

You can also configure at the time of application startup using the below extension method.

    public void ConfigureServices(IServiceCollection services)
    {

        services.AddControllers()
            .AddJsonOptions(
                x =>
                {
                    x.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
                });
    }
like image 155
Ziaullah Khan Avatar answered Nov 08 '22 03:11

Ziaullah Khan


Simply add JsonProperty attribute and set jsonProperty name

public class MyRootNode
{
    [JsonProperty(PropertyName = "action")]
    public string Action {get;set;}
    [JsonProperty(PropertyName = "myData")]
    public MyData Data {get;set;}
}

public class MyData
{
    [JsonProperty(PropertyName = "name")]
    public string Name {get;set;}
}

UPD: and yes, add some base type as @mjwills suggest

like image 24
demo Avatar answered Nov 08 '22 03:11

demo