1

I've got an ASP.NET MVC application that serializes a number of different objects using Json.NET. However, I've recently had to change how some of these objects work, principally converting a number of its properties from type double to type double[].

This has worked fine for new instances of that object, but when deserializing the object I get the following JsonSerializationException:

Error converting value 0 to type 'System.Double[]'. Path 'Results[1].Data.BufferMinimumTime', line 1, position 1102.

This error was expected, but I'm unsure of how to handle this. Ideally, all past results could be deserialized and converted into double[] objects (the conversion is fairly simple - the double will always be the same size, and given the double value, the array would take the form {value, 0d, value}), but I'm not sure where to insert this.

I've added a method with the System.Runtime.Serialization.OnDeserializedAttribute attribute to try and catch this before the exception is thrown, with no success.

If anyone has any suggestions to convert this model before it fails, it would be much appreciated.

Dan McElroy
  • 426
  • 4
  • 19
  • Theoretically, you can do the conversion inside your own custom [JsonConverter][1]. [1]: http://www.newtonsoft.com/json/help/html/CustomJsonConverter.htm – Anton Gogolev Oct 19 '15 at 10:47
  • Change your property to an array, and use the converter from [How to handle both a single item and an array for the same property using JSON.net](http://stackoverflow.com/questions/18994685/how-to-handle-both-a-single-item-and-an-array-for-the-same-property-using-json-n) to read older files. – dbc Oct 19 '15 at 11:07
  • This is largely working for me, and I'll write up a solution soon crediting both of you. However, the solution in the answer provided by dbc does not play nice if converting `double[]` to `double[][]`, another issue I'm encountering. Working on resolving this to make sure the first part works, and I'll then answer this question. – Dan McElroy Oct 19 '15 at 16:22

0 Answers0