jubilant-funicular
Classes | Public Member Functions | Static Public Member Functions | Private Types | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
utils::Json Class Reference

#include <Json.h>

Collaboration diagram for utils::Json:
Collaboration graph
[legend]

Classes

class  iterator
 
struct  JsonToken
 Used for tokenizing strings before parsing them into Json objects. More...
 
union  JsonToken.__unnamed__
 
class  SetBeginEndKey
 Key unlocking the set_{begin,end}() "private" functions of class Json::iterator. More...
 

Public Member Functions

 Json (const std::string &str)
 
 Json (const char *str)
 
 Json (JsonNum num)
 
 Json (double num)
 
 Json (std::size_t num)
 
 Json (int num)
 
 Json (JsonObject obj)
 
 Json (JsonArray arr)
 
 Json (bool b)
 
 Json (const std::initializer_list< Json > &data)
 
 Json (const Json &other)
 
 Json (Json &&other)
 
JsonValueType get_type () const
 
bool is_string () const
 
bool is_number () const
 
bool is_object () const
 
bool is_array () const
 
bool is_bool () const
 
bool is_null () const
 
std::string as_string () const
 
JsonNum as_number () const
 
uint64_t as_uint () const
 
int64_t as_int () const
 
double as_float () const
 
double as_double () const
 
JsonObject as_object () const
 
JsonArray as_array () const
 
bool as_bool () const
 
std::size_t size () const
 
bool is_empty () const
 
bool has_key (const std::string &key) const
 
bool resize (std::size_t size)
 
bool push_back (const Json &val)
 
Jsonfront ()
 Returns first element if this is an array.
 
Jsonback ()
 Returns last element if this is an array.
 
Jsonmerge (const Json &other)
 
std::string dump (std::size_t indent=0, std::size_t offset=0) const
 
 operator std::string () const
 
 operator JsonNum () const
 
 operator uint64_t () const
 
 operator int64_t () const
 
 operator int () const
 
 operator float () const
 
 operator double () const
 
 operator JsonObject () const
 
 operator JsonArray () const
 
 operator bool () const
 
Jsonoperator= (const Json &other)
 
Jsonoperator= (Json &&other)
 
bool operator== (const Json &other) const
 
bool operator!= (const Json &other) const
 
bool operator== (const std::string &other) const
 
bool operator!= (const std::string &other) const
 
bool operator== (const char *other) const
 
bool operator!= (const char *other) const
 
bool operator== (const JsonNum &other) const
 
bool operator!= (const JsonNum &other) const
 
bool operator== (const int &other) const
 
bool operator!= (const int &other) const
 
bool operator== (const JsonObject &other) const
 
bool operator!= (const JsonObject &other) const
 
bool operator== (const JsonArray &other) const
 
bool operator!= (const JsonArray &other) const
 
bool operator== (const bool &other) const
 
bool operator!= (const bool &other) const
 
Jsonoperator[] (const std::string &key)
 When m_type == NONE, first converts this into an Object. More...
 
Jsonoperator[] (const std::string &key) const
 
Jsonoperator[] (const char *key)
 
Jsonoperator[] (const char *key) const
 
Jsonoperator[] (std::size_t idx)
 When m_type == NONE, first converts this into an Array.
 
Jsonoperator[] (std::size_t idx) const
 
Jsonoperator[] (int idx)
 
Jsonoperator[] (int idx) const
 
iterator< 0 > begin ()
 
iterator< 1 > cbegin () const
 
iterator< 0 > end ()
 
iterator< 1 > cend () const
 

Static Public Member Functions

static Json string (const std::string &str)
 
static Json string (const char *str)
 
static Json number (JsonNum num)
 
static Json object (JsonObject obj)
 
static Json array (JsonArray arr)
 
static Json boolean (bool boolean)
 
static Json null ()
 
static Json parse (const std::string &json)
 Parses Json value from a string.
 
static Json from_file (const std::string &path)
 

Private Types

enum  JsonTokenType { SYMBOL, NUMTKN }
 

Private Member Functions

 Json (JsonValueType type)
 

Static Private Member Functions

static bool lex_string (std::string &str, JsonToken &ret)
 Attempts to retreive a string token from the beginning of str.
 
static bool lex_number (std::string &str, JsonToken &ret)
 Attempts to retreive a number token from the beginning of str.
 
static bool lex_bool (std::string &str, JsonToken &ret)
 Attempts to retreive a boolean token from the beginning of str.
 
static bool lex_null (std::string &str, JsonToken &ret)
 Attempts to retreive a null token from the beginning of str.
 
static std::queue< JsonTokentokenize (std::string curr)
 Converts curr into a stream of tokens. More...
 
static Json parse_tokens (std::queue< JsonToken > &tokens)
 Converts stream of tokens into a Json value.
 

Private Attributes

JsonValueType m_type
 
union {
char * m_str
 
JsonNum m_num
 
JsonObject * m_obj
 
JsonArray * m_arr
 
bool m_bool
 
}; 
 

Detailed Description

Todo:
Make as_* functions safer and more versatile

An arbitrary Json value (see tests/utils/json_tests.cpp for example usage)

Definition at line 93 of file Json.h.


Class Documentation

◆ utils::Json::JsonToken.__unnamed__

union utils::Json::JsonToken.__unnamed__

Definition at line 258 of file Json.h.

Class Members
JsonNum num
char * str

Member Function Documentation

◆ dump()

std::string utils::Json::dump ( std::size_t  indent = 0,
std::size_t  offset = 0 
) const

Returns a string representation of the value

Note: never set second parameter

Todo:
Clean up code

Definition at line 189 of file Json.cpp.

◆ from_file()

Json utils::Json::from_file ( const std::string &  path)
static
Todo:
Move functionality to common.h

Definition at line 357 of file Json.cpp.

◆ merge()

Json & utils::Json::merge ( const Json other)

Adds everything stored in other into this

Returns this

Definition at line 153 of file Json.cpp.

◆ operator[]() [1/2]

Json & utils::Json::operator[] ( const std::string &  key)

When m_type == NONE, first converts this into an Object.

Todo:
Signal error if m_type != OBJECT

Definition at line 85 of file Json.cpp.

◆ operator[]() [2/2]

Json & utils::Json::operator[] ( const std::string &  key) const
Todo:
Signal error if m_type != OBJECT

Definition at line 93 of file Json.cpp.

◆ push_back()

bool utils::Json::push_back ( const Json val)

Returns false if m_type != ARRAY (or NONE)

If m_type == NONE, converts this into an ARRAY

Definition at line 131 of file Json.cpp.

Referenced by parse_tokens().

◆ resize()

bool utils::Json::resize ( std::size_t  size)

Returns false if m_type is not NONE or ARRAY

If m_type == NONE, converts this into an ARRAY

Definition at line 121 of file Json.cpp.

◆ tokenize()

queue< Json::JsonToken > utils::Json::tokenize ( std::string  curr)
staticprivate

Converts curr into a stream of tokens.

Todo:
Error cleanly

Definition at line 281 of file Json.cpp.


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