I have an object with the following property (it has miliseconds since 1970):
public double AsOfDate { get; set; }
When I serialize it (in ASP .NET MVC4 application) it's fine. However when I later send back the JSON object to the server, I get the deserialization error:
Error reading date. Unexpected token: Integer. Path 'performance.asOfDate', line 1, position 95.
And stack trace:
at Newtonsoft.Json.JsonReader.ReadAsDateTimeInternal() at Newtonsoft.Json.JsonTextReader.ReadAsDateTime() at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType(JsonReader reader, JsonContract contract, Boolean hasConverter) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ResolvePropertyAndConstructorValues(JsonObjectContract contract, JsonProperty containerProperty, JsonReader reader, Type objectType) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObjectFromNonDefaultConstructor(JsonReader reader, JsonObjectContract contract, JsonProperty containerProperty, ConstructorInfo constructorInfo, String id) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateNewObject(JsonReader reader, JsonObjectContract objectContract, JsonProperty containerMember, JsonProperty containerProperty, String id, Boolean& createdFromNonDefaultConstructor) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ResolvePropertyAndConstructorValues(JsonObjectContract contract, JsonProperty containerProperty, JsonReader reader, Type objectType) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObjectFromNonDefaultConstructor(JsonReader reader, JsonObjectContract contract, JsonProperty containerProperty, ConstructorInfo constructorInfo, String id) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateNewObject(JsonReader reader, JsonObjectContract objectContract, JsonProperty containerMember, JsonProperty containerProperty, String id, Boolean& createdFromNonDefaultConstructor) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
So it seems that during deserialization phase, JSON library is trying to parse asOfDate as DateTime only because it has Date string in the name? Is there a way to resolve this issue? Here's sample request body:
{
"performance":{
"performanceSinceInception":0.76,
"performanceMtd":0.41,
"asOf":1382486400000,
"inceptionDate":1359676800000,
"monthPerformanceStart":1380585600000
},
"rating":1,
"isActive":false,
"effectiveFrom":1359676800000,
"effectiveTo":1383919343000,
"globalValidationFlag":true,
"whitelistValidationFlag":false,
"canBeReactivated":false,
"canBeRemoved":false,
"belongsToMainBucket":true
}
I've tested it, and when I change property name from asOfDate to asOf it works. So I blame convention based deserialization. Is there a way to override it ?