Giri's C++ Support Library
C++ library providing everything you need to quickly create awesome applications.
giri::json::JSON Class Referencefinal

Class to represent and use JSON objects. Class may throw exceptions of type std::error_code on error. More...

#include <JSON.h>

Classes

class  JSONConstWrapper
 Provides const iterators to iterate over objects/arrays. More...
 
class  JSONWrapper
 Provides iterators to iterate over objects/arrays. More...
 

Public Types

enum class  Class {
  Null , Object , Array , String ,
  Floating , Integral , Boolean
}
 

Public Member Functions

 JSON (Class type)
 
 JSON (std::initializer_list< JSON > list)
 
 JSON (JSON &&other)
 
JSONoperator= (JSON &&other)
 
 JSON (const JSON &other)
 
JSONoperator= (const JSON &other)
 
template<typename T >
 JSON (T b, typename std::enable_if< std::is_same< T, bool >::value >::type *=0)
 
template<typename T >
 JSON (T i, typename std::enable_if< std::is_integral< T >::value &&!std::is_same< T, bool >::value >::type *=0)
 
template<typename T >
 JSON (T f, typename std::enable_if< std::is_floating_point< T >::value >::type *=0)
 
template<typename T >
 JSON (T s, typename std::enable_if< std::is_convertible< T, std::string >::value >::type *=0)
 
 JSON (std::nullptr_t)
 
template<typename T >
void append (T arg)
 
template<typename T , typename... U>
void append (T arg, U... args)
 
template<typename T >
std::enable_if< std::is_same< T, bool >::value, JSON & >::type operator= (T b)
 
template<typename T >
std::enable_if< std::is_integral< T >::value &&!std::is_same< T, bool >::value, JSON & >::type operator= (T i)
 
template<typename T >
std::enable_if< std::is_floating_point< T >::value, JSON & >::type operator= (T f)
 
template<typename T >
std::enable_if< std::is_convertible< T, std::string >::value, JSON & >::type operator= (T s)
 
JSONoperator[] (const std::string &key)
 
JSONoperator[] (unsigned index)
 
JSONat (const std::string &key)
 
const JSONat (const std::string &key) const
 
JSONat (unsigned index)
 
const JSONat (unsigned index) const
 
std::size_t length () const
 
bool hasKey (const std::string &key) const
 
std::size_t size () const
 
Class JSONType () const
 
bool IsNull () const
 
bool IsArray () const
 
bool IsBoolean () const
 
bool IsFloating () const
 
bool IsIntegral () const
 
bool IsString () const
 
bool IsObject () const
 
std::string ToString (std::error_code &ec) const noexcept
 
std::string ToString () const
 
std::string ToUnescapedString (std::error_code &ec) const noexcept
 
std::string ToUnescapedString () const
 
double ToFloat (std::error_code &ec) const noexcept
 
double ToFloat () const
 
long long ToInt (std::error_code &ec) const noexcept
 
long long ToInt () const
 
bool ToBool (std::error_code &ec) const noexcept
 
bool ToBool () const
 
JSONWrapper< std::map< std::string, JSON > > ObjectRange ()
 
JSONWrapper< std::deque< JSON > > ArrayRange ()
 
JSONConstWrapper< std::map< std::string, JSON > > ObjectRange () const
 
JSONConstWrapper< std::deque< JSON > > ArrayRange () const
 
std::string dump (int depth=1, std::string tab=" ") const
 
std::string dumpMinified () const
 

Static Public Member Functions

static JSON Make (Class type)
 
static JSON Load (const std::string &str)
 
static JSON Load (const std::string &str, std::error_code &ec) noexcept
 

Friends

std::ostream & operator<< (std::ostream &, const JSON &)
 

Detailed Description

Class to represent and use JSON objects. Class may throw exceptions of type std::error_code on error.

Example Usage:

Array Example

Example to show how to use Arrays.

#include <JSON.h>
#include <iostream>
using namespace std;
int main()
{
JSON array;
array[2] = "Test2";
cout << array << endl;
array[1] = "Test1";
cout << array << endl;
array[0] = "Test0";
cout << array << endl;
array[3] = "Test4";
cout << array << endl;
// Arrays can be nested:
JSON Array2;
Array2[2][0][1] = true;
cout << Array2 << endl;
}
Lightweight JSON library for exporting/importing data in JSON format from/to C++. Can be used standal...
Class to represent and use JSON objects. Class may throw exceptions of type std::error_code on error.
Definition: JSON.h:445
Definition: JSON.h:123

Initialization Example

Simple example which shows how to directly load an object.

#include <JSON.h>
#include <iostream>
#include <cstddef>
using namespace std;
int main()
{
JSON obj( {
"Key", 1,
"Key3", true,
"Key4", nullptr,
"Key2", {
"Key4", "VALUE",
"Arr", giri::json::Array( 1, "Str", false )
}
} );
cout << obj << endl;
}

Iterator Example

This example shows how to iterate over stored arrays and objects.

#include <JSON.h>
#include <iostream>
using namespace std;
void dumpArrayConst( const JSON &array ) {
for( auto &j : array.ArrayRange() )
std::cout << "Value: " << j << "\n";
}
void dumpArray( JSON &array ) {
for( auto &j : array.ArrayRange() )
std::cout << "Value: " << j << "\n";
}
void dumpObjectConst( const JSON &object ) {
for( auto &j : object.ObjectRange() )
std::cout << "Object[ " << j.first << " ] = " << j.second << "\n";
}
void dumpObject( JSON &object ) {
for( auto &j : object.ObjectRange() )
std::cout << "Object[ " << j.first << " ] = " << j.second << "\n";
}
int main()
{
JSON array = JSON::Make( JSON::Class::Array );
JSON obj = JSON::Make( JSON::Class::Object );
array[0] = "Test0";
array[1] = "Test1";
array[2] = "Test2";
array[3] = "Test3";
obj[ "Key0" ] = "Value1";
obj[ "Key1" ] = array;
obj[ "Key2" ] = 123;
std::cout << "=============== tests ================\n";
dumpArray( array );
dumpObject( obj );
std::cout << "============ const tests =============\n";
dumpArrayConst( array );
dumpObjectConst( obj );
}
JSONWrapper< std::map< std::string, JSON > > ObjectRange()
Definition: JSON.h:1027
static JSON Make(Class type)
Definition: JSON.h:609

Datatypes Example

In this example it is shown, how to store all supported datatypes within an JSON object.

#include <JSON.h>
#include <iostream>
using namespace std;
int main()
{
// Example of creating each type
// You can also do JSON::Make( JSON::Class )
JSON null;
JSON Bool( true );
JSON Str( "RawString" );
JSON Str2( string( "C++String" ) );
JSON Int( 1 );
JSON Float( 1.2 );
JSON Arr = giri::json::Array();
JSON Obj = giri::json::Object();
// Types can be overwritten by assigning
// to the object again.
Bool = false;
Bool = "rtew";
Bool = 1;
Bool = 1.1;
Bool = string( "asd" );
// Append to Arrays, appending to a non-array
// will turn the object into an array with the
// first element being the value that's being
// appended.
Arr.append( 1 );
Arr.append( "test" );
Arr.append( false );
// Access Array elements with operator[]( unsigned ).
// Note that this does not do bounds checking, and
// returns a reference to a JSON object.
JSON& val = Arr[0];
// Arrays can be intialized with any elements and
// they are turned into JSON objects. Variadic
// Templates are pretty cool.
JSON Arr2 = giri::json::Array( 2, "Test", true );
// Objects are accessed using operator[]( string ).
// Will create new pairs on the fly, just as std::map
// would.
Obj["Key1"] = 1.0;
Obj["Key2"] = "Value";
JSON Obj2 = giri::json::Object();
Obj2["Key3"] = 1;
Obj2["Key4"] = Arr;
Obj2["Key5"] = Arr2;
// Nested Object
Obj["Key6"] = Obj2;
// Dump Obj to a string.
cout << Obj << endl;
// We can also use a more JSON-like syntax to create
// JSON Objects.
JSON Obj3 = {
"Key1", "Value",
"Key2", true,
"Key3", {
"Key4", giri::json::Array( "This", "Is", "An", "Array" ),
"Key5", {
"BooleanValue", true
}
}
};
cout << Obj3 << endl;
}

Load string Example

This example shows how to load an object from string.

#include <JSON.h>
#include <iostream>
using namespace std;
int main()
{
JSON Int = JSON::Load( " 123 " );
JSON Float = JSON::Load( " 123.234 " );
JSON Str = JSON::Load( "\"String\"" );
JSON EscStr = JSON::Load( "\" \\\"Some\\/thing\\\" \"" );
JSON Arr = JSON::Load( "[1,2, true, false,\"STRING\", 1.5]" );
JSON Obj = JSON::Load( "{ \"Key\" : \"StringValue\","
" \"Key2\" : true, "
" \"Key3\" : 1234, "
" \"Key4\" : null }" );
cout << Int << endl;
cout << Float << endl;
cout << Str << endl;
cout << EscStr << endl;
cout << Arr << endl;
cout << Obj << endl;
}
static JSON Load(const std::string &str)
Definition: JSON.h:1421

Assignment of primitives Example

Assign and print primitives.

#include <JSON.h>
#include <iostream>
#include <ios>
using namespace std;
int main()
{
JSON obj;
obj = true;
cout << "Value: " << boolalpha << obj.ToBool() << endl;
obj = "Test String";
cout << "Value: " << obj.ToString() << endl;
obj = 2.2;
cout << "Value: " << obj.ToFloat() << endl;
obj = 3;
cout << "Value: " << obj.ToInt() << endl;
}

Member Function Documentation

◆ append() [1/2]

template<typename T >
void giri::json::JSON::append ( arg)
inline

Allows appending items to array. Appending to a non-array will turn the object into an array with the first element being the value that's being appended.

Parameters
argItem to append.

◆ append() [2/2]

template<typename T , typename... U>
void giri::json::JSON::append ( arg,
U...  args 
)
inline

Allows appending items to array. Appending to a non-array will turn the object into an array with the first element being the value that's being appended.

Parameters
argItem to append.
argsFurther items to append.

◆ ArrayRange() [1/2]

JSONWrapper<std::deque<JSON> > giri::json::JSON::ArrayRange ( )
inline

Returns Array range which allows iterating over the array items.

Returns
Array range which allows iterating over the array items.

◆ ArrayRange() [2/2]

JSONConstWrapper<std::deque<JSON> > giri::json::JSON::ArrayRange ( ) const
inline

Returns ArrayRange which allows iterating over the array items.

Returns
ArrayRange which allows iterating over the array items.

◆ at() [1/4]

JSON& giri::json::JSON::at ( const std::string &  key)
inline

Allows getting an object entry by key.

Parameters
keyKey to access.
Returns
object entry by key.

◆ at() [2/4]

const JSON& giri::json::JSON::at ( const std::string &  key) const
inline

Allows getting an object entry by key.

Parameters
keyKey to access.
Returns
object entry by key.

◆ at() [3/4]

JSON& giri::json::JSON::at ( unsigned  index)
inline

Allows getting an array entry by index.

Parameters
indexIndex to access.
Returns
array entry by index.

◆ at() [4/4]

const JSON& giri::json::JSON::at ( unsigned  index) const
inline

Allows getting an array entry by index.

Parameters
indexIndex to access.
Returns
array entry by index.

◆ dump()

std::string giri::json::JSON::dump ( int  depth = 1,
std::string  tab = "  " 
) const
inline

Returns the whole json object as formatted string.

Parameters
depthnumber of indentation per level (defaults to 1)
tabindentation character(s) (defaults to two spaces)
Returns
json object as formatted string.

◆ dumpMinified()

std::string giri::json::JSON::dumpMinified ( ) const
inline

Returns the whole json object as minified string.

Returns
json object as minified string.

◆ hasKey()

bool giri::json::JSON::hasKey ( const std::string &  key) const
inline
Parameters
keyKey to check.
Returns
true if the object holds a item with the given key, false otherwise.

◆ IsArray()

bool giri::json::JSON::IsArray ( ) const
inline
Returns
true if the object is Array, false otherwise.

◆ IsBoolean()

bool giri::json::JSON::IsBoolean ( ) const
inline
Returns
true if the object is Boolean, false otherwise.

◆ IsFloating()

bool giri::json::JSON::IsFloating ( ) const
inline
Returns
true if the object is Floating, false otherwise.

◆ IsIntegral()

bool giri::json::JSON::IsIntegral ( ) const
inline
Returns
true if the object is Integral, false otherwise.

◆ IsNull()

bool giri::json::JSON::IsNull ( ) const
inline
Returns
true if the object is Null, false otherwise.

◆ IsObject()

bool giri::json::JSON::IsObject ( ) const
inline
Returns
true if the object is a JSONObject, false otherwise.

◆ IsString()

bool giri::json::JSON::IsString ( ) const
inline
Returns
true if the object is String, false otherwise.

◆ JSONType()

Class giri::json::JSON::JSONType ( ) const
inline
Returns
Class type of the object.

◆ length()

std::size_t giri::json::JSON::length ( ) const
inline
Returns
The number of items stored within an Array. -1 if class type is not Array.

◆ Load() [1/2]

JSON giri::json::JSON::Load ( const std::string &  str)
inlinestatic

Create a JSON object from string, throws std::error_code on error.

Parameters
strJSON string to parse and load.
Returns
New JSON object representing the json defined by the parsed string.

◆ Load() [2/2]

JSON giri::json::JSON::Load ( const std::string &  str,
std::error_code &  ec 
)
inlinestaticnoexcept

Create a JSON object from string.

Parameters
strJSON string to parse and load.
ec[OUT] Output parameter giving feedback if parsing was successful.
Returns
New JSON object representing the json defined by the parsed string.

◆ Make()

static JSON giri::json::JSON::Make ( Class  type)
inlinestatic

Creates a new JSON object.

Parameters
typeClass type to create.
Returns
JSON object of given class type.

◆ ObjectRange() [1/2]

JSONWrapper<std::map<std::string,JSON> > giri::json::JSON::ObjectRange ( )
inline

Returns ObjectRange which allows iterating over the object items.

Returns
ObjectRange which allows iterating over the object items.

◆ ObjectRange() [2/2]

JSONConstWrapper<std::map<std::string,JSON> > giri::json::JSON::ObjectRange ( ) const
inline

Returns ObjectRange which allows iterating over the object items.

Returns
ObjectRange which allows iterating over the object items.

◆ operator[]() [1/2]

JSON& giri::json::JSON::operator[] ( const std::string &  key)
inline

Allows accessing and creating object entries by key.

Parameters
keyKey to access, will be created if not existent.
Returns
The object stored at key.

◆ operator[]() [2/2]

JSON& giri::json::JSON::operator[] ( unsigned  index)
inline

Allows accessing and creating array entries by index.

Parameters
indexIndex to access, will be created if not existent.
Returns
The object stored at index.

◆ size()

std::size_t giri::json::JSON::size ( ) const
inline
Returns
The number of items stored within an array or object. -1 if class type is neither array nor object.

◆ ToBool() [1/2]

bool giri::json::JSON::ToBool ( ) const
inline
Returns
If class type is Integral, Floating or Boolean, the stored value. If the class type is String, an conversion will be tried. Throws std::error_code on conversion error.

◆ ToBool() [2/2]

bool giri::json::JSON::ToBool ( std::error_code &  ec) const
inlinenoexcept
Parameters
ok[OUT] Output parameter giving feedback if the conversion was successful.
Returns
If class type is Integral, Floating or Boolean, the stored value. If the class type is String, an conversion will be tried. false otherwise or on conversion error.

◆ ToFloat() [1/2]

double giri::json::JSON::ToFloat ( ) const
inline
Returns
If class type is Integral, Floating or Boolean, the stored value. If the class type is String, an conversion will be tried. Throws std::error_code on conversion error.

◆ ToFloat() [2/2]

double giri::json::JSON::ToFloat ( std::error_code &  ec) const
inlinenoexcept
Parameters
ec[OUT] Output parameter giving feedback if the conversion was successful.
Returns
If class type is Integral, Floating or Boolean, the stored value. If the class type is String, an conversion will be tried. 0.0 otherwise or on conversion error.

◆ ToInt() [1/2]

long long giri::json::JSON::ToInt ( ) const
inline
Returns
If class type is Integral, Floating or Boolean, the stored value. If the class type is String, an conversion will be tried. Throws std::error_code on conversion error.

◆ ToInt() [2/2]

long long giri::json::JSON::ToInt ( std::error_code &  ec) const
inlinenoexcept
Parameters
ec[OUT] Output parameter giving feedback if the conversion was successful.
Returns
If class type is Integral, Floating or Boolean, the stored value. If the class type is String, an conversion will be tried. 0 otherwise or on conversion error.

◆ ToString() [1/2]

std::string giri::json::JSON::ToString ( ) const
inline
Returns
If class type is String, the stored value. If class type is Null, JSONObject, Array, Boolean, Floating or Integral a conversion will be tried. Comma of a converted Floating value depends on the users locale setting. Throws std::error_code on conversion error.

◆ ToString() [2/2]

std::string giri::json::JSON::ToString ( std::error_code &  ec) const
inlinenoexcept
Parameters
ec[OUT] Output parameter giving feedback if the conversion was successful.
Returns
If class type is String, the stored value. If class type is Null, JSONObject, Array, Boolean, Floating or Integral a conversion will be tried. Comma of a converted Floating value depends on the users locale setting. Returns empty string otherwise or on conversion error.

◆ ToUnescapedString() [1/2]

std::string giri::json::JSON::ToUnescapedString ( ) const
inline

Useful if json objects are stored within the json as string.

Returns
If class type is String, the stored value without escaping. If class type is Null, JSONObject, Array, Boolean, Floating or Integral a conversion will be tried. Comma of a converted Floating value depends on the users locale setting. Throws std::error_code on conversion error.

◆ ToUnescapedString() [2/2]

std::string giri::json::JSON::ToUnescapedString ( std::error_code &  ec) const
inlinenoexcept

Useful if json objects are stored within the json as string.

Parameters
ec[OUT] Output parameter giving feedback if the conversion was successful.
Returns
If class type is String, the stored value without escaping. If class type is Null, JSONObject, Array, Boolean, Floating or Integral a conversion will be tried. Comma of a converted Floating value depends on the users locale setting. Returns empty string otherwise or on conversion error.

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