ambiguos
ambiguos

Reputation: 21

Is there any way to map jsonProperty of nested fields within an object?

I have two classes Car.java and Bus.java

        Car.java   
        private Property property;
       Bus.java
       private Property property;

The Property.java class has number and colour.

        Property.java
        private String number;
        private String colour;

Depending on where it is called from, Car.java or Bus.java ,I want the properties field to be called carNumber, carColour, busNumber, busColour respectively in the output json.

Is there any way to achieve this using a single property class? The output json should be "Car":{ "Property" : { "carNumber" : "num" "carColor" : "blue" } }

Upvotes: 0

Views: 188

Answers (1)

lucid
lucid

Reputation: 2900

Yes, you can use @JsonUnwrapped with prefix.

// Car.Java 

@JsonUnwrapped(prefix="car-")
private Property property;

It will generate JSON as below (after serialization)

{ "car-name": "car name", "car-number" : "car number" }

Upvotes: 2

Related Questions