Json.NET-序列化没有属性名称的通用类型包装器 [英] Json.NET - Serialize generic type wrapper without property name

查看:66
本文介绍了Json.NET-序列化没有属性名称的通用类型包装器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通用类型,该类型可以包装一个原始类型以赋予其值相等语义

I have a generic type that wraps a single primitive type to give it value equality semantics

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) { }
}

问题是当序列化像这样的类型时:

The issue is when serializing a type like:

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

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

每个从ValueObject<T>继承的对象都包装在Value属性中(如预期).例如

Each object the inherits from ValueObject<T> is wrapped in a Value property (as expected). For example

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<T>子类型的类型,排除了Value属性,以便上面的示例将输出

Is there a way to write a custom JsonConverter so the the Value property is excluded for types subclassing ValueObject<T> so that the above example would output

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

我宁愿拥有一个可以处理所有ValueObject<T>的单个JsonConverter,而不是必须为每个ValueObject<T>子类定义一个单独的JsonConverter

I would prefer to have a single JsonConverter that can handle all ValueObject<T> rather than having to define a separate JsonConverter for each ValueObject<T> subclass

我的第一次尝试是

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:序列化/反序列化属性中显示的作为一个值,而不是一个对象 .但是,由于ValueObject<T>没有非通用方法来获取和设置Value作为对象,因此您将需要使用反射.

You can do this with a custom JsonConverter similar to the ones shown in Json.Net: Serialize/Deserialize property as a value, not as an object. However, since ValueObject<T> does not have a non-generic method to get and set the Value as an object, you will need to use reflection.

这是一种方法:

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>:

You could then apply the converter directly to ValueType<T> like so:

[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小提琴#1 此处.

Working sample .Net fiddle #1 here.

或者,您可以考虑添加非通用方法以object的形式访问值,例如像这样:

Alternatively, you might consider adding a non-generic method to access the value as an object, e.g. like so:

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() 变得更加简单:

With this addition, WriteJson() becomes much simpler:

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

工作示例.Net小提琴#2 此处.

Working sample .Net fiddle #2 here.

注意:

  • ReadJson()假定Value<T>的每个子类都有一个公共构造函数,该公共构造函数采用类型为T的单个参数.

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

使用 [JsonConverter(typeof(ValueConverter))]将转换器直接应用于ValueType<T> 的性能会稍好一些,因为 CanConvert 永远都不需要打电话.请参见 性能提示:JsonConverters 有关详细信息.

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.

您需要确定如何处理null JSON令牌.它应该导致ValueType<T>为空,还是分配的ValueType<T>Value为空?

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?

ValueType<T>的第二个版本中,我明确实现了IHasValue.GetValue(),以防止在静态类型代码中使用ValueType<T>实例的情况下阻止其使用.

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.

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

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();
}

然后在JsonSerializerSettings.Converters中而不是直接在ValueObject<T>中应用转换器.

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

这篇关于Json.NET-序列化没有属性名称的通用类型包装器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆