Jackson: Deserialize one value to two java fields

Viewed 202

So I have one field in my JSON

{
"Number": "2737212281"}

And I want to deserialize this field to two java fields

@Column(name = "TRANSACTION_CURRENCY", length = 5)
@JsonProperty("TransactionCurrency")
@JsonAlias({"Number"})
private String TransactionCurrency;

@Column(name = "SD_DOCUMENT_REASON", length = 3, nullable = true)
@JsonProperty("SDDocumentReason")
@JsonProperty("Number")
private String SDDocumentReason;

to have the same value in both fields, for some reason the lib just takes the first field(TransactionCurrency)

1 Answers

Using @JsonCreator could be an option.

For example, for JSON that looks like: { "field" : "anything" }

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

class Foo {
    private final String field0;
    private final String field1;

    @JsonCreator
    public Foo(@JsonProperty("field") String value) {
        this.field0 = value;
        this.field1 = value;
    }
}
Related