Skip to content

toObjects() with unknownTypeClass() and nested JSON is broken #425

@Kai-W

Description

@Kai-W

Serializing a nestend Java object e.g. {"outer":{"inner":"value"}} results in an wrong java object. The values of the inner Object get additonally added to the outer object.

String json = "{\"outer\":{\"inner\":\"value\"}}";
System.out.println(json);
ReadOptions options = new ReadOptionsBuilder().unknownTypeClass(LinkedHashMap.class).build();
Object objects = JsonIo.toObjects(json,options, Object.class);
System.out.println(objects);
System.out.println(JsonIo.toJson(objects, null));
{"outer":{"inner":"value"}}
{inner=value, outer={inner=value}}
{"@type":"LinkedHashMap","inner":"value","outer":{"@type":"LinkedHashMap","inner":"value"}}

With no unknownType set or JsonObject as unkonwn type it works as expected but interestingly the resulting toJson is different. With explicit JsonObject as unknonwType there are no @type marks.

with unknownTypeClass(JsonObject.class)

JsonObject(id:-1, type:not set, target:null, line:0, col:0, size:1)
{"outer":{"inner":"value"}}

without any unknownTypeClass

JsonObject(id:-1, type:java.util.Map, target:null, line:0, col:0, size:1)
{"@type":"java.util.Map","outer":{"@type":"java.util.Map","inner":"value"}}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions