4

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!

kometen
  • 6,536
  • 6
  • 41
  • 51
Héctor
  • 24,444
  • 35
  • 132
  • 243

1 Answers1

1

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.

Community
  • 1
  • 1
Whimusical
  • 6,401
  • 11
  • 62
  • 105
  • Thanks for answering. I want my field `name` to be `json.t` + `json.model`. How can I help me your solution? Please, explain me. – Héctor Feb 02 '16 at 15:28
  • If you want a fast solution, just use @JsonProperty on the setters instead of the field. Then use sort of synch mechanism for setting the concat value. If you can even change the POJO, just create 2 private fields, and do the concatenation at getter level – Whimusical Feb 02 '16 at 15:32
  • @bigdestroyer I mean, do you really need the `name` variable when you can have `tm`and `model` @JsonProperty fields like `ìd` and then simply have a `public String getConcat()` method that returns the concatenation of both fields? – Whimusical Feb 02 '16 at 15:50
  • Well, it is not a matter of life or death but it would be nice this way, beacuse I will never need the two fields in a separated way. But, of course, if there is no solution to achieve that, I will use your solution. – Héctor Feb 02 '16 at 15:52
  • I edited adding a solution with what you want, but I really encourage you to use private fields, store both variables, and use a public getter which does the concatenation on-the-fly when called. That way you decouple the serialization logic from the object's format fields. Also with provided solution, you will have to work a bit further in case you need serialization again, since you will need to split the field manually into 2 json vars again – Whimusical Feb 02 '16 at 16:08