Below code uses Jackson Json API to convert an Object into a JSON String
We will be converting Person class which has Company instance composed in it.
If we don't create the getter methods then the below exception will come:
Exception in thread "main" org.codehaus.jackson.map.JsonMappingException: No serializer found for class com.blogspot.javasampleprogram.Person and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) )
Result: {"company":{"salary":15001.9,"companyName":"ABC"},"age":36,"name":"Jason Stathom"}
We will have to add the following Jackson API dependency:
We will be converting Person class which has Company instance composed in it.
class Person {
String name;
int age;
Company company;
/**
* @return the company
*/
public Company getCompany() {
return company;
}
public Person(String name, int age, Company company){
this.name = name;
this.age = age;
this.company = company;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @return the name
*/
public String getName() {
return name;
}
}
class Company {
String companyName;
float salary;
public Company(String companyName, float salary) {
this.companyName=companyName;
this.salary = salary;
}
/**
* @return the salary
*/
public float getSalary() {
return salary;
}
/**
* @return the companyName
*/
public String getCompanyName() {
return companyName;
}
}
If we don't create the getter methods then the below exception will come:
Exception in thread "main" org.codehaus.jackson.map.JsonMappingException: No serializer found for class com.blogspot.javasampleprogram.Person and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) )
package com.blogspot.javasampleprogram;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.ObjectMapper;
/**
* @author http://java-sample-program.blogspot.in/
* Object to Json Conversion
*/
public class JsonStringGenerator {
/**
* Method to get a JSON string representing the object
* supplied.
* @param object for which JSON string is required
* @return Json String
* @throws IOException
*/
public static String createJsonString(Object object) throws IOException {
Writer writer = new StringWriter();
JsonFactory factory = new JsonFactory();
JsonGenerator generator = factory.createJsonGenerator(writer);
generator.setCodec(new ObjectMapper());
generator.writeObject(object);
generator.close();
System.out.println("Json string " + writer.toString());
return writer.toString();
}
public static void main(String[] args) throws IOException {
Company company = new Company("ABC", 15001.90f);
Person p1 = new Person("Jason Stathom", 36, company);
createJsonString(p1);
}
}
Result: {"company":{"salary":15001.9,"companyName":"ABC"},"age":36,"name":"Jason Stathom"}
We will have to add the following Jackson API dependency:
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>com.springsource.org.codehaus.jackson.mapper</artifactId>
<version>1.4.3</version>
</dependency>