Json Box  0.6.1
This is a JSON C++ library. It can write and read JSON files with ease and speed.
Classes | Public Types | Public Member Functions | Static Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes | Friends | List of all members
JsonBox::Value Class Reference

#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 ()
 
Valueoperator= (const Value &src)
 
Valueoperator= (const std::string &src)
 
Valueoperator= (const char *src)
 
Valueoperator= (int src)
 
Valueoperator= (double src)
 
Valueoperator= (const Object &src)
 
Valueoperator= (const Array &src)
 
Valueoperator= (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
 
Valueoperator[] (const std::string &key)
 
Valueoperator[] (const char *key)
 
Valueoperator[] (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 ObjectgetObject () const
 
void setObject (const Object &newObject)
 
const ArraygetArray () 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 &currentCharacter)
 

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)
 

Detailed Description

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.

See also
JsonBox::Array
JsonBox::Object

Member Enumeration Documentation

Represents the different types a value can be. A value can only be one of these types at a time. The UNKNOWN type is only used temporarily internally when loading values from an input stream or file.

Enumerator
STRING 
INTEGER 
DOUBLE 
OBJECT 
ARRAY 
BOOLEAN 
NULL_VALUE 
UNKNOWN 

Constructor & Destructor Documentation

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.

Parameters
inputInput 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.

Parameters
newStringString used as the value.
JsonBox::Value::Value ( const char *  newCString)

Constructs the value from a C-style string.

Parameters
newCStringC-style string used as the value.
JsonBox::Value::Value ( int  newInt)

Constructs the value from an integer.

Parameters
newIntInteger used as the value.
JsonBox::Value::Value ( double  newDouble)

Constructs the value from a double.

Parameters
newDoubleDouble used as the value.
JsonBox::Value::Value ( const Object newObject)

Constructs the value from an object.

Parameters
newObjectObject used as the value.
JsonBox::Value::Value ( const Array newArray)

Constructs the value from an array.

Parameters
newArrayArray used as the value.
JsonBox::Value::Value ( bool  newBoolean)

Constructs the value from a boolean.

Parameters
newBooleanBoolean used as the value.
JsonBox::Value::Value ( const Value src)

Copy constructor.

Parameters
srcValue to make a copy of.
JsonBox::Value::~Value ( )

Destructor. Frees up the memory used by the value's allocated pointers.

Member Function Documentation

void JsonBox::Value::clear ( )
private

Frees up the dynamic memory allocated by the value.

std::string JsonBox::Value::escapeAllCharacters ( const std::string &  str)
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'.

Parameters
strString make a copy and have its characters escaped.
Returns
Copy of the recieved string, but with the concerned characters escaped.
See also
JsonBox::Value::escapeMinimumCharacters(const std::string& str)
std::string JsonBox::Value::escapeMinimumCharacters ( const std::string &  str)
static

Replaces characters with their JSON equivalent. The only difference from escapeAllCharacters is that the solidi won't be escaped in this method.

Parameters
strString to have its characters escaped.
Returns
Copy of the recieved string, but with the concerned characters escaped.
See also
JsonBox::Value::escapeAllCharacters(const std::string& str)
const std::string JsonBox::Value::escapeToUnicode ( char  charToEscape)
static

Escapes a character to its unicode equivalent. This function only takes characters from '/0' to '/x1f'.

Parameters
charToEscapeCharacter to escape, must be between '\0' and '\x1f'.
Returns
String with the character escaped in the format "\u00xx". "xx" being the hexadecimal ASCII code of the character escaped.
const Array & JsonBox::Value::getArray ( ) const

Gets the value's array value.

Returns
Value's array value, or an empty Array if the value doesn't contain an array.
bool JsonBox::Value::getBoolean ( ) const

Gets the value's boolean value.

Returns
Value's boolean value, or false if the value doesn't contain a boolean.
double JsonBox::Value::getDouble ( ) const

Gets the value's double value.

Returns
Value's double value, or 0.0 if the value doesn't contain a numeric value.
float JsonBox::Value::getFloat ( ) const

Gets the value's float value.

Returns
Value's float value, or 0.0f if the value doesn't contain a numeric value.
int JsonBox::Value::getInteger ( ) const

Gets the value's integer value.

Returns
Value's integer value, or 0 if the value doesn't contain a numeric value.
const Object & JsonBox::Value::getObject ( ) const

Gets the value's object value.

Returns
Value's object value, or an empty object if the value doesn't contain an object.
const std::string & JsonBox::Value::getString ( ) const

Gets the value's string value.

Returns
Value's string value, or an empty string if the value doesn't contain a string.
const std::string JsonBox::Value::getToString ( ) const

Gets the value's string value or converts its numeric, boolean or null value to a string.

Returns
Value's string value. If the value contains a numeric, a boolean or a null value, it is converted to a string.
Value::Type JsonBox::Value::getType ( ) const

Gets the value's type.

Returns
Value's type, does not return Type::UNKOWN, would return NULL_VALUE if no type has been given to the value yet.
See also
JsonBox::Type
bool JsonBox::Value::isArray ( ) const

Checks if the value is an array.

Returns
True if the value contains an array, false if not.
bool JsonBox::Value::isBoolean ( ) const

Checks if the value is a boolean.

Returns
True if the value contains a boolean, false if not.
bool JsonBox::Value::isDouble ( ) const

Checks if the value is a double.

Returns
True if the value contains a double, false if not.
bool JsonBox::Value::isHexDigit ( char  digit)
staticprivate

Checks if the char given is a hex digit.

Returns
True if the char contains an hexadecimal digit (0-9, a-f or A-F).
bool JsonBox::Value::isInteger ( ) const

Checks if the value is an integer.

Returns
True if the value contains an integer, false if not.
bool JsonBox::Value::isNull ( ) const

Checks if the value is null.

Returns
True if the value contains nothing.
bool JsonBox::Value::isNumeric ( ) const

Checks if the value is either an integer or a double.

Returns
True if the value contains a double or an integer, false if not.
bool JsonBox::Value::isObject ( ) const

Checks if the value is an object.

Returns
True if the value contains an object, false if not.
bool JsonBox::Value::isString ( ) const

Checks if the value is a string.

Returns
True if the value contains a string, false if not.
bool JsonBox::Value::isStringable ( ) const

Checks if the value can be a string.

Returns
True if the value is either a string, an integer, a boolean or a null value.
bool JsonBox::Value::isWhiteSpace ( char  whiteSpace)
staticprivate

Checks if the char given is a JSON whitespace.

Returns
True if the char is either a space, a horizontal tab, a line feed or a carriage return.
void JsonBox::Value::loadFromFile ( const std::string &  filePath)

Loads a value from a file. Loads the file then calls the loadFromStream(...) method.

Parameters
filePathPath to the JSON file to load.
See also
JsonBox::Value::loadFromStream
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.

Parameters
inputInput 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.

Parameters
jsonString 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.

Parameters
rhsRight hand side value to check for not equality with.
Returns
True if the contents of the two values are different, false if not.
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.

Parameters
rhsRight hand side value to check.
Returns
True if the contents of of the instance are lexicographically less than the contents of the right hand side value.
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.

Parameters
rhsRight hand side value to check.
Returns
True if the contents of of the instance are lexicographically less than or equal the contents of the right hand side value.
Value & JsonBox::Value::operator= ( const Value src)

Assignation operator overload.

Parameters
srcValue to copy.
Returns
Reference to the modified value.
Value & JsonBox::Value::operator= ( const std::string &  src)

Assignation operator overload.

Parameters
srcString to copy.
Returns
Reference to the modified value.
Value & JsonBox::Value::operator= ( const char *  src)

Assignation operator overload.

Parameters
srcString to copy.
Returns
Reference to the modified value.
Value & JsonBox::Value::operator= ( int  src)

Assignation operator overload.

Parameters
srcInteger to copy.
Returns
Reference to the modified value.
Value & JsonBox::Value::operator= ( double  src)

Assignation operator overload.

Parameters
srcDouble to copy.
Returns
Reference to the modified value.
Value & JsonBox::Value::operator= ( const Object src)

Assignation operator overload.

Parameters
srcObject to copy.
Returns
Reference to the modified value.
Value & JsonBox::Value::operator= ( const Array src)

Assignation operator overload.

Parameters
srcArray to copy.
Returns
Reference to the modified value.
Value & JsonBox::Value::operator= ( bool  src)

Assignation operator overload.

Parameters
srcBoolean to copy.
Returns
Reference to the modified value.
bool JsonBox::Value::operator== ( const Value rhs) const

Checks if the current value is equal to the right hand side value.

Parameters
rhsRight hand side value to check for equality with.
Returns
True if the contents of the two values are equal, false if not.
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.

Parameters
rhsRight hand side value to check.
Returns
True if the contents of of the instance are lexicographically greater than the contents of the right hand side value.
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.

Parameters
rhsRight hand side value to check.
Returns
True if the contents of of the instance are lexicographically greater than or equal the contents of the right hand side value.
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.

Parameters
keyKey identifier of the object's value to get.
Returns
Reference to the object's member's value.
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.

Parameters
keyKey identifier of the object's value to get.
Returns
Reference to the object's member's value.
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.

Parameters
indexIndex of the value to get.
Returns
Reference to the value at the received index in the array.
void JsonBox::Value::output ( std::ostream &  output,
bool  indent = true,
bool  escapeAll = false 
) const
private

Outputs the value in JSON format.

Parameters
outputOutput stream used to output the value in JSON format.
indentSpecifies if the JSON being output must be indented or not. False is to output the JSON in compact format.
escapeAllSpecifies if the strings must escape all characters or only the minimum.
See also
JsonBox::Value::escapeAllCharacters(const std::string str)
JsonBox::Value::escapeMinimumCharacters(const std::string str)
JsonBox::Value::output(std::ostream& output, unsigned int& level, bool indent, bool escapeAll)
void JsonBox::Value::readArray ( std::istream &  input,
Array result 
)
staticprivate

Reads a JSON array from an input stream.

Parameters
inputInput stream to read the array from.
resultArray read from the input stream.
void JsonBox::Value::readNumber ( std::istream &  input,
JsonBox::Value result 
)
staticprivate

Reads a JSON number from an input stream.

Parameters
inputInput stream to read the array from.
resultValue containing the integer or the double read from the input stream.
void JsonBox::Value::readObject ( std::istream &  input,
Object result 
)
staticprivate

Reads a JSON object from an input stream.

Parameters
inputInput stream to read the object from.
resultObject read from the input stream.
void JsonBox::Value::readString ( std::istream &  input,
std::string &  result 
)
staticprivate

Reads a JSON string from an input stream.

Parameters
inputInput stream to read the string value from.
resultUTF-8 string read from the input stream.
void JsonBox::Value::readToNonWhiteSpace ( std::istream &  input,
char &  currentCharacter 
)
staticprivate

Advances through the input stream until it reaches a character that is not a whitespace.

Parameters
inputInput stream to read the whitespace characters from.
currentCharacterChar 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.

Parameters
newArrayNew 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.

Parameters
newBooleanNew 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.

Parameters
newDoubleNew 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.

Parameters
newIntNew 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.

Parameters
newObjectNew 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.

Parameters
newStringNew 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.

Parameters
defaultValueBoolean value to return if the value doesn't contain a boolean value.
Returns
Value's boolean value, or defaultValue if the value doesn't contain a boolean.
double JsonBox::Value::tryGetDouble ( double  defaultValue) const

Tries to get the value's double value.

Parameters
defaultValueDouble value to return if the value doesn't contain a numeric value.
Returns
Value's double value, or defaultValue if the value doesn't contain a numeric value.
float JsonBox::Value::tryGetFloat ( float  defaultValue) const

Tries to get the value's float value.

Parameters
defaultValueFloat value to return if the value doesn't contain a numeric value.
Returns
Value's float value, or defaultValue if the value doesn't contain a numeric value.
int JsonBox::Value::tryGetInteger ( int  defaultValue) const

Tries to get the value's integer value.

Parameters
defaultValueInteger value to return if the value doesn't contain a numeric value.
Returns
Value's integer value, or defaultValue 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.

Parameters
defaultValueString value to return if the value doesn't contain a string.
Returns
Value's string value, or defaultValue 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(...).

Parameters
filePathPath to the file to write.
indentSpecifies if the output is to have nice indentation or not.
escapeAllSpecifies if all the JSON escapable characters should be escaped or not.
See also
JsonBox::Value::writeToStream
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.

Parameters
outputOutput stream to write the value to.
indentSpecifies if the output is to have nice indentation or not.
escapeAllSpecifies if all the JSON escapable characters should be escaped or not.
See also
JsonBox::Value::operator<<(std::ostream& output, const Value& v)
JsonBox::Value::escapeAllCharacters
JsonBox::Value::escapeMinimumCharacters

Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  output,
const Value v 
)
friend

Output operator overload. Outputs the value as valid JSON. Does not do any indentation.

Parameters
outputOutput stream in which the valid JSON is written.
vValue to be output in the stream.
Returns
Output parameter with the valud json written into it.

Member Data Documentation

ValueDataPointer JsonBox::Value::data
private

Pointer to the Value's data.

const Array JsonBox::Value::EMPTY_ARRAY = Array()
staticprivate

Default empty array value returned by getArray() when the value doesn't contain an array.

See also
JsonBox::Value::getArray
const bool JsonBox::Value::EMPTY_BOOL = false
staticprivate

Default boolean value returned by getBoolean() when the value doesn't contain a boolean.

See also
JsonBox::Value::getBoolean
const double JsonBox::Value::EMPTY_DOUBLE = 0.0
staticprivate

Default double value returned by getDouble() when the value doesn't contain a double.

See also
JsonBox::Value::getDouble
const int JsonBox::Value::EMPTY_INT = 0
staticprivate

Default int value returned by getInteger() when the value doesn't contain an integer.

See also
JsonBox::Value::getInt
const Object JsonBox::Value::EMPTY_OBJECT = Object()
staticprivate

Default empty object value returned by getObject() when the value doesn't contain an object.

See also
JsonBox::Value::getObject
const std::string JsonBox::Value::EMPTY_STRING = std::string()
staticprivate

Empty string returned by getString() when the value doesn't contain a string.

See also
JsonBox::Value::getString
Type JsonBox::Value::type
private

Type of data the value contains.


The documentation for this class was generated from the following files: