0

How to serialize that object. But only with fields of object anotherObject but without "anotherObject" key in json

class A{
   int some = 1;
   B anotherObject = new B();
}

class B{
    int someB = 2;
}

I need as serializing result next JSON

{
    "A":{
       some: 1,
       anotherSome: 2
    }
}
Dmitry Shabalin
  • 141
  • 1
  • 9

2 Answers2

2

You can use @JsonUnwrapped annotation.

class A {
    @JsonProperty
    int some = 1;

    @JsonUnwrapped
    B anotherObject = new B();
}

class B {
    @JsonProperty
    int someB = 2;
}
mwajcht
  • 71
  • 4
1

Add to your class a method

int getAnotherSome() {
  return anotherObject.someB
}

And annotate

@JsonIgnore
B anotherObject = new B();

And that should do a trick

Michael Gantman
  • 7,315
  • 2
  • 19
  • 36