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.

@JsonUnwrapped conflicts with @JsonAnySetter/@JsonAnyGetter

See original GitHub issue

I have this class:

@RooJavaBean(settersByDefault = false)
@JsonPropertyOrder({"txid", "vout"})
public class TransactionOutputRef extends BaseClass {

    @JsonProperty("txid")
    private String txId;

    @JsonProperty("vout")
    private Integer vout;

    /**
     * Full constructor.
     * 
     * @param txId - transaction id.
     * @param vout - output number.
     */
    public TransactionOutputRef(@JsonProperty("txid") String txId, @JsonProperty("vout") Integer vout) {
        this.txId = txId;
        this.vout = vout;
    }


}

Which is used as an @JsonUnwrapped property in this class:

@RooJavaBean(settersByDefault = false)
public class ListUnspentResult extends JsonExtra {

    @JsonUnwrapped
    private TransactionOutputRef txRef;

    private String scriptPubKey;
    private BigDecimal amount;
    private Integer confirmations;

    private Map<String, Object> otherFields = newHashMap();

    /**
     * Sets name and value of other (unknown) JSON fields.
     * 
     * @param field
     * @param value
     */
    @JsonAnySetter
    public void set(String field, Object value)  {
        otherFields.put(field, value);
    }


    /**
     * Gets names and values of all other (unknown) JSON fields.
     * 
     * @return Names of other fields available. 
     */
    @JsonAnyGetter
    public Map<String, Object> getOtherFields() {
        return Collections.unmodifiableMap(otherFields);
    }

}

As you can see the class also has @JsonAnyGetter and @JsonAnySetter methods to collect other fields.

Now, if i deserialize this:

{"txid":"280acc1c3611fee83331465c715b0da2d10b65733a688ee2273fdcc7581f149b","vout":0,"scriptPubKey":"76a91426ab1c83e2a8269b7007baf0244151cca4c5e3fd88ac","amount":5.00000000,"confirmations":956}

I get an ListUnspentResult object, where txid and vout are stored in both the txRef property annotated with @JsonUnwrapped AND in the otherFields map, and when I serialize it back to json, I get this:

{"txid":"280acc1c3611fee83331465c715b0da2d10b65733a688ee2273fdcc7581f149b","vout":0,"scriptPubKey":"76a91426ab1c83e2a8269b7007baf0244151cca4c5e3fd88ac","amount":5.00000000,"confirmations":956,"vout":0,"txid":"280acc1c3611fee83331465c715b0da2d10b65733a688ee2273fdcc7581f149b"}

where txid and vout (the properties of TransactionOutputRef ) are included twice.

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
michalmelacommented, Aug 4, 2017

For any poor souls who stumbled here looking for answers like I did, see https://github.com/fasterxml/jackson-databind/issues/349

2reactions
zardojuniorcommented, Jul 25, 2017

Hi @clanie Im having the same problem. Did you solve yours? How?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why I'm not able to unwrap and serialize a Java map using the ...
@JsonUnwrapped doesn't work for maps, only for proper POJOs with getters and setters. For maps, You should use @JsonAnyGetter and @JsonAnySetter (available ...
Read more >
Jackson Annotation Examples - Baeldung
The @JsonAnyGetter annotation allows for the flexibility of using a Map field ... First, we'll use @JsonAnySetter to deserialize the entity ...
Read more >
Guide to Jackson Annotations | Novixys Software Dev Blog
Introduction. Jackson provides a number of annotations which help tweak various facets of JSON serialization and deserialization.
Read more >
Аннотации Jackson | nsergey.com
С аннотацией @JsonAnyGetter Jackson сериализует атрибуты key-value как ... Применим @JsonAnySetter для десериализации объектаExtendableBean:.
Read more >
Jackson Annotation Examples - 码农在路上 - 博客园
The @JsonAnyGetter annotation allows the flexibility of using a Map field as ... Let's see how this works – we'll use @JsonAnySetter to ......
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