I've created a JsonConverter for one of my objects to write and parse an added property. I've followed the example provided with the documentation for CustomJsonConverter but there seems to either be a bug, or some missing instructions. I can see that ids and refs are getting added to the JSON output. The problem is it appears that the context has been lost once we enter into the custom converter; thus resetting the id counter to 1. Additionally, a reference to an already converted parent class was not picked up thus causing a new instance in the JSON output.
I've attached an NUnit Test that demonstrates my trouble. I've gone extra deep on the nesting just to be thorough. Only the test that passes is where you start the JSON serialize with the class that's used by the converter. Here is an example of failed output where you can see that object C which gets the extra data from the custom converter has an incorrect $id.
Object structure is A contains a B contains a C contains a D contains an E. All child objects have a reference to their parent.
JSON results:
{
"$id": "1",
"KidB": {
"$id": "2",
"ParentA": {
"$ref": "1"
},
"KidC": {
"ExtraThing": "Foo!",
"$id": "1", <-- Should have been 3
"ParentB": { <-- Should have been a reference
"$id": "2", <-- Symptoms of lost context continue.
"ParentA": {
"$id": "3",
"KidB": {
"$ref": "2"
}
},
"KidC": {
"$ref": "1"
}
},
"KidD": {
"$id": "4",
"ParentC": {
"$ref": "1"
},
"KidE": {
"$id": "5",
"ParentD": {
"$ref": "4"
}
}
}
}
}
}
I've tried experimenting with the reference resolving that's exposed in JsonSerializer that's passed into the converter, but it complains only being available to internals.
I've attached an NUnit Test that demonstrates my trouble. I've gone extra deep on the nesting just to be thorough. Only the test that passes is where you start the JSON serialize with the class that's used by the converter. Here is an example of failed output where you can see that object C which gets the extra data from the custom converter has an incorrect $id.
Object structure is A contains a B contains a C contains a D contains an E. All child objects have a reference to their parent.
JSON results:
{
"$id": "1",
"KidB": {
"$id": "2",
"ParentA": {
"$ref": "1"
},
"KidC": {
"ExtraThing": "Foo!",
"$id": "1", <-- Should have been 3
"ParentB": { <-- Should have been a reference
"$id": "2", <-- Symptoms of lost context continue.
"ParentA": {
"$id": "3",
"KidB": {
"$ref": "2"
}
},
"KidC": {
"$ref": "1"
}
},
"KidD": {
"$id": "4",
"ParentC": {
"$ref": "1"
},
"KidE": {
"$id": "5",
"ParentD": {
"$ref": "4"
}
}
}
}
}
}
I've tried experimenting with the reference resolving that's exposed in JsonSerializer that's passed into the converter, but it complains only being available to internals.