簡體   English   中英

Json.NET - 序列化沒有屬性名稱的泛型類型包裝器

[英]Json.NET - Serialize generic type wrapper without property name

我有一個通用類型,它包裝一個原始類型以賦予它值相等語義

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<T>類型排除Value屬性,以便上面的示例輸出

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

我寧願具有單個JsonConverter ,可以處理所有ValueObject<T>而不是必須定義一個單獨的JsonConverter每個ValueObject<T>子類

我的第一次嘗試是

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

您可以使用類似於Json.Net: Serialize/Deserialize property as a value, not as an object 中所示的自定義JsonConverter來執行此操作。 但是,由於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 fiddle #1在這里

或者,您可以考慮添加一個非泛型方法來訪問作為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在這里

筆記:

  • ReadJson()假設Value<T>每個子類都有一個公共構造函數,它接受一個T類型的參數。

  • 使用[JsonConverter(typeof(ValueConverter))]將轉換器直接應用於ValueType<T>將有稍微好一點的性能,因為CanConvert永遠不需要被調用。 有關詳細信息,請參閱性能提示:JsonConverters

  • 您需要決定如何處理null JSON 令牌。 它應該導致一個空的ValueType<T> ,還是一個分配的ValueType<T>和一個空的Value

  • ValueType<T>的第二個版本中,我顯式實現了IHasValue.GetValue()以阻止在靜態類型代碼中使用ValueType<T>實例的情況下使用它。

  • 如果您真的只想將轉換器應用於子類ValueObject<T>而不是ValueObject<T>本身,請在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<T>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM