11#include < json/object.h>
22
3- json::object::object () { return ; }
3+ json::object::object () : _type(type::Null) { return ; }
4+
5+ json::object::object (value &data) {
6+ this ->_type = this ->getType (data);
7+ this ->_value = std::move (data);
8+ return ;
9+ }
410
511json::object::~object () { return ; }
12+
13+ void json::object::operator =(value &data) {
14+ this ->_type = this ->getType (data);
15+ this ->_value = std::move (data);
16+ return ;
17+ }
18+
19+ bool json::object::isNull () const { return this ->_type == type::Null; }
20+ bool json::object::isBoolean () const { return this ->_type == type::Boolean; }
21+ bool json::object::isInteger () const { return this ->_type == type::Integer; }
22+ bool json::object::isDouble () const { return this ->_type == type::Double; }
23+ bool json::object::isString () const { return this ->_type == type::String; }
24+ bool json::object::isArray () const { return this ->_type == type::Array; }
25+ bool json::object::isObject () const { return this ->_type == type::Object; }
26+
27+ void json::object::push_back (value &data) {
28+ this ->setArrayIfNull ();
29+ if (!this ->isArray ()) {
30+ throw std::runtime_error (" [ERROR] json::object::push_back(value "
31+ " &data) - Cannot push_back into non-array." );
32+ return ;
33+ }
34+
35+ std::get<std::vector<object>>(this ->_value ).push_back (object (data));
36+ return ;
37+ }
38+ void json::object::push_back (value data) {
39+ this ->setArrayIfNull ();
40+ if (!this ->isArray ()) {
41+ throw std::runtime_error (" [ERROR] json::object::push_back(value "
42+ " data) - Cannot push_back into non-array." );
43+ return ;
44+ }
45+
46+ std::get<std::vector<object>>(this ->_value ).push_back (object (data));
47+ return ;
48+ }
49+ void json::object::push_back (object &data) {
50+ this ->setArrayIfNull ();
51+ if (!this ->isArray ()) {
52+ throw std::runtime_error (" [ERROR] json::object::push_back(object &data) - "
53+ " Cannot push_back into non-array." );
54+ return ;
55+ }
56+
57+ std::get<std::vector<object>>(this ->_value ).push_back (std::move (data));
58+ return ;
59+ }
60+ json::object::value &json::object::operator [](size_t index) {
61+ this ->setArrayIfNull ();
62+ if (!this ->isArray ()) {
63+ throw std::runtime_error (" [ERROR] &json::object::operator=(size_t index) - "
64+ " Cannot index non-array." );
65+ }
66+
67+ return std::get<std::vector<object>>(this ->_value )[index]._value ;
68+ }
69+
70+ void json::object::insert (const std::string &itemKey, value &itemValue) {
71+ this ->setObjectIfNull ();
72+ if (!this ->isObject ()) {
73+ throw std::runtime_error (
74+ " [ERROR] json::object::insert(const std::string &itemKey, value "
75+ " &itemValue) - Cannot insert into non-objects" );
76+ return ;
77+ }
78+
79+ std::get<std::map<std::string, object>>(this ->_value )[itemKey] =
80+ object (itemValue);
81+ return ;
82+ }
83+ void json::object::insert (const std::string &itemKey, value itemValue) {
84+ this ->setObjectIfNull ();
85+ if (!this ->isObject ()) {
86+ throw std::runtime_error (
87+ " [ERROR] json::object::insert(const std::string &itemKey, value "
88+ " itemValue) - Cannot insert into non-objects" );
89+ return ;
90+ }
91+
92+ std::get<std::map<std::string, object>>(this ->_value )[itemKey] =
93+ object (itemValue);
94+ return ;
95+ }
96+ void json::object::insert (const std::string &itemKey, object &itemValue) {
97+ this ->setObjectIfNull ();
98+ if (!this ->isObject ()) {
99+ throw std::runtime_error (
100+ " [ERROR] json::object::insert(const std::string &itemKey, object "
101+ " &itemValue) - Cannot insert into non-objects" );
102+ return ;
103+ }
104+
105+ std::get<std::map<std::string, object>>(this ->_value )[itemKey] = itemValue;
106+ return ;
107+ }
108+ bool json::object::contains (const std::string &itemKey) {
109+ this ->setObjectIfNull ();
110+ if (!this ->isObject ()) {
111+ throw std::runtime_error (" [ERROR] json::object::contains(const std::string "
112+ " &itemKey) - Cannot reference non-objects" );
113+ return false ;
114+ }
115+
116+ if (std::get<std::map<std::string, object>>(this ->_value ).find (itemKey) ==
117+ std::get<std::map<std::string, object>>(this ->_value ).end ()) {
118+ return false ;
119+ }
120+ return true ;
121+ }
122+ json::object::value &json::object::operator [](const std::string &itemKey) {
123+ this ->setObjectIfNull ();
124+ if (!this ->isObject ()) {
125+ throw std::runtime_error (" [ERROR] json::object::contains(const std::string "
126+ " &itemKey) - Cannot reference non-objects" );
127+ }
128+
129+ return std::get<std::map<std::string, object>>(this ->_value )[itemKey]._value ;
130+ }
131+
132+ size_t json::object::size () {
133+ if (this ->isArray ()) {
134+ return std::get<std::vector<object>>(this ->_value ).size ();
135+ }
136+
137+ if (this ->isObject ()) {
138+ return std::get<std::map<std::string, object>>(this ->_value ).size ();
139+ }
140+
141+ if (this ->isString ()) {
142+ return std::get<std::string>(this ->_value ).size ();
143+ }
144+
145+ throw std::runtime_error (" [ERROR] json::object::size() - Cannot get size of "
146+ " non-(string, array, map)" );
147+ return 0 ;
148+ }
149+
150+ void json::object::setArrayIfNull () {
151+ if (this ->isNull ()) {
152+ this ->_type = type::Array;
153+ this ->_value = std::vector<object>();
154+ }
155+ return ;
156+ }
157+ void json::object::setObjectIfNull () {
158+ if (this ->isNull ()) {
159+ this ->_type = type::Object;
160+ this ->_value = std::map<std::string, object>();
161+ }
162+ return ;
163+ }
164+ json::type json::object::getType (value &data) {
165+ if (std::holds_alternative<std::nullptr_t >(data)) {
166+ return type::Null;
167+ } else if (std::holds_alternative<bool >(data)) {
168+ return type::Boolean;
169+ } else if (std::holds_alternative<int >(data)) {
170+ return type::Integer;
171+ } else if (std::holds_alternative<double >(data)) {
172+ return type::Double;
173+ } else if (std::holds_alternative<std::string>(data)) {
174+ return type::String;
175+ } else if (std::holds_alternative<std::vector<object>>(data)) {
176+ return type::Array;
177+ } else if (std::holds_alternative<std::map<std::string, object>>(data)) {
178+ return type::Object;
179+ } else {
180+ throw std::runtime_error (
181+ " [ERROR] json::object::object(value &objectValue) - "
182+ " Invalid type." );
183+ }
184+ }
0 commit comments