Skip to content

Commit 9c47772

Browse files
author
Jadit19
committed
add elementary implementation of json
1 parent 28c5918 commit 9c47772

File tree

4 files changed

+252
-5
lines changed

4 files changed

+252
-5
lines changed

example/main.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,17 @@
1-
int main(int argc, char **argv) { return 0; }
1+
#include <iostream>
2+
3+
#include <json/object.h>
4+
5+
int main(int argc, char **argv) {
6+
json::object person;
7+
person.insert("name", "Adit");
8+
person.insert("age", 20);
9+
10+
json::object launguages;
11+
launguages.push_back("English");
12+
launguages.push_back("Hindi");
13+
launguages.push_back("French");
14+
person.insert("launguages", launguages);
15+
16+
return 0;
17+
}

include/json/object.h

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,60 @@
11
#pragma once
22

33
#include <map>
4+
#include <stdexcept>
45
#include <string>
6+
#include <utility>
57
#include <variant>
68
#include <vector>
79

10+
#include <json/type.h>
11+
812
namespace json {
913

1014
class object {
11-
public:
15+
private:
1216
using value =
1317
std::variant<std::nullptr_t, bool, int, double, std::string,
1418
std::vector<object>, std::map<std::string, object>>;
1519

20+
type _type;
21+
value _value;
22+
23+
void setArrayIfNull();
24+
void setObjectIfNull();
25+
26+
static type getType(value &data);
27+
28+
public:
1629
object();
30+
object(value &data);
1731
~object();
1832

19-
private:
20-
value data;
33+
void operator=(value &data);
34+
35+
bool isNull() const;
36+
bool isBoolean() const;
37+
bool isInteger() const;
38+
bool isDouble() const;
39+
bool isString() const;
40+
bool isArray() const;
41+
bool isObject() const;
42+
43+
// For arrays
44+
void push_back(value &data);
45+
void push_back(value data);
46+
void push_back(object &data);
47+
value &operator[](size_t index);
48+
49+
// For objects
50+
void insert(const std::string &itemKey, value &itemValue);
51+
void insert(const std::string &itemKey, value itemValue);
52+
void insert(const std::string &itemKey, object &itemValue);
53+
bool contains(const std::string &itemKey);
54+
value &operator[](const std::string &itemKey);
55+
56+
// For arrays and objects
57+
size_t size();
2158
};
2259

2360
} // namespace json

include/json/type.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
3+
namespace json {
4+
5+
enum type {
6+
Null = 0,
7+
Boolean = 1,
8+
Integer = 2,
9+
Double = 3,
10+
String = 4,
11+
Array = 5,
12+
Object = 6
13+
};
14+
15+
}

src/object.cpp

Lines changed: 180 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,184 @@
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

511
json::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

Comments
 (0)