简体   繁体   中英

Json.Net deserialization $type info to object.property

When serializing an object using Json.Net library. With TypeNameHandling we can add type info into JSON like:

var testJson = @"{                              
                              'parameters': [
                                {
                                  'key': 'bool define',
                                  'field': {
                                    '$type': 'SomeCustomType',
                                    'description': 'user defined'
                                  }
                                }
                              ]
                            }";

When I try to deserialize that type to object:

var customObject = JsonConvert.DeserializeObject<CustomType>(json, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.None });

    [DataContract]
    public class CustomType
    {       
        [DataMember(Name = "parameters")]
        public List<Parameter> Parameters { get; set; }
    }

    [DataContract]
    public class Parameter
    {
        [DataMember(Name ="key")]
        public string Key { get; set; }
        [DataMember(Name ="field")]
        public UserCustomType Field { get; set; }
    }

    [DataContract]
    public class UserCustomType 
    {
        [JsonProperty("$type")]
        [DataMember(Name = "$type")]
        public string Type { get; set; }

        [DataMember(Name = "description")]
        public string Description { get; set; }
    }

When I deserialize that JSON string into CustomType, I always get null in a UserCustomType.Type field, but for example description has some value. Is it possible to get that $type field with Deserialize? ( I know that is always we can user reflection, but don't know is there more simple solution for that) ?

By Setting the metadatapropertyhandling to ignore, and you can then serialize/deserialize the property using the PropertyName attribute as $ indicates metadata

var customObject = JsonConvert.DeserializeObject<CustomType>(json, new JsonSerializerSettings { MetadataPropertyHandling = MetadataPropertyHandling.Ignore });

[JsonProperty("$type")]
public string Type{ get; set; }

You can change the class to:

[DataContract]
public class UserCustomType
{
    [JsonProperty("type")]
    [DataMember(Name = "@type")]
    public string @Type { get; set; }

    [DataMember(Name = "description")]
    public string Description { get; set; }
}

You can use the below json:

var testJson = @"{                              
                          'parameters': [
                            {
                              'key': 'bool define',
                              'field': {
                                'type': 'SomeCustomType',
                                'description': 'user defined'
                              }
                            }
                          ]
                        }";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM