Json.NET - 序列化没有属性名称的通用类型包装器

9

我有一个通用类型,它将单个基本类型包装起来,以赋予其值相等的语义。

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<T> 继承的对象都被包装在一个 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"
}

我希望有一个通用的JsonConverter来处理所有ValueObject<T>,而不是为每个ValueObject<T>子类定义单独的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;
    }
}

当然,您可以添加自定义的JsonConverter,就像Json.Net: Serialize/Deserialize property as a value, not as an object中所示的那样。但是使用转换器有时可能会干扰多态性和TypeNameHandling。您是否在需要保留原始类型信息的情况下使用ValueObject<T>?为什么只针对继承ValueObject<T>的类型? - dbc
或者,你的困难在于编写JsonConverter.CanConvert方法吗? - dbc
1
我已经更新了问题。我希望不必为每个子类编写自定义转换器。 - kimsagro
1个回答

8
你可以使用类似于自定义的 JsonConverter的方法,就像在Json.Net: 以值的形式序列化/反序列化属性,而不是作为对象中所示的那样。但是,由于ValueObject<T>没有非泛型的方法来获取和设置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<T>,如下所示:
[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 的示例代码 点击这里

或者,您可以考虑添加一个非泛型方法来访问该值作为 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小提琴#2 这里

注:

  • ReadJson() assumes that every subclass of Value<T> has a public constructor taking a single argument of type T.

  • Applying the converter directly to ValueType<T> using [JsonConverter(typeof(ValueConverter))] will have slightly better performance, since CanConvert need never get called. See Performance Tips: JsonConverters for details.

  • You need to decide how to handle a null JSON token. Should it result in a null ValueType<T>, or an allocated ValueType<T> with a null Value?

  • In the second version of ValueType<T> I implemented IHasValue.GetValue() explicitly to discourage its use in cases where an instance of ValueType<T> is used in statically typed code.

  • If you really only want to apply the converter to types subclassing ValueObject<T> and not ValueObject<T> itself, in GetValueType(Type objectType) add a call to .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();
    }
    

    And then apply the converter in JsonSerializerSettings.Converters rather than directly to ValueObject<T>.


对于自定义序列化我不是很了解,所以我的第一次尝试是直接操作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))); return existingJsonWrappedInValueProperty.ToObject(objectType, serializer); } - kimsagro
@kimsagro - 不确定,我需要看到一个完整的、格式正确的例子。JObject是一种中间表示形式,上述转换器避免了创建它。 - dbc
@kimsagro - 好的,那么你的问题是什么呢?是“如何在不使用属性名称的情况下序列化通用类型包装器”还是“如何修复我的当前ValueObjectOfTConverter”?如果是后者,有什么问题没有解决吗?只是需要编写WriteJson()吗?如果是这样,你应该能够使用我答案中的那个。 - dbc

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接