Answer the question
In order to leave comments, you need to log in
Jackson POJO serialization. How to pull a single property from an encapsulated object?
There is an object of class A. It needs to be turned into json
class A {
private String id;
private String descr;
private B b;
}
class B {
private String id;
...
}
{
"id": "a_id",
"b": {"id":"b_id"}
}
A a = ...
B b = ...
a.setB(b); b.setA(a);
ObjectMapper mapper = ...
String value = mapper.writeValueAsString(a);
{
"id": "a_id",
"b": {"id":"b_id"}
}
A a = ...
B b = ...
a.setB(b); b.setA(a);
ObjectMapper mapper = ...
ObjectNode node = (ObjectNode )mapper.convertValue(a, JsonNode.class);
node.remove(propertyToDelete);
String value = node.toString();
node.remove(propertyToDelete);
can be very repetitive. depends on the number of fields to hide in json... not very convenient :)
Answer the question
In order to leave comments, you need to log in
I did not understand you want to pull out the fields of class B without referring to the class itself? And you are not satisfied with:
A a = new A();
B b = new B();
a.setB(b);
// инициализирум поля и преобразовываем в JSON
...
// после парсинга из JSON
String bId = a.getB().getId();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question