The format for TimeSpan used by WCF is not the same one used by the Newtonsoft JSON serializer (JSON.NET). You can serialize one TimeSpan instance using the DataContractJsonSerializer (DCJS), and that will be the format required by a WCF REST service. Using Json to Serialize/Deserialize TimeSpan. Ask Question 9. I'm trying to deserialize/serialize a timespan. I use Newtonsoft.json.jsonconvert. So what i do is.
- Newtonsoft Json Serialize Timespan. 7/23/2017 0 Comments With no standard for dates in JSON, the number of possible different formats when interoping with other systems is endless. Fortunately Json.NET has a. Tutorial shows how to Issue JSON Web Token (JWT) in ASP.NET Web API 2,Owin middleware, then build list of Resource Servers relies on the.
- JSON Date and DateTime serialisation in c# & newtonsoft. You can serialize it to Json either as a full ISO8601 string with zero hour,minute, or as a date-only.
- 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.
Active5 years, 6 months ago
I'm trying to find a way to call a WCF method using JSON and pass a TimeSpan as parameter but I always receive a 'Bad request' response from the service.
Here is a snippet code service interface:
Service call:
This is just an example. When calling a service without TimeSpan everything works fine. I need to put it work in order do keep compatibility with other clients that are consuming the service in the typical way.
Response:
The remote server returned an error: (400) Bad Request.
Am I passing the wrong TimeSpan json representation? Or is there a way to define how to deserialize the TimeSpan when the service handles the request?
Thanks in advance
BonomiBonomi
1,1741 gold badge21 silver badges31 bronze badges
2 Answers
The format for
TimeSpan
used by WCF is not the same one used by the Newtonsoft JSON serializer (JSON.NET) . You can serialize one TimeSpan instance using the DataContractJsonSerializer
(DCJS), and that will be the format required by a WCF REST service.
The code below will print out the formats of some
carlosfigueiracarlosfigueira
TimeSpan
values as serialized by both JSON.NET and DCJS:
72.9k14 gold badges116 silver badges163 bronze badges
For anyone who still have a problem with WCF serializer, I have found the solution like this : use your own formatter but make sure clearing all formatter.
рüффп
3,54514 gold badges56 silver badges94 bronze badges
user3334445user3334445
Not the answer you're looking for? Browse other questions tagged .netwcfjsonresttimespan or ask your own question.
Active9 months ago
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
DomBatDomBat
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 :
Newtonsoft Json Serialize To File
returns
On my machine. Notice the timezone offset. That's also part of the standard.
Z
means UTC.
OFF YOUR ANTIVIRUS BEFORE RUN CHEAT. DOWNLOAD HERE: PASS: 1234 ENJOY YOUR GAME. NEW PAYDAY 2 Hack Trainer DLC Unlocker, No Cheater Tag, Infinite How to install: 1.
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
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 :
Newtonsoft Json Serialize Deserialize
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.
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