37 json::json() noexcept : m_type(null_e)
51 delete m_value.u_object;
52 m_value.u_object =
nullptr;
55 delete m_value.u_array;
56 m_value.u_array =
nullptr;
59 delete m_value.u_string;
60 m_value.u_string =
nullptr;
70 void json::copy_json(
const json &other)
75 m_type = other.m_type;
76 m_raw_value = other.m_raw_value;
78 if (m_type == object_e)
80 m_value.u_object =
new map<string, unique_ptr<json>>;
82 for (
const auto &p : *other.m_value.u_object)
84 (*m_value.u_object)[p.first] = unique_ptr<json>(
new json(*p.second));
87 else if (m_type == array_e)
89 m_value.u_array =
new vector<unique_ptr<json>>;
91 for (
const auto &v : *other.m_value.u_array)
93 m_value.u_array->push_back(unique_ptr<json>(
new json(*v)));
96 else if (m_type == string_e)
98 m_value.u_string = other.m_value.u_string ==
nullptr ?
nullptr :
new string(*other.m_value.u_string);
102 m_value = other.m_value;
107 json::json(
const json &other) : m_type(null_e)
118 void json::move_json(
json &other)
123 m_type = other.m_type;
124 m_value = other.m_value;
125 m_raw_value = move(other.m_raw_value);
127 other.m_type = null_e;
147 m_value.u_object =
new map<string, unique_ptr<json>>();
150 m_value.u_array =
new vector<unique_ptr<json>>();
153 m_value.u_string =
new string;
156 m_value.u_boolean =
false;
159 m_value.u_number_int = 0;
161 case number_double_e:
162 m_value.u_number_double = 0;
174 if (t != string_e && t != number_int_e && t != number_double_e)
180 m_value.u_string =
nullptr;
185 m_type = number_int_e;
186 m_value.u_number_int = i;
191 m_type = number_double_e;
192 m_value.u_number_double = d;
198 m_value.u_boolean = b;
204 m_value.u_string =
new string(s);
210 m_value.u_string =
new string(s);
220 m_value.u_string = s.release();
226 m_type = number_int_e;
227 m_value.u_number_int = i;
234 m_type = number_double_e;
235 m_value.u_number_double = d;
243 m_value.u_boolean = b;
251 m_value.u_string =
new string(s);
259 m_value.u_string =
new string(s);
267 m_value.u_string = s.release();
275 for (
const auto &p : o)
277 (*m_value.u_object)[p.first] = unique_ptr<json>(
new json(*p.second));
286 for (
const auto &v : a)
288 m_value.u_array->push_back(unique_ptr<json>(
new json(*v)));
307 return *m_value.u_array;
312 return *m_value.u_array;
317 return *m_value.u_object;
322 return *m_value.u_object;
330 json::operator int()
const 332 if (m_raw_value.size() > 0)
336 else if (m_type == number_int_e)
338 return m_value.u_number_int;
340 else if (m_type == number_double_e)
342 return static_cast<int>(m_value.u_number_double);
350 json::operator double()
const 352 if (m_raw_value.size() > 0)
356 else if (m_type == number_double_e)
358 return m_value.u_number_double;
360 else if (m_type == number_int_e)
362 return static_cast<double>(m_value.u_number_int);
370 json::operator
const std::string&()
const 372 if (m_raw_value.size() > 0)
376 else if (m_type == string_e)
378 return *(m_value.u_string);
386 json::operator bool()
const 388 if (m_type == boolean_e)
390 return m_value.u_boolean;
392 else if (m_type == number_int_e)
394 return m_value.u_number_int != 0;
404 if (m_type == object_e)
406 auto i = m_value.u_object->find(name);
407 if (i == m_value.u_object->end())
409 return *((*m_value.u_object)[name] = unique_ptr<json>(
new json));
424 return (*
this)[string(name)];
429 if (m_type == array_e)
431 if (index < m_value.u_array->size())
433 return *(*m_value.u_array)[index];
450 return (*
this)[
static_cast<size_t>(index)];
460 if (m_type == array_e)
462 if (index < m_value.u_array->size())
464 return *(*m_value.u_array)[index];
481 return (*
this)[
static_cast<size_t>(index)];
491 if (m_type == object_e)
493 auto i = m_value.u_object->find(name);
494 if (i == m_value.u_object->end())
511 return (*
this)[string(name)];
521 if (m_type == array_e)
523 json *jp = j.release();
524 m_value.u_array->push_back(unique_ptr<json>(jp));
535 return insert(name, unique_ptr<json>(
new json(j)));
540 if (m_type == object_e)
543 m_value.u_object->insert(pair<
string, unique_ptr<json>>(name, unique_ptr<json>(r = j.release())));
565 return "number (int)";
566 case number_double_e:
567 return "number (double)";
575 bool json::number_equal(
const json &other)
const 577 if (m_raw_value.size() == 0 && other.m_raw_value.size() == 0)
579 if (m_type == number_int_e)
581 return m_value.u_number_int == other.m_value.u_number_int;
586 return m_value.u_number_double == other.m_value.u_number_double;
595 bool json::string_equal(
const json &other)
const 597 if (m_raw_value.size() == 0 && other.m_raw_value.size() == 0)
599 return *m_value.u_string == *other.m_value.u_string;
607 bool json::object_equal(
const json &other)
const 609 return m_value.u_object->size() == other.m_value.u_object->size() &&
611 m_value.u_object->begin(),
612 m_value.u_object->end(),
613 other.m_value.u_object->begin(),
614 [] (
const pair<const string, unique_ptr<json>> &a,
const pair<const string, unique_ptr<json>> &b)
615 {
return a.first == b.first && *(a.second) == *(b.second); });
618 bool json::array_equal(
const json &other)
const 620 return m_value.u_array->size() == other.m_value.u_array->size() &&
622 m_value.u_array->begin(),
623 m_value.u_array->end(),
624 other.m_value.u_array->begin(),
625 [] (
const unique_ptr<json> &a,
const unique_ptr<json> &b)
626 {
return *a == *b; });
631 if (m_type == other.m_type)
636 return object_equal(other);
638 return array_equal(other);
640 return m_value.u_boolean == other.m_value.u_boolean;
644 return number_equal(other);
645 case number_double_e:
646 return number_equal(other);
648 return string_equal(other);
653 else if ((m_type == number_int_e && other.m_type == number_double_e) ||
654 (m_type == number_double_e && other.m_type == number_int_e))
656 return static_cast<double>(*this) ==
static_cast<double>(other);
667 return !(*
this == other);
672 return static_cast<int>(*this) == i;
677 return static_cast<double>(*this) == d;
682 return static_cast<string>(*this) == s;
687 return strcmp(static_cast<string>(*this).c_str(), s) == 0;
692 return static_cast<int>(*this) != i;
697 return static_cast<double>(*this) != d;
702 return static_cast<string>(*this) != s;
707 return strcmp(static_cast<string>(*this).c_str(), s) != 0;
712 return static_cast<int>(*this) < i;
717 return static_cast<double>(*this) < d;
722 return static_cast<string>(*this) < s;
727 return strcmp(static_cast<string>(*this).c_str(), s) < 0;
732 return static_cast<int>(*this) <= i;
737 return static_cast<double>(*this) <= d;
742 return static_cast<string>(*this) < s;
747 return strcmp(static_cast<string>(*this).c_str(), s) <= 0;
752 return static_cast<int>(*this) > i;
757 return static_cast<double>(*this) > d;
762 return static_cast<string>(*this) > s;
767 return strcmp(static_cast<string>(*this).c_str(), s) > 0;
772 return static_cast<int>(*this) >= i;
777 return static_cast<double>(*this) >= d;
782 return static_cast<string>(*this) >= s;
787 return strcmp(static_cast<string>(*this).c_str(), s) >= 0;
792 if (m_type == string_e && other.m_type == string_e)
794 return static_cast<string>(*this) <
static_cast<string>(other);
796 else if (m_type == number_int_e && other.m_type == number_int_e)
798 return static_cast<int>(*this) <
static_cast<int>(other);
800 else if ((m_type == number_double_e && other.m_type == number_double_e) ||
801 (m_type == number_double_e && other.m_type == number_int_e) ||
802 (m_type == number_int_e && other.m_type == number_double_e))
804 return static_cast<double>(*this) <
static_cast<double>(other);
817 if (m_type == string_e && other.m_type == string_e)
819 return static_cast<string>(*this) <=
static_cast<string>(other);
821 else if (m_type == number_int_e && other.m_type == number_int_e)
823 return static_cast<int>(*this) <=
static_cast<int>(other);
825 else if ((m_type == number_double_e && other.m_type == number_double_e) ||
826 (m_type == number_double_e && other.m_type == number_int_e) ||
827 (m_type == number_int_e && other.m_type == number_double_e))
829 return static_cast<double>(*this) <=
static_cast<double>(other);
842 return !(*
this <= other);
847 return !(*
this < other);
852 const json *res =
this;
856 switch (t.get_type())
858 case pointer::token::all_e:
861 case pointer::token::object_e:
862 if (res->m_type == object_e)
864 auto i = res->m_value.u_object->find(t.get_name());
865 if (i == res->m_value.u_object->end())
871 res = i->second.get();
880 case pointer::token::array_e:
881 if (res->m_type == array_e)
883 if (t.get_index() < res->m_value.u_array->size())
885 res = (*res->m_value.u_array)[t.get_index()].get();
The json_array_index_range_exception class.
const std::string & get_raw_value() const
Base class for all exceptions thrown by the library.
std::vector< std::unique_ptr< json > > & get_array()
Specific class of exceptions for an invalid object key.
#define NAMESPACE
You can change the namespace of the whole library by changing this value.
Internal error - the m_type member variable has an invalid value.
bool operator==(const json &other) const
Attempt to to access an index in an array object that is out of range.
Attempt to call a method that assumes the json instance is an array.
const json & insert(const std::string &name, const json &j)
const char * get_instance_type_name() const
const json & append(const json &j)
Attempt to access a non-existent slot in a json instance.
Common defs needed everywhere and, as far as is possible, platform specific changes.
The json_invalid_key_exception class.
const json & find(const pointer &p) const
bool operator<=(const json &other) const
Invalid pointer token type.
JSON pointers as per the RFT.
Attempt to call a method that assumes the json instance is an int or double.
Pointer doesn't point to anything in the json instance.
Raw values cannot be directly compared.
Attempt to call a method that assumes the json instance is an object.
Attempt to construct an instance with a raw value that isn't a number or a string.
bool operator>(const json &other) const
json & operator[](const std::string &name)
All json things are represented by instances of this class.
json & operator=(const json &other)
bool operator!=(const json &other) const
std::map< std::string, std::unique_ptr< json > > & get_object()
bool operator>=(const json &other) const
Tried to compare two different types (e.g. int vs string)
Attempt to call a method that assumes the json instance is a string.
type get_instance_type() const
Raw values cannot be cast.
bool operator<(const json &other) const
Attempt to call a method that assumes the json instance is a boolean or castable to one...
const std::list< token > & get_path() const
Specific class of exceptions for an invalid object key.