Using @JsonSerialize annotation,
you can specify custom serializer for a model class and @JsonDeserialize
annotation used to specify custom deserializer.
Step 1: Define serializer by extending JsonSerializer
class.
public class AddressSerializer extends JsonSerializer<Address> {
@Override
public void serialize(Address address, JsonGenerator gen, SerializerProvider serializers)
throws IOException, JsonProcessingException {
gen.writeStartObject();
gen.writeStringField("address", getCommmaSeparatedAddress(address));
gen.writeEndObject();
}
private static String getCommmaSeparatedAddress(Address address) {
StringBuilder builder = new StringBuilder();
builder.append(address.getStreet()).append(",").append(address.getCity()).append(",")
.append(address.getCountry());
return builder.toString();
}
}
Step 2: Define deserializer by extending JsonDeserializer
class.
public class AddressDeserializer extends JsonDeserializer<Address> {
@Override
public Address deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
TreeNode node = jp.getCodec().readTree(jp);
String addressStr = node.get("address").toString();
return getAddress(addressStr);
}
private static Address getAddress(String addr) {
Address address = new Address();
String[] tokens = addr.split(",");
address.setStreet(tokens[0]);
address.setCity(tokens[1]);
address.setCountry(tokens[2]);
return address;
}
}
Step 3: Specify serializer using @JsonSerialize annotation
and deserializer using @JsonDeserialize annotation.
@JsonDeserialize(using = AddressDeserializer.class)
@JsonSerialize(using = AddressSerializer.class)
public class Address {
.....
.....
}
Find the below working
application.
package com.sample.app.model;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.sample.app.serializer.AddressDeserializer;
import com.sample.app.serializer.AddressSerializer;
@JsonDeserialize(using = AddressDeserializer.class)
@JsonSerialize(using = AddressSerializer.class)
public class Address {
private String street;
private String city;
private String country;
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Address [street=");
builder.append(street);
builder.append(", city=");
builder.append(city);
builder.append(", country=");
builder.append(country);
builder.append("]");
return builder.toString();
}
}
Employee.java
package com.sample.app.model;
public class Employee {
private String firstName;
private String lastName;
private Address address;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Employee [firstName=");
builder.append(firstName);
builder.append(", lastName=");
builder.append(lastName);
builder.append(", address=");
builder.append(address);
builder.append("]");
return builder.toString();
}
}
AddressSerializer.java
package com.sample.app.serializer;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.sample.app.model.Address;
public class AddressSerializer extends JsonSerializer<Address> {
@Override
public void serialize(Address address, JsonGenerator gen, SerializerProvider serializers)
throws IOException, JsonProcessingException {
gen.writeStartObject();
gen.writeStringField("address", getCommmaSeparatedAddress(address));
gen.writeEndObject();
}
private static String getCommmaSeparatedAddress(Address address) {
StringBuilder builder = new StringBuilder();
builder.append(address.getStreet()).append(",").append(address.getCity()).append(",")
.append(address.getCountry());
return builder.toString();
}
}
AddressDeserializer.java
package com.sample.app.serializer;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.TreeNode;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.sample.app.model.Address;
public class AddressDeserializer extends JsonDeserializer<Address> {
@Override
public Address deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
TreeNode node = jp.getCodec().readTree(jp);
String addressStr = node.get("address").toString();
return getAddress(addressStr);
}
private static Address getAddress(String addr) {
Address address = new Address();
String[] tokens = addr.split(",");
address.setStreet(tokens[0]);
address.setCity(tokens[1]);
address.setCountry(tokens[2]);
return address;
}
}
App.java
package com.sample.app;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sample.app.model.Address;
import com.sample.app.model.Employee;
public class App {
public static void main(String args[]) throws JsonParseException, JsonMappingException, IOException {
ObjectMapper mapper = new ObjectMapper();
Address address = new Address();
address.setCity("Bangalore");
address.setStreet("Chowdeswari Street");
address.setCountry("India");
Employee emp = new Employee();
emp.setFirstName("Rama Krishna");
emp.setLastName("Gurram");
emp.setAddress(address);
String json = mapper.writeValueAsString(emp);
Employee emp1 = mapper.readValue(json, Employee.class);
System.out.println("Serialized Data : \n" + json);
System.out.println("\nDeserialized Data : \n" + emp1);
}
}
Run App.java, you will see
below messages in console.
Serialized Data : {"firstName":"Rama Krishna","lastName":"Gurram","address":{"address":"Chowdeswari Street,Bangalore,India"}} Deserialized Data : Employee [firstName=Rama Krishna, lastName=Gurram, address=Address [street="Chowdeswari Street, city=Bangalore, country=India"]]
Genson is an API, used to process JSON
for Java and Scala languages. it is easy to use.
Following are the key features of Genson
API.
a.
Convert Java object to JSON and Vice versa
b.
Full support of generic types, polymorphic types and immutable objects that
don't provide a default constructor.
In following
posts, I am going to explain,
No comments:
Post a Comment