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.
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 |
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 |
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 |
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 |
finalize()
Pops all levels out to the root.
Declaration
Void finalize() Returns
| Type | Description |
| Void |
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. |
popLevel()
Pops a level (closes an array or an object) in the JSONBuilder.
Declaration
Void popLevel() Returns
| Type | Description |
| Void |
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 |
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 |
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 |