-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deserializing referenced JSON System.Text.Json #42584
Comments
I believe we should handle the same format used by Newtonsoft. @jozkee does anything stick out about this error? @JeremyMahieu can you share the payload you're deserializing and the types you're deserializing to? |
That format looks like what JSON schema uses for backtracking:
We do the same, I wonder, does the same error occurs when you use Newtonsoft? |
Actually, seems like the json-decycle library you linked is using a different format for preserving references:
|
Here's a sample program where it works identical to Newtonsoft. ASP.NET also perfect, will try with the original problematic payload tomorrow.. |
Thanks for the sample; I did a few corrections to your console app given that as is there is no exception happening:
After the changes, I was able to identify what was happening. When Newtonsoft steps into System.Text.Json tries to resolve it as well, but when the reference is unable to resolve, i.e: not found, we throw the You can bypass this validation by implementing your own If you want me to provide a snippet showing how to do so, let me know, you can also take a look at PreserveReferenceResolver, it is our internal resolver which is used by |
Thank you! So the only reason Newtonsoft works is because it doesn't throw but idealy I want it to work properly so I need to fix my json format. Any advice on what Javascript/Typescript library I could use that generates the propper This library seems to do it if you set some options |
Unfortunately there are many javascript libraries that deal with this problem on its own unique way; I mentioned flatted and dojo toolkit as an example in the feature specification. As of now, I couldn't find a javascript counterpart to the Newtonsoft/System.Text.Json format. |
For Newtonsoft, one recommended library is jsonnetdecycler. The library was quite old (dates back to 2012), and author removed the original bitbucket repository. Luckily, there is a rewrite of that lib in TypeScript available at: https://github.com/Zerq/BridgeBurner/blob/192059fa753aef74bb5e16d4fc9ad0502702c11c/BridgeBurner/BridgeBurner/src/json/jsonnetdecycler.ts. |
Description
I've recently switched to using json with references because I had circular objects.
This works fine in one direction, serialization. However when sending back json files with references, the deserialization gives errors.
This json comes from a Typescript application. I realize there are multiple formats. Presumably I'm using an incorrect format. I know Newtonsoft used to use the $ref $id system and this worked fine. What format does System.Text.Json use?
Configuration
.NET 5.0.100-rc.1.20452.10
Windows 10
Library used to serialize: https://www.npmjs.com/package/json-decycle
The text was updated successfully, but these errors were encountered: