System.Text.Json deserialization of arrays with various types
We're trying to migrate from Newtonsoft to System.Text.Json
We have a test case which checks the roundtrip of serializing an object and then deserializing that result, and comparing the deserialized result back to the original object.
The object in question is:
new object[] { 1.1, 23.0, "sdfsd" }
In both Newtonsoft and System.Text.Json, this is serialized into:
"[1.1,23,"sdfsd"]"
When deserializing the above string, Newtonsoft will return an object[] which matches the original.
System.Text.Json deserializes the above string into an object[] with JsonElements which have a ValueKind and value. It's important to note that this only occurs when deserializing an object[] with a variety of types.
Is this by design? I haven't been able to find any documentation regarding the expectations here.
I personally think that the System.Text.Json solution is more useful, and it can easily be mapped to the Newtonsoft result using something as simple as result.Select(element => (object)element.Value).ToArray(). Boxing the values resulting in an object[].
I don't think using arrays with elements of various types (or javascript tuples) for communicating with APIs is that common, nor do I think it makes sense to support that pattern directly in C#, because I think it is an anti-pattern in C#.
Why would you want your values boxed? Why don't you want the proper naming and types that an object offers through properties vs an object[]? Why do you expect to receive such a payload in the first place?
I'm not saying that I don't understand the annoyance of this difference in behaviour, in the context of migrating from Newtonsoft to System.Text.Json
Here's some documentation we have provided on this behavior: https://docs.microsoft.com/dotnet/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to#deserialization-of-object-properties
Benefits
- Provides universal and consistent behavior for deserializing
System.Objectmembers and collection elements. - No "guessing" or ambiguous algorithm for what the expected primitive to return is, such as sometimes treating a JSON string as a
DateTime(detected by parsing), or creating aSystem.Doublefor a JSON number if a decimal point is present (which can cause accuracy issues ifSystem.Decimalis expected). Similarly, creating aSystem.Int64for a JSON number if there is no decimal point can cause range issues if a System.UInt64 or BigInteger is expected.
Downsides
- Potential usability inconvenience when compared with the Newtonsoft.Json experience
- Perf cost due to creating
JsonElement(whereas creating a primitive is cheaper). This perf cost was significant in 3.x-5.0 but has been recently improved in 6.0 (https://github.com/dotnet/runtime/pull/42538)
Overall, we believe that the default behavior provides the best trade-offs and prioritizes correctness over convenience and performance.
Alternatives
- For Newtonsoft-like semantics, please see this sample. There's also a feature request to provide a built-in, opt-in only feature that implements this behavior - https://github.com/dotnet/runtime/issues/29960.
- Utilize samples for
dynamicdeserialization which allows primitives to be returned based on typed variable assignment or explicit casts - https://github.com/dotnet/runtime/pull/42097.