1private Car parseCar(JsonNode node) {
2 Car car;
3 ObjectMapper mapper = new ObjectMapper();
4 SimpleModule module = new SimpleModule().addDeserializer(Car.class, new CarDeserializer());
5 mapper.registerModule(module);
6 organization = mapper.convertValue(node, Car.class);
7 return car;
8}
9
10//deserializer class
11public class CarDeserializer extends StdDeserializer<Car> {
12
13 public CarDeserializer() {
14 super(Car.class);
15 }
16
17 @Override
18 public Car deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
19 ObjectMapper objectMapper = new ObjectMapper();
20 objectMapper.registerModule(new JodaModule());
21
22 Car car = new Car();
23 car.setName(getValueAsText(root, "carName"));
24 car.setDoorCount(getValueAsInt(root,"doorCount"));
25 car.setColor(getValueAsText(root,"color"));
26 car.setType(getValueAsText(root,"type"));
27
28 return car;
29 }
30
31}
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}