我使用以下类通过两个ASP.NET服务交换JSON数据:
[DataContract]
public class Filter
{
[DataMember]
public string Name {get; set;}
[DataMember]
public FilterOperator Operator {get; set;}
[DataMember]
public object Value {get; set;}
}
问题在于:如果我在DateTime
内设置Value
,则会将其反序列化为字符串:
Value = "/Date(1476174483233+0200)/"
这可能是因为反序列化器无法知道最初序列化时值的类型:
JSON = {"Value":"\/Date(1476174483233+0200)\/"}
正如here所解释的那样,DataContractJsonSerializer
在__type
属性的帮助下支持多态性。
我尝试在课程顶部添加[KnownType(typeof(DateTime))]
属性,但它没有帮助。
但是,如果我在Tuple<DateTime>
内设置Value
属性(以及类中相应的KnownType
属性),则它可以正常工作(正确反序列化的值):
Value = {(10/11/2016 10:49:30 AM)}
在JSON内部,__type
被发出
JSON = {
"Value": {
"__type" : "TupleOfdateTime:#System",
"m_Item1" : "\/Date(1476175770028+0200)\/"
}
}
有没有办法强制DataContractJsonSerializer
发出正确的信息来正确序列化/反序列化DateTime
(这意味着序列化后我得到DateTime
而不是字符串)?
我尝试在EmitTypeInformation = EmitTypeInformation.Always
中设置DataContractJsonSerializerSettings
,但这没有帮助。
答案 0 :(得分:1)
问题是DataContractJsonSerializer
仅为对应于JSON对象的类型插入多态类型提示属性"__type"
- 由{
和{包围的无序名称/值对集合{1}}。如果类型映射到其他任何东西(即JSON数组或基元),那么就没有地方可以插入类型提示。此限制记录在Stand-Alone JSON Serialization:
类型提示仅适用于复杂类型
无法为非复杂类型发出类型提示。例如,如果操作具有Object返回类型但返回Circle,则JSON表示可以如前所示,并保留类型信息。但是,如果返回Uri,则JSON表示是一个字符串,并且用于表示Uri的字符串将丢失。这不仅适用于原始类型,也适用于集合和数组。
因此,您需要做的是修改您的}
类以序列化和反序列化一个通用代理对象,使其值封装值的类型信息,沿着this question for Json.Net中的那一行:
Filter
现在你的JSON看起来像是:
[DataContract]
public class Filter
{
[DataMember]
public string Name { get; set; }
[DataMember]
public FilterOperator Operator { get; set; }
[IgnoreDataMember]
public object Value { get; set; }
[DataMember]
TypedSurrogate TypedValue
{
get
{
return TypedSurrogate.CreateSurrogate(Value);
}
set
{
if (value is TypedSurrogate)
Value = ((TypedSurrogate)value).ObjectValue;
else
Value = value;
}
}
}
[DataContract]
// Include some well-known primitive types. Other types can be included at higher levels
[KnownType(typeof(TypedSurrogate<string>))]
[KnownType(typeof(TypedSurrogate<bool>))]
[KnownType(typeof(TypedSurrogate<byte>))]
[KnownType(typeof(TypedSurrogate<sbyte>))]
[KnownType(typeof(TypedSurrogate<char>))]
[KnownType(typeof(TypedSurrogate<short>))]
[KnownType(typeof(TypedSurrogate<ushort>))]
[KnownType(typeof(TypedSurrogate<int>))]
[KnownType(typeof(TypedSurrogate<long>))]
[KnownType(typeof(TypedSurrogate<uint>))]
[KnownType(typeof(TypedSurrogate<ulong>))]
[KnownType(typeof(TypedSurrogate<float>))]
[KnownType(typeof(TypedSurrogate<double>))]
[KnownType(typeof(TypedSurrogate<decimal>))]
[KnownType(typeof(TypedSurrogate<DateTime>))]
[KnownType(typeof(TypedSurrogate<Uri>))]
[KnownType(typeof(TypedSurrogate<Guid>))]
[KnownType(typeof(TypedSurrogate<string[]>))]
public abstract class TypedSurrogate
{
protected TypedSurrogate() { }
[IgnoreDataMember]
public abstract object ObjectValue { get; }
public static TypedSurrogate CreateSurrogate<T>(T value)
{
if (value == null)
return null;
var type = value.GetType();
if (type == typeof(T))
return new TypedSurrogate<T>(value);
// Return actual type of subclass
return (TypedSurrogate)Activator.CreateInstance(typeof(TypedSurrogate<>).MakeGenericType(type), value);
}
}
[DataContract]
public class TypedSurrogate<T> : TypedSurrogate
{
public TypedSurrogate() : base() { }
public TypedSurrogate(T value)
: base()
{
this.Value = value;
}
public override object ObjectValue { get { return Value; } }
[DataMember]
public T Value { get; set; }
}