Tag Archives: Interface

Seriliasing Interfaces in JSON (or using a JsonConverter in JSON.NET)

Imagine that you have the following interface:

    public interface IProduct
    {
        int Id { get; set; }
        decimal UnitPrice { get; set; }
    }

This is an interface, and so may have a number of implementations; however, we know that every implementation will contain at least 2 fields, and what type they will be. If we wanted to serialise this, we’d probably write something like this:

        private static string SerialiseProduct(IProduct product)
        {
            string json = JsonConvert.SerializeObject(product);
            return json;
        }

If you were to call this from a console app, it would work fine:

        static void Main(string[] args)
        {
            IProduct product = new Product()
            {
                Id = 1,
                UnitPrice = 12.3m
            };

            string json = SerialiseProduct(product);
            Console.WriteLine(json);

Okay, so far so good. Now, let’s deserialise:

        private static IProduct DeserialiseProduct(string json)
        {
            IProduct product = JsonConvert.DeserializeObject<IProduct>(json);

            return product;
        }

And let’s call it:

        static void Main(string[] args)
        {
            IProduct product = new Product()
            {
                Id = 1,
                UnitPrice = 12.3m
            };

            string json = SerialiseProduct(product);
            Console.WriteLine(json);

            IProduct product2 = DeserialiseProduct(json);
            Console.WriteLine(product2.Id);
            
            Console.ReadLine();

        }

So, that runs fine:

Newtonsoft.Json.JsonSerializationException: ‘Could not create an instance of type SerialiseInterfaceJsonNet.IProduct. Type is an interface or abstract class and cannot be instantiated.

No.

Why?

The reason is that you can’t create an interface; for example:

That doesn’t even compile, but effectively, that’s what’s happening behind the scenes.

Converters

Json.Net allows the use of something called a converter. What that means is that I can inject functionality into the deserialisation process that tells Json.Net what to do with this interface. Here’s a possible converter for our class:

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

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            return serializer.Deserialize(reader, typeof(Product));
        }

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

It’s a relatively simple interface, you tell it how to identify your class, and then how to read and write the Json.

Finally, you just need to tell the converter to use this:

        private static IProduct DeserialiseProduct(string json)
        {
            var settings = new JsonSerializerSettings();
            settings.Converters.Add(new ProductConverter());

            IProduct product = JsonConvert.DeserializeObject<IProduct>(json, settings);

            return product;
        }

By using the settings parameter.

References

http://www.jerriepelser.com/blog/custom-converters-in-json-net-case-study-1/

Returning an unknown interface from a WCF Service

The Problem

There are times when creating a WCF service that you may want to return a value, whilst not knowing precisely which value; for example, a service contract such as this:

[ServiceContract]
public interface IMyService
{
    [OperationContract()]
    FunctionResult MyFunction(string parameter);
}

… and a data contact like this:

[DataContract]
public class FunctionResult
{
    private int _myData;
    [DataMember(Name="MyData")]
    public int MyData
    {
        get { return _myData; }
        set { _myData = value; }
    }
	
    // Here, I want to return a class, 
    // but it may be a number of different 
    // options that share no commonality
}

So, how?

One solution to this, right or wrong, is to create a blank interface:

interface IData { }

Then to implement it in whichever class I want to return:

public class MyClass : IData

Finally, include this in my `FunctionResult` return class:

[DataContract]
public class FunctionResult
{
    private int _myData;
    [DataMember(Name="MyData")]
    public int MyData
    {
        get { return _myData; }
        set { _myData = value; }
    }

    private IData _returnData;
    [DataMember(Name="ReturnData")]
    public IData ReturnData
    {
        get { return _returnData; }
        set { _returnData = value; }
    }
}

You do need to declare the concrete class as a `ServiceKnownType`:

[ServiceContract]
[ServiceKnownType(typeof(MyClass))]
public interface IMyService
{
    [OperationContract()]
    OperationResult MyFunction(string paramater);
}

Acknowledgements

The `ServiceKnownType` wasn’t something I had come across before – thankfully, we have StackOverflow for that kind of thing:

http://stackoverflow.com/questions/310160/passing-interface-in-a-wcf-service