I have a really simple struct like this.
[Serializable]
public struct Pair<T, K> {
public T First { get; private set; }
public K Second { get; private set; }
public Pair(T first, K second) {
First = first;
Second = second;
}
...
}
Let's say I have a Pair<double, double>
instance. Serialization works fine, and I get the expected {First:5.0,Second:10.0}
for example.
But whenever I try to deserialize, I get the default value of {0; 0}
. When I add the JsonConstructor
attribute to the constructor, everything works as expected.
I would have expected that Json.NET is able to find the constructor by convention in such simple cases, but it seems like no. If there is any other way, I would like to avoid adding this attribute because that would introduce a dependency on Json.NET in this lower-level kind of util library.
Is there any way to do this properly without the JsonConstructor
attribute and also without having to write tons of custom value converter code?
Note 1
I'm aware of the built-in Tuple
types and I'm also guessing that those are supported natively. In this case I have an older codebase which uses this struct quite a lot, so I have no way to use Tuple
currently.