JSONBuilder is a simple non-intrusive Java library for generating JSON strings out of POJO object structures. It is non-intrusive in that it doesn't require any modification of existing POJO structures, including adding annotations.
- Lightweight
- Non-intrusive
- Simple use
Person person = new Person(12, "John Doe", "1978-10-21");
JSONBuilder json = new JSONBuilder();
json.include("id","name");
String s = json.serialize(person);
The String 's' now is equal to:
{"id":12,"name":"John Doe"}
Nested structures are supported
Person person = new Person(12, "John Doe", "1978-10-21", new Address("Downing street", 12));
JSONBuilder json = new JSONBuilder();
json.include("id","name","address.street");
String s = json.serialize(person);
Will generate:
{"id":12,"name":"John Doe","address":{"street":"Downing street"}}
Transposition allows properties to be relocated in the output tree under a different path than in the source tree.
Person person = new Person(12, "John Doe", "1978-10-21", new Address("Downing street", 12));
JSONBuilder json = new JSONBuilder();
json.include("id","name");
json.includeTransposed("address.street","street")
String s = json.serialize(person);
Will generate:
{"id":12,"name":"John Doe","street":"Downing street"}
- Datatype conversion using json.withTransformer(...)
- Serialization of collections (including primitives)
- Serialization of maps (as key-value pairs)
- Case conversion using Guava's CaseFormat as JSONBuilder constructor parameters
- See the tests provided for samples