Skip to content
This repository has been archived by the owner on Mar 26, 2020. It is now read-only.

Commit

Permalink
Store builded object on the stack.
Browse files Browse the repository at this point in the history
For partial building, we need to temporary store the object being build as we
maybe interrupted. This commits change object and array parsing to use the
values stack has a temporary place for storing the object being build.
  • Loading branch information
canatella committed Dec 29, 2016
1 parent 621d48c commit 7ed92d1
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 deletions json11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -808,21 +808,23 @@ struct JsonParserPriv final {
void parse_object() {
assert(states.top() >= VALUE_OBJECT && states.top() <= OBJECT_VALUE);

map<string, Json> data;
values.push(map<string, Json>());

set_state(OBJECT_KEY_OR_END);
char ch = get_next_token();
if (ch == '}') {
pop_state();
return values.push(data);
return;
}

while (1) {
if (need_data)
return;

if (ch != '"')
if (ch != '"') {
values.pop();
return fail("expected '\"' in object, got " + esc(ch));
}

set_state(OBJECT_KEY);
push_state(VALUE_STRING);
Expand All @@ -833,16 +835,20 @@ struct JsonParserPriv final {
string key = values.top().string_value();
values.pop();

if (failed)
if (failed) {
values.pop();
return values.push(Json());
}

set_state(OBJECT_COLON);
ch = get_next_token();
if (need_data)
return;

if (ch != ':')
if (ch != ':') {
values.pop();
return fail("expected ':' in object, got " + esc(ch));
}

set_state(OBJECT_VALUE);
push_state(EXPECT_VALUE);
Expand All @@ -853,25 +859,30 @@ struct JsonParserPriv final {
Json value = values.top();
values.pop();

if (failed)
if (failed) {
values.pop();
return values.push(Json());
}

map<string, Json> data = values.top().object_items();
data[std::move(key)] = value;
values.pop();
values.push(data);

set_state(OBJECT_COMMA_OR_END);
ch = get_next_token();
if (need_data)
return;

if (ch == '}') {
values.push(data);
pop_state();
break;
}

if (ch != ',')
if (ch != ',') {
values.pop();
return fail("expected ',' in object, got " + esc(ch));

}
ch = get_next_token();
}
}
Expand All @@ -883,14 +894,14 @@ struct JsonParserPriv final {
void parse_array() {
assert(states.top() >= VALUE_ARRAY && states.top() <= ARRAY_VALUE);

vector<Json> data;
values.push(vector<Json>());

set_state(ARRAY_VALUE_OR_END);
char ch = get_next_token();

if (ch == ']') {
pop_state();
return values.push(data);
return;
}

while (1) {
Expand All @@ -909,23 +920,30 @@ struct JsonParserPriv final {
Json value = values.top();
values.pop();

if (failed)
if (failed) {
values.pop();
return values.push(Json());
}

vector<Json> data = values.top().array_items();
data.push_back(value);
values.pop();
values.push(data);

set_state(ARRAY_COMMA_OR_END);
ch = get_next_token();
if (need_data)
return;

if (ch == ']') {
values.push(data);
pop_state();
break;
}

if (ch != ',')
if (ch != ',') {
values.pop();
return fail("expected ',' in list, got " + esc(ch));
}

ch = get_next_token();
(void)ch;
Expand Down

0 comments on commit 7ed92d1

Please sign in to comment.