Question

I have this Object

public class ConversationAPI
{
    [JsonProperty(PropertyName = "lU")]
    public DateTime LastUpdated { get; set; }

    [JsonProperty(PropertyName = "m", TypeNameHandling = TypeNameHandling.All)]
    public List<Message> Messages { get; set; }
}

Which I send from the API as a json and I deserialize in my Client Application.

The List<Message> Messages property contains either 

 [Serializable]
    public class Message
    {
        [JsonProperty(PropertyName = "t")]
        public string Text { get; set; }

        [JsonProperty(PropertyName = "ty")]
        public MessageType Type { get; set; }
    }

or

[Serializable]
    public class DerivedMessage : Message
    {
        [JsonProperty(PropertyName = "sos")]
        public string SomeOtherStuff{ get; set; }
    }

I can't seem to be able to deserialize the Array of derived types. I've tried this

var settings = new JsonSerializerSettings
                    {
                        TypeNameHandling = TypeNameHandling.All,
                        TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Full
                    };
var conversation = JsonConvert.DeserializeObject<ConversationResponse>(response.Content, settings);

I would like the List Messages to have both Message and DerivedMessage objects.

Any ideas? Thanks

Was it helpful?

Solution

Found the solution. I used a custom converter

public class MessageConverter : JsonCreationConverter<ConversationAPI.Message>
{
    private const string SomeOtherStuffField = "sos";

    protected override ConversationAPI.Message Create(Type objectType, JObject jObject)
    {
        if (FieldExists(SomeOtherStuffField , jObject))
        {
            return new ConversationAPI.DerivedMessage ();
        }

        return new ConversationAPI.Message();
    }

    private bool FieldExists(string fieldName, JObject jObject)
    {
        return jObject[fieldName] != null;
    }
}

public abstract class JsonCreationConverter<T> : JsonConverter
{
    /// <summary>
    /// Create an instance of objectType, based properties in the JSON object
    /// </summary>
    /// <param name="objectType">type of object expected</param>
    /// <param name="jObject">contents of JSON object that will be deserialized</param>
    /// <returns></returns>
    protected abstract T Create(Type objectType, JObject jObject);

    public override bool CanConvert(Type objectType)
    {
        return typeof(T).IsAssignableFrom(objectType);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        // Load JObject from stream
        JObject jObject = JObject.Load(reader);

        // Create target object based on JObject
        T target = Create(objectType, jObject);

        // Populate the object properties
        serializer.Populate(jObject.CreateReader(), target);

        return target;
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

You would use this like so:

var jsonText = "{a string of json to convert}"
JsonConverter[] conv = new JsonConverter[] { new MessageConverter() };
var jsonResponse = JsonConvert.DeserializeObject<ConversationAPI>(jsonText, conv);

OTHER TIPS

I took the time to test the code that @Mihai published. I like this solution because it does not change the content of the json file; serialization is as usual (no $type or other attributes are added). De-serialization determines if the object is base or derived by checking if a derived field exists in the json. This is not bulletproof but works well in most cases.

I had to fix some syntax to make it run and to understand how it works. Here is the modified code with a working usage example:

using System;
using System.Collections.Generic;
using System.IO;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace DerivedDeSerJson
{
    [Serializable]
    public class Message
    {
        public string Text { get; set; }
    }

    [Serializable]
    public class DerivedMessage : Message
    {
        public string SomeOtherStuff { get; set; }
    }

    public class ConversationAPI
    {
        public DateTime LastUpdated { get; set; }

        public List<Message> Messages { get; set; }
    }

    public class MessageConverter : JsonCreationConverter<Message>
    {
        private const string SomeOtherStuffField = "SomeOtherStuff";

        protected override Message Create(Type objectType, JObject jObject)
        {
            if (FieldExists(SomeOtherStuffField, jObject))
            {
                return new DerivedMessage();
            }

            return new Message();
        }

        private bool FieldExists(string fieldName, JObject jObject)
        {
            return jObject[fieldName] != null;
        }
    }

    public abstract class JsonCreationConverter<T> : JsonConverter
    {
        /// <summary>
        /// Create an instance of objectType, based properties in the JSON object
        /// </summary>
        /// <param name="objectType">type of object expected</param>
        /// <param name="jObject">contents of JSON object that will be deserialized</param>
        /// <returns></returns>
        protected abstract T Create(Type objectType, JObject jObject);

        public override bool CanConvert(Type objectType)
        {
            return typeof(T).IsAssignableFrom(objectType);
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            // Load JObject from stream
            JObject jObject = JObject.Load(reader);

            // Create target object based on JObject
            T target = Create(objectType, jObject);

            // Populate the object properties
            serializer.Populate(jObject.CreateReader(), target);

            return target;
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            throw new NotImplementedException();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            ConversationAPI conversation = new ConversationAPI()
            {
                LastUpdated = DateTime.Now,
                Messages = new List<Message>()
                {
                   new Message() {Text = "Msg1"},
                   new DerivedMessage() {Text = "Msg2", SomeOtherStuff = "stuff"},
                }
            };
            string jsonText;
            JsonSerializer serializer = new JsonSerializer() { Formatting = Formatting.Indented };
            using (TextWriter text = new StringWriter())
            using (JsonWriter writer = new JsonTextWriter(text))
            {
                serializer.Serialize(writer, conversation);
                jsonText = text.ToString();
            }
            Console.WriteLine(jsonText);
            //Output:
            //{
            //    "LastUpdated": "2020-06-08T17:05:33.7114095+03:00",
            //    "Messages": 
            //    [
            //        { "Text": "Msg1" },
            //        { "SomeOtherStuff": "stuff", "Text": "Msg2" }
            //    ]
            //}

            JsonConverter[] conv = new JsonConverter[] { new MessageConverter() };
            ConversationAPI jsonResponse = JsonConvert.DeserializeObject<ConversationAPI>(jsonText, conv);

            foreach (var msg in jsonResponse.Messages)
            {
                Console.WriteLine(msg.Text);
                Console.WriteLine(msg.ToString());      // Print type name
            }
            //Output:
            // Msg1
            // DerivedDeSerJson.Message
            // Msg2
            // DerivedDeSerJson.DerivedMessage
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top