Serialization & Deserialization code example

Example 1: java deserializer

private Car parseCar(JsonNode node) {
        Car car;
        ObjectMapper mapper = new ObjectMapper();
        SimpleModule module = new SimpleModule().addDeserializer(Car.class, new CarDeserializer());
        mapper.registerModule(module);
        organization = mapper.convertValue(node, Car.class);
        return car;
}

//deserializer class
public class CarDeserializer extends StdDeserializer {

    public CarDeserializer() {
        super(Car.class);
    }

    @Override
    public Car deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.registerModule(new JodaModule());

        Car car = new Car();
        car.setName(getValueAsText(root, "carName"));
        car.setDoorCount(getValueAsInt(root,"doorCount"));
        car.setColor(getValueAsText(root,"color"));
		car.setType(getValueAsText(root,"type"));

        return car;
    }

}

Example 2: what is deserialization

Deserialization; API JSON/XML ^ MAP it to Java Object
(JSON TO JAVA OBJECT)



Serialization; when we MAP a Java object to API JSON
format (CONVERT JAVA OBJECT TO JSON);

Example 3: Serialization and Deserialization

Serialization; when we MAP a Java object to API JSON
format (CONVERT JAVA OBJECT TO JSON);

Deserialization; API JSON/XML ^ MAP it to Java Object
(JSON TO JAVA OBJECT)

Tags:

Misc Example