GithubHelp home page GithubHelp logo

jsonnetdiscriminator's Introduction

JsonNetDiscriminator

Lastest Version 0.0.1.375

What is JsonNetDiscriminator

This is a Newtonsoft JSON.NET extension that helps to face inheritance problems during JSON deserialization.

Suppose to have the following models.

public class Vehicle
{
    [JsonProperty(PropertyName = "color")]
    public string Color { get; set; }

    [JsonProperty(PropertyName = "dateOfRegistration")]
    public string DateOfRegistration { get; set; }
}

public class Van : Vehicle
{
    [JsonProperty(PropertyName = "carryingCapacity")]
    public decimal? CarryingCapacity { get; set; }
}

public class Car : Vehicle
{
    [JsonProperty(PropertyName = "ndoors")]
    public int? NumberOfDoors { get; set; }

    [JsonProperty(PropertyName = "engine")]
    public Engine CarEngine { get; set; }
}

Thanks to inheritance, in C# we don't have to declare a property that hold the information about the type of the object. Unluckly, JSON object declaration isn't as strict as C# one.

Now suppose that you have to deserialize the following JSON:

{ "color": "red", "dateOfRegistration": "2016-04-09", }

Looking at the object properties, you can infer that it could be deserialize into a Vehicle object.

And now?

{ "color": "red", "dateOfRegistration": "2016-04-09", "ndoors": 5 }

As we can see, into json there is the property ndoors which let us think about Car object but this isn't true into many other situations.

To have the certainty that we are speaking about a Car object, a discriminator property have to be added.

{ "type": "Car", "color": "red", "dateOfRegistration": "2016-04-09", "ndoors": 5 }

In this case, thanks to type property, we are sure that this JSON can be deserialize into a Car object.

And now JsonNetDiscriminator comes in handy.

You simply have to enrich Vehicle class with JsonPropertyDiscriminator attribute with the right matching types

[JsonPropertyDiscriminator("type", "Car", typeof(Car))]
[JsonPropertyDiscriminator("type", "Van", typeof(Van))]
public class Vehicle
{
    [JsonProperty(PropertyName = "color")]
    public string Color { get; set; }

    [JsonProperty(PropertyName = "dateOfRegistration")]
    public string DateOfRegistration { get; set; }
}

In this case we are saying that if the JSON contains the type property with its value Car, this JSON can be deserialize into a Car object. If the value is Van, into a Van object.

Calling JSON deserialization method

var vehicle = JsonConvert.DeserializeObject<Vehicle>(json, new JsonDiscriminatorConverter(typeof(Vehicle)));

vehicle will be a Car object.

jsonnetdiscriminator's People

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.