TheGutoKlein
TheGutoKlein

Reputation: 51

Jackson: Deserialize one value to two java fields

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)

Upvotes: 3

Views: 824

Answers (1)

RCB
RCB

Reputation: 845

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;
    }
}

Upvotes: 1

Related Questions