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);
376 void erase (
const std::string& name);
380 void format (
const std::string& f);
381 const std::string_view
format ()
const;
384 mlib::erc write (std::ostream& os,
int flags,
int spaces,
int level,
385 const std::string_view
format)
const;
394 std::unique_ptr <std::string> fmt;
396 friend std::ostream&
operator<< (std::ostream& os,
const node& n);
434 new (&obj) nodes_array ();
435 for (
size_t i = 0; i < vec.size (); ++i)
436 arr.emplace_back (std::make_unique<node> (vec[i]));
444 new (&obj) nodes_map ();
451 if (t == type::object)
452 return const_iterator (*
this, obj.begin ());
453 else if (t == type::array)
454 return const_iterator (*
this, arr.begin ());
456 return const_iterator (*
this,
false);
462 if (t == type::object)
463 return iterator (*
this, obj.begin ());
464 else if (t == type::array)
465 return iterator (*
this, arr.begin ());
467 return iterator (*
this,
false);
473 if (t == type::object)
474 return const_iterator (*
this, obj.end ());
475 else if (t == type::array)
476 return const_iterator (*
this, arr.end ());
478 return const_iterator (*
this,
true);
484 if (t == type::object)
485 return iterator (*
this, obj.end ());
486 else if (t == type::array)
487 return iterator (*
this, arr.end ());
489 return iterator (*
this,
true);
493inline node::operator std::string ()
const
502inline node::operator
const char* ()
const
504 if (t != type::string)
510inline node::operator double ()
const
516inline node::operator float ()
const
518 return (
float)to_num ();
521inline node::operator int ()
const
523 return (
int)to_num ();
527inline node::operator bool ()
const
539 (&obj)->~nodes_map ();
542 (&arr)->~nodes_array ();
545 (&str)->~basic_string ();
554 new (&obj) nodes_map ();
557 new (&arr) nodes_array ();
560 new (&str) std::string ();
580 return (t == type::object) ? (int)obj.size ()
581 : (t == type::array) ? (
int)arr.size ()
582 : (t != type::null) ? 1
589 fmt.reset (f.empty () ?
nullptr :
new std::string(f));
595 return fmt? *fmt : std::string_view();
611 return const_cast<node&
> (
static_cast<const node&
> (*this).at (name));
621 return const_cast<node&
> (
static_cast<const node&
> (*this).at (index));
638 omanip (
void (*fn)(std::ios_base&,
int),
int val) : pfun(fn), arg(val) {}
639 void (* pfun)(std::ios_base&, int);
642 friend std::ostream& operator << (std::ostream& strm,
const omanip& manip) {
643 (*manip.pfun) (strm, manip.arg);
649void indenter (std::ios_base& os,
int spaces);
651inline omanip spaces (
int nspc)
653 return omanip (&indenter, nspc);
656inline std::ostream& indent (std::ostream& os)
661inline std::ostream& tabs (std::ostream& os)
667std::ostream& noindent (std::ostream& os);
669std::ostream& utf8 (std::ostream& os);
671std::ostream& noutf8 (std::ostream& os);
676std::ostream& operator<< (std::ostream& os,
const node& n);
679std::istream& operator>> (std::istream& is,
node& n);
685 n.
clear (type::array);
686 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:599
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:533
int size() const
Return number of direct descendants.
Definition json.h:578
bool operator==(const node &other) const
Equality operator.
Definition json.cpp:299
type kind() const
Return the type of node.
Definition json.h:572
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:622
bool to_bool() const
Conversion to boolean value.
Definition json.cpp:991
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:919
const_iterator end() const
End iterator (const variant)
Definition json.h:471
node & at(const std::string &name)
Return value of an object node element.
Definition json.h:609
const_iterator begin() const
Begin iterator (const variant)
Definition json.h:449
std::string to_str() const
String conversion function.
Definition json.cpp:955
node & operator=(const node &rhs)
Principal assignment operator.
Definition json.cpp:110
node * search_key(const std::string &name)
Search all descendant nodes for the given key name.
Definition json.cpp:384
operator std::string() const
Return node value as a string (if possible)
Definition json.h:493
const std::string_view format() const
Return output format for numerical values.
Definition json.h:593
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:1035
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:683
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