30#define JSON_FMT_INDENT     0x01  
   31#define JSON_FMT_QUOTESLASH 0x02  
   32#define JSON_FMT_UTF8       0x04  
   35#define ERR_JSON_INVTYPE  -1  
   36#define ERR_JSON_TOOMANY  -2  
   37#define ERR_JSON_ITERTYPE -3  
   38#define ERR_JSON_ITERPOS  -4  
   39#define ERR_JSON_INPUT    -5  
   40#define ERR_JSON_SIZE     -7  
   41#define ERR_JSON_MISSING  -8  
   64  using pnode = std::unique_ptr<node>;
 
   65  using nodes_map = std::map<const std::string, pnode>;
 
   66  using nodes_array = std::vector<pnode>;
 
   70  node (
const std::string& s);
 
   77  node (
const std::vector<T>& vec);
 
   80  node (
const T& t, 
decltype (&T::to_json)* = 
nullptr);
 
   93    using difference_type = std::ptrdiff_t;
 
   94    using value_type = node;
 
   95    using reference = 
typename std::conditional_t<C_, node const&, node&>;
 
   96    using pointer = 
typename std::conditional_t<C_, const node*, node*>;
 
   98      typename std::conditional_t<C_, nodes_map::const_iterator, nodes_map::iterator>;
 
  100      typename std::conditional_t<C_, nodes_array::const_iterator, nodes_array::iterator>;
 
  101    using iterator_category = std::bidirectional_iterator_tag;
 
  105      : target (other.target)
 
  107      if (target.t == type::object)
 
  108        new (&objit) obj_iter (other.objit);
 
  109      else if (target.t == type::array)
 
  110        new (&arrit) arr_iter (other.arrit);
 
  112        at_end = other.at_end;
 
 
  118      if (target.t == type::object)
 
  119        (&objit)->~obj_iter ();
 
  120      else if (target.t == type::array)
 
  121        (&arrit)->~arr_iter ();
 
 
  127      if (target.t == type::object)
 
  129        if (objit == target.obj.end ())
 
  131        return *objit->second;
 
  133      else if (target.t == type::array)
 
  135        if (arrit == target.arr.end ())
 
 
  148      if (target.t == type::object)
 
  150        if (objit == target.obj.end ())
 
  152        return objit->second.get ();
 
  154      else if (target.t == type::array)
 
  156        if (arrit == target.arr.end ())
 
  158        return arrit->get ();
 
 
  166    const std::string& 
name ()
 const 
  168      if (target.t != type::object)
 
  170      if (objit == target.obj.end ())
 
 
  179      if (target.t == type::object)
 
  181      else if (target.t == type::array)
 
 
  193      if (target.t == type::object)
 
  195      else if (target.t == type::array)
 
 
  209      if (target.t == type::object)
 
  211      else if (target.t == type::array)
 
 
  223      if (target.t == type::object)
 
  225      else if (target.t == type::array)
 
 
  237      if (&target != &other.target)
 
  239      else if (target.t == type::object)
 
  240        return (objit == other.objit);
 
  241      else if (target.t == type::array)
 
  242        return (arrit == other.arrit);
 
  244        return (at_end == other.at_end);
 
 
 
  290  template <
class T, 
typename B = decltype (&T::to_json)>
 
  301    clear (type::boolean);
 
 
  307  template <
typename T, 
typename B = std::enable_if_t<std::is_arithmetic_v<T>>>
 
  310    clear (type::numeric);
 
 
  318    clear (type::string);
 
 
  326    clear (type::string);
 
 
  332  explicit operator std::string () 
const;
 
  333  explicit operator const char* () 
const;
 
  334  explicit operator double () 
const;
 
  335  explicit operator float () 
const;
 
  336  explicit operator int () 
const;
 
  337  explicit operator bool () 
const;
 
  339  double to_num () 
const;
 
  340  std::string 
to_str () 
const;
 
  346  node& 
at (
const std::string& name);
 
  347  const node& 
at (
const std::string& name) 
const;
 
  352  const node& 
at (
size_t index) 
const;
 
  355  const_iterator 
begin () 
const;
 
  357  const_iterator 
end () 
const;
 
  367  mlib::erc write (std::string& s, 
int flags=0, 
int spaces=0) 
const;
 
  370  bool has (
const std::string& name) 
const;
 
  371  const node* 
find (
const std::string& name) 
const;
 
  372  node* 
find (
const std::string& name);
 
  374  void erase (
const std::string& name);
 
  378  void format (
const std::string& f);
 
  379  const std::string_view 
format () 
const;
 
  382  mlib::erc write (std::ostream& os, 
int flags, 
int spaces, 
int level,
 
  383                   const std::string_view 
format) 
const;
 
  392  std::unique_ptr <std::string> fmt;
 
  394  friend std::ostream& 
operator<< (std::ostream& os, 
const node& n);
 
 
  432  new (&obj) nodes_array ();
 
  433  for (
size_t i = 0; i < vec.size (); ++i)
 
  434    arr.emplace_back (std::make_unique<node> (vec[i]));
 
 
  442  new (&obj) nodes_map ();
 
 
  449  if (t == type::object)
 
  450    return const_iterator (*
this, obj.begin ());
 
  451  else if (t == type::array)
 
  452    return const_iterator (*
this, arr.begin ());
 
  454    return const_iterator (*
this, 
false);
 
 
  460  if (t == type::object)
 
  461    return iterator (*
this, obj.begin ());
 
  462  else if (t == type::array)
 
  463    return iterator (*
this, arr.begin ());
 
  465    return iterator (*
this, 
false);
 
 
  471  if (t == type::object)
 
  472    return const_iterator (*
this, obj.end ());
 
  473  else if (t == type::array)
 
  474    return const_iterator (*
this, arr.end ());
 
  476    return const_iterator (*
this, 
true);
 
 
  482  if (t == type::object)
 
  483    return iterator (*
this, obj.end ());
 
  484  else if (t == type::array)
 
  485    return iterator (*
this, arr.end ());
 
  487    return iterator (*
this, 
true);
 
 
  491inline node::operator std::string ()
 const 
 
  500inline node::operator 
const char* () 
const 
  502  if (t != type::string)
 
  508inline node::operator double ()
 const 
 
  514inline node::operator float ()
 const 
  516  return (
float)to_num ();
 
 
  519inline node::operator int ()
 const 
  521  return (
int)to_num ();
 
  525inline node::operator bool ()
 const 
 
  537    (&obj)->~nodes_map ();
 
  540    (&arr)->~nodes_array ();
 
  543    (&str)->~basic_string ();
 
  552    new (&obj) nodes_map ();
 
  555    new (&arr) nodes_array ();
 
  558    new (&str) std::string ();
 
 
  578  return (t == type::object)  ? (int)obj.size ()
 
  579         : (t == type::array) ? (
int)arr.size ()
 
  580         : (t != type::null)  ? 1
 
 
  587  fmt.reset (f.empty () ? 
nullptr : 
new std::string(f));
 
 
  593  return fmt? *fmt : std::string_view();
 
 
  609  return const_cast<node&
> (
static_cast<const node&
> (*this).at (name));
 
 
  619  return const_cast<node&
> (
static_cast<const node&
> (*this).at (index));
 
 
  625  omanip (
void (*fn)(std::ios_base&, 
int), 
int val) : pfun(fn), arg(val) {}
 
  626  void (* pfun)(std::ios_base&, int);
 
  629  friend std::ostream& operator << (std::ostream& strm, 
const omanip& manip) {
 
  630    (*manip.pfun) (strm, manip.arg);
 
  636void indenter (std::ios_base& os, 
int spaces);
 
  638inline omanip spaces (
int nspc)
 
  640  return omanip (&indenter, nspc);
 
  643inline std::ostream& indent (std::ostream& os)
 
  648inline std::ostream& tabs (std::ostream& os)
 
  654std::ostream& noindent (std::ostream& os);
 
  656std::ostream& utf8 (std::ostream& os);
 
  658std::ostream& noutf8 (std::ostream& os);
 
  663std::ostream& operator<< (std::ostream& os, 
const node& n);
 
  666std::istream& operator>> (std::istream& is, 
node& n);
 
  672  n.
clear (type::array);
 
  673  for (
int i = 0; i < vec.size (); ++i)
 
 
objects returned as a function result or thrown directly.
Definition errorcode.h:73
 
void raise() const
Definition errorcode.h:603
 
An error facility routes a group of errors handled in a similar manner.
Definition errorcode.h:141
 
node iterator
Definition json.h:89
 
pointer operator->() const
Value pointer.
Definition json.h:146
 
iterator_type< C_ > operator--()
Decrement operator (prefix)
Definition json.h:221
 
~iterator_type()
Destructor.
Definition json.h:116
 
reference operator*() const
Dereference value.
Definition json.h:125
 
iterator_type(const iterator_type &other)
Copy constructor.
Definition json.h:104
 
bool operator!=(const iterator_type< C_ > &other) const
Inequality operator.
Definition json.h:248
 
iterator_type< C_ > & operator++()
Increment operator (prefix)
Definition json.h:191
 
bool operator==(const iterator_type< C_ > &other) const
Equality operator.
Definition json.h:235
 
const std::string & name() const
Object name.
Definition json.h:166
 
Representation of a JSON node.
Definition json.h:62
 
bool operator!=(const node &other) const
inequality operator
Definition json.h:597
 
void erase(const std::string &name)
Erase the child with that name, if it exists.
Definition json.cpp:339
 
void clear(type t=type::null)
Remove previous node content.
Definition json.h:531
 
int size() const
Return number of direct descendants.
Definition json.h:576
 
bool operator==(const node &other) const
Equality operator.
Definition json.cpp:299
 
type kind() const
Return the type of node.
Definition json.h:570
 
bool has(const std::string &name) const
Return true if node is an object and has a child with that name.
Definition json.cpp:331
 
void push_back(const node &n)
Append an element at the end of an array.
Definition json.cpp:288
 
mlib::erc read(std::istream &s)
Deserialize the node from an input stream.
Definition json.cpp:590
 
bool to_bool() const
Conversion to boolean value.
Definition json.cpp:959
 
const node * find(const std::string &name) const
If node is an object and has a child with the given name, returns a pointer to child node.
Definition json.cpp:353
 
mlib::erc write(std::string &s, int flags=0, int spaces=0) const
Write node to a string.
Definition json.cpp:887
 
const_iterator end() const
End iterator (const variant)
Definition json.h:469
 
node & at(const std::string &name)
Return value of an object node element.
Definition json.h:607
 
const_iterator begin() const
Begin iterator (const variant)
Definition json.h:447
 
std::string to_str() const
String conversion function.
Definition json.cpp:923
 
node & operator=(const node &rhs)
Principal assignment operator.
Definition json.cpp:110
 
operator std::string() const
Return node value as a string (if possible)
Definition json.h:491
 
const std::string_view format() const
Return output format for numerical values.
Definition json.h:591
 
node & operator[](const std::string &name)
Return value of an object node element.
Definition json.cpp:159
 
friend std::ostream & operator<<(std::ostream &os, const node &n)
Insertion operator serializes a JSON node to an output stream.
Definition json.cpp:1003
 
node(type t=type::null)
Constructor for an empty node.
Definition json.cpp:34
 
Definition of mlib::erc and mlib::errfac classes.
 
mlib::errfac & Errors()
Return error facility used for JSON errors.
Definition json.cpp:21
 
constexpr int max_string_length
Maximum string length.
Definition json.h:27
 
constexpr int max_object_names
Maximum number of properties for a node.
Definition json.h:24
 
#define ERR_JSON_INVTYPE
invalid node type
Definition json.h:35
 
#define ERR_JSON_ITERTYPE
invalid iterator type
Definition json.h:37
 
#define ERR_JSON_ITERPOS
invalid iterator position
Definition json.h:38
 
void to_json(node &n, const std::vector< T > &vec)
Assign array value to a node.
Definition json.h:670
 
constexpr int max_array_size
Maximum number of array elements in a JSON node.
Definition json.h:21
 
type
JSON node types.
Definition json.h:51