1 // File: vogl_json.inl
\r
6 inline json_value::json_value()
\r
7 : m_type(cJSONValueTypeNull), m_line(0)
\r
12 inline json_value::json_value(bool val)
\r
13 : m_type(cJSONValueTypeBool), m_line(0)
\r
15 m_data.m_nVal = val;
\r
18 inline json_value::json_value(int32 nVal)
\r
19 : m_type(cJSONValueTypeInt), m_line(0)
\r
21 m_data.m_nVal = nVal;
\r
24 inline json_value::json_value(uint32 nVal)
\r
25 : m_type(cJSONValueTypeInt), m_line(0)
\r
27 m_data.m_nVal = nVal;
\r
30 inline json_value::json_value(int64_t nVal)
\r
31 : m_type(cJSONValueTypeInt), m_line(0)
\r
33 m_data.m_nVal = nVal;
\r
36 // Note uint64_t values may be encoded as hex strings or int64_t
\r
37 inline json_value::json_value(uint64_t nVal)
\r
38 : m_type(cJSONValueTypeNull), m_line(0)
\r
44 inline json_value::json_value(double flVal)
\r
45 : m_type(cJSONValueTypeDouble), m_line(0)
\r
47 m_data.m_flVal = flVal;
\r
50 inline json_value::json_value(char *pStr)
\r
51 : m_type(cJSONValueTypeString), m_line(0)
\r
53 m_data.m_pStr = vogl_strdup(pStr);
\r
56 inline json_value::json_value(const char *pStr)
\r
57 : m_type(cJSONValueTypeString), m_line(0)
\r
59 m_data.m_pStr = vogl_strdup(pStr);
\r
62 inline json_value::json_value(const dynamic_string &str)
\r
63 : m_type(cJSONValueTypeString), m_line(0)
\r
65 m_data.m_pStr = vogl_strdup(str.get_ptr());
\r
68 inline json_value::json_value(const json_node *pNode)
\r
69 : m_type(cJSONValueTypeNode), m_line(0)
\r
71 m_data.m_pNode = get_json_node_pool()->alloc(*pNode);
\r
74 inline json_value::json_value(json_value_type_t type)
\r
79 if (type == cJSONValueTypeNode)
\r
80 m_data.m_pNode = get_json_node_pool()->alloc();
\r
81 else if (type == cJSONValueTypeString)
\r
82 m_data.m_pStr = vogl_strdup("");
\r
85 inline json_value::~json_value()
\r
90 inline json_value_type_t json_value::get_type() const
\r
95 inline const json_value_data_t &json_value::get_raw_data() const
\r
100 inline json_value_data_t &json_value::get_raw_data()
\r
105 inline bool json_value::is_null() const
\r
107 return m_type == cJSONValueTypeNull;
\r
110 inline bool json_value::is_valid() const
\r
112 return m_type != cJSONValueTypeNull;
\r
115 inline bool json_value::is_bool() const
\r
117 return m_type == cJSONValueTypeBool;
\r
120 inline bool json_value::is_int() const
\r
122 return m_type == cJSONValueTypeInt;
\r
125 inline bool json_value::is_double() const
\r
127 return m_type == cJSONValueTypeDouble;
\r
130 inline bool json_value::is_numeric() const
\r
132 return (m_type == cJSONValueTypeInt) || (m_type == cJSONValueTypeDouble);
\r
135 inline bool json_value::is_string() const
\r
137 return m_type == cJSONValueTypeString;
\r
140 inline bool json_value::is_node() const
\r
142 return m_type == cJSONValueTypeNode;
\r
145 inline bool json_value::is_object_or_array() const
\r
147 return m_type == cJSONValueTypeNode;
\r
150 inline const json_node *json_value::get_node_ptr() const
\r
152 return (m_type == cJSONValueTypeNode) ? m_data.m_pNode : NULL;
\r
154 inline json_node *json_value::get_node_ptr()
\r
156 return (m_type == cJSONValueTypeNode) ? m_data.m_pNode : NULL;
\r
159 inline void json_value::clear()
\r
161 set_value_to_null();
\r
165 inline void json_value::assume_ownership(json_value &src_val)
\r
167 set_value_to_null();
\r
171 inline void json_value::release_ownership(json_value &dst_value)
\r
173 dst_value.set_value_to_null();
\r
174 dst_value.swap(*this);
\r
177 inline void json_value::init(json_value_type_t type)
\r
182 if (type == cJSONValueTypeNode)
\r
183 m_data.m_pNode = get_json_node_pool()->alloc();
\r
184 else if (type == cJSONValueTypeString)
\r
185 m_data.m_pStr = vogl_strdup("");
\r
189 inline json_node *json_value::init_object()
\r
191 return set_value_to_node(true);
\r
194 inline json_node *json_value::init_array()
\r
196 return set_value_to_node(false);
\r
199 inline json_node *json_value::set_value_to_object(json_node *pParent)
\r
201 json_node *pRoot = get_json_node_pool()->alloc(pParent, true);
\r
202 set_value_assume_ownership(pRoot);
\r
206 inline json_node *json_value::set_value_to_array(json_node *pParent)
\r
208 json_node *pRoot = get_json_node_pool()->alloc(pParent, false);
\r
209 set_value_assume_ownership(pRoot);
\r
213 inline json_node *json_value::set_value_to_node(bool is_object, json_node *pParent)
\r
215 json_node *pRoot = get_json_node_pool()->alloc(pParent, is_object);
\r
216 set_value_assume_ownership(pRoot);
\r
220 inline void json_value::set_value_to_null()
\r
224 m_type = cJSONValueTypeNull;
\r
227 inline void json_value::set_value(bool val)
\r
230 m_data.m_nVal = val;
\r
231 m_type = cJSONValueTypeBool;
\r
234 inline void json_value::set_value(int8 nVal)
\r
237 m_data.m_nVal = nVal;
\r
238 m_type = cJSONValueTypeInt;
\r
241 inline void json_value::set_value(int16 nVal)
\r
244 m_data.m_nVal = nVal;
\r
245 m_type = cJSONValueTypeInt;
\r
248 inline void json_value::set_value(int32 nVal)
\r
251 m_data.m_nVal = nVal;
\r
252 m_type = cJSONValueTypeInt;
\r
255 inline void json_value::set_value(int64_t nVal)
\r
258 m_data.m_nVal = nVal;
\r
259 m_type = cJSONValueTypeInt;
\r
262 inline void json_value::set_value(uint8 nVal)
\r
264 set_value(static_cast<int64_t>(nVal));
\r
267 inline void json_value::set_value(uint16 nVal)
\r
269 set_value(static_cast<int64_t>(nVal));
\r
272 inline void json_value::set_value(uint32 nVal)
\r
274 set_value(static_cast<int64_t>(nVal));
\r
277 inline void json_value::set_value(double flVal)
\r
280 m_data.m_flVal = flVal;
\r
281 m_type = cJSONValueTypeDouble;
\r
284 inline void json_value::set_value(const char *pStr)
\r
287 m_data.m_pStr = vogl_strdup(pStr);
\r
288 m_type = cJSONValueTypeString;
\r
291 inline void json_value::set_value(const dynamic_string &str)
\r
294 m_data.m_pStr = vogl_strdup(str.get_ptr());
\r
295 m_type = cJSONValueTypeString;
\r
298 inline void json_value::set_value(const json_node *pNode)
\r
301 m_data.m_pNode = get_json_node_pool()->alloc(*pNode);
\r
302 m_type = cJSONValueTypeNode;
\r
305 inline void json_value::set_value_assume_ownership(char *pStr)
\r
308 m_data.m_pStr = pStr;
\r
309 m_type = cJSONValueTypeString;
\r
312 inline void json_value::set_value_assume_ownership(json_node *pNode)
\r
315 m_data.m_pNode = pNode;
\r
316 m_type = cJSONValueTypeNode;
\r
319 inline json_value &json_value::operator=(bool val)
\r
325 inline json_value &json_value::operator=(int32 nVal)
\r
331 inline json_value &json_value::operator=(uint32 nVal)
\r
337 inline json_value &json_value::operator=(int64_t nVal)
\r
343 inline json_value &json_value::operator=(uint64_t nVal)
\r
349 inline json_value &json_value::operator=(double flVal)
\r
355 inline json_value &json_value::operator=(const char *pStr)
\r
361 inline bool json_value::is_object() const
\r
363 return (m_type == cJSONValueTypeNode) ? get_node_ptr()->is_object() : false;
\r
366 inline bool json_value::is_array() const
\r
368 return (m_type == cJSONValueTypeNode) ? get_node_ptr()->is_array() : false;
\r
371 inline bool json_value::operator==(const json_value &other) const
\r
373 if (m_type != other.m_type)
\r
378 case cJSONValueTypeNull:
\r
380 case cJSONValueTypeBool:
\r
381 case cJSONValueTypeInt:
\r
382 case cJSONValueTypeDouble:
\r
383 return m_data.m_nVal == other.m_data.m_nVal;
\r
384 case cJSONValueTypeString:
\r
385 return vogl_strcmp(as_string_ptr(), other.as_string_ptr()) == 0;
\r
386 case cJSONValueTypeNode:
\r
387 return *get_node_ptr() == *other.get_node_ptr();
\r
392 // Attempts to convert a JSON value to each type.
\r
393 // Returns false and sets val to def if the value cannot be converted (out of range, or type is obviously incompatible).
\r
394 // Note that if trying to convert a negative value to an unsigned type, or a value which is too large will fail and you'll get the default.
\r
395 inline bool json_value::get_bool(bool &val, bool def) const
\r
399 val = (m_data.m_nVal != 0);
\r
403 return convert_to_bool(val, def);
\r
406 inline bool json_value::get_numeric(int32 &val, int32 def) const
\r
408 if ((is_int()) && (m_data.m_nVal == static_cast<int32>(m_data.m_nVal)))
\r
410 val = static_cast<int32>(m_data.m_nVal);
\r
414 return convert_to_int32(val, def);
\r
417 inline bool json_value::get_numeric(int64_t &val, int64_t def) const
\r
421 val = m_data.m_nVal;
\r
425 return convert_to_int64(val, def);
\r
428 inline bool json_value::get_numeric(uint64_t &val, uint64_t def) const
\r
430 return convert_to_uint64(val, def);
\r
433 inline bool json_value::get_numeric(float &val, float def) const
\r
437 val = static_cast<float>(m_data.m_flVal);
\r
441 return convert_to_float(val, def);
\r
444 inline bool json_value::get_numeric(double &val, double def) const
\r
448 val = m_data.m_flVal;
\r
452 return convert_to_double(val, def);
\r
455 inline bool json_value::get_string(dynamic_string &val, const char *pDef) const
\r
459 val = m_data.m_pStr;
\r
463 return convert_to_string(val, pDef);
\r
466 inline bool json_value::as_bool(bool def) const
\r
469 get_bool(result, def);
\r
473 inline int json_value::as_int(int def) const
\r
476 get_numeric(result, def);
\r
480 inline int json_value::as_int32(int32 def) const
\r
483 get_numeric(result, def);
\r
487 inline uint32 json_value::as_uint32(uint32 def) const
\r
490 get_numeric(result, def);
\r
494 inline int64_t json_value::as_int64(int64_t def) const
\r
497 get_numeric(result, def);
\r
501 inline uint64_t json_value::as_uint64(uint64_t def) const
\r
504 get_numeric(result, def);
\r
508 inline float json_value::as_float(float def) const
\r
511 get_numeric(result, def);
\r
515 inline double json_value::as_double(double def) const
\r
518 get_numeric(result, def);
\r
522 // Returns value as a string, or the default string if the value cannot be converted.
\r
523 inline dynamic_string json_value::as_string(const char *pDef) const
\r
525 dynamic_string result;
\r
526 get_string(result, pDef);
\r
530 // Returns pointer to null terminated string or NULL if the value is not a string.
\r
531 inline const char *json_value::as_string_ptr(const char *pDef) const
\r
533 return is_string() ? m_data.m_pStr : pDef;
\r
536 inline bool json_value::operator!=(const json_value &other) const
\r
538 return !(*this == other);
\r
541 inline void json_value::swap(json_value &other)
\r
543 std::swap(m_type, other.m_type);
\r
544 VOGL_ASSUME(sizeof(m_data.m_nVal) == sizeof(m_data));
\r
545 std::swap(m_data.m_nVal, other.m_data.m_nVal);
\r
548 inline void json_value::set_line(uint line)
\r
553 inline uint json_value::get_line() const
\r
558 template <typename T>
\r
559 bool json_node::get_value_as_int(const char *pKey, T &val, T def) const
\r
563 int index = find_key(pKey);
\r
568 if (!get_value(index).get_numeric(v, 0))
\r
571 if ((v < int_traits<T>::cMin) || (v > int_traits<T>::cMax))
\r
573 val = static_cast<T>(v);
\r
577 inline bool json_node::operator==(const json_node &other) const
\r
579 if (m_is_object != other.m_is_object)
\r
581 if (m_keys.size() != other.m_keys.size())
\r
583 if (m_values.size() != other.m_values.size())
\r
587 for (uint i = 0; i < m_values.size(); i++)
\r
589 int j = other.find_key(m_keys[i].get_ptr());
\r
590 if (j == cInvalidIndex)
\r
593 if (m_values[i] != other.m_values[j])
\r
599 for (uint i = 0; i < m_values.size(); i++)
\r
601 if (m_values[i] != other.m_values[i])
\r
608 // Parent/child retrieval
\r
609 inline const json_node *json_node::get_parent() const
\r
614 // true if the value at the specified index is an object or array.
\r
615 inline bool json_node::is_child(uint index) const
\r
617 return get_value_type(index) == cJSONValueTypeNode;
\r
620 // true if the value at the specified index is an object.
\r
621 inline bool json_node::is_child_object(uint index) const
\r
623 const json_node *pChild = get_child(index);
\r
626 return pChild->is_object();
\r
629 // true if the value at the specified index is an array.
\r
630 inline bool json_node::is_child_array(uint index) const
\r
632 const json_node *pChild = get_child(index);
\r
635 return pChild->is_array();
\r
638 // Returns pointer to the child array or object at the specified index, or NULL if the value is not an array or object.
\r
639 inline const json_node *json_node::get_child(uint index) const
\r
641 return m_values[index].get_node_ptr();
\r
644 inline json_node *json_node::get_child(uint index)
\r
646 return m_values[index].get_node_ptr();
\r
649 // Object/array info
\r
650 inline bool json_node::is_object() const
\r
652 return m_is_object;
\r
655 inline bool json_node::is_array() const
\r
657 return !m_is_object;
\r
660 inline uint json_node::size() const
\r
662 return m_values.size();
\r
665 inline bool json_node::has_key(const char *pKey) const
\r
667 return find_key(pKey) >= 0;
\r
670 // true if the value associated with the specified key is an object
\r
671 inline bool json_node::has_object(const char *pKey) const
\r
673 int index = find_key(pKey);
\r
676 return is_child_object(index);
\r
679 // true if the value associated with the specified key is an array
\r
680 inline bool json_node::has_array(const char *pKey) const
\r
682 int index = find_key(pKey);
\r
685 return is_child_array(index);
\r
688 inline const dynamic_string &json_node::get_key(uint index) const
\r
690 return m_keys[index];
\r
693 // returns g_null_json_value if the key does not exist
\r
694 inline const json_value &json_node::operator[](const char *pKey) const
\r
696 return find_value(pKey);
\r
699 // Value retrieval/finding
\r
700 inline json_value_type_t json_node::get_value_type(uint index) const
\r
702 return m_values[index].get_type();
\r
705 inline const json_value &json_node::get_value(uint index) const
\r
707 return m_values[index];
\r
710 inline const json_value &json_node::operator[](uint index) const
\r
712 return m_values[index];
\r
715 // Returns NULL if the value is not an object or array.
\r
716 inline const json_node *json_node::get_value_as_object_or_array(uint index) const
\r
718 return get_child(index);
\r
721 inline const json_node *json_node::get_value_as_object(uint index) const
\r
723 if (!is_child_object(index))
\r
726 return get_child(index);
\r
729 inline const json_node *json_node::get_value_as_array(uint index) const
\r
731 if (!is_child_array(index))
\r
734 return get_child(index);
\r
737 inline bool json_node::operator!=(const json_node &other) const
\r
739 return !(*this == other);
\r
742 // Retrieves the json_value at the specified index.
\r
743 inline json_value &json_node::get_value(uint index)
\r
745 return m_values[index];
\r
748 inline json_value &json_node::operator[](uint index)
\r
750 return m_values[index];
\r
753 // Sets the parent node of this node.
\r
754 inline void json_node::set_parent(const json_node *pParent)
\r
756 m_pParent = pParent;
\r
759 inline void json_node::init_object()
\r
761 set_is_object(true);
\r
764 inline void json_node::init_array()
\r
766 set_is_object(false);
\r
769 // Changes the number of elements in this node. Enlarging will add empty keys (for objects), and values of type cJSONValueTypeNull.
\r
770 inline uint json_node::enlarge(uint n)
\r
772 uint cur_size = size();
\r
773 resize(cur_size + n);
\r
777 // Retrieves the json_value associated with a key, or adds a new named key/value to the node, which must be an object. The newly added json_value will have type cJSONValueTypeNull.
\r
778 inline json_value &json_node::get_or_add(const dynamic_string &key)
\r
780 return get_or_add(key.get_ptr());
\r
783 // Adds a named key/value to the node. The json_value will have type cJSONValueTypeNull. If the node is not an object it will be upgraded to an object.
\r
784 inline json_value &json_node::add(const dynamic_string &key)
\r
786 return add(key.get_ptr());
\r
789 // Adds a named child object to the node. If the node is not an object it will be upgraded to an object.
\r
790 inline json_node &json_node::add_object(const dynamic_string &key)
\r
792 return add_object(key.get_ptr());
\r
795 // Adds a named child array to the node. If the node is not an object it will be upgraded to an object.
\r
796 inline json_node &json_node::add_array(const dynamic_string &key)
\r
798 return add_array(key.get_ptr());
\r
801 // Adds a key/value to a node. If the node is not an object it will be upgraded to an object.
\r
802 inline json_value &json_node::add_key_value(const dynamic_string &key, const json_value &val)
\r
804 return add_key_value(key.get_ptr(), val);
\r
807 // Adds a new key/value to a node. pValueToParse must be JSON text.
\r
808 inline bool add_key_and_parsed_value(const dynamic_string &key, const char *pValueToParse)
\r
810 return add_key_and_parsed_value(key.get_ptr(), pValueToParse);
\r
813 inline bool json_node::erase(const dynamic_string &key)
\r
815 return erase(key.get_ptr());
\r
818 // Set the light number associated with this node.
\r
819 inline void json_node::set_line(uint line)
\r
824 // Gets the light number associated with this node.
\r
825 inline uint json_node::get_line() const
\r
830 template <typename T>
\r
831 bool json_node::add_object(const char *pKey, const T &obj)
\r
833 json_value &val = pKey ? add(pKey) : add_value();
\r
834 return json_serialize(obj, val);
\r
837 template <typename T>
\r
838 bool json_node::get_object(const char *pKey, T &obj) const
\r
842 VOGL_ASSERT_ALWAYS;
\r
846 const json_value &val = find_value(pKey);
\r
847 if (val == g_null_json_value)
\r
850 return json_deserialize(obj, val);
\r
853 template <typename T>
\r
854 bool json_node::get_object(uint index, T &obj) const
\r
856 return json_deserialize(obj, get_value(index));
\r
859 template <typename T>
\r
860 inline bool json_node::add_vector(const char *pKey, const T &vec)
\r
862 json_node &obj = pKey ? add_object(pKey) : *this;
\r
864 obj.add_key_value("type", "vector");
\r
866 uint size = vec.size();
\r
867 obj.add_key_value("size", size);
\r
871 json_node &elements = obj.add_array("elements");
\r
872 elements.reserve(size);
\r
874 for (uint i = 0; i < size; i++)
\r
876 json_value &new_val = elements.add_value();
\r
877 if (!json_serialize(vec[i], new_val))
\r
880 if (new_val.is_node())
\r
881 new_val.get_node_ptr()->set_parent(&elements);
\r
888 template <typename T>
\r
889 inline bool json_node::get_vector(const char *pKey, T &vec) const
\r
891 const json_node *pObj = pKey ? find_child_object(pKey) : this;
\r
895 if (!pObj->is_object())
\r
898 if (pObj->value_as_string("type") != "vector")
\r
901 uint64_t size64 = pObj->value_as_uint64("size");
\r
902 if (size64 > cUINT32_MAX)
\r
905 uint size = static_cast<uint>(size64);
\r
911 const json_node *pElements = pObj->find_child_array("elements");
\r
913 if ((!pElements) || (pElements->size() != size))
\r
916 for (uint i = 0; i < size; i++)
\r
917 if (!json_deserialize(vec[i], pElements->get_value(i)))
\r
924 template <typename T>
\r
925 inline bool json_node::add_map(const char *pKey, const T &map)
\r
927 json_node &obj = pKey ? add_object(pKey) : *this;
\r
929 obj.add_key_value("type", "map");
\r
930 obj.add_key_value("size", map.size());
\r
934 json_node &arr_node = obj.add_array("objects");
\r
935 arr_node.reserve(map.size());
\r
937 typename T::const_iterator end_it(map.end());
\r
938 for (typename T::const_iterator it = map.begin(); it != end_it; ++it)
\r
940 json_node &element = arr_node.add_object();
\r
942 json_value &val = element.add("key");
\r
943 json_serialize(it->first, val);
\r
945 val.get_node_ptr()->set_parent(&arr_node);
\r
947 if (!is_empty_type<typename T::referent_type>::cValue)
\r
949 json_value &val = element.add("value");
\r
950 json_serialize(it->second, val);
\r
952 val.get_node_ptr()->set_parent(&arr_node);
\r
960 template <typename T>
\r
961 inline bool json_node::get_map(const char *pKey, T &map) const
\r
963 const json_node *pObj = pKey ? find_child_object(pKey) : this;
\r
967 if (!pObj->is_object())
\r
970 if (pObj->value_as_string("type") != "map")
\r
973 if (!pObj->has_key("size"))
\r
976 uint64_t size64 = pObj->value_as_uint32("size");
\r
977 if ((size64) && (!pObj->has_key("objects")))
\r
980 // TODO: fix this once vogl::vector supports 64-bit arrays
\r
981 if (size64 > cUINT32_MAX)
\r
989 uint size = static_cast<uint>(size64);
\r
993 const json_node *pObjects_arr = pObj->find_child_array("objects");
\r
997 if (pObjects_arr->size() != size)
\r
1000 for (uint i = 0; i < pObjects_arr->size(); i++)
\r
1002 const json_node *pArr_node = pObjects_arr->get_child(i);
\r
1006 const json_value &key_val = pArr_node->find_value("key");
\r
1007 if (key_val == g_null_json_value)
\r
1010 typename T::key_type key;
\r
1011 if (!json_deserialize(key, key_val))
\r
1014 typename T::referent_type val;
\r
1016 if (!is_empty_type<typename T::referent_type>::cValue)
\r
1018 const json_value &value_val = pArr_node->find_value("value");
\r
1019 if (value_val == g_null_json_value)
\r
1022 if (!json_deserialize(val, value_val))
\r
1026 if (!map.insert(key, val).second)
\r
1033 template <typename T>
\r
1034 inline bool json_serialize(const T &v, json_value &val)
\r
1036 return v.json_serialize(val);
\r
1039 template <typename T>
\r
1040 inline bool json_deserialize(T &v, const json_value &val)
\r
1042 return v.json_deserialize(val);
\r
1045 inline bool json_serialize(const int8 &v, json_value &val)
\r
1051 inline bool json_deserialize(int8 &v, const json_value &val)
\r
1053 return val.get_numeric(v);
\r
1056 inline bool json_serialize(const int16 &v, json_value &val)
\r
1062 inline bool json_deserialize(int16 &v, const json_value &val)
\r
1064 return val.get_numeric(v);
\r
1067 inline bool json_serialize(const int32 &v, json_value &val)
\r
1073 inline bool json_deserialize(int32 &v, const json_value &val)
\r
1075 return val.get_numeric(v);
\r
1078 inline bool json_serialize(const int64_t &v, json_value &val)
\r
1084 inline bool json_deserialize(int64_t &v, const json_value &val)
\r
1086 return val.get_numeric(v);
\r
1089 inline bool json_serialize(const uint8 &v, json_value &val)
\r
1095 inline bool json_deserialize(uint8 &v, const json_value &val)
\r
1097 return val.get_numeric(v);
\r
1100 inline bool json_serialize(const uint16 &v, json_value &val)
\r
1106 inline bool json_deserialize(uint16 &v, const json_value &val)
\r
1108 return val.get_numeric(v);
\r
1111 inline bool json_serialize(const uint32 &v, json_value &val)
\r
1117 inline bool json_deserialize(uint32 &v, const json_value &val)
\r
1119 return val.get_numeric(v);
\r
1122 inline bool json_serialize(const uint64_t &v, json_value &val)
\r
1128 inline bool json_deserialize(uint64_t &v, const json_value &val)
\r
1130 return val.get_numeric(v);
\r
1133 inline bool json_serialize(const float &v, json_value &val)
\r
1139 inline bool json_deserialize(float &v, const json_value &val)
\r
1141 return val.get_numeric(v);
\r
1144 inline bool json_serialize(const double &v, json_value &val)
\r
1150 inline bool json_deserialize(double &v, const json_value &val)
\r
1152 return val.get_numeric(v);
\r
1155 inline bool json_serialize(const json_node &v, json_value &val)
\r
1157 val.set_value(&v);
\r
1161 inline bool json_deserialize(json_node &v, const json_value &val)
\r
1163 if (!val.is_object_or_array())
\r
1165 v = *val.get_node_ptr();
\r
1169 template <typename Key, typename Value, typename Hasher, typename Equals>
\r
1170 inline bool json_serialize(const hash_map<Key, Value, Hasher, Equals> &hash_map, json_value &val)
\r
1172 json_node *pObj = val.init_object();
\r
1173 return pObj->add_map(NULL, hash_map);
\r
1176 template <typename Key, typename Value, typename Hasher, typename Equals>
\r
1177 inline bool json_deserialize(hash_map<Key, Value, Hasher, Equals> &hash_map, const json_value &val)
\r
1179 if (!val.is_object())
\r
1181 const json_node *pNode = val.get_node_ptr();
\r
1182 return pNode->get_map(NULL, hash_map);
\r
1185 template <typename Key, typename Value, typename Hasher, typename Equals, typename Allocator>
\r
1186 inline bool json_serialize(const rh_hash_map<Key, Value, Hasher, Equals, Allocator> &rh_hash_map, json_value &val)
\r
1188 json_node *pObj = val.init_object();
\r
1189 return pObj->add_map(NULL, rh_hash_map);
\r
1192 template <typename Key, typename Value, typename Hasher, typename Equals, typename Allocator>
\r
1193 inline bool json_deserialize(rh_hash_map<Key, Value, Hasher, Equals, Allocator> &rh_hash_map, const json_value &val)
\r
1195 if (!val.is_object())
\r
1197 const json_node *pNode = val.get_node_ptr();
\r
1198 return pNode->get_map(NULL, rh_hash_map);
\r
1201 template <typename Key, typename Value, typename LessComp, typename EqualComp, uint MaxLevels>
\r
1202 inline bool json_serialize(const vogl::map<Key, Value, LessComp, EqualComp, MaxLevels> &map, json_value &val)
\r
1204 json_node *pObj = val.init_object();
\r
1205 return pObj->add_map(NULL, map);
\r
1208 template <typename Key, typename Value, typename LessComp, typename EqualComp, uint MaxLevels>
\r
1209 inline bool json_deserialize(vogl::map<Key, Value, LessComp, EqualComp, MaxLevels> &map, const json_value &val)
\r
1211 if (!val.is_object())
\r
1213 const json_node *pNode = val.get_node_ptr();
\r
1214 return pNode->get_map(NULL, map);
\r
1217 template <typename T>
\r
1218 inline bool json_serialize(const vogl::vector<T> &vec, json_value &val)
\r
1220 json_node *pObj = val.init_object();
\r
1221 return pObj->add_vector(NULL, vec);
\r
1224 template <typename T>
\r
1225 inline bool json_deserialize(vogl::vector<T> &vec, const json_value &val)
\r
1227 if (!val.is_object())
\r
1229 const json_node *pNode = val.get_node_ptr();
\r
1230 return pNode->get_vector(NULL, vec);
\r
1233 template <typename T, uint N>
\r
1234 inline bool json_serialize(const vogl::growable_array<T, N> &vec, json_value &val)
\r
1236 json_node *pObj = val.init_object();
\r
1237 return pObj->add_vector(NULL, vec);
\r
1240 template <typename T, uint N>
\r
1241 inline bool json_deserialize(vogl::growable_array<T, N> &vec, const json_value &val)
\r
1243 if (!val.is_object())
\r
1245 const json_node *pNode = val.get_node_ptr();
\r
1246 return pNode->get_vector(NULL, vec);
\r
1249 // root value manipulation
\r
1250 inline const json_value &json_document::get_value() const
\r
1255 inline json_value &json_document::get_value()
\r
1260 // This returns a pointer because a valid JSON document can be a single value (in which case the document's value is not a node pointer).
\r
1261 inline const json_node *json_document::get_root() const
\r
1263 return get_node_ptr();
\r
1266 inline json_node *json_document::get_root()
\r
1268 return get_node_ptr();
\r
1271 inline void json_document::clear(bool reinitialize_to_object)
\r
1273 json_value::clear();
\r
1274 if (reinitialize_to_object)
\r
1278 // document's filename
\r
1279 inline const dynamic_string &json_document::get_filename() const
\r
1281 return m_filename;
\r
1284 inline void json_document::set_filename(const char *pFilename)
\r
1286 m_filename.set(pFilename ? pFilename : "");
\r
1289 // error messages due to failed parses
\r
1290 inline const dynamic_string &json_document::get_error_msg() const
\r
1292 return m_error_msg;
\r
1295 inline uint json_document::get_error_line() const
\r
1297 return m_error_line;
\r
1300 inline void json_document::clear_error()
\r
1302 m_error_msg.clear();
\r
1306 } // namespace vogl
\r