@@ -24,6 +24,14 @@ bool json::object::isString() const { return this->_type == type::String; }
2424bool json::object::isArray () const { return this ->_type == type::Array; }
2525bool json::object::isObject () const { return this ->_type == type::Object; }
2626
27+ void json::object::setArrayIfNull () {
28+ if (this ->isNull ()) {
29+ this ->_type = type::Array;
30+ this ->_value = std::vector<object>();
31+ }
32+ return ;
33+ }
34+
2735void json::object::push_back (value &data) {
2836 this ->setArrayIfNull ();
2937 if (!this ->isArray ()) {
@@ -147,13 +155,74 @@ size_t json::object::size() {
147155 return 0 ;
148156}
149157
150- void json::object::setArrayIfNull () {
158+ std::string json::object::dump (int indent, int baseline) {
159+ std::string result = " " ;
160+ indent = indent < 0 ? 0 : indent;
161+
151162 if (this ->isNull ()) {
152- this ->_type = type::Array;
153- this ->_value = std::vector<object>();
163+ result += " null" ;
164+ } else if (this ->isBoolean ()) {
165+ result += std::get<bool >(this ->_value ) ? " true" : " false" ;
166+ } else if (this ->isInteger ()) {
167+ result += std::to_string (std::get<int >(this ->_value ));
168+ } else if (this ->isDouble ()) {
169+ result += std::to_string (std::get<double >(this ->_value ));
170+ } else if (this ->isString ()) {
171+ result += " \" " + std::get<std::string>(this ->_value ) + " \" " ;
172+ } else if (this ->isArray ()) {
173+ result += " [" ;
174+ result += indent > 0 ? " \n " : " " ;
175+ for (size_t i = 0 ; i < this ->size (); i++) {
176+ result += std::string (indent, ' ' ) +
177+ std::get<std::vector<object>>(this ->_value )[i].dump (indent * 2 ,
178+ indent);
179+ if (i != this ->size () - 1 ) {
180+ result += " ," ;
181+ }
182+ result += indent > 0 ? " \n " : " " ;
183+ }
184+ result += std::string (baseline, ' ' ) + " ]" ;
185+ } else if (this ->isObject ()) {
186+ result += " {" ;
187+ result += indent > 0 ? " \n " : " " ;
188+ size_t i = 0 ;
189+ for (std::pair<const std::string, object> &item :
190+ std::get<std::map<std::string, object>>(this ->_value )) {
191+ result += std::string (indent, ' ' ) + " \" " + item.first +
192+ " \" : " + item.second .dump (indent * 2 , indent);
193+ if (i != this ->size () - 1 ) {
194+ result += " ," ;
195+ }
196+ result += indent > 0 ? " \n " : " " ;
197+ i++;
198+ }
199+ result += std::string (baseline, ' ' ) + " }" ;
200+ } else {
201+ throw std::runtime_error (" [ERROR] json::object::dump(int indent = 0) - "
202+ " Invalid type." );
203+ }
204+
205+ if (indent == 0 ) {
206+ result.erase (std::remove_if (result.begin (), result.end (), ::isspace),
207+ result.end ());
154208 }
209+ return result;
210+ }
211+ void json::object::dumps (const std::string &filePath, int indent) {
212+ std::ofstream jsonFile (filePath);
213+ if (!jsonFile.is_open ()) {
214+ throw std::runtime_error (" [ERROR] json::object::dumps(const std::string "
215+ " &filePath, int indent = 0) - Cannot open " +
216+ filePath);
217+ }
218+
219+ std::string _json = this ->dump (indent);
220+ jsonFile << _json;
221+ jsonFile.close ();
222+
155223 return ;
156224}
225+
157226void json::object::setObjectIfNull () {
158227 if (this ->isNull ()) {
159228 this ->_type = type::Object;
0 commit comments