diff --git a/doomsday/sdk/libcore/src/data/json.cpp b/doomsday/sdk/libcore/src/data/json.cpp index 4c797299f2..3a2190c7ed 100644 --- a/doomsday/sdk/libcore/src/data/json.cpp +++ b/doomsday/sdk/libcore/src/data/json.cpp @@ -307,6 +307,14 @@ class JSONParser //--------------------------------------------------------------------------------------- static Block recordToJSON(Record const &rec); +static Block valueToJSON(Value const &value); + +static Block valueToJSONWithTabNewlines(Value const &value) +{ + Block json = valueToJSON(value); + json.replace('\n', "\n\t"); + return json; +} static Block valueToJSON(Value const &value) { @@ -328,9 +336,8 @@ static Block valueToJSON(Value const &value) { out += ","; } - Block value = valueToJSON(*i->second); - value.replace('\n', "\n\t"); - out += "\n\t" + valueToJSON(*i->first.value) + ": " + value; + out += "\n\t" + valueToJSON(*i->first.value) + ": " + + valueToJSONWithTabNewlines(*i->second); } return out + "\n}"; } @@ -344,9 +351,7 @@ static Block valueToJSON(Value const &value) { out += ","; } - Block value = valueToJSON(**i); - value.replace('\n', "\n\t"); - out += "\n\t" + value; + out += "\n\t" + valueToJSONWithTabNewlines(**i); } return out + "\n]"; } @@ -376,10 +381,7 @@ static Block recordToJSON(Record const &rec) Block out = "{\n\t\"__obj__\": \"Record\""; rec.forMembers([&out] (String const &name, Variable const &var) { - out += ","; - Block value = valueToJSON(var.value()); - value.replace('\n', "\n\t"); - out += "\n\t\"" + name.toUtf8() + "\": " + value; + out += ",\n\t\"" + name.toUtf8() + "\": " + valueToJSONWithTabNewlines(var.value()); return LoopContinue; }); return out + "\n}"; diff --git a/doomsday/tests/test_record/main.cpp b/doomsday/tests/test_record/main.cpp index 8134c34f44..1919308414 100644 --- a/doomsday/tests/test_record/main.cpp +++ b/doomsday/tests/test_record/main.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -47,6 +48,8 @@ int main(int argc, char **argv) rec.add(new Variable("size", new NumberValue(1024))); LOG_MSG("With two variables:\n") << rec; + LOG_MSG("Record as JSON:\n") << composeJSON(rec).constData(); + Record rec2; Block b; Writer(b) << rec; @@ -72,6 +75,8 @@ int main(int argc, char **argv) Record copied = before; DENG2_ASSERT(copied.hasSubrecord("subrecord")); LOG_MSG("Copied:\n") << copied; + + LOG_MSG("...and as JSON:\n") << composeJSON(copied).constData(); } catch (Error const &err) {