MLIB
Loading...
Searching...
No Matches
json::node Class Reference

Representation of a JSON node. More...

#include <json.h>

Data Structures

class  iterator_type
 node iterator More...
 

Public Types

using pnode = std::unique_ptr< node >
 
using nodes_map = std::map< const std::string, pnode >
 
using nodes_array = std::vector< pnode >
 
typedef iterator_type< falseiterator
 
typedef iterator_type< trueconst_iterator
 

Public Member Functions

 node (type t=type::null)
 Constructor for an empty node.
 
 node (const std::string &s)
 Constructor for a string type node.
 
 node (const char *s)
 Alternate constructor for a string node.
 
 node (double d)
 Constructor for a numeric node.
 
 node (int d)
 Alternate constructor for a numeric node.
 
 node (bool b)
 Constructor for a boolean node.
 
template<typename T >
 node (const std::vector< T > &vec)
 Constructor from a vector.
 
template<class T >
 node (const T &t, decltype(&T::to_json) *=nullptr)
 Constructor from an object.
 
 node (const node &other)
 Copy constructor.
 
 node (node &&other)
 Move constructor.
 
nodeoperator= (const node &rhs)
 Principal assignment operator.
 
nodeoperator= (node &&rhs)
 Move assignment operator.
 
template<class T , typename B = decltype (&T::to_json)>
nodeoperator= (const T &t)
 Assignment operator.
 
nodeoperator= (bool b)
 Boolean assignment.
 
template<typename T , typename B = std::enable_if_t<std::is_arithmetic_v<T>>>
nodeoperator= (T n)
 Numeric assignment.
 
nodeoperator= (const std::string &s)
 String assignment.
 
nodeoperator= (const char *s)
 C string assignment.
 
 operator std::string () const
 Return node value as a string (if possible)
 
 operator const char * () const
 
 operator double () const
 Return numeric value of a node.
 
 operator float () const
 Return numeric value of a node.
 
 operator int () const
 
 operator bool () const
 Return boolean value of the node.
 
double to_num () const
 
std::string to_str () const
 String conversion function.
 
bool to_bool () const
 Conversion to boolean value.
 
nodeoperator[] (const std::string &name)
 Return value of an object node element.
 
const nodeoperator[] (const std::string &name) const
 Return value of an object node element (const version).
 
nodeat (const std::string &name)
 Return value of an object node element.
 
const nodeat (const std::string &name) const
 Return value of an object node element.
 
nodeoperator[] (size_t index)
 Return value of an array node element.
 
const nodeoperator[] (size_t index) const
 Return value of an array node element (const version).
 
nodeat (size_t index)
 Return value of an object node element.
 
const nodeat (size_t index) const
 Return value of an array node element (const version).
 
const_iterator begin () const
 Begin iterator (const variant)
 
iterator begin ()
 Begin iterator (non-const variant)
 
const_iterator end () const
 End iterator (const variant)
 
iterator end ()
 End iterator (non-const variant)
 
bool operator== (const node &other) const
 Equality operator.
 
bool operator!= (const node &other) const
 inequality operator
 
mlib::erc read (std::istream &s)
 Deserialize the node from an input stream.
 
mlib::erc read (const std::string &s)
 Parse a string to a JSON node.
 
mlib::erc write (std::ostream &os, int flags=0, int spaces=0, int level=0) const
 Write node to a stream.
 
mlib::erc write (std::string &s, int flags=0, int spaces=0) const
 Write node to a string.
 
bool has (const std::string &name) const
 Return true if node is an object and has a child with that name.
 
void erase (const std::string &name)
 Erase the child with that name, if it exists.
 
void clear (type t=type::null)
 Remove previous node content.
 
type kind () const
 Return the type of node.
 
int size () const
 Return number of direct descendants.
 

Detailed Description

Representation of a JSON node.

Member Function Documentation

◆ at() [1/4]

node & json::node::at ( const std::string &  name)
inline

Return value of an object node element.

If element doesn't exist it throws an ERR_JSON_MISSING exception.

◆ at() [2/4]

const node & json::node::at ( const std::string &  name) const

Return value of an object node element.

If element doesn't exist throws an ERR_JSON_MISSING exception.

◆ at() [3/4]

node & json::node::at ( size_t  index)
inline

Return value of an object node element.

If element doesn't exist it throws an ERR_JSON_MISSING exception.

◆ at() [4/4]

const node & json::node::at ( size_t  index) const

Return value of an array node element (const version).

If element doesn't exist throws an ERR_JSON_MISSING exception.

◆ operator[]() [1/4]

node & json::node::operator[] ( const std::string &  name)

Return value of an object node element.

If name doesn't exist, it is appended to node.

◆ operator[]() [2/4]

const node & json::node::operator[] ( const std::string &  name) const

Return value of an object node element (const version).

If element doesn't exist it throws an ERR_JSON_MISSING exception.

◆ operator[]() [3/4]

Return value of an array node element.

If element doesn't exist the array is extended with null elements.

◆ operator[]() [4/4]

const node & json::node::operator[] ( size_t  index) const

Return value of an array node element (const version).

If element doesn't exist throws an ERR_JSON_MISSING exception.

◆ read()

erc json::node::read ( const std::string &  s)

Parse a string to a JSON node.

Returns an error if there are any extra characters after the JSON text.

◆ write()

erc json::node::write ( std::ostream &  os,
int  flags = 0,
int  spaces = 0,
int  level = 0 
) const

Write node to a stream.

Parameters
osstream to write to
flagsformatting flags
spacesnumber of spaces to indent. If 0 uses tabs instead of spaces
levelstarting indentation level

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