Skip to content

Deserialization with Default Typing and @JsonIdentityInfo in untyped collections fails #2780

Description

@alex-t0

I need to serialize/deserialize untyped collections, and I get issue when deserialing. I push failing unit test in my repository: https://github.com/alex-t0/deserialization-fail-example/blob/master/src/test/java/deserialization/fail/example/DeserializationFromUntypedCollectionsTest.java.

In this test I have simple User class and dummy UserContainer class, that has one User property. Next, I create an array of 2 elements: user and userContainer with reference to same user.

When first element of array is user, and second is container - deserialization works fine. I get this serialized string:

[
    "java.util.ArrayList",
    [
        [
            "deserialization.fail.example.User",
            {
                "id": 42,
                "login": "cool_man"
            }
        ],
        [
            "deserialization.fail.example.UserContainer",
            {
                "user": 42
            }
        ]
    ]
]

Because jackson knows what is 42 here.

But if I put userContainer at first place in my array, and user at second place, serialized data would be

[
    "java.util.ArrayList",
    [
        [
            "deserialization.fail.example.UserContainer",
            {
                "user": [
                    "deserialization.fail.example.User",
                    {
                        "id": 42,
                        "login": "cool_man"
                    }
                ]
            }
        ],
        42
    ]
]

So, user serialized as id, but no type information saved about user. And when I deserialize this I get an array with Integer 42 at second place, not user.

I think that when serializing untyped collection jackson must "start from scratch" for every element, and "forget" previously found object id's. Size of json will increase, but will work.

Maу be somebody offer better solution.

Thanks!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.2objectidpolymorphic-handlingProblem with polymorphic type handling (`@JsonTypeInfo`, Default Typing)

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions