28#define JSON_FMT_INDENT 0x01
29#define JSON_FMT_QUOTESLASH 0x02
32#define ERR_JSON_INVTYPE -1
33#define ERR_JSON_TOOMANY -2
34#define ERR_JSON_ITERTYPE -3
35#define ERR_JSON_ITERPOS -4
36#define ERR_JSON_INPUT -5
37#define ERR_JSON_SIZE -7
38#define ERR_JSON_MISSING -8
61 using pnode = std::unique_ptr<node>;
62 using nodes_map = std::map<const std::string, pnode>;
63 using nodes_array = std::vector<pnode>;
67 node (
const std::string& s);
74 node (
const std::vector<T>& vec);
77 node (
const T& t,
decltype (&T::to_json)* =
nullptr);
89 using difference_type = std::ptrdiff_t;
91 using reference =
typename std::conditional_t<C_, node const&, node&>;
92 using pointer =
typename std::conditional_t<C_, const node*, node*>;
94 typename std::conditional_t<C_, nodes_map::const_iterator, nodes_map::iterator>;
96 typename std::conditional_t<C_, nodes_array::const_iterator, nodes_array::iterator>;
97 using iterator_category = std::bidirectional_iterator_tag;
101 : target (other.target)
103 if (target.t == type::object)
104 new (&objit) obj_iter (other.objit);
105 else if (target.t == type::array)
106 new (&arrit) arr_iter (other.arrit);
108 at_end = other.at_end;
114 if (target.t == type::object)
115 (&objit)->~obj_iter ();
116 else if (target.t == type::array)
117 (&arrit)->~arr_iter ();
123 if (target.t == type::object)
125 if (objit == target.obj.end ())
127 return *objit->second;
129 else if (target.t == type::array)
131 if (arrit == target.arr.end ())
144 if (target.t == type::object)
146 if (objit == target.obj.end ())
148 return objit->second.get ();
150 else if (target.t == type::array)
152 if (arrit == target.arr.end ())
154 return arrit->get ();
162 const std::string&
name ()
const
164 if (target.t != type::object)
166 if (objit == target.obj.end ())
175 if (target.t == type::object)
177 else if (target.t == type::array)
189 if (target.t == type::object)
191 else if (target.t == type::array)
205 if (target.t == type::object)
207 else if (target.t == type::array)
219 if (target.t == type::object)
221 else if (target.t == type::array)
233 if (&target != &other.target)
235 else if (target.t == type::object)
236 return (objit == other.objit);
237 else if (target.t == type::array)
238 return (arrit == other.arrit);
240 return (at_end == other.at_end);
255 iterator_type (
const node& n,
const arr_iter& it)
260 iterator_type (
const node& n,
const obj_iter& it)
275 typedef iterator_type<false> iterator;
276 typedef iterator_type<true> const_iterator;
286 template <
class T,
typename B = decltype (&T::to_json)>
297 clear (type::boolean);
303 template <
typename T,
typename B = std::enable_if_t<std::is_arithmetic_v<T>>>
306 clear (type::numeric);
314 clear (type::string);
322 clear (type::string);
328 explicit operator std::string ()
const;
329 explicit operator const char* ()
const;
330 explicit operator double ()
const;
331 explicit operator float ()
const;
332 explicit operator int ()
const;
333 explicit operator bool ()
const;
335 double to_num ()
const;
336 std::string
to_str ()
const;
342 node&
at (
const std::string& name);
343 const node&
at (
const std::string& name)
const;
348 const node&
at (
size_t index)
const;
350 const_iterator
begin ()
const;
352 const_iterator
end ()
const;
362 mlib::erc write (std::ostream& os,
int flags = 0,
int spaces = 0,
int level = 0)
const;
363 mlib::erc write (std::string& s,
int flags = 0,
int spaces = 0)
const;
366 bool has (
const std::string& name)
const;
367 void erase (
const std::string& name);
418 new (&obj) nodes_array ();
419 for (
size_t i = 0;
i <
vec.size (); ++
i)
420 arr.emplace_back (std::make_unique<node> (
vec[
i]));
428 new (&obj) nodes_map ();
435 if (t == type::object)
437 else if (t == type::array)
446 if (t == type::object)
447 return iterator (*
this, obj.begin ());
448 else if (t == type::array)
449 return iterator (*
this, arr.begin ());
457 if (t == type::object)
459 else if (t == type::array)
468 if (t == type::object)
469 return iterator (*
this, obj.end ());
470 else if (t == type::array)
471 return iterator (*
this, arr.end ());
488 if (t != type::string)
502 return (
float)to_num ();
507 return (
int)to_num ();
523 (&obj)->~nodes_map ();
526 (&arr)->~nodes_array ();
538 new (&obj) nodes_map ();
541 new (&arr) nodes_array ();
544 new (&str) std::string ();
564 return (t == type::object) ? (
int)obj.size ()
565 : (t == type::array) ? (
int)arr.size ()
566 : (t != type::null) ? 1
583 return const_cast<node&
> (
static_cast<const node&
> (*this).
at (name));
593 return const_cast<node&
> (
static_cast<const node&
> (*this).
at (index));
598 omanip (
void (*fn)(std::ios_base&,
int),
int val) : pfun(fn), arg(val) {}
599 void (* pfun)(std::ios_base&, int);
602 friend std::ostream& operator << (std::ostream& strm,
const omanip& manip) {
603 (*manip.pfun) (strm, manip.arg);
609void indenter (std::ios_base& os,
int spaces);
611inline omanip spaces (
int nspc)
613 return omanip (&indenter, nspc);
616inline std::ostream& indent (std::ostream& os)
621inline std::ostream& tabs (std::ostream& os)
626std::ostream& noindent (std::ostream& os);
628std::ostream& operator<< (std::ostream& os,
const node& n);
629std::istream& operator>> (std::istream& is, node& n);
635 n.
clear (type::array);
636 for (
int i = 0; i < vec.size (); ++i)
node iterator
Definition json.h:86
pointer operator->()
Value pointer.
Definition json.h:142
iterator_type< C_ > & operator++()
Increment operator (prefix)
Definition json.h:187
const std::string & name() const
Object name.
Definition json.h:162
iterator_type< C_ > operator--()
Decrement operator (prefix)
Definition json.h:217
bool operator==(const iterator_type< C_ > &other) const
Equality operator.
Definition json.h:231
reference operator*()
Dereference value.
Definition json.h:121
bool operator!=(const iterator_type< C_ > &other) const
Inequality operator.
Definition json.h:244
Representation of a JSON node.
Definition json.h:59
node & operator[](const std::string &name)
Return value of an object node element.
Definition json.cpp:158
bool has(const std::string &name) const
Return true if node is an object and has a child with that name.
Definition json.cpp:317
node & operator=(const node &rhs)
Principal assignment operator.
Definition json.cpp:111
void clear(type t=type::null)
Remove previous node content.
Definition json.h:517
node & at(const std::string &name)
Return value of an object node element.
Definition json.h:581
const_iterator end() const
End iterator (const variant)
Definition json.h:455
bool to_bool() const
Conversion to boolean value.
Definition json.cpp:854
mlib::erc read(std::istream &s)
Deserialize the node from an input stream.
Definition json.cpp:524
bool operator==(const node &other) const
Equality operator.
Definition json.cpp:285
int size() const
Return number of direct descendants.
Definition json.h:562
type kind() const
Return the type of node.
Definition json.h:556
bool operator!=(const node &other) const
inequality operator
Definition json.h:571
void erase(const std::string &name)
Erase the child with that name, if it exists.
Definition json.cpp:325
std::string to_str() const
String conversion function.
Definition json.cpp:839
mlib::erc write(std::ostream &os, int flags=0, int spaces=0, int level=0) const
Write node to a stream.
Definition json.cpp:721
node(type t=type::null)
Constructor for an empty node.
Definition json.cpp:36
const_iterator begin() const
Begin iterator (const variant)
Definition json.h:433
objects returned as a function result or thrown directly.
Definition errorcode.h:68
void raise() const
Definition errorcode.h:598
An error facility routes a group of errors handled in a similar manner.
Definition errorcode.h:136
Definition of erc and erfac classes.
mlib::errfac & Errors()
Return error facility used for JSON errors.
Definition json.cpp:23
#define ERR_JSON_INVTYPE
invalid node type
Definition json.h:32
#define ERR_JSON_ITERTYPE
invalid iterator type
Definition json.h:34
#define ERR_JSON_ITERPOS
invalid iterator position
Definition json.h:35
constexpr int max_array_size
Maximum number of array elements in a JSON node.
Definition json.h:19
void to_json(node &n, const std::vector< T > &vec)
Assign array value to a node.
Definition json.h:633
type
JSON node types.
Definition json.h:48
constexpr int max_string_length
Maximum string length.
Definition json.h:25
constexpr int max_object_names
Maximum number of properties for a node.
Definition json.h:22