How to implement custom JsonConverter in JSON.NET to deserialize a List of base class objects?

前端 未结 9 2046
星月不相逢
星月不相逢 2020-11-21 05:07

I am trying to extend the JSON.net example given here http://james.newtonking.com/projects/json/help/CustomCreationConverter.html

I have another sub class deriving

9条回答
  •  萌比男神i
    2020-11-21 05:53

    Using the standard CustomCreationConverter, I was struggling to work how to generate the correct type (Person or Employee), because in order to determine this you need to analyse the JSON and there is no built in way to do this using the Create method.

    I found a discussion thread pertaining to type conversion and it turned out to provide the answer. Here is a link: Type converting.

    What's required is to subclass JsonConverter, overriding the ReadJson method and creating a new abstract Create method which accepts a JObject.

    The JObject class provides a means to load a JSON object and provides access to the data within this object.

    The overridden ReadJson method creates a JObject and invokes the Create method (implemented by our derived converter class), passing in the JObject instance.

    This JObject instance can then be analysed to determine the correct type by checking existence of certain fields.

    Example

    string json = "[{
            \"Department\": \"Department1\",
            \"JobTitle\": \"JobTitle1\",
            \"FirstName\": \"FirstName1\",
            \"LastName\": \"LastName1\"
        },{
            \"Department\": \"Department2\",
            \"JobTitle\": \"JobTitle2\",
            \"FirstName\": \"FirstName2\",
            \"LastName\": \"LastName2\"
        },
            {\"Skill\": \"Painter\",
            \"FirstName\": \"FirstName3\",
            \"LastName\": \"LastName3\"
        }]";
    
    List persons = 
        JsonConvert.DeserializeObject>(json, new PersonConverter());
    
    ...
    
    public class PersonConverter : JsonCreationConverter
    {
        protected override Person Create(Type objectType, JObject jObject)
        {
            if (FieldExists("Skill", jObject))
            {
                return new Artist();
            }
            else if (FieldExists("Department", jObject))
            {
                return new Employee();
            }
            else
            {
                return new Person();
            }
        }
    
        private bool FieldExists(string fieldName, JObject jObject)
        {
            return jObject[fieldName] != null;
        }
    }
    
    public abstract class JsonCreationConverter : JsonConverter
    {
        /// 
        /// Create an instance of objectType, based properties in the JSON object
        /// 
        /// type of object expected
        /// 
        /// contents of JSON object that will be deserialized
        /// 
        /// 
        protected abstract T Create(Type objectType, JObject jObject);
    
        public override bool CanConvert(Type objectType)
        {
            return typeof(T).IsAssignableFrom(objectType);
        }
    
        public override bool CanWrite
        {
            get { return false; }
        }
    
        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;
        }
    }
    

提交回复
热议问题