Permalink
Browse files

json: Fix formatting of nested arrays/objects.

  • Loading branch information...
unknownbrackets committed Apr 15, 2018
1 parent 03cc4ea commit b658130924437b1d0f899487509c7dca2a915511
Showing with 17 additions and 2 deletions.
  1. +15 −2 ext/native/json/json_writer.cpp
  2. +2 −0 ext/native/json/json_writer.h
@@ -75,13 +75,26 @@ const char *JsonWriter::arrayComma() const {
}
}
void JsonWriter::pushDict() {
str_ << arrayComma() << arrayIndent() << "{";
stack_.back().first = false;
stack_.push_back(StackEntry(DICT));
}
void JsonWriter::pushDict(const char *name) {
str_ << comma() << indent() << "\"";
writeEscapedString(name);
str_ << "\": {";
stack_.back().first = false;
stack_.push_back(StackEntry(DICT));
}
void JsonWriter::pushArray() {
str_ << arrayComma() << arrayIndent() << "[";
stack_.back().first = false;
stack_.push_back(StackEntry(ARRAY));
}
void JsonWriter::pushArray(const char *name) {
str_ << comma() << indent() << "\"";
writeEscapedString(name);
@@ -300,14 +313,14 @@ static void json_stringify_array(JsonWriter &writer, const json_value *value) {
break;
case JSON_OBJECT:
writer.begin();
writer.pushDict();
for (const json_value *it = value->first_child; it; it = it->next_sibling) {
json_stringify_object(writer, it);
}
writer.pop();
break;
case JSON_ARRAY:
writer.beginArray();
writer.pushArray();
for (const json_value *it = value->first_child; it; it = it->next_sibling) {
json_stringify_array(writer, it);
}
@@ -21,7 +21,9 @@ class JsonWriter {
void beginArray();
void beginRaw();
void end();
void pushDict();
void pushDict(const char *name);
void pushArray();
void pushArray(const char *name);
void pop();
void writeBool(bool value);

0 comments on commit b658130

Please sign in to comment.