I saw other similar questions/answers but none show both serialization/deserialization
Example:
public class DeepNested {
[JsonProperty]
int X { get; }
[JsonProperty]
int Y { get; }
public DeepNested(int x, int y) { X = x; Y = y; }
[JsonConstructor]
public DeepNested(DeepNested dn) { X = dn.X; Y = dn.Y; }
}
public class Nested {
[JsonProperty]
DeepNested DN { get; }
[JsonProperty]
int Z { get; }
[JsonProperty]
int K { get; }
[JsonConstructor]
public Nested(DeepNested dn, int z, int k) { DN = new DeepNested(dn); Z = z; K = k; }
}
public class C {
[JsonProperty]
Nested N { get; }
[JsonConstructor]
public C(Nested n) { N = n; }
}
class Program {
static void Main(string[] args) {
var deepNested = new DeepNested(1,2);
var nested = new Nested(deepNested, 3, 4);
C c = new C(nested);
string json = JsonConvert.SerializeObject(c);
C c2 = JsonConvert.DeserializeObject<C>(json);
Console.WriteLine(json);
}
}
I get an exception on DeepNested.DeepNested(DeepNested dn)
System.NullReferenceException: 'Object reference not set to an instance of an object.'
The debugger shows dn is null
This seems be a serious limitation of Json.NET unless I'm missing something ?