I got a flat JSON string like
{"ccy":"EUR",
"value1":500,
"value2":200,
"date":"2017-07-25",
"type":"",
... <many other pairs>}
The JSON string shall be deserialized in Java using Jackson:
public class Data
{
@JsonProperty("ccy")
private String currency;
private Amount value1;
private Amount value2;
@JsonProperty("date")
private String date;
@JsonProperty("type")
private String type;
... <many other members>
}
with
public class Amount
{
private double value;
private String currency;
public Amount(double value, String currency)
{
this.value = value;
this.currency = currency;
}
}
What is the correct use of Jackson annotations to fill the value1 and value2 fields in my Data class?
I tried custom setters like:
@JsonSetter("value1")
private void setValue1(double value1)
{
this.value1 = new Amount(value1, this.currency);
}
@JsonSetter("value2")
private void setValue2(double value2)
{
this.value2 = new Amount(value2, this.currency);
}
But this only works if this.currency
is deserialized first (what I cannot rely on).
Is there a neat solution that does not use a custom constructor as Data(@JsonProperty("value1") double value1, (@JsonProperty("value2") double value2, (@JsonProperty("ccy") String currency) {...}
?
edit: An approach that uses Jackson would be preferred.