Skip to content

JsonIdentityInfo for Map (not pojo) #5120

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

Open
1 task done
zengkid opened this issue Apr 26, 2025 · 5 comments
Open
1 task done

JsonIdentityInfo for Map (not pojo) #5120

zengkid opened this issue Apr 26, 2025 · 5 comments
Labels
to-evaluate Issue that has been received but not yet evaluated

Comments

@zengkid
Copy link

zengkid commented Apr 26, 2025

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

I know that @JsonIdentityInfo can resolve the recyle reference for pojo, but how about the Map object?

Version Information

No response

Reproduction

<-- Any of the following

  1. Brief code sample/snippet: include here in preformatted/code section
  2. Longer example stored somewhere else (diff repo, snippet), add a link
  3. Textual explanation: include here
    -->
// sample for map
Map category = new HashMap();
category.put("id", 1);
category.put("name", "category1");

Map product = new HashMap();
product.put("id", 1);
product.put("name", "product1");
product.put("price", "100");
product.put("category", category);

List<Map> products = new ArrayList<>();
products.add(product);
category.put("products", products);

//

Expected behavior

No response

Additional context

No response

@zengkid zengkid added the to-evaluate Issue that has been received but not yet evaluated label Apr 26, 2025
@JooHyukKim
Copy link
Member

What about it? Could u clarify the intention of this issue?

Seems like Bug Report has been filed. Did identity-info feature ever worked with Maps?

@zengkid
Copy link
Author

zengkid commented Apr 26, 2025

I'de like to serialize an immutable Map, however, the Map has cyclical references (e.g. map of maps of maps). I can't use any of the POJO annotations (backrefs, Ignore, etc.) as its a dynamic model. I just want to specify the max depth to traverse down the map tree (graph).

Anyone know the easiest way to control the depth of the serializer in either configuration, or perhaps how to override the recursive traversal method in jackson?

@pjfanning
Copy link
Member

Jackson has StreamReadConstraints and one applies a default limit to how deep the Jackson parser will traverse. Nesting Depth is the constraint name.

@cowtowncoder
Copy link
Member

cowtowncoder commented Apr 26, 2025

Easiest thing would be to use a wrapper, to be honest: POJO that contains Map, with @JsonIdentityInfo.

Otherwise in theory you could use mix-in annotations to associate with specific Map type but I am not confident it would work correctly.

This may be one of rare cases where I'd suggest considering custom (de)serializers.

@cowtowncoder
Copy link
Member

On constraints: for reading (deserialization), we have StreamReadConstraints; for writing (serialization), StreamWriteConstraints. Both allow limiting maximum nesting depth -- on exceeding, exception is thrown which may or may not be what you want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
to-evaluate Issue that has been received but not yet evaluated
Projects
None yet
Development

No branches or pull requests

4 participants