Reputation: 3751
I have a problem related to JSON.NET. I have a project on my side and the project itself has been already converted from Entity Framework to JSON values (all the selects and queries are written).
Because of the circular reference problems, I am using JSON.NET. However, when my code is being generated into JSON, the first child of the element gets the $id value first and the parent itself gets the $ref value.
This is a very tackling issue for me and I know I can write my own anonymous queries to avoid this problem. But because of the size of the project, it is too late to change all the queries, because the old programmer has already written the project as it is.
I was wondering if there is a way to set this option in JSON.NET.
Let me explain my problem in this way:
Let's say I have a JSON like this that is coming from JSON.NET:
1-Question $id:1
1-a- Response_Group $id:2
**1-a-a-Response $id: 3**
**1-Response $ref:3**
If I want to change this behavior I want to get this:
1-Question $id:1
1-a- Response_Group $id:2
**1-a-a-Response $ref:3**
**1-Response $id:3**
The upper branch dominates the lower branch.
Is there way to achieve this on JSON.NET side? If there isn't an option in JSON.NET, should I just fork the JSON.NET project and start changing the logic in that way?
If there isn't a way to solve this then I will start writing anonymous queries.
Upvotes: 0
Views: 126
Reputation: 129777
There is not an option to change the way this works in Json.Net. The reason it works the way it does is because Json.Net uses a forward-only reader when deserializing the JSON, so a particular $id
must come before any corresponding $ref
values. Otherwise it cannot resolve the references.
To make it work the way you are suggesting, you would either need to make two passes over the JSON (which may not always be possible, e.g. with network streams) or generate proxy objects to stand in for the unresolved references (which is harder than it sounds).
No one is stopping you from forking the project if you think you want to tackle this, but I think your best bet is to try to work with the functionality that Json.Net already offers if at all possible.
Upvotes: 1