Home | Examples | Documentation
Java Objects Converter is an open source library to convert Java objects into XML or JSON in a very easy way and without any configuration. You only have to pass the object as argument of our tool and it automatically converts into XML or JSON the entire tree of objects that are contained by the given object.
Easy generic Java objects converter
Java Objects Converter supports objects which are instance of the following classes:
- Classes that follow the JavaBean code covention, the properties must be accessed by getter methods
- Arrays (i.e. Object[], Person[], String[], etc.)
- Classes which implements Collection (i.e. ArrayList, HashSet, etc.)
- Classes which implements Map (i.e. HashMap, TreeMap, etc.)
- Classes which implements CharSequence (i.e. String, StringBuilder, StringBuffer, etc.)
- Primitives and their wrappers (i.e. Float, int, Long, etc.)
- Any other classes are treated as JavaBean, so they will only be represented if they have properties accessible by getter methods.
If any property of any object is null, this property will not be represented in the XML or JSON.
Sample
Given the following class:
public class Person {
public enum Sex {
MALE, FEMALE;
}
private String name;
private Integer age;
private double money;
private Sex sex;
//Getters and Setters.....
}
From Java objects to JSON
This is the code needed to convert a Person object to JSON.
Person person =
new Person("Peter", 39, 32453.43, Sex.MALE);
String json = JavaObjectsConverter.toJson(person);
System.out.println(json);
This is the output, an JSON which represents the Person object with its properties.
{
"age": 39,
"money": 32453.43,
"sex": "MALE",
"name": "Peter"
}
From Java objects to XML
This is the code needed to convert a Person object to XML.
Person person =
new Person("Peter", 39, 32453.43, Sex.MALE);
String xml = JavaObjectsConverter.toXML(person);
System.out.println(xml);
This is the output, an XML which represents the Person object with its properties.
<Person>
<age>39</age>
<money>32453.43</money>
<sex>MALE</sex>
<name>Peter</name>
</Person>