JavaScriptSerializer.Deserialize does not accept a date that has been serialized by JavaScriptSerializer.Serialize

How can I find the source of this problem where the date value serialized by the JavaScriptSerializer cannot be deserialized using 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 an exception String was not recognized as a valid DateTime .

If a date is serialized by a JavaScriptSerializer, then why can't it be deserialized by a JavaScriptSerializer?

+4
source share
1 answer

If the model is of type Model, try specifying the type in the Deserialize call.

 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 can serialize and deserialize error-free dates this way.

+2
source

Source: https://habr.com/ru/post/1386964/


All Articles