serialization and 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<Car> {

    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: java serializer

public class someClass {
//your lcode and logic...
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addSerializer(MyAwesomeClass.class, new MyAwesomeSerializer());
mapper.registerModule(module);

jsonNode = mapper.convertValue(myAwesomeObject, JsonNode.class);
//... your code and logic
}
  
public class MyAwesomeSerializer extends StdSerializer<MyAwesomeClass> {

    public MyAwesomeSerializer() {
        super(MyAwesomeClass.class);
    }

    @Override
    public void serialize(MyAwesomeClass myAwesomeClass, JsonGenerator jgen, SerializerProvider provider) throws IOException {
        ObjectMapper mapper = new ObjectMapper();

        jgen.writeStartObject();
        jgen.writeStringField("name", myAwesomeClass.getName());
        jgen.writeStringField("age", myAwesomeClass.getAge());

          jgen.writeArrayFieldStart("hobbies");
          for (Hobby hobby : myAwesomeClass.getHobbies()) {
              jgen.writeObject(mapper.convertValue(hobby, JsonNode.class));
          }
          jgen.writeEndArray();
      
        jgen.writeEndObject();
    }

}

Example 3: a["default"].serialize(n)

var paramObj = {};
$.each($('#myForm').serializeArray(), function(_, kv) {
  if (paramObj.hasOwnProperty(kv.name)) {
    paramObj[kv.name] = $.makeArray(paramObj[kv.name]);
    paramObj[kv.name].push(kv.value);
  }
  else {
    paramObj[kv.name] = kv.value;
  }
});

Example 4: serialization vs deserialization

DE-SERIALIZATION:  JSON --> JAVA OBJECT
SERIALIZATION: 	   JSON <-- JAVA OBJECT

I add jacksonDataBinder dependency in my pom.xml file -> 
it is a json parser. That is used for converting
from java object to json and from json to java object.

Example 5: 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:

Java Example