How can JSON be deserialized in Java when it contains $ref and $id generated by Json.Net C#

Gabriel Luca :

I have a JSON string generated using Json.Net C# library with this settings:

JsonConvert.DefaultSettings = () => new JsonSerializerSettings
            {
                Formatting = Newtonsoft.Json.Formatting.Indented,
                ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore,
                PreserveReferencesHandling = PreserveReferencesHandling.All
            };

Note the PreserveReferencesHandling to have references generated in json instead of the same object being copied (duplicated) on each object reference.

How can I deserialize the result in java ? Google Gson library fails to parse.

UPDATE: The error can't help much in my opinion: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 154944 path $.Tables[0].Columns[4].ForeignKeyTable.ForeignKeyChildren1.Columns[0].ForeignKeyTable.Indexes

I'll put a small fragment of the json data, note the $id, $ref: JSON with references format

Gabriel Luca :

Finally I found a java library that I modified to be conform to JsonNet C# serializer format:

https://github.com/jdereg/json-io

Changed @id, @ref and @items to $id, $ref and $values and the string value in $id and $ref from JsonNet C# parsed to java Long (it is a number even in C# serialized format but it is as string type)

Maybe this can help others as well..

https://github.com/LucaGabi/json-io.git

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=152905&siteId=1