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.

Polymorphic type lost when using `@JsonValue`

See original GitHub issue

When suppressing all getters but one with @JsonIgnore and choosing to use a byte array for serialization (marking its getter with @JsonValue), the typing of the object is changed to “[B”, which is deserialized to a byte array. I would have expected verbose typing and usage of the constructor marked with @JsonCreator that accepts the byte array to construct the object on deserialization. The behavior is as expected when choosing more fields for serialization, which is redundant data in this case.

Running jackson-databind 2.7.4 on Java 1.8.0_91.

Configuration of the ObjectMapper:

private final ObjectMapper mapper;
public JsonFilter() {
    this.mapper = new ObjectMapper();
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    mapper.enableDefaultTyping();
}

Serialization: mapper.writeValueAsString(message) Deserialization: mapper.readValue(json, RemoteCall.class)

Getter and field:

/** @serial */
private byte[] apdu;

@JsonValue
public byte[] getBytes() {
    return apdu.clone();
}

Constructor:

@JsonCreator
public CommandAPDU(@JsonProperty(value = "bytes") byte[] apdu) {
    this.apdu = apdu.clone();
    parse();
    LOG.v("com.ubitricity.devices.common.pal.CommandAPDU creator (1)");
}

Serializes to "args":[["[B","AKQEAAnw8fLz9AAAAgA="],["net.sf.lipermi.call.RemoteInstance",{"instanceId":"b0e15098-f49e-4328-b072-fc5df42799bd","className":"com.ubitricity.devices.common.tasks.ResponseReceiver"}]] where “args” is an Object array field of the enclosing object.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
cowtowncodercommented, Sep 28, 2016

Ok, one minor change to code: it should be:

@JsonCreator
public CommandAPDU(byte[] apdu) {
    this.apdu = apdu.clone();
    parse();
    LOG.v("com.ubitricity.devices.common.pal.CommandAPDU creator (1)");
}

that is, ensure it’s delegating Creator, NOT property-one.

That’s not the main problem, but once serialization works, this is what would prevent deser.

It also looks like I can actually fix this for 2.8, and no new API is needed.

0reactions
ondersoncommented, Jun 16, 2017

Hi,

Either fix #1385 or #466 causes problem in CAMEL-11308

Downgrade of jackson2 dependency to 2.8.3 resolves the issue.

Could you check this cross reference?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is polymorphic deserialization possible in System.Text.Json?
The answer is yes and no, depending on what you mean by "possible". There is no polymorphic deserialization (equivalent to Newtonsoft. Json's ...
Read more >
How to map polymorphic JSON objects with JPA and Hibernate
Learn how you can map polymorphic JSON objects using JPA, the Hibernate Types project, and the Jackson library.
Read more >
Using @JsonTypeInfo annotation to handle polymorphic types
In cases where polymorphic types are persisted to JSON, there's no way for Jackson to figure out the right type during deserialization.
Read more >
(De)Serializing Polymorphic Types from/to Json with Gson | Ruedi's ...
In Json objects are simply represented as a bunch of properties. When deserializing an object graph, the type information is recovered using the...
Read more >
Duplicate field when serializing: bug or not a bug?
The tricky part, and root of most bugs is that of difference between actual type (one annotated with @JsonValue) and proxy type (return...
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