C# Json.NET-序列化不带属性名的泛型类型包装器
我有一个泛型类型,它包装了一个原语类型,为它提供了值相等语义C# Json.NET-序列化不带属性名的泛型类型包装器,c#,json.net,C#,Json.net,我有一个泛型类型,它包装了一个原语类型,为它提供了值相等语义 public class ValueObject<T> { public T Value { get; } public ValueObject(T value) => Value = value; // various other equality members etc... } 从ValueObject继承的每个对象都包装在Value属性中(如预期的那样)。比如说 var custo
public class ValueObject<T>
{
public T Value { get; }
public ValueObject(T value) => Value = value;
// various other equality members etc...
}
从ValueObject
继承的每个对象都包装在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
,从而将Value
属性排除在子类化ValueObject
的类型之外,从而输出上述示例
{
"id": "f5ce21a5-a0d1-4888-8d22-6f484794ac7c",
"email": "some@email.com"
}
我更希望有一个可以处理所有ValueObject
的JsonConverter
,而不是为每个ValueObject
子类定义一个单独的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;
}
}
公共类值ObjectOfConverter:JsonConverter
{
私有静态只读类型ValueObjectGenericType=typeof(ValueObject);
私有静态只读字符串ValuePropertyName=nameof(ValueObject.Value);
公共覆盖布尔CanConvert(类型objectType)=>
IsSubclassOfGenericType(objectType,ValueObjectGenericType);
公共重写对象ReadJson(JsonReader阅读器,类型objectType,对象existingValue,JsonSerializer序列化程序)
{
//将“f5ce21a5-a0d1-4888-8d22-6f484794ac7c”=>“值”转换为“f5ce21a5-a0d1-4888-8d22-6f484794ac7c”
var existingJsonWrappedInValueProperty=newjobject(newjproperty(ValuePropertyName,JToken.Load(reader));
返回现有的JSONWrappedInValueProperty.ToObject(objectType,序列化程序);
}
公共重写void WriteJson(JsonWriter编写器、对象值、JsonSerializer序列化器)
{
//实施
}
私有静态bool IsSubclassOfGenericType(Type-ToCheck,Type-openGenericType)
{
while(typeToCheck!=null&&typeToCheck!=typeof(对象))
{
var cur=typeToCheck.IsGenericType?typeToCheck.GetGenericTypeDefinition():typeToCheck;
if(openGenericType==cur)返回true;
typeToCheck=typeToCheck.BaseType;
}
返回false;
}
}
您可以使用类似于中所示的方法来执行此操作。但是,由于ValueObject
没有非通用方法来获取和设置值作为对象,因此需要使用反射
这里有一种方法:
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;
}
}
}
工作示例.Net fiddle#1
可选地,您可以考虑添加非泛型方法来访问该值作为<代码>对象< /> >,例如:S/
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
}
工作示例.Net fiddle#2
注:
ReadJson()
假设Value
的每个子类都有一个公共构造函数,它使用一个T
类型的参数
- 使用将转换器直接应用于
ValueType
using将有稍好的性能,因为不需要调用。有关详细信息,请参阅
- 您需要决定如何处理
null
JSON令牌。它是否会导致空值值类型
,或分配的值类型
为空值值
- 在
ValueType
的第二个版本中,我明确实现了IHasValue.GetValue()
,以阻止在静态类型代码中使用ValueType
实例的情况下使用它
- 如果确实只想将转换器应用于类型子类化
ValueObject
而不是ValueObject
本身,请在GetValueType(Type objectType)
中添加对的调用。跳过(1)
:
静态类型GetValueType(类型objectType)
{
返回对象类型
.basetypes和self()
.Skip(1)//未子类化时,不要将转换器应用于ValueObject
.Where(t=>t.IsGenericType&&t.GetGenericTypeDefinition()==typeof(ValueObject))
.Select(t=>t.GetGenericArguments()[0])
.FirstOrDefault();
}
然后在JsonSerializerSettings.Converters
中应用转换器,而不是直接应用到ValueObject
您可以使用类似于中所示的工具来完成此操作。但是,由于ValueObject
没有非通用方法来获取和设置值作为对象,因此需要使用反射
这里有一种方法:
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;
}
}
}
工作示例.Net fiddle#1
可选地,您可以考虑添加非泛型方法来访问该值作为<代码>对象< /> >,例如:S/
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
}
工作示例.Net fiddle#2
注:
ReadJson()
假设Value
的每个子类都有一个公共构造函数,它使用一个T
类型的参数
- 使用将转换器直接应用于
ValueType
using将有稍好的性能,因为不需要调用。有关详细信息,请参阅
- 您需要决定如何处理
null
JSON令牌。它是否会导致空值值类型
,或分配的值类型
为空值值
- 在
ValueType
的第二个版本中,我明确实现了IHasValue.GetValue()
,以阻止在静态类型代码中使用ValueType
实例的情况下使用它
- 如果确实只想将转换器应用于类型子类化
ValueObject
而不是ValueObject
本身,请在GetValueType(Type objectType)
中添加对的调用。跳过(1)
:
静态类型GetValueType(类型objectType)
{
返回对象类型
.basetypes和self()
.Skip(1)//未子类化时,不要将转换器应用于ValueObject
.Where(t=>t.IsGenericType&&t.GetGenericTypeDefinition()==typeof(ValueObject))
.Select(t=>t.GetGenericArguments()[0])
.FirstOrDefault();
}
然后应用转换器
var settings = new JsonSerializerSettings
{
Converters = { new ValueConverter() },
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
var customerAsJson = JsonConvert.SerializeObject(customer, Formatting.Indented, settings);
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
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
serializer.Serialize(writer, ((IHasValue)value).GetValue());
}
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();
}