Newtonsoft Json Serialize Timespan
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 upHave 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
edited
Using the extension method 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
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! |
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:
|
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 Viltersten4 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.
Newtonsoft Json Serialize To Stream
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);
KevinKevinThese 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:
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.