Back to Site
Loading...
Searching...
No Matches
librats::Json Class Reference

#include <json.h>

Collaboration diagram for librats::Json:
[legend]

Classes

class  ItemsProxy
 A key/value view supporting structured bindings: for (auto&& [key, val] : obj.items()) { ... } For arrays, key() is the decimal index. More...
 
class  Iterator
 
class  Object
 Insertion-ordered string->Json map with O(1) average lookup. More...
 

Public Types

enum class  Type : uint8_t {
  Null , Boolean , Integer , Unsigned ,
  Float , String , Array , Object ,
  Discarded
}
 
using Array = std::vector< Json >
 
using iterator = Iterator< false >
 
using const_iterator = Iterator< true >
 

Public Member Functions

 Json () noexcept
 
 Json (std::nullptr_t) noexcept
 
 Json (bool b) noexcept
 
template<typename T , typename std::enable_if< std::is_integral< T >::value &&!std::is_same< T, bool >::value, int >::type = 0>
 Json (T v) noexcept
 
template<typename T , typename std::enable_if< std::is_floating_point< T >::value, int >::type = 0>
 Json (T v) noexcept
 
 Json (const char *s)
 
 Json (const std::string &s)
 
 Json (std::string &&s)
 
 Json (std::initializer_list< Json > init)
 nlohmann-style brace initialisation.
 
 Json (const Json &other)
 
 Json (Json &&other) noexcept
 
Jsonoperator= (const Json &other)
 
Jsonoperator= (Json &&other) noexcept
 
 ~Json ()
 
std::string dump (int indent=-1) const
 
Type type () const noexcept
 
bool is_null () const noexcept
 
bool is_boolean () const noexcept
 
bool is_number () const noexcept
 
bool is_number_integer () const noexcept
 
bool is_number_unsigned () const noexcept
 
bool is_number_float () const noexcept
 
bool is_string () const noexcept
 
bool is_array () const noexcept
 
bool is_object () const noexcept
 
bool is_discarded () const noexcept
 
bool is_primitive () const noexcept
 
bool is_structured () const noexcept
 
std::size_t size () const noexcept
 Number of elements (array/object), or 0 for null, 1 for any scalar.
 
bool empty () const noexcept
 True for null, an empty array, or an empty object.
 
Jsonoperator[] (const std::string &key)
 
Jsonoperator[] (const char *key)
 
const Jsonoperator[] (const std::string &key) const
 
const Jsonoperator[] (const char *key) const
 
Jsonoperator[] (int index)
 
Jsonoperator[] (std::size_t index)
 
const Jsonoperator[] (int index) const
 
const Jsonoperator[] (std::size_t index) const
 
Jsonat (const std::string &key)
 Bounds/existence-checked access; throws JsonError when missing.
 
const Jsonat (const std::string &key) const
 
Jsonat (std::size_t index)
 
const Jsonat (std::size_t index) const
 
bool contains (const std::string &key) const
 
bool erase (const std::string &key)
 
void erase (std::size_t index)
 
Jsonfront ()
 
const Jsonfront () const
 
Jsonback ()
 
const Jsonback () const
 
void push_back (const Json &value)
 
void push_back (Json &&value)
 
template<typename... Args>
Jsonemplace_back (Args &&... args)
 
void clear ()
 
template<typename T >
get () const
 
template<typename T , typename std::enable_if<(std::is_arithmetic< T >::value||std::is_same< T, std::string >::value) &&!std::is_same< T, Json >::value, int >::type = 0>
 operator T () const
 Implicit conversion to arithmetic types and std::string, so call sites like int v = j["x"]; and std::string s = j["y"]; read naturally.
 
template<typename T >
value (const std::string &key, const T &default_value) const
 value(key, default): typed object lookup with a fallback.
 
std::string value (const std::string &key, const char *default_value) const
 const char* default resolves to a std::string result (nlohmann parity).
 
iterator begin ()
 
iterator end ()
 
const_iterator begin () const
 
const_iterator end () const
 
const_iterator cbegin () const
 
const_iterator cend () const
 
ItemsProxy< false > items ()
 
ItemsProxy< true > items () const
 
bool operator== (const Json &other) const
 
bool operator!= (const Json &other) const
 
Arrayas_array ()
 
const Arrayas_array () const
 
Objectas_object ()
 
const Objectas_object () const
 

Static Public Member Functions

static Json array ()
 Explicit empties (also handy to force kind on an otherwise-null value).
 
static Json object ()
 
static Json parse (const std::string &text, std::nullptr_t=nullptr, bool allow_exceptions=true)
 
static Json parse (const char *text, std::nullptr_t=nullptr, bool allow_exceptions=true)
 
template<typename InputIt >
static Json parse (InputIt first, InputIt last, std::nullptr_t=nullptr, bool allow_exceptions=true)
 

Friends

class JsonParser
 

Detailed Description

Definition at line 56 of file json.h.

Member Typedef Documentation

◆ Array

using librats::Json::Array = std::vector<Json>

Definition at line 70 of file json.h.

◆ const_iterator

Definition at line 343 of file json.h.

◆ iterator

Definition at line 342 of file json.h.

Member Enumeration Documentation

◆ Type

enum class librats::Json::Type : uint8_t
strong
Enumerator
Null 
Boolean 
Integer 
Unsigned 
Float 
String 
Array 
Object 
Discarded 

Definition at line 58 of file json.h.

Constructor & Destructor Documentation

◆ Json() [1/11]

librats::Json::Json ( )
inlinenoexcept

Definition at line 133 of file json.h.

◆ Json() [2/11]

librats::Json::Json ( std::nullptr_t  )
inlinenoexcept

Definition at line 134 of file json.h.

◆ Json() [3/11]

librats::Json::Json ( bool  b)
inlinenoexcept

Definition at line 135 of file json.h.

◆ Json() [4/11]

template<typename T , typename std::enable_if< std::is_integral< T >::value &&!std::is_same< T, bool >::value, int >::type = 0>
librats::Json::Json ( v)
inlinenoexcept

Definition at line 141 of file json.h.

◆ Json() [5/11]

template<typename T , typename std::enable_if< std::is_floating_point< T >::value, int >::type = 0>
librats::Json::Json ( v)
inlinenoexcept

Definition at line 153 of file json.h.

◆ Json() [6/11]

librats::Json::Json ( const char *  s)
inline

Definition at line 155 of file json.h.

◆ Json() [7/11]

librats::Json::Json ( const std::string &  s)
inline

Definition at line 156 of file json.h.

◆ Json() [8/11]

librats::Json::Json ( std::string &&  s)
inline

Definition at line 157 of file json.h.

◆ Json() [9/11]

librats::Json::Json ( std::initializer_list< Json init)

nlohmann-style brace initialisation.

A list whose every element is a two-element array with a string first element is read as an object; otherwise it is an array.

◆ Json() [10/11]

librats::Json::Json ( const Json other)
inline

Definition at line 164 of file json.h.

◆ Json() [11/11]

librats::Json::Json ( Json &&  other)
inlinenoexcept

Definition at line 165 of file json.h.

◆ ~Json()

librats::Json::~Json ( )
inline

Definition at line 170 of file json.h.

Member Function Documentation

◆ array()

static Json librats::Json::array ( )
inlinestatic

Explicit empties (also handy to force kind on an otherwise-null value).

Definition at line 173 of file json.h.

References arr_.

◆ as_array() [1/2]

Array & librats::Json::as_array ( )

◆ as_array() [2/2]

const Array & librats::Json::as_array ( ) const

◆ as_object() [1/2]

Object & librats::Json::as_object ( )

◆ as_object() [2/2]

const Object & librats::Json::as_object ( ) const

◆ at() [1/4]

Json & librats::Json::at ( const std::string &  key)

Bounds/existence-checked access; throws JsonError when missing.

◆ at() [2/4]

const Json & librats::Json::at ( const std::string &  key) const

◆ at() [3/4]

Json & librats::Json::at ( std::size_t  index)

◆ at() [4/4]

const Json & librats::Json::at ( std::size_t  index) const

◆ back() [1/2]

Json & librats::Json::back ( )

◆ back() [2/2]

const Json & librats::Json::back ( ) const

◆ begin() [1/2]

iterator librats::Json::begin ( )
inline

Definition at line 345 of file json.h.

◆ begin() [2/2]

const_iterator librats::Json::begin ( ) const
inline

Definition at line 347 of file json.h.

◆ cbegin()

const_iterator librats::Json::cbegin ( ) const
inline

Definition at line 349 of file json.h.

◆ cend()

const_iterator librats::Json::cend ( ) const
inline

Definition at line 350 of file json.h.

◆ clear()

void librats::Json::clear ( )

◆ contains()

bool librats::Json::contains ( const std::string &  key) const
inline

Definition at line 250 of file json.h.

◆ dump()

std::string librats::Json::dump ( int  indent = -1) const

◆ emplace_back()

template<typename... Args>
Json & librats::Json::emplace_back ( Args &&...  args)
inline

Definition at line 264 of file json.h.

◆ empty()

bool librats::Json::empty ( ) const
noexcept

True for null, an empty array, or an empty object.

◆ end() [1/2]

iterator librats::Json::end ( )
inline

Definition at line 346 of file json.h.

◆ end() [2/2]

const_iterator librats::Json::end ( ) const
inline

Definition at line 348 of file json.h.

◆ erase() [1/2]

bool librats::Json::erase ( const std::string &  key)

◆ erase() [2/2]

void librats::Json::erase ( std::size_t  index)

◆ front() [1/2]

Json & librats::Json::front ( )

◆ front() [2/2]

const Json & librats::Json::front ( ) const

◆ get()

template<typename T >
T librats::Json::get ( ) const
inline

Definition at line 274 of file json.h.

◆ is_array()

bool librats::Json::is_array ( ) const
inlinenoexcept

Definition at line 217 of file json.h.

◆ is_boolean()

bool librats::Json::is_boolean ( ) const
inlinenoexcept

Definition at line 207 of file json.h.

◆ is_discarded()

bool librats::Json::is_discarded ( ) const
inlinenoexcept

Definition at line 219 of file json.h.

◆ is_null()

bool librats::Json::is_null ( ) const
inlinenoexcept

Definition at line 206 of file json.h.

◆ is_number()

bool librats::Json::is_number ( ) const
inlinenoexcept

Definition at line 208 of file json.h.

◆ is_number_float()

bool librats::Json::is_number_float ( ) const
inlinenoexcept

Definition at line 215 of file json.h.

◆ is_number_integer()

bool librats::Json::is_number_integer ( ) const
inlinenoexcept

Definition at line 211 of file json.h.

◆ is_number_unsigned()

bool librats::Json::is_number_unsigned ( ) const
inlinenoexcept

Definition at line 214 of file json.h.

◆ is_object()

bool librats::Json::is_object ( ) const
inlinenoexcept

Definition at line 218 of file json.h.

◆ is_primitive()

bool librats::Json::is_primitive ( ) const
inlinenoexcept

Definition at line 220 of file json.h.

◆ is_string()

bool librats::Json::is_string ( ) const
inlinenoexcept

Definition at line 216 of file json.h.

◆ is_structured()

bool librats::Json::is_structured ( ) const
inlinenoexcept

Definition at line 223 of file json.h.

◆ items() [1/2]

ItemsProxy< false > librats::Json::items ( )
inline

Definition at line 366 of file json.h.

◆ items() [2/2]

ItemsProxy< true > librats::Json::items ( ) const
inline

Definition at line 367 of file json.h.

◆ object()

static Json librats::Json::object ( )
inlinestatic

Definition at line 174 of file json.h.

References obj_.

◆ operator T()

template<typename T , typename std::enable_if<(std::is_arithmetic< T >::value||std::is_same< T, std::string >::value) &&!std::is_same< T, Json >::value, int >::type = 0>
librats::Json::operator T ( ) const
inline

Implicit conversion to arithmetic types and std::string, so call sites like int v = j["x"]; and std::string s = j["y"]; read naturally.

Definition at line 294 of file json.h.

◆ operator!=()

bool librats::Json::operator!= ( const Json other) const
inline

Definition at line 372 of file json.h.

◆ operator=() [1/2]

Json & librats::Json::operator= ( const Json other)

◆ operator=() [2/2]

Json & librats::Json::operator= ( Json &&  other)
noexcept

◆ operator==()

bool librats::Json::operator== ( const Json other) const

◆ operator[]() [1/8]

Json & librats::Json::operator[] ( const char *  key)
inline

Definition at line 233 of file json.h.

References operator[]().

Referenced by operator[]().

◆ operator[]() [2/8]

const Json & librats::Json::operator[] ( const char *  key) const
inline

Definition at line 235 of file json.h.

References operator[]().

Referenced by operator[]().

◆ operator[]() [3/8]

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

◆ operator[]() [4/8]

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

◆ operator[]() [5/8]

Json & librats::Json::operator[] ( int  index)
inline

Definition at line 237 of file json.h.

References operator[]().

Referenced by operator[]().

◆ operator[]() [6/8]

const Json & librats::Json::operator[] ( int  index) const
inline

Definition at line 239 of file json.h.

◆ operator[]() [7/8]

Json & librats::Json::operator[] ( std::size_t  index)

◆ operator[]() [8/8]

const Json & librats::Json::operator[] ( std::size_t  index) const

◆ parse() [1/3]

static Json librats::Json::parse ( const char *  text,
std::nullptr_t  = nullptr,
bool  allow_exceptions = true 
)
static

◆ parse() [2/3]

static Json librats::Json::parse ( const std::string &  text,
std::nullptr_t  = nullptr,
bool  allow_exceptions = true 
)
static

◆ parse() [3/3]

template<typename InputIt >
static Json librats::Json::parse ( InputIt  first,
InputIt  last,
std::nullptr_t  = nullptr,
bool  allow_exceptions = true 
)
inlinestatic

Definition at line 189 of file json.h.

◆ push_back() [1/2]

void librats::Json::push_back ( const Json value)

◆ push_back() [2/2]

void librats::Json::push_back ( Json &&  value)

◆ size()

std::size_t librats::Json::size ( ) const
noexcept

Number of elements (array/object), or 0 for null, 1 for any scalar.

◆ type()

Type librats::Json::type ( ) const
inlinenoexcept

Definition at line 205 of file json.h.

◆ value() [1/2]

std::string librats::Json::value ( const std::string &  key,
const char *  default_value 
) const
inline

const char* default resolves to a std::string result (nlohmann parity).

Definition at line 306 of file json.h.

◆ value() [2/2]

template<typename T >
T librats::Json::value ( const std::string &  key,
const T &  default_value 
) const
inline

value(key, default): typed object lookup with a fallback.

Returns the default when this is not an object or the key is absent.

Definition at line 299 of file json.h.

Friends And Related Symbol Documentation

◆ JsonParser

friend class JsonParser
friend

Definition at line 412 of file json.h.

Member Data Documentation

◆ arr_

Array* librats::Json::arr_

Definition at line 390 of file json.h.

Referenced by array().

◆ bool_

bool librats::Json::bool_

Definition at line 385 of file json.h.

◆ float_

double librats::Json::float_

Definition at line 388 of file json.h.

◆ int_

int64_t librats::Json::int_

Definition at line 386 of file json.h.

◆ obj_

Object* librats::Json::obj_

Definition at line 391 of file json.h.

Referenced by object().

◆ str_

std::string* librats::Json::str_

Definition at line 389 of file json.h.

◆ uint_

uint64_t librats::Json::uint_

Definition at line 387 of file json.h.


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