Skip to content

Commit c0b2a42

Browse files
committed
remove some parsing bugs
1 parent defbfe8 commit c0b2a42

File tree

4 files changed

+44
-11
lines changed

4 files changed

+44
-11
lines changed

include/json/object.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ namespace json {
1313

1414
class object {
1515
private:
16-
using value = std::variant<std::nullptr_t, bool, int, double, std::string>;
16+
using value =
17+
std::variant<std::nullptr_t, bool, long long int, double, std::string>;
1718
using all_values =
18-
std::variant<std::nullptr_t, bool, int, double, std::string,
19+
std::variant<std::nullptr_t, bool, long long int, double, std::string,
1920
std::vector<object>, std::map<std::string, object>>;
2021

2122
type _type;
@@ -44,6 +45,7 @@ class object {
4445
object &operator=(std::nullptr_t data);
4546
object &operator=(bool data);
4647
object &operator=(int data);
48+
object &operator=(long long int data);
4749
object &operator=(double data);
4850
object &operator=(const char *data);
4951
object &operator=(std::vector<object> data);

include/json/parse.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class parser {
2222
object parseMap();
2323

2424
void skipWhitespace();
25+
char getPrevChar();
2526
char getCurrChar();
2627
char getNextChar();
2728

src/object.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ json::object::object(all_values data) {
1111
else if (data.index() == 1)
1212
*this = std::get<bool>(data);
1313
else if (data.index() == 2)
14-
*this = std::get<int>(data);
14+
*this = std::get<long long int>(data);
1515
else if (data.index() == 3)
1616
*this = std::get<double>(data);
1717
else if (data.index() == 4)
@@ -43,6 +43,13 @@ json::object &json::object::operator=(bool data) {
4343
return *this;
4444
}
4545
json::object &json::object::operator=(int data) {
46+
this->_value = static_cast<long long int>(data);
47+
this->_type.set(type::integer);
48+
this->clearArray();
49+
this->clearMap();
50+
return *this;
51+
}
52+
json::object &json::object::operator=(long long int data) {
4653
this->_value = data;
4754
this->_type.set(type::integer);
4855
this->clearArray();
@@ -196,7 +203,7 @@ std::string json::object::dumps(size_t indent, size_t baseIndent) {
196203
else
197204
return "false";
198205
} else if (this->_type.get() == type::integer) {
199-
return std::to_string(std::get<int>(this->_value));
206+
return std::to_string(std::get<long long int>(this->_value));
200207
} else if (this->_type.get() == type::number) {
201208
return std::to_string(std::get<double>(this->_value));
202209
} else if (this->_type.get() == type::string) {

src/parse.cpp

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,27 @@ json::object json::parser::parseNumber() {
6969
}
7070

7171
if (isInt)
72-
return object(std::stoi(number));
72+
return object(std::stoll(number));
7373
return object(std::stod(number));
7474
}
7575
json::object json::parser::parseString() {
7676
std::string value = "";
77-
value += this->getNextChar();
78-
char nextChar;
77+
char nextChar = this->getNextChar();
7978

80-
while ((nextChar = this->getNextChar()) != '"')
79+
if (nextChar != '"') {
8180
value += nextChar;
82-
this->getNextChar();
8381

82+
while (true) {
83+
nextChar = this->getNextChar();
84+
if (nextChar == '"') {
85+
if (this->getPrevChar() != '\\')
86+
break;
87+
}
88+
value += nextChar;
89+
}
90+
}
91+
92+
this->getNextChar();
8493
return object(value);
8594
}
8695
json::object json::parser::parseArray() {
@@ -114,14 +123,22 @@ json::object json::parser::parseMap() {
114123
while (true) {
115124
this->getNextChar();
116125
this->skipWhitespace();
117-
if (this->getCurrChar() == '}')
126+
if (this->getCurrChar() == '}') {
127+
this->getNextChar();
118128
break;
129+
}
119130

120131
if (this->getCurrChar() == '"') {
121132
std::string key = "";
122133
char nextChar;
123-
while ((nextChar = this->getNextChar()) != '"')
134+
while (true) {
135+
nextChar = this->getNextChar();
136+
if (nextChar == '"') {
137+
if (this->getPrevChar() != '\\')
138+
break;
139+
}
124140
key += nextChar;
141+
}
125142

126143
this->getNextChar();
127144
this->skipWhitespace();
@@ -154,6 +171,12 @@ void json::parser::skipWhitespace() {
154171
}
155172
return;
156173
}
174+
char json::parser::getPrevChar() {
175+
if (this->index > 0) {
176+
return this->input[this->index - 1];
177+
}
178+
return '\0';
179+
}
157180
char json::parser::getCurrChar() {
158181
if (this->index < this->input.size()) {
159182
return this->input[this->index];

0 commit comments

Comments
 (0)