-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?

ThinkGeek
  • 4,749
  • 13
  • 44
  • 91

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
  • 40,893
  • 16
  • 117
  • 183