7

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!

Will Hartung
  • 115,893
  • 19
  • 128
  • 203
fiberOptics
  • 6,955
  • 25
  • 70
  • 105
  • What code are you using to produce and parse that data? (The backslash/forward-slash sequences also smell fishy.) – Donal Fellows Feb 21 '12 at 09:34
  • I use, RequestFormat = WebMessageFormat.Json, but I'm sure that there is no problem with StartDate and EndDate, I've tested it many times. – fiberOptics Feb 21 '12 at 10:08
  • BTW, I tried to retrieve data, and I found that duration has this kind of value, "Duration":PT20H8M, which TimeSpan value: 20:08:00, are you familiar with this? Thanks – fiberOptics Feb 21 '12 at 10:09
  • thats [ISO 8601 Time intervals](http://en.wikipedia.org/wiki/ISO_8601#Time_intervals) – Shiplu Mokaddim Feb 21 '12 at 14:52

2 Answers2

3

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

Community
  • 1
  • 1
SeanCocteau
  • 1,838
  • 19
  • 25
  • Thanks for good explanation. Actually I'm about to convert them to string then parse the data later. But still I'm hoping to find easiest way. – fiberOptics Feb 22 '12 at 01:15
-1

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?

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
  • 1
  • 1
krisragh MSFT
  • 1,908
  • 1
  • 13
  • 22
  • Thanks, but I've tested it many times and surely no problem with properties and values EXCEPT the "Duration" value which is a TimeSpan type that (for I think) Json cannot serialize. – fiberOptics Feb 22 '12 at 01:18