【问题标题】:Json.NET - Serialize generic type wrapper without property nameJson.NET - 序列化没有属性名称的泛型类型包装器
【发布时间】:2018-05-13 07:56:48
【问题描述】:

我有一个泛型类型,它包装了一个原始类型以赋予它值相等语义

public class ValueObject<T>
{
    public T Value { get; }
    public ValueObject(T value) => Value = value;

    // various other equality members etc...
}

用法如下:

public class CustomerId : ValueObject<Guid>
{
    public CustomerId(Guid value) : base(value) { }
}

public class EmailAddress : ValueObject<string>
{
    public EmailAddress(string value) : base(value) { }
}

问题是在序列化如下类型时:

public class Customer
{
    public CustomerId Id { get; }
    public EmailAddress Email { get; }

    public Customer(CustomerId id, EmailAddress email) 
    { 
        Id = id;
        Email = email;
    }
}

ValueObject&lt;T&gt; 继承的每个对象都包含在Value 属性中(如预期的那样)。例如

var customerId = new CustomerId(Guid.NewGuid());
var emailAddress = new EmailAddress("some@email.com");

var customer = new Customer(customerId, emailAddress);

var customerAsJson = JsonConvert.SerializeObject(customer, Formatting.Indented, new JsonSerializerSettings
{
    ContractResolver = new CamelCasePropertyNamesContractResolver() 
})

结果

{
  "id": {
    "value": "f5ce21a5-a0d1-4888-8d22-6f484794ac7c"
  },
  "email": {
    "value": "some@email.com"
  }
}

有没有办法编写自定义 JsonConverter 以便为子类化 ValueObject&lt;T&gt; 的类型排除 Value 属性,以便上面的示例输出

{
  "id": "f5ce21a5-a0d1-4888-8d22-6f484794ac7c",
  "email": "some@email.com"
}

我希望有一个可以处理所有ValueObject&lt;T&gt;JsonConverter,而不是必须为每个ValueObject&lt;T&gt; 子类定义一个单独的JsonConverter

我的第一次尝试是

public class ValueObjectOfTConverter : JsonConverter
{
    private static readonly Type ValueObjectGenericType = typeof(ValueObject<>);
    private static readonly string ValuePropertyName = nameof(ValueObject<object>.Value);

    public override bool CanConvert(Type objectType) =>
        IsSubclassOfGenericType(objectType, ValueObjectGenericType);

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        // converts "f5ce21a5-a0d1-4888-8d22-6f484794ac7c" => "value": "f5ce21a5-a0d1-4888-8d22-6f484794ac7c"
        var existingJsonWrappedInValueProperty = new JObject(new JProperty(ValuePropertyName, JToken.Load(reader)));
        return existingJsonWrappedInValueProperty.ToObject(objectType, serializer);
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        // to implement
    }

    private static bool IsSubclassOfGenericType(Type typeToCheck, Type openGenericType)
    {
        while (typeToCheck != null && typeToCheck != typeof(object))
        {
            var cur = typeToCheck.IsGenericType ? typeToCheck.GetGenericTypeDefinition() : typeToCheck;
            if (openGenericType == cur) return true;

            typeToCheck = typeToCheck.BaseType;
        }

        return false;
    }
}

【问题讨论】:

标签: c# json.net


【解决方案1】:

您可以使用类似于 Json.Net: Serialize/Deserialize property as a value, not as an object 中所示的custom JsonConverter 来执行此操作。但是,由于ValueObject&lt;T&gt; 没有非泛型方法来获取和设置Value 为对象,因此您需要使用反射。

这是一种方法:

class ValueConverter : JsonConverter
{
    static Type GetValueType(Type objectType)
    {
        return objectType
            .BaseTypesAndSelf()
            .Where(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(ValueObject<>))
            .Select(t => t.GetGenericArguments()[0])
            .FirstOrDefault();
    }

    public override bool CanConvert(Type objectType)
    {
        return GetValueType(objectType) != null;
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        // You need to decide whether a null JSON token results in a null ValueObject<T> or 
        // an allocated ValueObject<T> with a null Value.
        if (reader.SkipComments().TokenType == JsonToken.Null)
            return null;
        var valueType = GetValueType(objectType);
        var value = serializer.Deserialize(reader, valueType);

        // Here we assume that every subclass of ValueObject<T> has a constructor with a single argument, of type T.
        return Activator.CreateInstance(objectType, value);
    }

    const string ValuePropertyName = nameof(ValueObject<object>.Value);

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        var contract = (JsonObjectContract)serializer.ContractResolver.ResolveContract(value.GetType());
        var valueProperty = contract.Properties.Where(p => p.UnderlyingName == ValuePropertyName).Single();
        // You can simplify this to .Single() if ValueObject<T> has no other properties:
        // var valueProperty = contract.Properties.Single();
        serializer.Serialize(writer, valueProperty.ValueProvider.GetValue(value));
    }
}

public static partial class JsonExtensions
{
    public static JsonReader SkipComments(this JsonReader reader)
    {
        while (reader.TokenType == JsonToken.Comment && reader.Read())
            ;
        return reader;
    }
}

public static class TypeExtensions
{
    public static IEnumerable<Type> BaseTypesAndSelf(this Type type)
    {
        while (type != null)
        {
            yield return type;
            type = type.BaseType;
        }
    }
}

然后您可以将转换器直接应用到ValueType&lt;T&gt;,如下所示:

[JsonConverter(typeof(ValueConverter))]
public class ValueObject<T>
{
    // Remainder unchanged
}

或者在设置中应用它:

var settings = new JsonSerializerSettings
{
    Converters = { new ValueConverter() },
    ContractResolver = new CamelCasePropertyNamesContractResolver() 
};
var customerAsJson = JsonConvert.SerializeObject(customer, Formatting.Indented, settings);

工作示例 .Net fiddle #1 here.

或者,您可以考虑添加一个非泛型方法以将值作为object 访问,例如像这样:

public interface IHasValue
{
    object GetValue(); // A method rather than a property to ensure the non-generic value is never serialized directly.
}

public class ValueObject<T> : IHasValue
{
    public T Value { get; }
    public ValueObject(T value) => Value = value;

    // various other equality members etc...

    #region IHasValue Members

    object IHasValue.GetValue() => Value;

    #endregion
}

有了这个添加,WriteJson() 变得更加简单:

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

工作示例 .Net fiddle #2 here.

注意事项:

  • ReadJson() 假定Value&lt;T&gt; 的每个子类都有一个公共构造函数,该构造函数采用T 类型的单个参数。

  • 使用[JsonConverter(typeof(ValueConverter))] 将转换器直接应用到ValueType&lt;T&gt; 的性能会稍好一些,因为永远不需要调用CanConvert。详情请见Performance Tips: JsonConverters

  • 您需要决定如何处理null JSON 令牌。它应该导致一个空的ValueType&lt;T&gt;,还是一个分配的ValueType&lt;T&gt; 和一个空的Value

  • ValueType&lt;T&gt; 的第二个版本中,我明确实现了IHasValue.GetValue(),以阻止在静态类型代码中使用ValueType&lt;T&gt; 实例的情况下使用它。

  • 如果您真的只想将转换器应用于 类型 子类化 ValueObject&lt;T&gt; 而不是 ValueObject&lt;T&gt; 本身,请在 GetValueType(Type objectType) 中添加对 .Skip(1) 的调用:

    static Type GetValueType(Type objectType)
    {
        return objectType
            .BaseTypesAndSelf()
            .Skip(1) // Do not apply the converter to ValueObject<T> when not subclassed
            .Where(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(ValueObject<>))
            .Select(t => t.GetGenericArguments()[0])
            .FirstOrDefault();
    }
    

    然后将转换器应用到JsonSerializerSettings.Converters,而不是直接应用到ValueObject&lt;T&gt;

【讨论】:

  • 对自定义序列化了解不多,第一次尝试是直接操作JObject来避免反射。这就是我使用ReadJson 的地方,这似乎有效,但我遇到了WriteJson 的问题。这个替代方案值得考虑吗? ` public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { var existingJsonWrappedInValueProperty = new JObject(new JProperty("Value"), JToken.Load(reader)));返回现有的JsonWrappedInValueProperty.ToObject(objectType, serializer); } `
  • @kimsagro - 不确定,我需要查看一个完整的、格式正确的示例。 JObject 是一个中间表示,但是上面的转换器避免创建它。
  • @kimsagro - 好的,那么你的问题是什么?是 [如何] 序列化没有属性名称的泛型类型包装器 还是 如何修复我当前的 ValueObjectOfTConverter?如果是第二个,什么不工作?只是需要写WriteJson()吗?如果是这样,您应该可以使用我的答案中的那个。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多