Newtonsoft Json Serialize Timespan

Posted on by admin
Newtonsoft Json Serialize Timespan Rating: 3,6/5 5115 votes
  1. Newtonsoft Json Serialize To Stream

Deserializes the XmlNode from a JSON string nested in a root element specified by deserializeRootElementName, writes a Json.NET array attribute for collections, and encodes special characters. Unix timestamp converter with newtonsoft.json. GitHub Gist: instantly share code, notes, and snippets.

Join GitHub today

GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.

Sign up New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comments

commented Mar 27, 2018
edited

Using the extension method Microsoft.AspNetCore.Blazor.HttpClientJsonExtensions.GetJsonAsync seems to have issues deserializing timespans.

Server code (asp.net core):

Client code (blazor page):

The JSON sent:

And the exception received:

Note that the page works fine with just datetimes (and for this case a workaround would be to send the start and end date instead of the length). However one would expect that since the server handles timespans perfectly fine and since the client handles datetimes perfectly fine that the client would also handle timespans.

I realize that ultimately this is likely a SimpleJson issue, but since this extension method is likely to be pretty heavily used it's important IMO for it to work for the commonly used core .NET types.

EDIT: After digging a bit it looks like the SimpleJson also didn't support DateTime or DateTimeOffset and this support was added to this repo's fork of it.

added this to Triage in Blazor via automationMar 27, 2018

added the up-for-grabs label Mar 27, 2018

commented Mar 27, 2018

Newtonsoft Json Serialize Timespan

If it's up for grabs I can give it a shot! Just wanted to confirm that it's indeed in the right direction

commented Mar 27, 2018

Sounds good, and thanks for offering to contribute!

added a commit to bdparrish/Blazor that referenced this issue Mar 28, 2018

Blazorautomation moved this from Triage to DoneMar 29, 2018

commented Mar 29, 2018

Ooo thanks @bdparrish. Didn't get a chance to dive into this yet

commented Aug 16, 2018

I solved this problem using this extension:

import nugget Newtonsoft.Json

added a commit to SteveSandersonMS/BlazorMigration that referenced this issue Nov 27, 2018
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment
Active1 month ago

I'm trying to deserialize/serialize a timespan,

but when the json is send it's set to 00:00:00is this even possible to do?

Konrad Viltersten
13.5k35 gold badges152 silver badges279 bronze badges
KevinKevin
2,7974 gold badges21 silver badges43 bronze badges

4 Answers

I tried #Jessycormier's method and it didn't work for me.I ran DataContractJsonSerializer to see what it would generate and I found that gave me a value that looked more like this.

The value shown above was for 1 day, 2 hours, 3 minutes, and 4 seconds.

So it looks like format is:

[-]P[{days}D][T[{hours}H][{min}M][{sec}S]]

Where:

- Indicates negative timespan, omitted for positive values
P must be the first character (unless negative time value)
T must precede the time portion of the timespan.
[] = optional part that may be omitted if 0.

RonnBlackRonnBlack

Newtonsoft Json Serialize To Stream

2,8203 gold badges21 silver badges19 bronze badges

I figured it out, Apparently it's a MS design flaw...

Since TimeSpan cannot be a parameterless object. XML cannot recreate it.

Take a look at this website.http://forums.silverlight.net/forums/p/51793/135450.aspx

So. Therefore TimeSpan cannot be converted. An easy way to do this isto change the timespan into a string, and then send the string over.and use TimeSpan.TryParse(String);

KevinKevin
2,7974 gold badges21 silver badges43 bronze badges

These answers are all outdated, so I thought I would provide an updated better answer. moment.js now directly supports .NET Timespan serialization format.

As of version 2.1.0, this is supported:

John KasterJohn Kaster

If you apply the exact format you can use a TimeSpan. The format is: '0.00:00:00.0000'

Setting a TimeSpan to 30 minutes

This solution works for me. I'm using MVC 4.0 with .Net framework 4.0.

JessycormierJessycormier

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