JSON.NET cannot handle simple array deserialization?

arrays, deserialization, json.net, strong-typing

Solution

Got the same issue, I used `List<T>` instead of `T[]` to fix it.

Problem

I created a simple class with one field. `class Test{int value;}` If I use the "preserve references" feature and set it to "all" (i.e. both objects and arrays), then when I simply serialize an array of Test objects, it gets serialized as a JSON object with a special "$values" member with the array values, along with the expected "$id" property to preserve the array reference. That much is fine, but once again the whole thing breaks on deserialization. Stepping through the source code, I discovered that simply because the test for "`IsReadOnlyOrFixedSize`" is true, it sets a flag "`createdFromNonDefaultConstructor`" to true, which doesn't even make any sense, because although it is a fixed size array, it is created from a default constructor, unless it considers any fixed size array constructor a non-default constructor. The bottom line is that it should be able to handle something so basic, and yet it throws this error: "`Cannot preserve reference to array or readonly list, or list created from a non-default constructor`". How can I deserialize a basic array while preserving all references in JSON.NET without getting an error?

Original source

Related problems