This closed ticket looks like it does not work in some cases: [IMPLEMENTED] Squidex.ClientLibrary: Json serializes dates to incorrect format
I’m using the Squidex.ClientLibrary nuget version 3.9.0
Setting DateTime.Now to a property makes it not serialize properly for some reason.
For example, TimeOne was set in the UI to now, while TimeTwo was set in code:
item.TimeTwo = DateTime.Now;
Result:
"TimeOne": {
"iv": "2019-09-20T17:54:12Z"
},
"TimeTwo": {
"iv": "2019-09-24T10:43:20.8948344-04:00"
}
My solution was to copy most of the invariantConverter but extend ISODateConverter, but I’m sure something more elegant can be done.
public sealed class DateInvariantConverter : IsoDateTimeConverter
{
public DateInvariantConverter()
{
base.DateTimeFormat = "yyyy-MM-ddTHH:mm:ssZ";
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteStartObject();
writer.WritePropertyName("iv");
base.WriteJson(writer, value, serializer);
writer.WriteEndObject();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
reader.Read();
if (reader.TokenType != JsonToken.PropertyName || !string.Equals(reader.Value.ToString(), "iv", StringComparison.OrdinalIgnoreCase))
{
throw new JsonSerializationException("Property must have a invariant language property.");
}
reader.Read();
var result = serializer.Deserialize(reader, objectType);
reader.Read();
return result;
}
public override bool CanConvert(Type objectType)
{
return false;
}
}
Using this converter it works properly. The deserialization is just copied, it does not have a problem.