Dynamically enable/disable he UNWRAP_ROOT_VALUE and WRAP_ROOT_VALUE in Jackson's ObjectMapper?
Asked Answered
B

1

8

Is there a way to enable/disable the UNWRAP_ROOT_VALUE and WRAP_ROOT_VALUE in Jackson's ObjectMapper dynamically.
I have to enable/disable these properties depending on what service is called, some requests require a JsonRootName and some do not.

I have the @JsonRootName annotation in the classes that do require it.
I have a custom ObjectMapper class that extends the Jackson Object mapper.
I am calling a method to enable/disable the properties depending on what service is called but is it doesn't seem to be working.

public void setWrapValue(boolean wrap) {

    final AnnotationIntrospector introspector = new JacksonAnnotationIntrospector();     

    this.configure(org.codehaus.jackson.map.DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, wrap);

    this.configure(org.codehaus.jackson.map.SerializationConfig.Feature.WRAP_ROOT_VALUE, wrap);
    this.setDeserializationConfig(this.getDeserializationConfig().withAnnotationIntrospector(introspector));

    this.setSerializationConfig(this.getSerializationConfig().withAnnotationIntrospector(introspector));
 }
Baer answered 19/6, 2013 at 9:26 Comment(5)
Isn't @JsonUnwrapped what you are looking for?Modestomodesty
I can't see that annotation, I'm using jackson 1.9.9Baer
Argh, can't you upgrade?Modestomodesty
I've put a request in to add the latest jars, it might take a while , is there any other option?Baer
Probably, you have to create two different ObjectMappers: in first you should enable this feature in second you should disable this feature. How did you solve this problem?Balancer
A
2

1) Per-class root wrapping

There are two issues on jackson-databind,

Without support from Jackson, I see no way to do this without writing more code than manually wrapping each class into a property.

One way could be leveraging JAXB's feature to de/serialize properties as per XPath-like expression (i.e. foo/bar would wrap a property under foo), but that is not supported by Jackson.

Edit:

I looked at the code, DefaultSerializerProvider and around. Jackson 2.9.9. Jackson currently does not distinguish "no property name" and "default property name". So, AFAICT, DefaultSerializerProvider doesn't know whether @JsonRootName is there or is empty.

If this disctinction was propagated, this could start working. I'm waiting for the maintainer to judge. However, without changes in Jackson itself, doing this from the outside would be a bit impractical.

2) Toggling root wrapping dynamically

Maybe you could have 2 ObjectMappers, one with and one without WRAP_ROOT_NAME, and use the right one.

However, if "some requests need it and some not" (assuming for the same endpoint), that's a bit weird. Or did you mean that for some endpoints you need to wrap types that you use as-s for other endpoints? Then maybe simple composition can be used. Hard to tell, please add some JSON examples and your model classes.

Acerbity answered 10/6, 2019 at 5:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.