JSON.net(de)序列化无类型属性

时间:2016-08-04 21:20:52

标签: c# json.net

假设我有这样一个类:

public class Example {
    public int TypedProperty { get; set; }
    public object UntypedProperty { get; set; }
}

假设某人出现并写道:

var example = new Example
{
    TypedProperty = 5,
    UntypedProperty = Guid.NewGuid()
}

如果我使用JsonConvert.SerializeObject(example)序列化,我会

{
  "TypedProperty": 5,
  "UntypedProperty": "24bd733f-2ade-4374-9db6-3c9f3d97b12c"
}

理想情况下,我想得到这样的东西:

{
  "TypedProperty": 5,
  "UntypedProperty":
    {
      "$type": "System.Guid,mscorlib",
      "$value": "24bd733f-2ade-4374-9db6-3c9f3d97b12c"
    }
 }

但是TypeNameHandling在这种情况下不起作用。我怎样才能(de)序列化一个无类型的属性?

2 个答案:

答案 0 :(得分:2)

如果您使用TypeNameHandling.AllTypeNameHandling.Auto序列化课程, 然后,当UntypedProperty属性被序列化为JSON容器(对象或数组)时,Json.NET应该通过将类型信息存储在"$type"属性的JSON文件中来正确地序列化和反序列化它。但是,如果UntypedProperty被序列化为JSON原语(字符串,数字或布尔值),则这不起作用,因为正如您所指出的,JSON原语没有机会包含{{1} } property。

当序列化具有类型"$type"的属性的类型时,解决方案是序列化可以封装类型信息的原始值的包装类,沿this answer行。这是注入这样一个包装器的custom JSON converter

object

然后使用[JsonConverter(typeof(UntypedToTypedValueConverter))]将其应用于您的类型:

public class UntypedToTypedValueConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        throw new NotImplementedException("This converter should only be applied directly via ItemConverterType, not added to JsonSerializer.Converters");
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        if (reader.TokenType == JsonToken.Null)
            return null;
        var value = serializer.Deserialize(reader, objectType);
        if (value is TypeWrapper)
        {
            return ((TypeWrapper)value).ObjectValue;
        }
        return value;
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        if (serializer.TypeNameHandling == TypeNameHandling.None)
        {
            Console.WriteLine("ObjectItemConverter used when serializer.TypeNameHandling == TypeNameHandling.None");
            serializer.Serialize(writer, value);
        }
        // Handle a couple of simple primitive cases where a type wrapper is not needed
        else if (value is string)
        {
            writer.WriteValue((string)value);
        }
        else if (value is bool)
        {
            writer.WriteValue((bool)value);
        }
        else
        {
            var contract = serializer.ContractResolver.ResolveContract(value.GetType());
            if (contract is JsonPrimitiveContract)
            {
                var wrapper = TypeWrapper.CreateWrapper(value);
                serializer.Serialize(writer, wrapper, typeof(object));
            }
            else
            {
                serializer.Serialize(writer, value);
            }
        }
    }
}

abstract class TypeWrapper
{
    protected TypeWrapper() { }

    [JsonIgnore]
    public abstract object ObjectValue { get; }

    public static TypeWrapper CreateWrapper<T>(T value)
    {
        if (value == null)
            return new TypeWrapper<T>();
        var type = value.GetType();
        if (type == typeof(T))
            return new TypeWrapper<T>(value);
        // Return actual type of subclass
        return (TypeWrapper)Activator.CreateInstance(typeof(TypeWrapper<>).MakeGenericType(type), value);
    }
}

sealed class TypeWrapper<T> : TypeWrapper
{
    public TypeWrapper() : base() { }

    public TypeWrapper(T value)
        : base()
    {
        this.Value = value;
    }

    public override object ObjectValue { get { return Value; } }

    public T Value { get; set; }
}

如果您无法以任何方式修改public class Example { public int TypedProperty { get; set; } [JsonConverter(typeof(UntypedToTypedValueConverter))] public object UntypedProperty { get; set; } } 类来添加此属性(您的评论该类不是我的改变建议那么多)您可以注入转换器custom contract resolver

Example

使用如下:

public class UntypedToTypedPropertyContractResolver : DefaultContractResolver
{
    readonly UntypedToTypedValueConverter converter = new UntypedToTypedValueConverter();

    // As of 7.0.1, Json.NET suggests using a static instance for "stateless" contract resolvers, for performance reasons.
    // http://www.newtonsoft.com/json/help/html/ContractResolver.htm
    // http://www.newtonsoft.com/json/help/html/M_Newtonsoft_Json_Serialization_DefaultContractResolver__ctor_1.htm
    // "Use the parameterless constructor and cache instances of the contract resolver within your application for optimal performance."
    // See also https://stackoverflow.com/questions/33557737/does-json-net-cache-types-serialization-information
    static UntypedToTypedPropertyContractResolver instance;

    // Explicit static constructor to tell C# compiler not to mark type as beforefieldinit
    static UntypedToTypedPropertyContractResolver() { instance = new UntypedToTypedPropertyContractResolver(); }

    public static UntypedToTypedPropertyContractResolver Instance { get { return instance; } }

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

        foreach (var property in contract.Properties.Concat(contract.CreatorParameters))
        {
            if (property.PropertyType == typeof(object)
                && property.Converter == null)
            {
                property.Converter = property.MemberConverter = converter;
            }
        }
        return contract;
    }
}

在这两种情况下,创建的JSON看起来像:

var settings = new JsonSerializerSettings 
{
    TypeNameHandling = TypeNameHandling.Auto,
    ContractResolver = UntypedToTypedPropertyContractResolver.Instance,
};

var json = JsonConvert.SerializeObject(example, Formatting.Indented, settings);

var example2 = JsonConvert.DeserializeObject<Example>(json, settings);

答案 1 :(得分:0)

查找SerializeWithJsonConverters.htmReadingWritingJSON。 致电:JsonConvert.SerializeObject(example, new ObjectConverter());

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

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
  throw new NotImplementedException();
}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
  Example e = (Example)value;

  writer.WriteStartObject();

  writer.WritePropertyName("TypedProperty");
  writer.WriteValue(e.TypedProperty);

  writer.WritePropertyName("UntypedProperty");
  writer.WriteStartObject();

  writer.WritePropertyName("$type");
  writer.WriteValue(e.UntypedProperty.GetType().FullName);

  writer.WritePropertyName("$value");
  writer.WriteValue(e.UntypedProperty.ToString());

  writer.WriteEndObject();

  writer.WriteEndObject();
}
}