Newtonsoft Json Serialize Timespan

Active9 months ago
  1. Newtonsoft Json Serialize Byte Array
  2. Newtonsoft Json Serialize Dynamic
  3. Newtonsoft Json Serialize Object
  4. Newtonsoft Json Serialize Deserialize
  5. Json Serialize Javascript
  6. Newtonsoft Json Serialize Timespan

May 25, 2012  Deserializing TimeSpan using JSON.net and.net WebApi [Answered] RSS. Came across this while trying to go the other direction i.e. Had problems with DateTimes and also Decimals. Tiny modification involving copying the MediaTypeMapping from the default converter. Serializing Dates in JSON. With no standard for dates in JSON, the number of possible different formats when interoping with other systems is endless. Converts the to its JSON string representation. Namespace: Newtonsoft.Json Assembly: Newtonsoft.Json (in Newtonsoft.Json.dll) Version: 12.0.1.

Json.NET is a popular high-performance JSON framework for.NET. JSON.NET is a great library for serializing objects to and from json strings. In case you need to have a more control of how your object is being serialized this post. Deserializing TimeSpan using JSON.net and.net WebApi. The solution was in configuring the WebApi project to leverage JSON.net for serialization by default in the. The quickest method of converting between JSON text and a.NET object is using the T:Newtonsoft.Json.JsonSerializer. The JsonSerializer converts.NET objects into their JSON equivalent and back again by mapping the.

We are sending JSON to an API defined by swagger that some properties are DateTime in the format yyyy-MM-ddThh:mm:ss.000Z (the milliseconds must be 3 digits or it fails validation at the endpoint) and some are Date (no time) properties.

I have seen many messages saying use the formatters like this:

but this does not convert the DateTimes into the correct format, and how does C# deal with a Date only type? It always seems to serialise as DateTime.MinValue()

Here is an example:

Someone sends me json as string but the the dates and datetimes in the incorrect format to be sent to the endpoint. I was hoping that the swagger class and json deserialisation would format them but it is not.

This is the swagger generated class

So I try and coerce the json to be correct but I'm doing it wrong or something is missing

DomBat
DomBatTimespanDomBat
1,0813 gold badges20 silver badges41 bronze badges

2 Answers

As I mentioned in a comment, there is no standard date representation in JSON. The ISO8601 is the de-facto standard, ie most people started using this some years ago. ISO8601 does not require milliseconds. If the other endpoint requires them, it's violating the defacto standard.

Json.NET uses IOS8601 since version 4.5. The current one is 10.0.3. The following code :

returns

On my machine. Notice the timezone offset. That's also part of the standard. Z means UTC.

You can specify your own time format, provided it's the correct one. In this case, it should be yyyy-MM-ddTH:mm:ss.fffZ. Notice the fff for milliseconds andHH for 24-hour.

The following code

returns

Newtonsoft Json Serialize Byte Array

The format string does not force a timezone translation. You can tell Json.NET to treat the time as Local or Utc through the DateTimeZoneHandling setting :

Returns :

UPDATE

As Matt Johnson explains, Z is just a literal, while K generates either Z or an offset, depending on the DateTimeZoneHandling setting.

The format string yyyy-MM-ddTH:mm:ss.fffK with DateTimeZoneHandling.Utc :

Will return :

Changing to DateTimeZoneHandling.Utc will return

Which, by the way is the default behaviour of Json.NET, apart from the forced millisecond precision.

Finally, .NET doesn't have a Date-only type yet. DateTime is used for both dates and date+time values. You can get the date part of a DateTime with the DateTime.Date property. You can retrieve the current date with DateTime.Today.

Newtonsoft Json Serialize Dynamic

Time of day is represented by the Timespan type. You can extract the time of day from a DateTime value with DateTime.TimeOfDay. Timespan isn't strictly a time-of-day type as it can represent more than 24 hours.

What was that yet?

Support for explicit Date, TimeOfDay is comming through the CoreFX Lab project. This contains 'experimental' features that are extremely likely to appear in the .NET Runtime like UTF8 support, Date, String, Channles. Some of these already appear as separate NuGet packages.

One can use the System.Time classes already, either by copying the code or adding them through the experimental NuGet source

Panagiotis KanavosPanagiotis Kanavos
63k6 gold badges89 silver badges123 bronze badges

Get current universaltime to json date time format and vice versa:

Here are both methods:

kumar kashyap pandeykumar kashyap pandey

Not the answer you're looking for? Browse other questions tagged c#jsondatedatetimejson.net or ask your own question.

Active7 years, 6 months ago

I use Fiddler to test my WCF Rest. I always get

with this post value:

I get the error in 'Duration' value. I've been searching on net but no luck at all.
I hope I'll find the answer here. Thanks a lot!

Will Hartung
97.2k18 gold badges112 silver badges189 bronze badges
fiberOpticsNewtonsoft serialize object to jsonfiberOptics
3,17420 gold badges57 silver badges97 bronze badges

2 Answers

The simple approach is to parse the timespan as a string and converting to a TimeSpan using its static 'parse' routine.

With JSON and WCF you are relying on the JSON Serialiser to convert objects back and forth, unfortunately once you start 'moving' away from native object types, i.e. strings, numerics, and into specific object, it tends to choke unless you use the exact format.

Personally, I've had no experience of passing Timespan's through the DataContractJsonSerializer what format is required, however this post will highlight the exact format along with whether it is possible

Newtonsoft Json Serialize Timespan

Newtonsoft Json Serialize Object

Community
SeanCocteauSeanCocteau

What is the .NET type of 'sub'? It looks like a collection or array type of some sort; what is the type that .NET expects those elements to have? Does that type have the properties Type, StartDate, EndDate, and Duration? Does it have any other properties that are marked with [IsRequiredAttribute] but not present?

If you craft up a fresh DataContractJsonSerializer (type = TimeSpan), and try to deserialize just the string '12:12:12', what happens then?

Newtonsoft Json Serialize Deserialize

I am not giving you an answer yet -- but I think doing these exercises should not solve your problem now, but it will also help diagnose future errors you may encounter in the deserialization of this string.

See also Deserialize array values to .NET properties using DataContractJsonSerializer for some pointers

Community
krisragh MSFT

Json Serialize Javascript

krisragh MSFT
1,8881 gold badge12 silver badges21 bronze badges

Newtonsoft Json Serialize Timespan

Not the answer you're looking for? Browse other questions tagged c#jsonwcf or ask your own question.