hansi
hansi

Reputation: 2308

Jackson JSON serialization mapping

I have two java classes A and B that I would like to be able to serialize to JSON such that their content looks the same to a consumer.

I ommitted the constructors/getters/setters to keep it minimal.

public class A {

    @JsonProperty("b")
    private B b;

}

public class B {

   @JsonProperty("propertyB")
   public String propertyB;

}

When I serialize A I get

{
   b: {
       propertyB: ''
   }
}

But I want it to look like B's serialization:

{
  propertyB: ''
}

Is there any way to achieve that simply by configuring the serialization process respectively, i.e. using jackson annotations or some other form of jackson configuration.

Thanks

Upvotes: 0

Views: 47

Answers (1)

varren
varren

Reputation: 14731

You can use @JsonUnwrapped

public class A {
    @JsonUnwrapped
    private B b;
}

https://fasterxml.github.io/jackson-annotations/javadoc/2.2.0/com/fasterxml/jackson/annotation/JsonUnwrapped.html

Upvotes: 1

Related Questions