-2

I have a class like:

   class Car {
        private Engine engine;
        private String color;  
        private int maxspeed;
    }

and Engine class like

class Engine {
    private String fueltype;
    private String enginetype;
}  

I want to convert the Car object to JSON using Jackson with structure like

'car': {
   'color': 'red',
   'maxspeed': '200',
   'fueltype': 'diesel',
   'enginetype': 'four-stroke'
 } 

How can I do that?

Lokesh Agrawal
  • 4,095
  • 10
  • 38
  • 76

1 Answers1

1

Try to use the @JsonUnwrapped annotation.

class Car {
    @JsonUnwrapped private Engine engine;
    private String color;  
    private int maxspeed;
}

For the opposite way, use @JsonCreator with @JsonProperty.

Nikolas Charalambidis
  • 35,162
  • 12
  • 84
  • 155