2012-03-24 22:39:19 +00:00
|
|
|
#include "json/json_writer.h"
|
|
|
|
|
|
|
|
JsonWriter::JsonWriter() {
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonWriter::~JsonWriter() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonWriter::begin() {
|
2012-10-30 12:20:55 +00:00
|
|
|
str_ << "{";
|
|
|
|
stack_.push_back(StackEntry(DICT));
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void JsonWriter::end() {
|
2012-10-30 12:20:55 +00:00
|
|
|
pop();
|
|
|
|
str_ << "\n";
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *JsonWriter::indent(int n) const {
|
2012-10-30 12:20:55 +00:00
|
|
|
static const char * const whitespace = " ";
|
|
|
|
return whitespace + (32 - n);
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *JsonWriter::indent() const {
|
2012-10-30 12:20:55 +00:00
|
|
|
int amount = (int)stack_.size() + 1;
|
|
|
|
amount *= 2; // 2-space indent.
|
|
|
|
return indent(amount);
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *JsonWriter::arrayIndent() const {
|
2012-10-30 12:20:55 +00:00
|
|
|
int amount = (int)stack_.size() + 1;
|
|
|
|
amount *= 2; // 2-space indent.
|
|
|
|
return stack_.back().first ? indent(amount) : "";
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *JsonWriter::comma() const {
|
2012-10-30 12:20:55 +00:00
|
|
|
if (stack_.back().first) {
|
|
|
|
return "";
|
|
|
|
} else {
|
|
|
|
return ",";
|
|
|
|
}
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *JsonWriter::arrayComma() const {
|
2012-10-30 12:20:55 +00:00
|
|
|
if (stack_.back().first) {
|
|
|
|
return "\n";
|
|
|
|
} else {
|
|
|
|
return ", ";
|
|
|
|
}
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void JsonWriter::pushDict(const char *name) {
|
2012-10-30 12:20:55 +00:00
|
|
|
str_ << comma() << "\n" << indent() << "\"" << name << "\": {";
|
|
|
|
stack_.push_back(StackEntry(DICT));
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void JsonWriter::pushArray(const char *name) {
|
2012-10-30 12:20:55 +00:00
|
|
|
str_ << comma() << "\n" << indent() << "\"" << name << "\": [";
|
|
|
|
stack_.push_back(StackEntry(ARRAY));
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void JsonWriter::writeBool(bool value) {
|
2012-10-30 12:20:55 +00:00
|
|
|
str_ << arrayComma() << arrayIndent() << (value ? "true" : "false");
|
|
|
|
stack_.back().first = false;
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void JsonWriter::writeBool(const char *name, bool value) {
|
2012-10-30 12:20:55 +00:00
|
|
|
str_ << comma() << "\n" << indent() << "\"" << name << "\": " << (value ? "true" : "false");
|
|
|
|
stack_.back().first = false;
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void JsonWriter::writeInt(int value) {
|
2012-10-30 12:20:55 +00:00
|
|
|
str_ << arrayComma() << arrayIndent() << value;
|
|
|
|
stack_.back().first = false;
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void JsonWriter::writeInt(const char *name, int value) {
|
2012-10-30 12:20:55 +00:00
|
|
|
str_ << comma() << "\n" << indent() << "\"" << name << "\": " << value;
|
|
|
|
stack_.back().first = false;
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void JsonWriter::writeFloat(double value) {
|
2012-10-30 12:20:55 +00:00
|
|
|
str_ << arrayComma() << arrayIndent() << value;
|
|
|
|
stack_.back().first = false;
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void JsonWriter::writeFloat(const char *name, double value) {
|
2012-10-30 12:20:55 +00:00
|
|
|
str_ << comma() << "\n" << indent() << "\"" << name << "\": " << value;
|
|
|
|
stack_.back().first = false;
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void JsonWriter::writeString(const char *value) {
|
2012-10-30 12:20:55 +00:00
|
|
|
str_ << arrayComma() << arrayIndent() << "\"" << value << "\"";
|
|
|
|
stack_.back().first = false;
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void JsonWriter::writeString(const char *name, const char *value) {
|
2012-10-30 12:20:55 +00:00
|
|
|
str_ << comma() << "\n" << indent() << "\"" << name << "\": \"" << value << "\"";
|
|
|
|
stack_.back().first = false;
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void JsonWriter::pop() {
|
2012-10-30 12:20:55 +00:00
|
|
|
BlockType type = stack_.back().type;
|
|
|
|
stack_.pop_back();
|
|
|
|
switch (type) {
|
|
|
|
case ARRAY:
|
|
|
|
str_ << "\n" << indent() << "]";
|
|
|
|
break;
|
|
|
|
case DICT:
|
|
|
|
str_ << "\n" << indent() << "}";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (stack_.size() > 0)
|
|
|
|
stack_.back().first = false;
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|