JSON Serialization and Deserialization

JSON Serialization

package com.json.demo9_5_21;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

public class JsonSerializeDemo {
	public static void main(String[] args) throws JsonProcessingException {
		ObjectMapper objectMapper = new ObjectMapper();
		Person person = new Person();
		person.setId(1);
		person.setName("Tyson");
		System.out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(person));
	}
}

class PersonSerializer extends JsonSerializer<Person>{

	@Override
 	public void serialize(Person person, JsonGenerator  jsonGenerator, SerializerProvider serializerProvider)
			throws IOException, JsonProcessingException {
		jsonGenerator.setPrettyPrinter(new DefaultPrettyPrinter());
		jsonGenerator.writeStartObject();
		jsonGenerator.writeFieldName("custome serializer");
		jsonGenerator.writeString("Person Json");
		
		jsonGenerator.writeStringField("Person Json id","Persons Id : "+person.getId());
		jsonGenerator.writeStringField("Person Json Name","Persons Name : "+person.getName());
		
		jsonGenerator.writeEndObject();
	}	
}

@JsonSerialize(using = PersonSerializer.class)
class Person {
	int id;
	String name;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

JSON Deserialization

package com.json.demo10_5_22;

import java.io.File;
import java.io.IOException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

public class JsonDeserializerDemo {
	public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
		ClassLoader classLoader = JsonDeserializerDemo.class.getClassLoader();
		ObjectMapper objectMapper = new ObjectMapper();
		Person person = objectMapper.readValue(new File(classLoader.getResource("person.json").getFile()),Person.class);
		System.out.println(person);
	}
}

class PersonDeserializer extends JsonDeserializer<Person>{

	@Override
	public Person deserialize(JsonParser jsonParser, DeserializationContext context)
			throws IOException, JsonProcessingException {
		ObjectCodec codec = jsonParser.getCodec();
		JsonNode root = codec.readTree(jsonParser);
		Person person = new Person();
		person.setId(Integer.valueOf(root.get("Person Json id").asText().replace("Persons Id : ","")));
		person.setName(root.get("Person Json Name").asText().replace("Persons Name :", ""));
		return person;
	}
	
}

@JsonDeserialize(using = PersonDeserializer.class)
class Person {
	int id;
	String name;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String toString() {
		return "Id : "+this.id+"\nName : "+this.name;
	}
}

Leave a Comment