Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass TimeSpan value in JSON format?

Tags:

json

c#

wcf

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

HTTP/1.1 400 Bad Request  

with this post value:

{
    "session":{
        "Session":"088a688d-ea69-4264-9266-381e9e540d00",
        "LoginID":"testid",
        "Serial":"testserial"
    },
    "sub":[
        {
            "Type":0,
            "StartDate":"\/Date(1319731200000+0800)\/",
            "EndDate":"\/Date(1319731200000+0800)\/",
            "Duration":"12:12:12"
        }
    ]
}  

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!

like image 817
fiberOptics Avatar asked Feb 21 '12 08:02

fiberOptics


1 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

like image 184
SeanCocteau Avatar answered Nov 04 '22 04:11

SeanCocteau