question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@JsonAnySetter with @JsonUnwrapped: deserialization fails with arrays

See original GitHub issue

My application makes heavy use of the @JsonAnySetter/Getter annotations which is currently throwing an exception when deserializing an JSON array of strings as a property value:

Can not deserialize instance of java.lang.Object out of END_ARRAY token at [Source: org.apache.cxf.transport.http.AbstractHTTPDestination$1@7a5d231e; line: 22, column: 5] (through reference chain: com.mycorp.core.web.rest.dto.ItemDTO["ZoomLinks"])
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:575)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:569)
at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer.deserialize(UntypedObjectDeserializer.java:90)
at com.fasterxml.jackson.databind.deser.SettableAnyProperty.deserialize(SettableAnyProperty.java:98)
at com.fasterxml.jackson.databind.deser.SettableAnyProperty.deserializeAndSet(SettableAnyProperty.java:88)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeWithUnwrapped(BeanDeserializer.java:515)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:271)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
at com.fasterxml.jackson.databind.ObjectReader._bind(ObjectReader.java:1179)
at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:635)

Here is the Java code:

public void addProperty(String key, Object value) {
if (props == null)
{ props = new HashMap<String, Object>(); }
props.put(key, value);
}
@JsonAnyGetter
public Map<String, Object> getProperties()
{ return props; }

and here is some sample JSON that will fail:

{ "_type" : "IST",
 "_spacename" : "Foo Models",
 "_name" : "BLAH-New",
 "_description" : "namespace.name: X THIN FIR.DR-WD12-New",
 "ZoomLinks": [ "foofoofoofoo", "barbarbarbar" ] }

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
borballcommented, Jan 16, 2017

@cowtowncoder It seems JsonAnySetter and JsonUnwrapped can work together in 2.9.0-SNAPSHOT but not in 2.8.6. I have added one test case here: https://github.com/borball/jackson-test/blob/master/src/test/java/com/fasterxml/jackson/test/JacksonTest.java#L15-L19 With 2.9.0-SNAPSHOT I can successfully get bean object refundQuery but with 2.8.6, it is null.

0reactions
cowtowncodercommented, Sep 28, 2019

I think that the way things are implemented duplicates would always have occurred.

Later issue #1811 cover the problem: I hope this can be resolved in 3.0.

Read more comments on GitHub >

github_iconTop Results From Across the Web

JsonAnySetter with @JsonUnwrapped: deserialization fails ...
My application makes heavy use of the @JsonAnySetter/Getter annotations which is currently throwing an exception when deserializing an JSON ...
Read more >
java - What's the Jackson deserialization equivalent of ...
For those who googled here like me, trying to resolve issue when deserializing unwrapepd Map , there is a solution with @JsonAnySetter :...
Read more >
Jackson JSON - Using @JsonUnwrapped to serialize ...
JsonUnwrapped is used to indicate that a property should be serialized unwrapped, i.e. the target property will not be serialized as JSON ...
Read more >
Jackson Annotation Examples - Baeldung
On deserialization, the properties from JSON will simply be added to the map. First, we'll use @JsonAnySetter to deserialize the entity ...
Read more >
Jackson Annotations for JSON - Spring Framework Guru
@JsonUnwrapped. The. @JsonUnwrapped. @JsonUnwrapped annotation unwraps the values during serialization and deserialization. It helps in rendering the values ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found