Skip to content

Commit

Permalink
Tests|libcore: Testing the Record-to-JSON conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Nov 7, 2016
1 parent e65ef21 commit ab1e339
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
22 changes: 12 additions & 10 deletions doomsday/sdk/libcore/src/data/json.cpp
Expand Up @@ -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)
{
Expand All @@ -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}";
}
Expand All @@ -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]";
}
Expand Down Expand Up @@ -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}";
Expand Down
5 changes: 5 additions & 0 deletions doomsday/tests/test_record/main.cpp
Expand Up @@ -24,6 +24,7 @@
#include <de/TextValue>
#include <de/NumberValue>
#include <de/Variable>
#include <de/data/json.h>

#include <QDebug>
#include <QTextStream>
Expand All @@ -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;
Expand All @@ -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)
{
Expand Down

0 comments on commit ab1e339

Please sign in to comment.