This Java tutorial uses MOXy to marshal Java objects to JSON string or write JSON to file. MOXy implements JAXB, allowing developers to provide their mapping information through annotations and provide many rich features that JAXB doesn’t provide by default.
1. MOXy Dependency
Include the latest version of MOXy to project runtime.
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.moxy</artifactId>
<version>4.0.0</version>
</dependency>
2. Setup
2.1. Add JAXB Annotations
If not done, start with adding the JAXB annotations in the class. This example uses Employee and Department classes.
@XmlRootElement(name = "employee")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String firstName;
private String lastName;
private Department department;
public Employee() {
super();
}
//Setters and Getters
}
@XmlRootElement(name = "department")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Department implements Serializable {
private static final long serialVersionUID = 1L;
Integer id;
String name;
public Department() {
super();
}
//Setters and Getters
}
2.2. Add jaxb.properties
When we get the instance of JAXBContext
, JAXB checks for jaxb.properties
file and construct the context. Here, we inject the JAXBContextFactory
from MOXy library.
Place
jaxb.properties
file in same package where JAXB annotated classes are placed.
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
3. Marshalling Java Object to JSON String
Now use javax.xml.bind.Marshaller
class to convert objects to JSON. In this example, we are converting an instance of Employee class and writing the generated JSON content to the console.
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import org.eclipse.persistence.jaxb.MarshallerProperties;
import com.howtodoinjava.demo.model.Department;
import com.howtodoinjava.demo.model.Employee;
public class JaxbExample
{
public static void main(String[] args)
{
Employee employee = new Employee(1, "Lokesh", "Gupta", new Department(101, "IT"));
jaxbObjectToJSON(employee);
}
private static void jaxbObjectToJSON(Employee employee)
{
try
{
JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
// To format JSON
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
//Set JSON type
jaxbMarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
jaxbMarshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true);
//Print JSON String to Console
StringWriter sw = new StringWriter();
jaxbMarshaller.marshal(employee, sw);
System.out.println(sw.toString());
}
catch (JAXBException e)
{
e.printStackTrace();
}
}
}
Program output:
{
"employee" : {
"department" : {
"id" : 101,
"name" : "IT"
},
"firstName" : "Lokesh",
"id" : 1,
"lastName" : "Gupta"
}
}
Read More : Convert Java Object to XML
3. Marshalling to JSON File
Use the marshal(object, File) method to write the generated JSON content into the given file.
Employee employee = new Employee(1, "Lokesh", "Gupta", new Department(101, "IT"));
File file = new File("employee.json");
jaxbMarshaller.marshal(employee, file);
Program output:
{
"employee" : {
"department" : {
"id" : 101,
"name" : "IT"
},
"firstName" : "Lokesh",
"id" : 1,
"lastName" : "Gupta"
}
}
Drop me your questions in the comments section related to this convert java object to JSON using MOXy.
Happy Learning !!
Comments