I have an issue that I am trying to understand regarding the way that DateTimeOffset values are serialized and deserialized using DataContractJsonSerializer and Json.NET's JsonConvert.
I have the following class
[DataContract]
public class TestToSeailize
{
[DataMember]
public DateTimeOffset SaveDate { get; set; }
}
I can serialize this using DataContractJsonSerializer:
TestToSeailize item = new TestToSeailize()
{
SaveDate = new DateTimeOffset(2020 , 06, 05 , 3 ,0, 0, TimeSpan.FromHours(5))
};
DataContractJsonSerializer serializer = new DataContractJsonSerializer(item.GetType(), settings);
using (MemoryStream ms = new MemoryStream())
{
serializer.WriteObject(ms, item);
var json = Encoding.UTF8.GetString(ms.ToArray());
Console.WriteLine(json);
return json;
}
this results in the following json
{"SaveDate":{"DateTime":"\/Date(1591308000000)\/","OffsetMinutes":300}
and using Json.NET I can do the following
TestToSeailize item = new TestToSeailize()
{
SaveDate = new DateTimeOffset(2020, 06, 05, 3, 0, 0, TimeSpan.FromHours(5))
};
string json = JsonConvert.SerializeObject(item);
this results in the following json
{"SaveDate":"2020-06-05T03:00:00+05:00"}
Why do these produce different json? Is there a way I can change the DataContract Serialization to product the same json that Json.NET does?
The actual issue I am trying to solve is to get the data serialized by DataContractJsonSerializer to be deserialized by the JsonConvert.DeserialzeObject method.
The JSON generated by
DataContractJsonSerializerforDateTimeOffsetandDateTimeis as documented. From Dates/Times and JSON:And from DateTime Wire Format:
For Newtonsoft see the documentation page Serializing Dates in JSON for a discussion of how it serializes dates and times. By default ISO 8601 format strings are used but several formats are supported.
Now, it is possible to customize the data contract
DateTimeformat by settingDataContractJsonSerializerSettings.DateTimeFormat:However the result for
DateTimeOffsetis as follows:Which is not the simple string you seek. There doesn't seem to be any documented way to override the serialization format for
DateTimeOffset. Demo fiddle #1 here.Since you wrote, The actual issue I am trying to solve is to get the data serialized by DataContractJsonSerializer to be deserialized by the JsonConvert DeserialzeObject method, it will be much easier to configure Json.NET to deserialize the
DataContractJsonSerializerformat. First, define the following customJsonConverter:Now you can deserialize the JSON generated by
DataContractJsonSerializerby adding the converter toJsonSerializerSettings.Converters:Notes:
If don't want to serialize in
DataContractJsonSerializerformat, passcanWrite : falseto the converter's constructor.If there is a possibility of getting ISO 8601 strings as well as complex objects for
DateTimeOffsetvalues, you may need to configureJsonSerializerSettings.DateParseHandling = DateParseHandling.NoneorDateParseHandling.DateTimeOffsetat a higher code level to avoid premature deserialization of ISO 8601 strings asDateTimeobjects byJsonTextReader.Demo fiddle #2 here.