I want to deserialize a string which is actually an array of object, this is the output of the serialization
[
{
\"CallType\":1,\
"ExecutionStart\":\"2018-07-03T12:25:55.1919951+03:00\",\
"ExecutionEnd\":\"2018-07-03T12:25:55.3980081+03:00\",\
"UnitExecutionStart\":\"0001-01-01T00:00:00\",\
"OverallExecution\":205
}
]
This is the object to which I want do deserialize
[JsonObject]
public class PerformanceMetricsItemDtoX
{
public PerformanceMetricsItemDtoX()
{
}
public CallType CallType { get; } //=> CallType is an enum
public DateTime ExecutionStart { get; }
public DateTime ExecutionEnd { get; }
public DateTime UnitExecutionStart { get; }
public long OverallExecution { get; }
}
After deserializing
var result = value.SelectMany(item =>
JsonConvert.DeserializeObject<List<PerformanceMetricsItemDtoX>>(item));
The final object result has default values so it does not retain the value that is stored in the serialized version.
What am i doing wrong ?
Thanks
Btw I've tried using
var result = new System.Web.Script.Serialization.JavaScriptSerializer()
.Deserialize<List<PerformanceMetricsItemDtoX>>(value.FirstOrDefault());
but the output is the same.
Your PerformanceMetricsItemDtoX
properties are readonly, so JsonConvert
(and no one, actually) can't assign any value to them, after constructor is invoked. Use this:
[JsonObject]
public class PerformanceMetricsItemDtoX
{
public CallType CallType { get; set; } //=> CallType is an enum
public DateTime ExecutionStart { get; set; }
public DateTime ExecutionEnd { get; set; }
public DateTime UnitExecutionStart { get; set; }
public long OverallExecution { get; set; }
}