I am having trouble with DateTimeFormat
in Newtonsoft.Json. The API that I am publishing to requires milliseconds on all of the dates. The object I am serializing has a number of date fields, and some of these are null for some of the data.
I can't seem to get the JSON to output both null for the null dates AND inlucde the milliseconds in the fields that have dates. I can get one or other working, but not both together.
Pretty sure it is something small, but I've not managed to get to the bottom of it and I've spent a few days at it.
This correctly outputs null, but does not include milliseconds:
public string ToJson()
{
return JsonConvert.SerializeObject(this, new MinDateTimeConverter()).ToString();
}
public class MinDateTimeConverter : IsoDateTimeConverter
{
public MinDateTimeConverter()
{
DateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.fff'Z'";
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.Value == null)
return DateTime.MinValue;
return (DateTime)reader.Value;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
DateTime dateTimeValue = (DateTime)value;
if (dateTimeValue == DateTime.MinValue)
{
writer.WriteNull();
return;
}
writer.WriteValue(value);
}
}
This is the output:
"dateOfBirth": "1943-03-02T00:00:00Z",
"dateOfDeath": null`
This correctly outputs dates including milliseconds, but null dates are converted to a DateTime
as well:
public string ToJson()
{
return JsonConvert.SerializeObject(this, new IsoDateTimeConverter() { DateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.fff'Z'" }).ToString();
}
This is the output:
"dateOfBirth": "1943-03-02T00:00:00.000Z",
"dateOfDeath": "0001-01-01T00:00:00.000Z"
I've looked at both the below SO articles but can't get them to work together.
Force JSON.NET to include milliseconds when serializing DateTime (even if ms component is zero)
JSON.NET Serialize DateTime.MinValue as null
Any help greatly appreciated.