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}
1Serialization; when we MAP a Java object to API JSON
2format (CONVERT JAVA OBJECT TO JSON);
3
4Deserialization; API JSON/XML ^ MAP it to Java Object
5(JSON TO JAVA OBJECT)
6Serialization: POJO to JSON (or string, byte array)
7when we MAP a Java object to API JSON format (Java to JSON)
8Car car = new Car(); car.setMake(“Toyota”);
9car.setModel(“Camry”); given().body(car).when().post(uri)
10De-Serialization: JSON to POJO
11( API JSON/XML → MAP it to Java Object (JSON TO JAVAOBJECT)
12Car car2 = new Car(); car2=when().get(uri).body.as(car.class);
13 car.setMake(“Toyota”); car.setModel(“Camry”);
14
1Serialization; when we MAP a Java object to API JSON
2format (CONVERT JAVA OBJECT TO JSON);
3
4Deserialization; API JSON/XML ^ MAP it to Java Object
5(JSON TO JAVA OBJECT)