Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't parse the data using JavaScriptDateTimeConverter

Tags:

json

c#

json.net

I try to parse simple JSON using Json.net

 string inputJson = @"
                {
                 ""modificationTime"" : ""\/Date(1224043200000)\/""
                 }";

And property is defined

[JsonProperty("modificationTime")]
[JsonConverter(typeof(JavaScriptDateTimeConverter))]
public DateTime ModificationTime { get; set; }

But DeserializeObject throw an exception with the following Message: "Unexpected token or value when parsing date. Token: Date, Value: 10/15/2008 04:00:00"

Well, as far as I see it actually has parsed the date, hasn't it? This exception is thrown from the line 68 in the JavaScriptDateTimeConverter.cs:

68 if (reader.TokenType != JsonToken.StartConstructor ||  string.Compare(reader.Value.ToString(), "Date", StringComparison.Ordinal) != 0)
69            throw new Exception("Unexpected token or value when parsing date. Token: {0}, Value: {1}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType, reader.Value));
70    
71          reader.Read();

In this place reader.TokenType is Date and reader.Value.ToString() is 10/15/2008 04:00:00. Any ideas?

like image 605
Kirill Lykov Avatar asked Nov 02 '10 10:11

Kirill Lykov


1 Answers

Json.NET deserializes dates with the format:

"\/Date(1224043200000)\/"

by default. JavaScriptDateTimeConverter is for dates with the format:

new Date(1234567890)
like image 175
James Newton-King Avatar answered Nov 03 '22 19:11

James Newton-King