Skip to content

CloudNDevOps.Newtonsoft.Extensions is a library that extends functionality provided by Newtonsoft needed by common applications. One of such functionalities is ability to de-serialize collection of object families.

License

Notifications You must be signed in to change notification settings

nvmathure/NewtonsoftExtensions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

62 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CloudNDevOps.Newtonsoft.Extensions

CloudNDevOps.Newtonsoft.Extensions is a library that extends functionality provided by Newtonsoft needed by common applications. One of such functionalities is ability to de-serialize collection of object families.

Installation

Use the package manager NuGet to install CloudNDevOps.Newtonsoft.Extensions.

Install-Package CloudNDevOps.Newtonsoft.Extensions -Version 1.0.1-beta1

Usage

Define Types

public class Customer
{
    public string Identifier { get; set; }

    public string Classifier { get; set; }
}

public class Individual : Customer
{
    public string FirstName { get; set; }

    public string LastName { get; set; }
}

public class Organization : Customer
{
    public string Name { get; set; }
}

Create Instance of Converter

First parameter is function, which need to return value of classifier based on instance of object. Second parameter is dictionary of value of classifier and type of derived class.

var typeFamilyConverter = new TypeFamilyConverter<Customer, string>(
    new Func<Customer, string>(e => e.Classifier),
    new Dictionary<string, Type>
    {
        { "Individual", typeof(Individual) },
        { "Organization", typeof(Organization) }
    });

De-Serialize using Newtonsoft JsonConvert Class

var result = JsonConvert.DeserializeObject<List<Customer>>(
    inputString,
    new JsonSerializerSettings
    {
        Converters = { typeFamilyConverter }
    });

License

MIT

About

CloudNDevOps.Newtonsoft.Extensions is a library that extends functionality provided by Newtonsoft needed by common applications. One of such functionalities is ability to de-serialize collection of object families.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages