Json Box
0.6.1
This is a JSON C++ library. It can write and read JSON files with ease and speed.
|
#include <Value.h>
Classes | |
union | ValueDataPointer |
Public Types | |
enum | Type { STRING, INTEGER, DOUBLE, OBJECT, ARRAY, BOOLEAN, NULL_VALUE, UNKNOWN } |
Public Member Functions | |
Value () | |
Value (std::istream &input) | |
Value (const std::string &newString) | |
Value (const char *newCString) | |
Value (int newInt) | |
Value (double newDouble) | |
Value (const Object &newObject) | |
Value (const Array &newArray) | |
Value (bool newBoolean) | |
Value (const Value &src) | |
~Value () | |
Value & | operator= (const Value &src) |
Value & | operator= (const std::string &src) |
Value & | operator= (const char *src) |
Value & | operator= (int src) |
Value & | operator= (double src) |
Value & | operator= (const Object &src) |
Value & | operator= (const Array &src) |
Value & | operator= (bool src) |
bool | operator== (const Value &rhs) const |
bool | operator!= (const Value &rhs) const |
bool | operator< (const Value &rhs) const |
bool | operator<= (const Value &rhs) const |
bool | operator> (const Value &rhs) const |
bool | operator>= (const Value &rhs) const |
Value & | operator[] (const std::string &key) |
Value & | operator[] (const char *key) |
Value & | operator[] (size_t index) |
Type | getType () const |
bool | isString () const |
bool | isStringable () const |
bool | isInteger () const |
bool | isDouble () const |
bool | isNumeric () const |
bool | isObject () const |
bool | isArray () const |
bool | isBoolean () const |
bool | isNull () const |
const std::string & | getString () const |
const std::string & | tryGetString (const std::string &defaultValue) const |
const std::string | getToString () const |
void | setString (const std::string &newString) |
int | getInteger () const |
int | tryGetInteger (int defaultValue) const |
void | setInteger (int newInt) |
double | getDouble () const |
double | tryGetDouble (double defaultValue) const |
float | getFloat () const |
float | tryGetFloat (float defaultValue) const |
void | setDouble (double newDouble) |
const Object & | getObject () const |
void | setObject (const Object &newObject) |
const Array & | getArray () const |
void | setArray (const Array &newArray) |
bool | getBoolean () const |
bool | tryGetBoolean (bool defaultValue) const |
void | setBoolean (bool newBoolean) |
void | setNull () |
void | loadFromString (const std::string &json) |
void | loadFromStream (std::istream &input) |
void | loadFromFile (const std::string &filePath) |
void | writeToStream (std::ostream &output, bool indent=true, bool escapeAll=false) const |
void | writeToFile (const std::string &filePath, bool indent=true, bool escapeAll=false) const |
Static Public Member Functions | |
static std::string | escapeMinimumCharacters (const std::string &str) |
static std::string | escapeAllCharacters (const std::string &str) |
static const std::string | escapeToUnicode (char charToEscape) |
Private Member Functions | |
void | clear () |
void | output (std::ostream &output, bool indent=true, bool escapeAll=false) const |
Static Private Member Functions | |
static bool | isHexDigit (char digit) |
static bool | isWhiteSpace (char whiteSpace) |
static void | readString (std::istream &input, std::string &result) |
static void | readObject (std::istream &input, Object &result) |
static void | readArray (std::istream &input, Array &result) |
static void | readNumber (std::istream &input, Value &result) |
static void | readToNonWhiteSpace (std::istream &input, char ¤tCharacter) |
Private Attributes | |
Type | type |
ValueDataPointer | data |
Static Private Attributes | |
static const std::string | EMPTY_STRING = std::string() |
static const int | EMPTY_INT = 0 |
static const double | EMPTY_DOUBLE = 0.0 |
static const Object | EMPTY_OBJECT = Object() |
static const Array | EMPTY_ARRAY = Array() |
static const bool | EMPTY_BOOL = false |
Friends | |
std::ostream & | operator<< (std::ostream &output, const Value &v) |
Represents a json value. Can be a string, an integer, a floating point number, an object, an array, a boolean value or a null value. To put it simply, it acts a lot like a variant. Objects are actually a map of strings and values while an array is a deque of values. The user doesn't have to worry about character escaping in strings, the i/o algorithms take care of that for the user.
enum JsonBox::Value::Type |
JsonBox::Value::Value | ( | ) |
Default constructor. Makes the value null.
JsonBox::Value::Value | ( | std::istream & | input | ) |
Loads the value from an input stream. If there is more than one value in the input stream, they are ignored. Only the first value is read.
input | Input stream to load the value from. Can also be an input file stream. |
JsonBox::Value::Value | ( | const std::string & | newString | ) |
Constructs the value from a string.
newString | String used as the value. |
JsonBox::Value::Value | ( | const char * | newCString | ) |
Constructs the value from a C-style string.
newCString | C-style string used as the value. |
JsonBox::Value::Value | ( | int | newInt | ) |
Constructs the value from an integer.
newInt | Integer used as the value. |
JsonBox::Value::Value | ( | double | newDouble | ) |
Constructs the value from a double.
newDouble | Double used as the value. |
JsonBox::Value::Value | ( | const Object & | newObject | ) |
Constructs the value from an object.
newObject | Object used as the value. |
JsonBox::Value::Value | ( | const Array & | newArray | ) |
Constructs the value from an array.
newArray | Array used as the value. |
JsonBox::Value::Value | ( | bool | newBoolean | ) |
Constructs the value from a boolean.
newBoolean | Boolean used as the value. |
JsonBox::Value::~Value | ( | ) |
Destructor. Frees up the memory used by the value's allocated pointers.
|
private |
Frees up the dynamic memory allocated by the value.
|
static |
Replaces characters with its JSON equivalent for escape characters. So for example, if in the string there is the newline character '
', it will be replaced by the two characters '\' and 'n'.
str | String make a copy and have its characters escaped. |
|
static |
Replaces characters with their JSON equivalent. The only difference from escapeAllCharacters is that the solidi won't be escaped in this method.
str | String to have its characters escaped. |
|
static |
Escapes a character to its unicode equivalent. This function only takes characters from '/0' to '/x1f'.
charToEscape | Character to escape, must be between '\0' and '\x1f'. |
const Array & JsonBox::Value::getArray | ( | ) | const |
bool JsonBox::Value::getBoolean | ( | ) | const |
Gets the value's boolean value.
double JsonBox::Value::getDouble | ( | ) | const |
Gets the value's double value.
float JsonBox::Value::getFloat | ( | ) | const |
Gets the value's float value.
int JsonBox::Value::getInteger | ( | ) | const |
Gets the value's integer value.
const Object & JsonBox::Value::getObject | ( | ) | const |
Gets the value's object value.
const std::string & JsonBox::Value::getString | ( | ) | const |
Gets the value's string value.
const std::string JsonBox::Value::getToString | ( | ) | const |
Gets the value's string value or converts its numeric, boolean or null value to a string.
Value::Type JsonBox::Value::getType | ( | ) | const |
Gets the value's type.
bool JsonBox::Value::isArray | ( | ) | const |
Checks if the value is an array.
bool JsonBox::Value::isBoolean | ( | ) | const |
Checks if the value is a boolean.
bool JsonBox::Value::isDouble | ( | ) | const |
Checks if the value is a double.
|
staticprivate |
Checks if the char given is a hex digit.
bool JsonBox::Value::isInteger | ( | ) | const |
Checks if the value is an integer.
bool JsonBox::Value::isNull | ( | ) | const |
Checks if the value is null.
bool JsonBox::Value::isNumeric | ( | ) | const |
Checks if the value is either an integer or a double.
bool JsonBox::Value::isObject | ( | ) | const |
Checks if the value is an object.
bool JsonBox::Value::isString | ( | ) | const |
Checks if the value is a string.
bool JsonBox::Value::isStringable | ( | ) | const |
Checks if the value can be a string.
|
staticprivate |
Checks if the char given is a JSON whitespace.
void JsonBox::Value::loadFromFile | ( | const std::string & | filePath | ) |
Loads a value from a file. Loads the file then calls the loadFromStream(...) method.
filePath | Path to the JSON file to load. |
void JsonBox::Value::loadFromStream | ( | std::istream & | input | ) |
Loads a Value from a stream containing valid JSON in UTF-8. Does not read the stream if it is in UTF-32 or UTF-16. All the json escape sequences in string values are converted to their char equivalent, including unicode characters. Unicode characters that use two "\u" sequences are interpreted as two unicode characters, so it doesn't support perfect parsing.
input | Input stream to read from. Can be a file stream. |
void JsonBox::Value::loadFromString | ( | const std::string & | json | ) |
Loads the current value from a string containing the JSON to parse.
json | String containing the JSON to parse. |
bool JsonBox::Value::operator!= | ( | const Value & | rhs | ) | const |
Checks if the current value is different from the right hand side value.
rhs | Right hand side value to check for not equality with. |
bool JsonBox::Value::operator< | ( | const Value & | rhs | ) | const |
Checks if the contents of instance are lexicographically less than the contents of the right hand side value.
rhs | Right hand side value to check. |
bool JsonBox::Value::operator<= | ( | const Value & | rhs | ) | const |
Checks if the contents of instance are lexicographically less than or equal the contents of the right hand side value.
rhs | Right hand side value to check. |
Value & JsonBox::Value::operator= | ( | const std::string & | src | ) |
Assignation operator overload.
src | String to copy. |
Value & JsonBox::Value::operator= | ( | const char * | src | ) |
Assignation operator overload.
src | String to copy. |
Value & JsonBox::Value::operator= | ( | int | src | ) |
Assignation operator overload.
src | Integer to copy. |
Value & JsonBox::Value::operator= | ( | double | src | ) |
Assignation operator overload.
src | Double to copy. |
Assignation operator overload.
src | Object to copy. |
Value & JsonBox::Value::operator= | ( | bool | src | ) |
Assignation operator overload.
src | Boolean to copy. |
bool JsonBox::Value::operator== | ( | const Value & | rhs | ) | const |
Checks if the current value is equal to the right hand side value.
rhs | Right hand side value to check for equality with. |
bool JsonBox::Value::operator> | ( | const Value & | rhs | ) | const |
Checks if the contents of instance are lexicographically greater than the contents of the right hand side value.
rhs | Right hand side value to check. |
bool JsonBox::Value::operator>= | ( | const Value & | rhs | ) | const |
Checks if the contents of instance are lexicographically greater than or equal the contents of the right hand side value.
rhs | Right hand side value to check. |
Value& JsonBox::Value::operator[] | ( | const std::string & | key | ) |
Bracket operator overload. If the value doesn't represent an object, it is changed to do so and accesses the object's member value. If the object's member doesn't exist, it is created.
key | Key identifier of the object's value to get. |
Value & JsonBox::Value::operator[] | ( | const char * | key | ) |
Bracket operator overload. If the value doesn't represent an object, it is changed to do so and accesses the object's member value. If the object's member doesn't exist, it is created.
key | Key identifier of the object's value to get. |
Value& JsonBox::Value::operator[] | ( | size_t | index | ) |
Bracket operator overload. If the value doesn't represent an array, it is changed to do so and accesses the array's value at the specified index. To make sure the index value exists when it creates the array, it initializes the array with empty values up to the required index. If the value already represents an array and the index is too high for the size of the array, the array is resized to be of size index + 1.
index | Index of the value to get. |
|
private |
Outputs the value in JSON format.
output | Output stream used to output the value in JSON format. |
indent | Specifies if the JSON being output must be indented or not. False is to output the JSON in compact format. |
escapeAll | Specifies if the strings must escape all characters or only the minimum. |
|
staticprivate |
Reads a JSON array from an input stream.
input | Input stream to read the array from. |
result | Array read from the input stream. |
|
staticprivate |
Reads a JSON number from an input stream.
input | Input stream to read the array from. |
result | Value containing the integer or the double read from the input stream. |
|
staticprivate |
Reads a JSON object from an input stream.
input | Input stream to read the object from. |
result | Object read from the input stream. |
|
staticprivate |
Reads a JSON string from an input stream.
input | Input stream to read the string value from. |
result | UTF-8 string read from the input stream. |
|
staticprivate |
Advances through the input stream until it reaches a character that is not a whitespace.
input | Input stream to read the whitespace characters from. |
currentCharacter | Char in which each character read is temporarily stored. After the method is called, this char contains the first non white space character reached. |
void JsonBox::Value::setArray | ( | const Array & | newArray | ) |
Sets the value as a JSON array.
newArray | New array value that the Value will contain. The value's type is changed if necessary to contain the array. |
void JsonBox::Value::setBoolean | ( | bool | newBoolean | ) |
Sets the value as a boolean.
newBoolean | New boolean value that the Value will contain. The value's type is changed if necessary to contain the boolean. |
void JsonBox::Value::setDouble | ( | double | newDouble | ) |
Sets the value as a double.
newDouble | New double value that the Value will contain. The value's type is changed if necessary to contain the double. |
void JsonBox::Value::setInteger | ( | int | newInt | ) |
Sets the value as an integer.
newInt | New integer value that the Value will contain. The value's type is changed if necessary to contain the integer. |
void JsonBox::Value::setNull | ( | ) |
Sets the value as a null value.
void JsonBox::Value::setObject | ( | const Object & | newObject | ) |
Sets the value as a JSON object.
newObject | New object value that the Value will contain. The value's type is changed if necessary to contain the object. |
void JsonBox::Value::setString | ( | const std::string & | newString | ) |
Sets the value as a string.
newString | New string value that the Value will contain. If the value's type is changed if necessary to contain the integer. |
bool JsonBox::Value::tryGetBoolean | ( | bool | defaultValue | ) | const |
Tries to get the value's boolean value.
defaultValue | Boolean value to return if the value doesn't contain a boolean value. |
double JsonBox::Value::tryGetDouble | ( | double | defaultValue | ) | const |
Tries to get the value's double value.
defaultValue | Double value to return if the value doesn't contain a numeric value. |
float JsonBox::Value::tryGetFloat | ( | float | defaultValue | ) | const |
Tries to get the value's float value.
defaultValue | Float value to return if the value doesn't contain a numeric value. |
int JsonBox::Value::tryGetInteger | ( | int | defaultValue | ) | const |
Tries to get the value's integer value.
defaultValue | Integer value to return if the value doesn't contain a numeric value. |
const std::string & JsonBox::Value::tryGetString | ( | const std::string & | defaultValue | ) | const |
Tries to get the value's string value.
defaultValue | String value to return if the value doesn't contain a string. |
void JsonBox::Value::writeToFile | ( | const std::string & | filePath, |
bool | indent = true , |
||
bool | escapeAll = false |
||
) | const |
Writes the value to a JSON file. Uses writeToStream(...).
filePath | Path to the file to write. |
indent | Specifies if the output is to have nice indentation or not. |
escapeAll | Specifies if all the JSON escapable characters should be escaped or not. |
void JsonBox::Value::writeToStream | ( | std::ostream & | output, |
bool | indent = true , |
||
bool | escapeAll = false |
||
) | const |
Writes the value to an output stream in valid JSON. Uses the overloaded output operator.
output | Output stream to write the value to. |
indent | Specifies if the output is to have nice indentation or not. |
escapeAll | Specifies if all the JSON escapable characters should be escaped or not. |
|
friend |
Output operator overload. Outputs the value as valid JSON. Does not do any indentation.
output | Output stream in which the valid JSON is written. |
v | Value to be output in the stream. |
|
private |
Pointer to the Value's data.
Default empty array value returned by getArray() when the value doesn't contain an array.
|
staticprivate |
Default boolean value returned by getBoolean() when the value doesn't contain a boolean.
|
staticprivate |
Default double value returned by getDouble() when the value doesn't contain a double.
|
staticprivate |
Default int value returned by getInteger() when the value doesn't contain an integer.
Default empty object value returned by getObject() when the value doesn't contain an object.
|
staticprivate |
Empty string returned by getString() when the value doesn't contain a string.
|
private |
Type of data the value contains.