Let’s say I have Java classes that looks like this:
public class A {
public String name;
public B b;
}
public class B {
public int foo;
public String bar;
}
I want to serialize an instance of A into JSON. I am going to use the ObjectMapper class from Jackson:
A a = new A(...);
String json = new ObjectMapper().writeValueAsString(a);
Using this code, my JSON would look like this:
{
"name": "MyExample",
"b": {
"foo": 1,
"bar": "something"
}
}
Instead, I want to annotate my Java classes so that the generated JSON will instead look like this:
{
"name", "MyExample",
"foo": 1,
"bar": "something"
}
Any ideas?
Personally I think you may be better off mapping structure to structure, and not doing additional transformations.
But if you do want to go with the plan, just use Jackson 2.x, and add
@JsonUnwrappedannotation on propertyb. That should do the trick.