My Json Date Lost Its Offset

What Happened

My golf buddies lost in a sand trap

I was using NewtonSoft like so many of us for Json Serialization and Deserialization. In this case I was trying to make sure our DateTimes were being returned in a proper ISO8601 format, defaulting to UTC unless a flag was set. If the flag was set it would then use the offset associated with a customer’s location, never to use the browser’s offset.

Sadly it was always rendering in Postman and SoapUI in my local time zone. Even when I would debug through the code, the return Ok(jsonObject); had the expected time zone and format.

The Fix

While we serialized out object with a JsonSerializerSettings object we were not using one when we deserialized that string to be returned. Our object used only two settings;

  • ContractResolver = new CamelCasePropertyNamesContractResolver()
  • DateParseHandling = DateParseHandling.None

Once I added using our JsonSerializerSettings object to the final deserialize the dates were rendered properly in Postman and SoapUI.

var jsonObject = JsonConvert.DeserializeObject(tempString, jsonSerializerSettings);

About SheldonS

Web developer for over 15 years mainly with Microsoft technologies from classic ASP to .NET 4. Husband, father, and aspiring amateur photographer.

Posted on October 17, 2019, in .NET, C# and tagged , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: