How can I find the source of this problem, where a date value serialized by JavaScriptSerializer cannot be deserialized by the JavaScriptSerializer?
In the calling application:
var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(model);
// generates this json
{'Guid':'guid','OrderNumber':'1','OrderDate':'\/Date(1299456000000)\/',
'OrderStatus':'Completed','DiscountRate':0.0000}
In the receiving application:
string json = @"{'Guid':'guid','OrderNumber':'1','OrderDate':'\/Date(1299456000000)\/',
'OrderStatus':'Completed','DiscountRate':0.0000}";
var serializer = new JavaScriptSerializer();
var model = serializer.Deserialize(json);
Throws a String was not recognized as a valid DateTime
exception.
If a date is serialized by JavaScriptSerializer then why can it not be deserialized by JavaScriptSerializer?
If model is of type Model then try specifying the type in the call to Deserialize.
string json = @"{'Guid':'guid','OrderNumber':'1','OrderDate':'\/Date(1299456000000)\/',
'OrderStatus':'Completed','DiscountRate':0.0000}";
var serializer = new JavaScriptSerializer();
var model = serializer.Deserialize<Model>(json);
I'm able to serialize and deserialize dates with no errors this way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With