Jackson deserialazing

25 views
Skip to first unread message

Max Szmid

unread,
Jul 13, 2023, 6:00:51 PM7/13/23
to jackson-user
I have this issue, when I try to return an object from my API, sometimes the format is as an object and other times it's just the ID of that object. I need it to always return the object. I've noticed that it only happens if the object it relates to has the same ID as another object already in the list. In this case, the first time it comes out as a "myClass" object and the second time, only with its ID value "3". Any solution for this?
class:
@JsonIdentityInfo( generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", resolver = EntityIdResolver.class, scope = MyClass.class) public class MyClass{....}


RESPONSE
{
"itemList": [
    {
        "id": 70110,
        "myClass": {
            "id": 3,
            "name": "itemName",
            "date": "2000-01-01"
        },
    },
    {
        "id": 70111,
        "myClass": 3,
    }
]
}

Expected response:
{
"itemList": [
    {
        "id": 70110,
        "myClass": {
            "id": 3,
            "name": "itemName",
            "date": "2000-01-01"
        },
    },
    {
        "id": 70111,
          "myClass": {
            "id": 3,
            "name": "itemName",
            "date": "2000-01-01"
             },
    }
]
}

Tatu Saloranta

unread,
Jul 13, 2023, 6:25:38 PM7/13/23
to jackso...@googlegroups.com
On Thu, Jul 13, 2023 at 3:00 PM Max Szmid <maxszmid...@gmail.com> wrote:
>
> I have this issue, when I try to return an object from my API, sometimes the format is as an object and other times it's just the ID of that object. I need it to always return the object. I've noticed that it only happens if the object it relates to has the same ID as another object already in the list. In this case, the first time it comes out as a "myClass" object and the second time, only with its ID value "3". Any solution for this?
> class:
> @JsonIdentityInfo( generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", resolver = EntityIdResolver.class, scope = MyClass.class) public class MyClass{....}

^^^^^^^^^

This is what specifies serialization changes. Do not add this
annotation if you do not want Object Ids being used as replacements.
It is only needed to keep Object identity and/or allow handling of
cyclic data structures.

-+ Tatu +-
> --
> You received this message because you are subscribed to the Google Groups "jackson-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/jackson-user/bc634d1d-14dc-4c9d-81ce-1c51d19c22a7n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages