184

I am trying to set up a reader that will take in JSON objects from various websites (think information scraping) and translate them into C# objects. I am currently using JSON.NET for the deserialization process. The problem I am running into is that it does not know how to handle interface-level properties in a class. So something of the nature:

public IThingy Thing

Will produce the error:

Could not create an instance of type IThingy. Type is an interface or abstract class and cannot be instantiated.

It is relatively important to have it be an IThingy as opposed to a Thingy since the code I am working on is considered sensitive and unit testing is highly important. Mocking of objects for atomic test scripts is not possible with fully-fledged objects like Thingy. They must be an interface.

I've been poring over JSON.NET's documentation for a while now, and the questions I could find on this site related to this are all from over a year ago. Any help?

Also, if it matters, my app is written in .NET 4.0.

2
  • possible duplicate of Using Json.NET converters to deserialize properties
    – nawfal
    Jul 20, 2014 at 11:47
  • 2
    I think that approach is flawed. If Thingy is a data model, it doesn't matter for unit tests, since it can be initialized and contains no logic and would have the least common nominator of all properties used for your scrapping and adding more specific ones in classes inheriting from it
    – Tseng
    Jul 8, 2021 at 14:14

17 Answers 17

134

@SamualDavis provided a great solution in a related question, which I'll summarize here.

If you have to deserialize a JSON stream into a concrete class that has interface properties, you can include the concrete classes as parameters to a constructor for the class! The NewtonSoft deserializer is smart enough to figure out that it needs to use those concrete classes to deserialize the properties.

Here is an example:

public class Visit : IVisit
{
    /// <summary>
    /// This constructor is required for the JSON deserializer to be able
    /// to identify concrete classes to use when deserializing the interface properties.
    /// </summary>
    public Visit(MyLocation location, Guest guest)
    {
        Location = location;
        Guest = guest;
    }
    public long VisitId { get; set; }
    public ILocation Location { get;  set; }
    public DateTime VisitDate { get; set; }
    public IGuest Guest { get; set; }
}
20
  • 24
    How would this work with an ICollection? ICollection<IGuest> Guests{get;set;}
    – DrSammyD
    Jan 30, 2014 at 16:43
  • 16
    It works with ICollection<ConcreteClass>, so ICollection<Guest> works. Just as an FYI, you can put the attribute [JsonConstructor] on your constructor so that it will use that by default if you happen to have multiple constructors
    – DrSammyD
    Feb 3, 2014 at 17:33
  • 9
    I am stuck to the same problem, in my case i have several implementation of the interface ( in your example the interface is ILocation ) so what if there are classes like MyLocation, VIPLocation, OrdinaryLocation. How to map these to Location property ? If you just have one implementation like MyLocation its easy, but how to do it if there are multiple implementations of ILocation ?
    – ATHER
    Oct 9, 2015 at 23:09
  • 11
    If you have more than one constructor, you can mark up your special constructor with the [JsonConstructor] attribute. May 23, 2016 at 12:49
  • 43
    This is not fine at all. The point of using interfaces is to use dependency injection, but doing this with an object typed parameter required by your constructor you totally screw up the point of having an interface as a property. Aug 29, 2016 at 9:34
88

Why use a converter? There is a native functionality in Newtonsoft.Json to solve this exact problem:

Set TypeNameHandling in the JsonSerializerSettings to TypeNameHandling.Auto

JsonConvert.SerializeObject(
  toSerialize,
  new JsonSerializerSettings()
  {
    TypeNameHandling = TypeNameHandling.Auto
  });

This will put every type into the json, that is not held as a concrete instance of a type but as an interface or an abstract class.

Make sure that you are using the same settings for serialization and deserialization.

I tested it, and it works like a charm, even with lists.

Search Results Web result with site links

⚠️ WARNING:

Only use this for json from a known and trusted source. User snipsnipsnip correctly mentioned that this is indeed a vunerability.

See CA2328 and SCS0028 for more information.


Source and an alternative manual implementation: Code Inside Blog

16
  • 3
    Perfect, this helped me for a quick & dirty deep clone ( stackoverflow.com/questions/78536/deep-cloning-objects ) Mar 24, 2017 at 10:53
  • 11
    I just tried this on Deserialization and it doesn't work. The subject line of this Stack Overflow question is, "Casting interfaces for deserialization in JSON.NET" Sep 13, 2017 at 13:38
  • 4
    @JustinRusso it only works when the json has been serialized with the same setting
    – Mafii
    Sep 13, 2017 at 13:40
  • 3
    Upvote for the quick, if not dirty, solution. If you are just serializing configurations, this works. Beats stopping development to build converters and certainly beats decorating every injected property. serializer.TypeNameHandling = TypeNameHandling.Auto; JsonConvert.DefaultSettings().TypeNameHandling = TypeNameHandling.Auto; Dec 20, 2017 at 3:32
  • 3
    @KyleDelaney AFAIU: 1) Attacks of this kind mostly target the types in the standard library which does some dynamic code execution. 2) Along with typename, JSON content have constructor argument, which is passed to vulnerable classes to be interpreted. See this Q&A for an example. Jun 11, 2020 at 1:28
69

(Copied from this question)

In cases where I have not had control over the incoming JSON (and so cannot ensure that it includes a $type property) I have written a custom converter that just allows you to explicitly specify the concrete type:

public class Model
{
    [JsonConverter(typeof(ConcreteTypeConverter<Something>))]
    public ISomething TheThing { get; set; }
}

This just uses the default serializer implementation from Json.Net whilst explicitly specifying the concrete type.

An overview are available on this blog post. Source code is below:

public class ConcreteTypeConverter<TConcrete> : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        //assume we can convert to anything for now
        return true;
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        //explicitly specify the concrete type we want to create
        return serializer.Deserialize<TConcrete>(reader);
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        //use the default serialization - it works fine
        serializer.Serialize(writer, value);
    }
}
5
  • 11
    I really like this approach and applied it to our own project. I even added a ConcreteListTypeConverter<TInterface, TImplementation> to handle class members of type IList<TInterface>.
    – Oliver
    Dec 6, 2012 at 12:45
  • 4
    That's a great bit of code. It might be nicer to have the actual code for concreteTypeConverter in the question though.
    – Chris
    Aug 27, 2015 at 10:16
  • 3
    @Oliver - Can you post your ConcreteListTypeConverter<TInterface, TImplementation> implementation?
    – Michael
    Oct 18, 2017 at 21:16
  • 3
    And if you have two implementors of ISomething?
    – bdaniel7
    Feb 1, 2019 at 15:21
  • For System.Text.Json you can use the same attribute way but a different JsonConverter: stackoverflow.com/a/64636093/558211
    – Rubenisme
    Nov 18, 2021 at 19:26
66

Use this class, for mapping abstract type, to real type:

public class AbstractConverter<TReal, TAbstract> 
    : JsonConverter where TReal : TAbstract
{
    public override Boolean CanConvert(Type objectType)
        => objectType == typeof(TAbstract);

    public override Object ReadJson(JsonReader reader, Type type, Object value, JsonSerializer jser)
        => jser.Deserialize<TReal>(reader);

    public override void WriteJson(JsonWriter writer, Object value, JsonSerializer jser)
        => jser.Serialize(writer, value);
}

and when deserialize:

var settings = new JsonSerializerSettings
{
    Converters = {
        new AbstractConverter<Thing, IThingy>(),
        new AbstractConverter<Thing2, IThingy2>()
    },
};

JsonConvert.DeserializeObject(json, type, settings);
10
  • 6
    It's worth putting this to the converter class declaration: where TReal : TAbstract to make sure it can cast to the type
    – Artemious
    Oct 30, 2018 at 21:33
  • 2
    A more complete where might be where TReal : class, TAbstract, new(). Feb 12, 2019 at 19:34
  • 4
    I used this converter with struct too, I believe "where TReal: TAbstract" is enough. Thanks all.
    – Simone S.
    Feb 14, 2019 at 15:08
  • 3
    Gold! Slick way to go.
    – SwissCoder
    May 27, 2019 at 13:03
  • 2
    Great. This solution, in contrast to the accepted answer, also works if you can't modify the actual class and need to implement the interface yourself.
    – Kabbalah
    May 31, 2019 at 7:31
44

To enable deserialization of multiple implementations of interfaces, you can use JsonConverter, but not through an attribute:

Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer();
serializer.Converters.Add(new DTOJsonConverter());
Interfaces.IEntity entity = serializer.Deserialize(jsonReader);

DTOJsonConverter maps each interface with a concrete implementation:

class DTOJsonConverter : Newtonsoft.Json.JsonConverter
{
    private static readonly string ISCALAR_FULLNAME = typeof(Interfaces.IScalar).FullName;
    private static readonly string IENTITY_FULLNAME = typeof(Interfaces.IEntity).FullName;


    public override bool CanConvert(Type objectType)
    {
        if (objectType.FullName == ISCALAR_FULLNAME
            || objectType.FullName == IENTITY_FULLNAME)
        {
            return true;
        }
        return false;
    }

    public override object ReadJson(Newtonsoft.Json.JsonReader reader, Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
    {
        if (objectType.FullName == ISCALAR_FULLNAME)
            return serializer.Deserialize(reader, typeof(DTO.ClientScalar));
        else if (objectType.FullName == IENTITY_FULLNAME)
            return serializer.Deserialize(reader, typeof(DTO.ClientEntity));

        throw new NotSupportedException(string.Format("Type {0} unexpected.", objectType));
    }

    public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
    {
        serializer.Serialize(writer, value);
    }
}

DTOJsonConverter is required only for the deserializer. The serialization process is unchanged. The Json object do not need to embed concrete types names.

This SO post offers the same solution one step further with a generic JsonConverter.

4
  • Wouldn't that WriteJson method's call to serializer.Serialize cause a stack overflow, since calling serialize on the value being serialized by the converter would cause the converter's WriteJson method to be called again recursively?
    – Triynko
    Dec 2, 2013 at 7:09
  • It should not, if the CanConvert () method returns a consistent result. Jan 10, 2014 at 13:38
  • 3
    Why are you comparing FullNames when you may just compare types directly? Aug 1, 2016 at 12:41
  • Just comparing types also is fine. Aug 1, 2016 at 12:46
25

Nicholas Westby provided a great solution in a awesome article.

If you want Deserializing JSON to one of many possible classes that implement an interface like that:

public class Person
{
    public IProfession Profession { get; set; }
}

public interface IProfession
{
    string JobTitle { get; }
}

public class Programming : IProfession
{
    public string JobTitle => "Software Developer";
    public string FavoriteLanguage { get; set; }
}

public class Writing : IProfession
{
    public string JobTitle => "Copywriter";
    public string FavoriteWord { get; set; }
}

public class Samples
{
    public static Person GetProgrammer()
    {
        return new Person()
        {
            Profession = new Programming()
            {
                FavoriteLanguage = "C#"
            }
        };
    }
}

You can use a custom JSON converter:

public class ProfessionConverter : JsonConverter
{
    public override bool CanWrite => false;
    public override bool CanRead => true;
    public override bool CanConvert(Type objectType)
    {
        return objectType == typeof(IProfession);
    }
    public override void WriteJson(JsonWriter writer,
        object value, JsonSerializer serializer)
    {
        throw new InvalidOperationException("Use default serialization.");
    }

    public override object ReadJson(JsonReader reader,
        Type objectType, object existingValue,
        JsonSerializer serializer)
    {
        var jsonObject = JObject.Load(reader);
        var profession = default(IProfession);
        switch (jsonObject["JobTitle"].Value())
        {
            case "Software Developer":
                profession = new Programming();
                break;
            case "Copywriter":
                profession = new Writing();
                break;
        }
        serializer.Populate(jsonObject.CreateReader(), profession);
        return profession;
    }
}

And you will need to decorate the "Profession" property with a JsonConverter attribute to let it know to use your custom converter:

    public class Person
    {
        [JsonConverter(typeof(ProfessionConverter))]
        public IProfession Profession { get; set; }
    }

And then, you can cast your class with an Interface:

Person person = JsonConvert.DeserializeObject<Person>(jsonString);
1
  • It actually has to be jsonObject.Value("JobTitle") . Great answer, the best one.
    – savram
    Sep 11 at 19:06
12

I found this useful. You might too.

Example Usage

public class Parent
{
    [JsonConverter(typeof(InterfaceConverter<IChildModel, ChildModel>))]
    IChildModel Child { get; set; }
}

Custom Creation Converter

public class InterfaceConverter<TInterface, TConcrete> : CustomCreationConverter<TInterface>
    where TConcrete : TInterface, new()
{
    public override TInterface Create(Type objectType)
    {
        return new TConcrete();
    }
}

Json.NET documentation

1
  • 2
    Not a workable solution. Does not address Lists and leads to sprinkling decorators/annotations everywhere. Dec 20, 2017 at 3:34
9

Two things you might try:

Implement a try/parse model:

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

  [JsonConverter(typeof(RichDudeConverter))]
  public IPerson Owner { get; set; }
}

public interface IPerson {
  string Name { get; set; }
}

public class Tycoon : IPerson {
  public string Name { get; set; }
}

public class Magnate : IPerson {
  public string Name { get; set; }
  public string IndustryName { get; set; }
}

public class Heir: IPerson {
  public string Name { get; set; }
  public IPerson Benefactor { get; set; }
}

public class RichDudeConverter : JsonConverter
{
  public override bool CanConvert(Type objectType)
  {
    return (objectType == typeof(IPerson));
  }

  public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
  {
    // pseudo-code
    object richDude = serializer.Deserialize<Heir>(reader);

    if (richDude == null)
    {
        richDude = serializer.Deserialize<Magnate>(reader);
    }

    if (richDude == null)
    {
        richDude = serializer.Deserialize<Tycoon>(reader);
    }

    return richDude;
  }

  public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
  {
    // Left as an exercise to the reader :)
    throw new NotImplementedException();
  }
}

Or, if you can do so in your object model, implement a concrete base class between IPerson and your leaf objects, and deserialize to it.

The first can potentially fail at runtime, the second requires changes to your object model and homogenizes the output to the lowest common denominator.

7
  • A try/parse model isn't feasible due to the scale I have to work with. I have to consider a scope of hundreds of base objects with even more hundreds of stub/helper objects to represent embedded JSON objects that happen a lot. It's not out of the question to change the object model, but wouldn't using a concrete base class in the properties make us unable to mock items for unit testing? Or am I getting that backward somehow?
    – tmesser
    Apr 25, 2011 at 18:28
  • You could still implement a mock from IPerson - note that the type of the Organisation.Owner property is still IPerson. But for deserialization of an arbitrary target you have to return a concrete type. If you don't own the type definition and you can't define the minimum set of properties that your code will require, then your last resort is something like a key/value bag. Using your facebook example comment - can you post in an answer what your (one or multiple) implementations of ILocation look like? That may help move things forward.
    – mcw
    Apr 25, 2011 at 19:09
  • Since the primary hope is mocking, the ILocation interface is, really, merely a facade for the Location concrete object. A quick example I just worked up would be something like this (pastebin.com/mWQtqGnB) for the interface and this (pastebin.com/TdJ6cqWV) for the concrete object.
    – tmesser
    Apr 26, 2011 at 14:56
  • And to go the next step, this is an example of what IPage would look like (pastebin.com/iuGifQXp) and Page (pastebin.com/ebqLxzvm). The problem, of course, being that while the deserialization of Page would generally work fine, it'll choke when it gets to the ILocation property.
    – tmesser
    Apr 26, 2011 at 15:02
  • Ok, so thinking about the objects that you're actually scraping and deserializing - is it generally the case that the JSON data is consistent with a single concrete class definition? Meaning (hypothetically) you wouldn't encounter "locations" with additional properties that would make Location unsuitable to use as the concrete type for the deserialized object? If so, attributing the ILocation property of Page with a "LocationConverter" should work. If not, and it's because the JSON data doesn't always conform to a rigid or consistent structure (like ILocation), then (... continued)
    – mcw
    Apr 26, 2011 at 20:22
8

For those that might be curious about the ConcreteListTypeConverter that was referenced by Oliver, here is my attempt:

public class ConcreteListTypeConverter<TInterface, TImplementation> : JsonConverter where TImplementation : TInterface 
{
    public override bool CanConvert(Type objectType)
    {
        return true;
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        var res = serializer.Deserialize<List<TImplementation>>(reader);
        return res.ConvertAll(x => (TInterface) x);
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        serializer.Serialize(writer, value);
    }
}
2
  • 2
    I'm confused with the overridden CanConvert(Type objectType) { return true;}. It seems hacky, how exactly is this helpful? I may be wrong but isn't that like telling a smaller inexperienced fighter that they're going to win the fight no matter the opponent?
    – Chef_Code
    May 4, 2017 at 20:23
  • Sample usage: public class Thing : IThingy { [JsonConverter(typeof(ConcreteListTypeConverter<IThingy, Thing>))] public List<IThingy> Things } Oct 20, 2020 at 15:16
6

No object will ever be an IThingy as interfaces are all abstract by definition.

The object you have that was first serialized was of some concrete type, implementing the abstract interface. You need to have this same concrete class revive the serialized data.

The resulting object will then be of some type that implements the abstract interface you are looking for.

From the documentation it follows that you can use

(Thingy)JsonConvert.DeserializeObject(jsonString, typeof(Thingy));

when deserializing to inform JSON.NET about the concrete type.

21
  • That is precisely the post from over a year ago that I was referring to. The only major suggestion (writing custom converters) is not terribly feasible with the scale I am forced to consider. JSON.NET has changed a lot in the intervening year. I perfectly understand the distinction between a class and an interface, but C# also supports implicit conversions from an interface to an object that implements the interface with regard to typing. I am essentially asking if there is a way to tell JSON.NET which object will implement this interface.
    – tmesser
    Apr 25, 2011 at 17:41
  • It was all there in the answer I pointed you to. Make sure there's a _type property that signals the concrete type to use. Apr 25, 2011 at 17:44
  • And I strongly doubt that C# supports any kind of 'implicit' typecasting from a variable declared as an interface to a concrete type without any sort of hints. Apr 25, 2011 at 17:46
  • Unless I read it wrong, the _type property was supposed to be in the JSON to be serialized. That works fine if you're only deserializing what you already serialized, but that's not what's going on here. I am pulling JSON from a number of sites that are not going to be following that standard.
    – tmesser
    Apr 25, 2011 at 17:49
  • @YYY - Do you control both the serialization to and deserialization from the source JSON? Because ultimately you'll need to either embed the concrete type in the serialized JSON as a hint to use when deserializing or you'll need to use some kind of try/parse model that detects/attempts-to-detect the concrete type at runtime and invoke the appropriate deserializer.
    – mcw
    Apr 25, 2011 at 17:50
4

For what it's worth, I ended up having to handle this myself for the most part. Each object has a Deserialize(string jsonStream) method. A few snippets of it:

JObject parsedJson = this.ParseJson(jsonStream);
object thingyObjectJson = (object)parsedJson["thing"];
this.Thing = new Thingy(Convert.ToString(thingyObjectJson));

In this case, new Thingy(string) is a constructor that will call the Deserialize(string jsonStream) method of the appropriate concrete type. This scheme will continue to go downward and downward until you get to the base points that json.NET can just handle.

this.Name = (string)parsedJson["name"];
this.CreatedTime = DateTime.Parse((string)parsedJson["created_time"]);

So on and so forth. This setup allowed me to give json.NET setups it can handle without having to refactor a large part of the library itself or using unwieldy try/parse models that would have bogged down our entire library due to the number of objects involved. It also means that I can effectively handle any json changes on a specific object, and I do not need to worry about everything that object touches. It's by no means the ideal solution, but it works quite well from our unit and integration testing.

4

Suppose an autofac setting like the following:

public class AutofacContractResolver : DefaultContractResolver
{
    private readonly IContainer _container;

    public AutofacContractResolver(IContainer container)
    {
        _container = container;
    }

    protected override JsonObjectContract CreateObjectContract(Type objectType)
    {
        JsonObjectContract contract = base.CreateObjectContract(objectType);

        // use Autofac to create types that have been registered with it
        if (_container.IsRegistered(objectType))
        {
           contract.DefaultCreator = () => _container.Resolve(objectType);
        }  

        return contract;
    }
}

Then, suppose your class is like this:

public class TaskController
{
    private readonly ITaskRepository _repository;
    private readonly ILogger _logger;

    public TaskController(ITaskRepository repository, ILogger logger)
    {
        _repository = repository;
        _logger = logger;
    }

    public ITaskRepository Repository
    {
        get { return _repository; }
    }

    public ILogger Logger
    {
        get { return _logger; }
    }
}

Therefore, the usage of the resolver in deserialization could be like:

ContainerBuilder builder = new ContainerBuilder();
builder.RegisterType<TaskRepository>().As<ITaskRepository>();
builder.RegisterType<TaskController>();
builder.Register(c => new LogService(new DateTime(2000, 12, 12))).As<ILogger>();

IContainer container = builder.Build();

AutofacContractResolver contractResolver = new AutofacContractResolver(container);

string json = @"{
      'Logger': {
        'Level':'Debug'
      }
}";

// ITaskRespository and ILogger constructor parameters are injected by Autofac 
TaskController controller = JsonConvert.DeserializeObject<TaskController>(json, new JsonSerializerSettings
{
    ContractResolver = contractResolver
});

Console.WriteLine(controller.Repository.GetType().Name);

You can see more details in http://www.newtonsoft.com/json/help/html/DeserializeWithDependencyInjection.htm

1
  • I will up-vote this as the best solution. DI has been so widely used this days by c# web devs, and this fit nicely as a centralized place to handle those type conversion by the resolver.
    – appletwo
    Jul 26, 2017 at 23:56
3

My solution to this one, which I like because it is nicely general, is as follows:

/// <summary>
/// Automagically convert known interfaces to (specific) concrete classes on deserialisation
/// </summary>
public class WithMocksJsonConverter : JsonConverter
{
    /// <summary>
    /// The interfaces I know how to instantiate mapped to the classes with which I shall instantiate them, as a Dictionary.
    /// </summary>
    private readonly Dictionary<Type,Type> conversions = new Dictionary<Type,Type>() { 
        { typeof(IOne), typeof(MockOne) },
        { typeof(ITwo), typeof(MockTwo) },
        { typeof(IThree), typeof(MockThree) },
        { typeof(IFour), typeof(MockFour) }
    };

    /// <summary>
    /// Can I convert an object of this type?
    /// </summary>
    /// <param name="objectType">The type under consideration</param>
    /// <returns>True if I can convert the type under consideration, else false.</returns>
    public override bool CanConvert(Type objectType)
    {
        return conversions.Keys.Contains(objectType);
    }

    /// <summary>
    /// Attempt to read an object of the specified type from this reader.
    /// </summary>
    /// <param name="reader">The reader from which I read.</param>
    /// <param name="objectType">The type of object I'm trying to read, anticipated to be one I can convert.</param>
    /// <param name="existingValue">The existing value of the object being read.</param>
    /// <param name="serializer">The serializer invoking this request.</param>
    /// <returns>An object of the type into which I convert the specified objectType.</returns>
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        try
        {
            return serializer.Deserialize(reader, this.conversions[objectType]);
        }
        catch (Exception)
        {
            throw new NotSupportedException(string.Format("Type {0} unexpected.", objectType));
        }
    }

    /// <summary>
    /// Not yet implemented.
    /// </summary>
    /// <param name="writer">The writer to which I would write.</param>
    /// <param name="value">The value I am attempting to write.</param>
    /// <param name="serializer">the serializer invoking this request.</param>
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

}

You could obviously and trivially convert it into an even more general converter by adding a constructor which took an argument of type Dictionary<Type,Type> with which to instantiate the conversions instance variable.

3

Several years on and I had a similar issue. In my case there were heavily nested interfaces and a preference for generating the concrete classes at runtime so that It would work with a generic class.

I decided to create a proxy class at run time that wraps the object returned by Newtonsoft.

The advantage of this approach is that it does not require a concrete implementation of the class and can handle any depth of nested interfaces automatically. You can see more about it on my blog.

using Castle.DynamicProxy;
using Newtonsoft.Json.Linq;
using System;
using System.Reflection;

namespace LL.Utilities.Std.Json
{
    public static class JObjectExtension
    {
        private static ProxyGenerator _generator = new ProxyGenerator();

        public static dynamic toProxy(this JObject targetObject, Type interfaceType) 
        {
            return _generator.CreateInterfaceProxyWithoutTarget(interfaceType, new JObjectInterceptor(targetObject));
        }

        public static InterfaceType toProxy<InterfaceType>(this JObject targetObject)
        {

            return toProxy(targetObject, typeof(InterfaceType));
        }
    }

    [Serializable]
    public class JObjectInterceptor : IInterceptor
    {
        private JObject _target;

        public JObjectInterceptor(JObject target)
        {
            _target = target;
        }
        public void Intercept(IInvocation invocation)
        {

            var methodName = invocation.Method.Name;
            if(invocation.Method.IsSpecialName && methodName.StartsWith("get_"))
            {
                var returnType = invocation.Method.ReturnType;
                methodName = methodName.Substring(4);

                if (_target == null || _target[methodName] == null)
                {
                    if (returnType.GetTypeInfo().IsPrimitive || returnType.Equals(typeof(string)))
                    {

                        invocation.ReturnValue = null;
                        return;
                    }

                }

                if (returnType.GetTypeInfo().IsPrimitive || returnType.Equals(typeof(string)))
                {
                    invocation.ReturnValue = _target[methodName].ToObject(returnType);
                }
                else
                {
                    invocation.ReturnValue = ((JObject)_target[methodName]).toProxy(returnType);
                }
            }
            else
            {
                throw new NotImplementedException("Only get accessors are implemented in proxy");
            }

        }
    }



}

Usage:

var jObj = JObject.Parse(input);
InterfaceType proxyObject = jObj.toProxy<InterfaceType>();
4
  • Thanks! This is the only answer the properly supports dynamic typing (duck typing) without forcing restrictions on the incoming json. Sep 26, 2017 at 0:49
  • No problem. I was a bit surprised to see there was nothing out there. It has moved on a bit since that original example so I decided to share the code. github.com/sudsy/JsonDuckTyper . I also published it on nuget as JsonDuckTyper. If you find you want to enhance it, just send me a PR and I'll be happy to oblige.
    – Sudsy
    Sep 26, 2017 at 20:39
  • When I was looking for a solution in this area I came across github.com/ekonbenefits/impromptu-interface also. It doesn't work in my case as it does not support dotnet core 1.0 but it might work for you.
    – Sudsy
    Sep 27, 2017 at 10:15
  • I did try with Impromptu Interface, but Json.Net wasn't happy doing a PopulateObject on the proxy generated by Impromptu Interface. I unfortunately gave up going for Duck Typing - it was just easier to create a custom Json Contract Serializer that used reflection to find an existing implementation of the requested interface and using that. Sep 28, 2017 at 22:03
2

Use this JsonKnownTypes, it's very similar way to use, it just add discriminator to json:

[JsonConverter(typeof(JsonKnownTypeConverter<Interface1>))]
[JsonKnownType(typeof(MyClass), "myClass")]
public interface Interface1
{  }
public class MyClass : Interface1
{
    public string Something;
}

Now when you serialize object in json will be add "$type" with "myClass" value and it will be use for deserialize

Json:

{"Something":"something", "$type":"derived"}
0

My solution was added the interface elements in the constructor.

public class Customer: ICustomer{
     public Customer(Details details){
          Details = details;
     }

     [JsonProperty("Details",NullValueHnadling = NullValueHandling.Ignore)]
     public IDetails Details {get; set;}
}
0

You can also use custom TextInputFormatter, no external libraries needed, also helps you gain insight on how you can handle (de)serialization of any type of data.

public class MyInputTypeFormatter : TextInputFormatter
{
    public MyInputTypeFormatter()
    {
        SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("application/json"));
        SupportedEncodings.Add(Encoding.UTF8);
    }



    protected override bool CanReadType(Type type)
    {
        return type == typeof(MyClass);
    }

    public override async Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding)
    {
        var httpContext = context.HttpContext;
        var serviceProvider = httpContext.RequestServices;
        var logger = serviceProvider.GetRequiredService<ILogger<ImageTypeConverter>>();
        using var reader = new StreamReader(httpContext.Request.Body, encoding);
        {
            var data = await reader.ReadToEndAsync();
            if (data.Contains("Hello"))
            {
                var myClass= new MyClass(data);
                return await InputFormatterResult.SuccessAsync(myClass);

            }
            else
            {
                return await InputFormatterResult.FailureAsync();

            }
        }


    }
}

Then, simply add this input formatter to the list of input formatters with

services.AddControllers(options=> {
            options.InputFormatters.Insert(0, new MyInputFormatter());
        });

0 here means this is the first input formatter invoked when model binding.

It seems like a lot of work but most of it is just boilerplate. I will explain how this works,

You have an action method/ route which has a parameter of MyClass type. When a request comes to it, your input formatter's CanReadType is invoked and it returns true meaning it will handle the deserialization.Then the ReadRequestBodyAsync method is invoked and the request data is given to it.

You can do whatever you want with the data and return an object of type MyClass if your deserialization succeeds. Else you just return a failure.

In the deserialization you can use

using (JsonDocument document = JsonDocument.Parse(jsonString))
{
JsonElement root = document.RootElement;
// ...
}

You can traverse elements as the input is parsed into a json object and then held into a DOM. Then you can see what they contain and manually create classes with their data and convert your input-as-interfaces into classes.

Note: JsonDocument was introduced in .Net 3.1 You can check out how to use it here

More about how to use TextInputFormatter and TextOutputFormatter The benefit of using a custom input formatter is that it provides a central class for handling your custom classes which may use multiple interfaces. It also gives you fine control over handling the input data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.