marciel.deg
marciel.deg

Reputation: 512

merging two class properties while serialization using jackson

Consider these classes:

public class A {
  private String a;
  private B b;
}

public class B {
  private String b1;
  private String b2;
}

Is there a way to serialize an object of A using Jackson, without using a custom serializer, so that the result is:

{"a":"aaa", "b1":"bbb1", "b2":"bbb2"}

?

Upvotes: 2

Views: 634

Answers (1)

Ryuzaki L
Ryuzaki L

Reputation: 40078

You can simply use @JsonUnwrapped on field

Annotation used to indicate that a property should be serialized "unwrapped"; that is, if it would be serialized as JSON Object, its properties are instead included as properties of its containing Object.

public class A {

     private String a;

     @JsonUnwrapped
     private B b;
}

public class B {

     private String b1;

     private String b2;

}

Upvotes: 1

Related Questions