How Can I deserialize a json into a structure?

Erick

I have a class mapping like this:

public class Settings
{
    [JsonProperty("id")]
    public string Id { get; set; }

    [JsonProperty("type")]
    public string Type { get; set; }

    [JsonProperty("content")]
    public ContentStructure Content { get; set; }
}


public struct ContentStructure
{
    public Content ContentClass;
    public string ContentString;

    public static implicit operator ContentStructure(Content content) => new ContentStructure { ContentClass = content };
    public static implicit operator ContentStructure(string @string) => new ContentStructure { ContentString = @string };
}


public class Content
{
    [JsonProperty("id")]
    public string Id { get; set; }

    [JsonProperty("duration")]
    public long Duration { get; set; }
}

When I attempt to deserialize the following JSON string:

{
    "id": "any_id",
    "type": "any_type",
    "content": {
        "id": "any_id",
        "duration": 1000
    }
}

I always get the deserialized settings object with the property settings.Content.ContentClass null, but whenever my JSON string has the property "content" as string (instead of an object) the structure field ContentString comes correctly. What I am doing wrong? How can I can convert the JSON string above correctly?

Peter Csala

Yet another solution could be to make use of the JsonSchema

First let's redefine your data model:

public abstract class Settings
{
    [JsonProperty("id")]
    public string Id { get; set; }

    [JsonProperty("type")]
    public string Type { get; set; }
}

public class SettingsV1 : Settings
{
    [JsonProperty("content")]
    public string Content { get; set; }
}

public class SettingsV2 : Settings
{
    [JsonProperty("content")]
    public Content Content { get; set; }
}

public class Content
{
    [JsonProperty("id")]
    public string Id { get; set; }

    [JsonProperty("duration")]
    public long Duration { get; set; }
}
  • Instead of having a middle man (ContentStructure) rather than you can have two separate Settings versions
  • The common fields are defined inside the abstract base class

Now, you can use these two versioned classes to define json schemas:

private static JSchema schemaV1;
private static JSchema schemaV2;

//...
var generator = new JSchemaGenerator();
schemaV1 = generator.Generate(typeof(SettingsV1));
schemaV2 = generator.Generate(typeof(SettingsV2));

Finally all you need to do is to do a preliminary check before calling the DeserializeObject with the proper type:

Settings settings = null;
var semiParsed = JObject.Parse(json);
if (semiParsed.IsValid(schemaV1))
{
    settings = JsonConvert.DeserializeObject<SettingsV1>(json);
}
else if (semiParsed.IsValid(schemaV2))
{
    settings = JsonConvert.DeserializeObject<SettingsV2>(json);
}
else
{
    throw new NotSupportedException("The provided json format is not supported");
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I deserialize a JSON array into a native .net data structure?

How can I deserialize JSON data to this class structure?

How can I deserialize complex json repsonse into easy-to-deserialize structure using Newtonsoft.Json?

How I can deserialize this json?

How can I choose what type to deserialize at runtime based on the structure of the json?

How can I deserialize json array?

In C#, how can i deserialize this json?

How Can I Deserialize this JSON string?

How can I Deserialize nested json with DataContractJsonSerializer?

I can't deserialize the JSON

How Can I Deserialize JSON Data Using C#?

How can I deserialize json object where in keys are present pointer?

How I can deserialize Json into Dictionary<String,List<String>>

How can I deserialize JSON with u' unicode markup in Java?

How to deserialize a JSON string so I can loop it in C#?

How can I get PureConfig to deserialize JSON as a JValue field?

How can I generically deserialize PropertyInfo with Json.NET?

How can I deserialize an invalid json ? Truncated list of objects

How can I deserialize a json dataset in .net core

How can I deserialize JSON when the object type is not known beforehand?

How can I deserialize a nested Json array to a dictionary?

How can I deserialize this JSON format using restsharp? (c#)

How can I deserialize a simple Json with square brackets in c#?

How can i deserialize my json in Flutter/dart

Newtonsoft JSON - How can I deserialize a custom collection?

How do I deserialize this type of data structure

Deserialize complex JSON structure

Can I deserialize a JSON array into properties of a class?

Can I Deserialize a specific field from JSON?