JsonObject jsonObject = JsonBuilderFactory.buildObject() //Create a new builder for an object
.addNull("nullKey") //1. Add a null to the object
.add("stringKey", "Hello") //2. Add a string to the object
.add("stringNullKey", (String) null) //3. Add a null string to the object
.add("numberKey", 2) //4. Add a number to the object
.add("numberNullKey", (Float) null) //5. Add a null number to the object
.add("booleanKey", true) //6. Add a boolean to the object
.add("booleanNullKey", (Boolean) null) //7. Add a null boolean to the object
.add("characterKey", 'c') //8. Add a character to the object
.add("characterNullKey", (Character) null) //9. Add a null character to the object
.addObject("objKey") //10. Add a nested object
.add("nestedPropertyKey", 4) //11. Add a nested property to the nested object
.end() //12. End nested object and return to the parent builder
.addArray("arrayKey") //13. Add an array to the object
.addObject() //14. Add a nested object to the array
.end() //15. End the nested object
.add("arrayElement") //16. Add a string to the array
.end() //17. End the array
.getJson(); //Get the JsonObject
String json = jsonObject.toString();
JsonObject jsonArray = JsonBuilderFactory.buildArray().addObject().end().add("foo", "bar").getJson(); //Error: tried to add a string with property key to array.
JsonObject jsonObject = JsonBuilderFactory.buildObject().addArray().end().add("foo").getJson(); //Error: tried to add a string without property key to an object.
JsonArray jsonArray = JsonBuilderFactory.buildObject().addArray("foo").getJson(); //Error: tried to assign an object to an array.
JsonObject jsonObject = JsonBuilderFactory.buildArray().addObject().getJson(); //Error: tried to assign an object to an array.