I use the following class to exchange JSON data over two ASP.NET services :
[DataContract]
public class Filter
{
[DataMember]
public string Name {get; set;}
[DataMember]
public FilterOperator Operator {get; set;}
[DataMember]
public object Value {get; set;}
}
Here is the problem : if I set a DateTime
inside Value
, it will be deserialized as string :
Value = "/Date(1476174483233+0200)/"
This is probably because deserializer has no clue to know what was the type of the value when serialized initially :
JSON = {"Value":"\/Date(1476174483233+0200)\/"}
As explained here, DataContractJsonSerializer
supports polymorphism, with the help of the __type
property.
I have tried to add [KnownType(typeof(DateTime))]
attribute on the top of the class but it does not help.
However if I set a Tuple<DateTime>
inside Value
property (and the appropriate KnownType
attribute on the class), it works (the value it deserialized properly) :
Value = {(10/11/2016 10:49:30 AM)}
Inside JSON, __type
is emited
JSON = {
"Value": {
"__type" : "TupleOfdateTime:#System",
"m_Item1" : "\/Date(1476175770028+0200)\/"
}
}
Is there a way to force DataContractJsonSerializer
to emit proper information to serialize/deserialize DateTime
properly (which mean I got a DateTime
after serialization instead of a string) ?
I have try to set EmitTypeInformation = EmitTypeInformation.Always
in DataContractJsonSerializerSettings
but it does not help.