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

#include <json.h>

Collaboration diagram for librats::Json:
[legend]

Classes

class  Object
 Insertion-ordered string->Json map with O(1) average lookup. More...
class  Iterator
class  ItemsProxy
 A key/value view supporting structured bindings: for (auto&& [key, val] : obj.items()) { ... } For arrays, key() is the decimal index. 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
Json & operator= (const Json &other)
Json & operator= (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.
Json & operator[] (const std::string &key)
Json & operator[] (const char *key)
const Json & operator[] (const std::string &key) const
const Json & operator[] (const char *key) const
Json & operator[] (int index)
Json & operator[] (std::size_t index)
const Json & operator[] (int index) const
const Json & operator[] (std::size_t index) const
Json & at (const std::string &key)
 Bounds/existence-checked access; throws JsonError when missing.
const Json & at (const std::string &key) const
Json & at (std::size_t index)
const Json & at (std::size_t index) const
bool contains (const std::string &key) const
bool erase (const std::string &key)
void erase (std::size_t index)
Json & front ()
const Json & front () const
Json & back ()
const Json & back () const
void push_back (const Json &value)
void push_back (Json &&value)
template<typename... Args>
Json & emplace_back (Args &&... args)
void clear ()
template<typename T>
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>
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
Array & as_array ()
const Array & as_array () const
Object & as_object ()
const Object & as_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]

◆ Json() [2/11]

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

Definition at line 134 of file json.h.

References Null.

◆ Json() [3/11]

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

Definition at line 135 of file json.h.

References bool_, and Boolean.

◆ 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 ( T v)
inlinenoexcept

Definition at line 141 of file json.h.

References int_, Integer, uint_, and Unsigned.

◆ Json() [5/11]

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

Definition at line 153 of file json.h.

References Float, and float_.

◆ Json() [6/11]

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

Definition at line 155 of file json.h.

References str_.

◆ Json() [7/11]

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

Definition at line 156 of file json.h.

References str_.

◆ Json() [8/11]

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

Definition at line 157 of file json.h.

References str_.

◆ 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.

References Json().

◆ Json() [11/11]

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

Definition at line 165 of file json.h.

References Json().

◆ ~Json()

librats::Json::~Json ( )
inline

Definition at line 170 of file json.h.

Member Function Documentation

◆ array()

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 Json(), arr_, and Array.

◆ 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

References Null.

◆ at() [1/4]

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

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

References Json().

◆ at() [2/4]

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

References Json().

◆ at() [3/4]

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

References Json().

◆ at() [4/4]

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

References Json().

◆ back() [1/2]

Json & librats::Json::back ( )

References Json().

Referenced by emplace_back().

◆ back() [2/2]

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

References Json().

◆ 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.

References size().

◆ clear()

void librats::Json::clear ( )

◆ contains()

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

Definition at line 250 of file json.h.

References is_object(), and obj_.

◆ 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.

References Json(), back(), and push_back().

◆ empty()

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

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

References empty().

Referenced by empty().

◆ end() [1/2]

iterator librats::Json::end ( )
inline

Definition at line 346 of file json.h.

References size().

◆ end() [2/2]

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

Definition at line 348 of file json.h.

References size().

◆ 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 ( )

References Json().

◆ front() [2/2]

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

References Json().

◆ get()

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

Definition at line 274 of file json.h.

Referenced by operator T().

◆ is_array()

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

Definition at line 217 of file json.h.

References Array.

Referenced by is_structured().

◆ is_boolean()

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

Definition at line 207 of file json.h.

References Boolean.

Referenced by is_primitive().

◆ is_discarded()

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

Definition at line 219 of file json.h.

References Discarded.

◆ is_null()

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

Definition at line 206 of file json.h.

References Null.

Referenced by is_primitive().

◆ is_number()

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

Definition at line 208 of file json.h.

References Float, Integer, and Unsigned.

Referenced by is_primitive().

◆ is_number_float()

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

Definition at line 215 of file json.h.

References Float.

◆ is_number_integer()

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

Definition at line 211 of file json.h.

References Integer, and Unsigned.

◆ is_number_unsigned()

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

Definition at line 214 of file json.h.

References Unsigned.

◆ is_object()

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

Definition at line 218 of file json.h.

References Object.

Referenced by contains(), is_structured(), value(), and value().

◆ is_primitive()

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

Definition at line 220 of file json.h.

References is_boolean(), is_null(), is_number(), and is_string().

◆ is_string()

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

Definition at line 216 of file json.h.

References String.

Referenced by is_primitive().

◆ is_structured()

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

Definition at line 223 of file json.h.

References is_array(), and is_object().

◆ 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()

Json librats::Json::object ( )
inlinestatic

Definition at line 174 of file json.h.

References Json(), obj_, and Object.

◆ 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.

References get().

◆ operator!=()

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

Definition at line 372 of file json.h.

References Json().

◆ operator=() [1/2]

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

References Json().

◆ operator=() [2/2]

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

References Json().

◆ operator==()

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

References Json().

◆ operator[]() [1/8]

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

Definition at line 233 of file json.h.

References Json(), and 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 Json(), and operator[]().

Referenced by operator[]().

◆ operator[]() [3/8]

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

References Json().

Referenced by operator[]().

◆ operator[]() [4/8]

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

References Json().

◆ operator[]() [5/8]

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

Definition at line 237 of file json.h.

References Json(), and operator[]().

Referenced by operator[]().

◆ operator[]() [6/8]

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

Definition at line 239 of file json.h.

References Json(), and operator[]().

◆ operator[]() [7/8]

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

References Json().

◆ operator[]() [8/8]

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

References Json().

◆ parse() [1/3]

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

References Json().

◆ parse() [2/3]

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

References Json().

Referenced by parse().

◆ parse() [3/3]

template<typename InputIt>
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.

References Json(), and parse().

◆ push_back() [1/2]

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

References Json(), and value().

Referenced by emplace_back().

◆ push_back() [2/2]

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

References Json(), and value().

◆ size()

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

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

Referenced by cend(), end(), and end().

◆ 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.

References Json(), is_object(), and obj_.

◆ 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.

References Json(), is_object(), and obj_.

Referenced by push_back(), and push_back().

◆ JsonParser

friend class JsonParser
friend

Definition at line 412 of file json.h.

References JsonParser.

Referenced by JsonParser.

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.

Referenced by Json().

◆ float_

double librats::Json::float_

Definition at line 388 of file json.h.

Referenced by Json().

◆ int_

int64_t librats::Json::int_

Definition at line 386 of file json.h.

Referenced by Json().

◆ obj_

Object* librats::Json::obj_

Definition at line 391 of file json.h.

Referenced by contains(), object(), value(), and value().

◆ str_

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

Definition at line 389 of file json.h.

Referenced by Json(), Json(), and Json().

◆ uint_

uint64_t librats::Json::uint_

Definition at line 387 of file json.h.

Referenced by Json().


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