Giter Site home page Giter Site logo

porrey / newtonsoft.json.interface Goto Github PK

View Code? Open in Web Editor NEW
2.0 3.0 1.0 277 KB

The Json.NET Interface Converter/Mapper is a JsonConverter attribute that allows interfaces to be mapped to concrete implementations of those interfaces for use when deserializing an object.

License: GNU Lesser General Public License v3.0

C# 97.22% Batchfile 2.78%
json serialize newtonsoft-json newtonsoft deserialize concrete jsonconverter-attribute model-conversion concrete-implementations mapper

newtonsoft.json.interface's Introduction

GitHub .NET Core Build/Test

Nuget Nuget

Newtonsoft.Json.Interface

The Json.NET Interface Converter/Mapper is a JsonConverter attribute that allows interfaces to be mapped to concrete implementations of those interfaces for use when deserializing an object.

This is useful when retrieving objects from a container that does not have access to the concrete implementation, but those objects need to be serialized and deserialized using he interface.

Using the attribute

Place the attribute over the interface definition and specify the class to be used for deserialization.

Basic Usage

Model Conversion

Place an attribute over the interface to define the concrete class to use when deserializing objects with the given interface.

/// <summary>
/// Specifies that the an object serialized as IUser should be
/// deserialized into an instance of User.
/// </summary>
[JsonConverter(typeof(InterfaceToConcreteConverter<IUser, User>))]
public interface IUser : IUser<string>
{

Model Property Conversion

Place an attribute over a model property that uses an interface as the type as shown below.

public class TestPropertyModel
{
    public int Id { get; set; }
    public string Comment { get; set; }

    [JsonConverter(typeof(ConcreteConverter<TestModel>))]
    public ITestModel Model { get; set; }
}

Example

Model Conversion

This example shows how to use the attribute on an interface to direct deserialization to a specific model or entity.

Define an interface as shown below.

public interface IMyModel
{
	string Description { get; set; }
	int Id { get; set; }
	string Name { get; set; }
}

Create a concrete implementation for the interface.

public class MyModel : IMyModel
{
	public int Id { get; set; }
	public string Name { get; set; }
	public string Description { get; set; }
}

Create an array of objects using the interface and then serialize the array to a JSON string.

IMyModel[] models = new MyModel[]
{
	new MyModel()
	{
		Id = 1,
		Name = "Model 1",
		Description = "My new model 1."
	},
	new MyModel()
	{
		Id = 2,
		Name = "Model 2",
		Description = "My new model 2."
	},
	new MyModel()
	{
		Id = 3,
		Name = "Model 3",
		Description = "My new model 3."
	},
};

string json = JsonConvert.SerializeObject(models);

An attempt to deserialize the above array without the attribute would result in the error shown below.

Now go back and add the attribute to the interface definition.

[JsonConverter(typeof(InterfaceToConcreteConverter<IMyModel, MyModel>))]
public interface IMyModel
{
	string Description { get; set; }
	int Id { get; set; }
	string Name { get; set; }
}

With the attribute on the the interface, the below code would run perfectly. Note that the interface is used in the call to JsonConvert.DeserializeObject and not the concrete class reference.

// ***
// *** Deserialize the items as a list of interfaces.
// ***
IEnumerable<IMyModel> deserializedItems = JsonConvert.DeserializeObject<IEnumerable<IMyModel>>(json);

newtonsoft.json.interface's People

Contributors

jtone123 avatar porrey avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

jtone123

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.