Jackson: get the whole object in a custom JsonDeserializer field

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 the REST API:

[
  { "idVehicle" : "1234DHR", "tm" : "Hyundai", "model" : "Oskarsuko"},
  //more vehicles
]

I want my model namefield to be JSON tmand modelconcatenated fields . I need to use 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!

+4
source share
1 answer

If I understand your question, you can have 2 setters that work in the same private field, and then you can mark the setters with @JsonProperty instead of the field. This may help you: @JsonProperty annotation in a field as well as getter / setter

@JsonGetter ( "var" ) @JsonSetter ( "var" ) @JsonProperty.

EDIT: , . , - , , POJO (, )

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

}

, , -.

+1

Source: https://habr.com/ru/post/1627040/


All Articles