Prevent Json.NET from interpreting a string as a date

Viewed 860

I have some attributes returned from a rest service that are served as an array of name-value pairs.

In some cases the value is a date expressed in universal sortable format: { "name": "Modification-Date", "value": "2017-11-13T15:15:13.968Z" }

When it gets parsed by the deserialiser, the value is identified as a date but given that the object the pair is deserialised into has type string for both name and value, the date is then converted to string and it loses precision: "13/11/2017 15:15:13"

This is easily visible by using a converter for the NameValue type.

if (reader.TokenType == JsonToken.StartObject)
{
    var item = JObject.Load(reader);

    return new NameValueFacet()
    {
        Name = item["name"].Value<string>(),
        Value = item["value"].Value<string>()
    };
}

item["value"].Type shows the type is Date.

How do I get to have Json.NET leave it as a string, "unparsed"?

1 Answers

You can try with Newtonsoft. See below.

JsonConvert.DeserializeObject<your_object>(your_json, new IsoDateTimeConverter{ DateTimeFormat = "dd/MM/yyy" });
Related