Reputation: 26044
I have this class:
@JsonIgnoreProperties(ignoreUnknown = true)
public class VehicleRestModel implements RestModel<Article> {
@JsonProperty("idVehicle")
public String id;
public String name;
}
And I get this JSON from REST API:
[
{ "idVehicle" : "1234DHR", "tm" : "Hyundai", "model" : "Oskarsuko"},
//more vehicles
]
I want my model's field name
to be JSON's fields tm
and model
concatenated. I have though to use a JsonDeserializer
but, how can I get the whole JSON object inside?
class MyDeserializer implements JsonDeserializer<String, String> {
@Override
public String deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
// I need the entire JSON here, in order to concatenate two fields
}
}
Thanks!
Upvotes: 4
Views: 938
Reputation: 6629
If I understand your question, you can have 2 setters which work on the same private field, and then you can mark the setters with @JsonProperty instead of the field. This can help you: @JsonProperty annotation on field as well as getter/setter
You can also play with @JsonGetter("var") and @JsonSetter("var") instead of @JsonProperty.
EDIT: Ok, a solution. It's the ugliest code I ever submitted, but if you want a fast thing and you can't really modify the POJO original interface (field, getter)
public class VehicleRestModel {
private String concatValue = "";
private int splitIndex;
@JsonSetter("var1")
public setVar1(String var1){ concatValue = var1 + concatValue.substring(splitIndex,concatValue.length()); ; splitIndex = var1.length(); }
@JsonSetter("var2")
public setVar2(String var2){ concatValue = concatValue.substring(0,splitIndex) + var2; }
}
Be careful with nulls, if you care, as they get appended as a literal in this demo code.
Upvotes: 1