1public class someClass {
2//your lcode and logic...
3ObjectMapper mapper = new ObjectMapper();
4SimpleModule module = new SimpleModule();
5module.addSerializer(MyAwesomeClass.class, new MyAwesomeSerializer());
6mapper.registerModule(module);
7
8jsonNode = mapper.convertValue(myAwesomeObject, JsonNode.class);
9//... your code and logic
10}
11
12public class MyAwesomeSerializer extends StdSerializer<MyAwesomeClass> {
13
14 public MyAwesomeSerializer() {
15 super(MyAwesomeClass.class);
16 }
17
18 @Override
19 public void serialize(MyAwesomeClass myAwesomeClass, JsonGenerator jgen, SerializerProvider provider) throws IOException {
20 ObjectMapper mapper = new ObjectMapper();
21
22 jgen.writeStartObject();
23 jgen.writeStringField("name", myAwesomeClass.getName());
24 jgen.writeStringField("age", myAwesomeClass.getAge());
25
26 jgen.writeArrayFieldStart("hobbies");
27 for (Hobby hobby : myAwesomeClass.getHobbies()) {
28 jgen.writeObject(mapper.convertValue(hobby, JsonNode.class));
29 }
30 jgen.writeEndArray();
31
32 jgen.writeEndObject();
33 }
34
35}
1public class someClass {
2//your lcode and logic...
3ObjectMapper mapper = new ObjectMapper();
4SimpleModule module = new SimpleModule();
5module.addSerializer(MyAwesomeClass.class, new MyAwesomeSerializer());
6mapper.registerModule(module);
7
8jsonNode = mapper.convertValue(myAwesomeObject, JsonNode.class);
9//... your code and logic
10}
11
12//serializer class
13public class MyAwesomeSerializer extends StdSerializer<MyAwesomeClass> {
14
15 public MyAwesomeSerializer() {
16 super(MyAwesomeClass.class);
17 }
18
19 @Override
20 public void serialize(MyAwesomeClass myAwesomeClass, JsonGenerator jgen, SerializerProvider provider) throws IOException {
21 ObjectMapper mapper = new ObjectMapper();
22
23 jgen.writeStartObject();
24 jgen.writeStringField("name", myAwesomeClass.getName());
25 jgen.writeStringField("age", myAwesomeClass.getAge());
26
27 jgen.writeArrayFieldStart("hobbies");
28 for (Hobby hobby : myAwesomeClass.getHobbies()) {
29 jgen.writeObject(mapper.convertValue(hobby, JsonNode.class));
30 }
31 jgen.writeEndArray();
32
33 jgen.writeEndObject();
34 }
35
36}