Skip to content

Commit 008c41e

Browse files
author
Jadit19
committed
add dumping function - string & file
1 parent 9c47772 commit 008c41e

3 files changed

Lines changed: 92 additions & 10 deletions

File tree

‎example/main.cpp‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,18 @@ int main(int argc, char **argv) {
77
person.insert("name", "Adit");
88
person.insert("age", 20);
99

10-
json::object launguages;
11-
launguages.push_back("English");
12-
launguages.push_back("Hindi");
13-
launguages.push_back("French");
14-
person.insert("launguages", launguages);
10+
json::object languages;
11+
languages.push_back("c++");
12+
languages.push_back("python");
13+
languages.push_back("javascript");
14+
person.insert("languages", languages);
15+
16+
json::object socials;
17+
socials.insert("github", "https://github.com/Jadit19");
18+
socials.insert("instagram", "https://instagram.com/jadit_19");
19+
person.insert("socials", socials);
20+
21+
person.dumps("../person.json", 2);
1522

1623
return 0;
1724
}

‎include/json/object.h‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#include <algorithm>
4+
#include <fstream>
35
#include <map>
46
#include <stdexcept>
57
#include <string>
@@ -23,8 +25,6 @@ class object {
2325
void setArrayIfNull();
2426
void setObjectIfNull();
2527

26-
static type getType(value &data);
27-
2828
public:
2929
object();
3030
object(value &data);
@@ -40,6 +40,8 @@ class object {
4040
bool isArray() const;
4141
bool isObject() const;
4242

43+
static type getType(value &data);
44+
4345
// For arrays
4446
void push_back(value &data);
4547
void push_back(value data);
@@ -55,6 +57,10 @@ class object {
5557

5658
// For arrays and objects
5759
size_t size();
60+
61+
// Overall
62+
std::string dump(int indent = 0, int baseline = 0);
63+
void dumps(const std::string &filePath, int indent = 0);
5864
};
5965

6066
} // namespace json

‎src/object.cpp‎

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ bool json::object::isString() const { return this->_type == type::String; }
2424
bool json::object::isArray() const { return this->_type == type::Array; }
2525
bool 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+
2735
void 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+
157226
void json::object::setObjectIfNull() {
158227
if (this->isNull()) {
159228
this->_type = type::Object;

0 commit comments

Comments
 (0)