Class JSONBuilder
The JSONBuilder class simplifies building JSON hierarchies. The result will be a string in correct JSON format, with string values properly escaped. This class is useful for example in combination with the HTTP class to make REST-calls.
Syntax
Examples
JSONBuilder jb;
jb.pushObject("");
jb.pushArray("persons");
jb.pushObject("");
jb.addString("firstname", "John");
jb.addInteger("age", 40);
jb.popLevel();
jb.pushObject("");
jb.addString("firstname", "Peter");
jb.addInteger("age", 34);
jb.popLevel();
jb.popLevel();
jb.popLevel(); // jb.finalize() could be used to pop all levels
Constructors
JSONBuilder()
Initializes a new instance of the JSONBuilder class.
Declaration
JSONBuilder
Examples
JSONBuilder jb;
jb.pushObject("");
jb.pushArray("persons");
jb.pushObject("");
jb.addString("firstname", "John");
jb.addInteger("age", 40);
jb.popLevel();
jb.pushObject("");
jb.addString("firstname", "Peter");
jb.addInteger("age", 34);
jb.popLevel();
jb.popLevel();
jb.popLevel(); // jb.finalize() could be used to pop all levels
Methods
addBoolean(String,Bool)
Adds a boolean value to the current scope.
Declaration
Void addBoolean(String key, Bool value)
Parameters
Type | Name | Description |
---|---|---|
String | key | |
Bool | value |
Returns
Type | Description |
---|---|
Void |
Remarks
If the current scope is an object, you must supply a key. If the current scope is an array, key must be empty.
Examples
JSONBuilder jb;
jb.pushObject("");
jb.pushArray("persons");
jb.pushObject("");
jb.addString("firstname", "John");
jb.addInteger("age", 40);
jb.popLevel();
jb.pushObject("");
jb.addString("firstname", "Peter");
jb.addInteger("age", 34);
jb.popLevel();
jb.popLevel();
jb.popLevel(); // jb.finalize() could be used to pop all levels
addFloat(String)
Adds a float value to the current scope.
Declaration
Void addFloat(String key, Float value)
Parameters
Type | Name | Description |
---|---|---|
String | key | |
value |
Returns
Type | Description |
---|---|
Void |
Remarks
If the current scope is an object, you must supply a key. If the current scope is an array, key must be empty.
Examples
JSONBuilder jb;
jb.pushObject("");
jb.pushArray("persons");
jb.pushObject("");
jb.addString("firstname", "John");
jb.addInteger("age", 40);
jb.popLevel();
jb.pushObject("");
jb.addString("firstname", "Peter");
jb.addInteger("age", 34);
jb.popLevel();
jb.popLevel();
jb.popLevel(); // jb.finalize() could be used to pop all levels
addInteger(String,Integer)
Adds an integer value to the current scope.
Declaration
Void addInteger(String key, Integer value)
Parameters
Type | Name | Description |
---|---|---|
String | key | |
Integer | value |
Returns
Type | Description |
---|---|
Void |
Remarks
If the current scope is an object, you must supply a key. If the current scope is an array, key must be empty.
Examples
JSONBuilder jb;
jb.pushObject("");
jb.pushArray("persons");
jb.pushObject("");
jb.addString("firstname", "John");
jb.addInteger("age", 40);
jb.popLevel();
jb.pushObject("");
jb.addString("firstname", "Peter");
jb.addInteger("age", 34);
jb.popLevel();
jb.popLevel();
jb.popLevel(); // jb.finalize() could be used to pop all levels
addString(String,String)
Adds a string value to the current scope.
Declaration
Void addString(String key, String value)
Parameters
Type | Name | Description |
---|---|---|
String | key | |
String | value |
Returns
Type | Description |
---|---|
Void |
Remarks
If the current scope is an object, you must supply a key. If the current scope is an array, key must be empty. The value does not need to be escaped, it will be escaped by JSONBuilder.
Examples
JSONBuilder jb;
jb.pushObject("");
jb.pushArray("persons");
jb.pushObject("");
jb.addString("firstname", "John");
jb.addInteger("age", 40);
jb.popLevel();
jb.pushObject("");
jb.addString("firstname", "Peter");
jb.addInteger("age", 34);
jb.popLevel();
jb.popLevel();
jb.popLevel(); // jb.finalize() could be used to pop all levels
finalize()
Pops all levels out to the root.
Declaration
Void finalize()
Returns
Type | Description |
---|---|
Void |
Remarks
You can call this function instead of multiple popLevel() if you are at then end of your structure.
Examples
JSONBuilder jb;
jb.pushObject("");
jb.pushArray("persons");
jb.pushObject("");
jb.addString("firstname", "John");
jb.addInteger("age", 40);
jb.popLevel();
jb.pushObject("");
jb.addString("firstname", "Peter");
jb.addInteger("age", 34);
jb.popLevel();
jb.popLevel();
jb.popLevel(); // jb.finalize() could be used to pop all levels
getString()
Returns the JSON-formatted string that has been built by all push and add methods.
Declaration
String getString()
Returns
Type | Description |
---|---|
String | The JSON-formatted string. |
Examples
JSONBuilder jb;
jb.pushObject("");
jb.pushArray("persons");
jb.pushObject("");
jb.addString("firstname", "John");
jb.addInteger("age", 40);
jb.popLevel();
jb.pushObject("");
jb.addString("firstname", "Peter");
jb.addInteger("age", 34);
jb.popLevel();
jb.popLevel();
jb.popLevel(); // jb.finalize() could be used to pop all levels
popLevel()
Pops a level (closes an array or an object) in the JSONBuilder.
Declaration
Void popLevel()
Returns
Type | Description |
---|---|
Void |
Remarks
Look at the root example for this class.
Examples
JSONBuilder jb;
jb.pushObject("");
jb.pushArray("persons");
jb.pushObject("");
jb.addString("firstname", "John");
jb.addInteger("age", 40);
jb.popLevel();
jb.pushObject("");
jb.addString("firstname", "Peter");
jb.addInteger("age", 34);
jb.popLevel();
jb.popLevel();
jb.popLevel(); // jb.finalize() could be used to pop all levels
pushArray(String)
Adds an array to the JSONBuilder. Subsequent methods will add members inside the array.
Declaration
pushArray(String array)
Parameters
Type | Name | Description |
---|---|---|
String | array |
Returns
Type | Description |
---|---|
Void |
Remarks
Normally, an array will be named (because it will be a named variable in the parent scope), except for when the array is the root of the JSON structure.
Examples
JSONBuilder jb;
jb.pushObject("");
jb.pushArray("persons");
jb.pushObject("");
jb.addString("firstname", "John");
jb.addInteger("age", 40);
jb.popLevel();
jb.pushObject("");
jb.addString("firstname", "Peter");
jb.addInteger("age", 34);
jb.popLevel();
jb.popLevel();
jb.popLevel(); // jb.finalize() could be used to pop all levels
pushObject(String)
Adds an object to the JSONBuilder. Subsequent methods will add members inside the object.
Declaration
Void pushObject(String object)
Parameters
Type | Name | Description |
---|---|---|
String | object |
Returns
Type | Description |
---|---|
Void |
Remarks
Normally, an object will be named (because it will be a named variable in the parent scope), except for when the object is the root of the JSON structure.
Examples
JSONBuilder jb;
jb.pushObject("");
jb.pushArray("persons");
jb.pushObject("");
jb.addString("firstname", "John");
jb.addInteger("age", 40);
jb.popLevel();
jb.pushObject("");
jb.addString("firstname", "Peter");
jb.addInteger("age", 34);
jb.popLevel();
jb.popLevel();
jb.popLevel(); // jb.finalize() could be used to pop all levels
setPrettyPrint(Integer)
Sets whether the JSON should be pretty formatted or not.
Declaration
Void SetPrettyPrint(Integer indent)
Parameters
Type | Name | Description |
---|---|---|
Integer | indent | Indent > 0 will cause pretty printing, where indent is the number of spaces to indent a block level with. |
Returns
Type | Description |
---|---|
Void |
Examples
JSONBuilder jb;
jb.pushObject("");
jb.pushArray("persons");
jb.pushObject("");
jb.addString("firstname", "John");
jb.addInteger("age", 40);
jb.popLevel();
jb.pushObject("");
jb.addString("firstname", "Peter");
jb.addInteger("age", 34);
jb.popLevel();
jb.popLevel();
jb.popLevel(); // jb.finalize() could be used to pop all levels