Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 71 additions & 22 deletions src/support/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,79 @@

namespace json {

void Value::stringify(std::ostream& os, bool pretty) {
if (isString()) {
std::stringstream wtf16;
[[maybe_unused]] bool valid =
wasm::String::convertWTF8ToWTF16(wtf16, getIString().view());
assert(valid);
// TODO: Use wtf16.view() once we have C++20.
wasm::String::printEscapedJSON(os, wtf16.str());
} else if (isArray()) {
os << '[';
auto first = true;
for (auto& item : getArray()) {
if (first) {
first = false;
} else {
// TODO pretty whitespace
os << ',';
void Value::stringify(std::ostream& os, bool pretty, int indent) {
auto doIndent = [&]() {
for (int i = 0; i < indent; i++) {
os << ' ';
}
};

auto maybeNewline = [&]() {
if (pretty) {
os << '\n';
doIndent();
}
};

switch (type) {
case String: {
std::stringstream wtf16;
[[maybe_unused]] bool valid =
wasm::String::convertWTF8ToWTF16(wtf16, getIString().view());
assert(valid);
wasm::String::printEscapedJSON(os, wtf16.view());
return;
}
case Array: {
os << '[';
indent++;
auto first = true;
for (auto& item : getArray()) {
if (first) {
first = false;
} else {
os << ',';
}
maybeNewline();
item->stringify(os, pretty, indent);
}
indent--;
maybeNewline();
os << ']';
return;
}
case Object: {
os << '{';
indent++;
auto first = true;
for (auto& [key, value] : getObject()) {
if (first) {
first = false;
} else {
os << ',';
}
maybeNewline();
os << "\"" << key << "\":";
if (pretty) {
os << ' ';
}
value->stringify(os, pretty, indent);
}
item->stringify(os, pretty);
indent--;
maybeNewline();
os << '}';
return;
}
os << ']';
} else {
WASM_UNREACHABLE("TODO: stringify all of JSON");
}
case Number:
os << getNumber();
return;
case Null:
os << "null";
return;
case Bool:
os << (getBool() ? "true" : "false");
return;
};
}

} // namespace json
24 changes: 22 additions & 2 deletions src/support/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <unordered_set>
#include <vector>

#include "support/insert_ordered.h"
#include "support/istring.h"
#include "support/safe_integer.h"
#include "support/string.h"
Expand Down Expand Up @@ -67,7 +68,18 @@ struct Value {
Ref& operator[](IString x) { return (*this->get())[x]; }
};

static Ref make() { return Ref(new Value); }
template<typename T> static Ref make(T t) { return Ref(new Value(t)); }
static Ref makeArray() {
Ref ret(new Value);
ret->setArray();
return ret;
}
static Ref makeObject() {
Ref ret(new Value);
ret->setObject();
return ret;
}

enum Type {
String = 0,
Expand All @@ -81,7 +93,7 @@ struct Value {
Type type = Null;

using ArrayStorage = std::vector<Ref>;
using ObjectStorage = std::unordered_map<IString, Ref>;
using ObjectStorage = wasm::InsertOrderedMap<IString, Ref>;

// MSVC does not allow unrestricted unions:
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2544.pdf
Expand All @@ -102,6 +114,10 @@ struct Value {
// constructors all copy their input
Value() {}
explicit Value(const char* s) : type(Null) { setString(s); }
explicit Value(const std::string& s) : type(Null) { setString(s.c_str()); }
explicit Value(const std::string_view& s) : type(Null) {
setString(std::string(s));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
setString(std::string(s));
setString(s.data());

This seems like it does the same thing a little more directly.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, but the data is not null-terminated?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right. Never mind then.

}
explicit Value(double n) : type(Null) { setNumber(n); }
explicit Value(ArrayStorage& a) : type(Null) {
setArray();
Expand Down Expand Up @@ -202,6 +218,10 @@ struct Value {
assert(isArray());
return *arr;
}
ObjectStorage& getObject() {
assert(isObject());
return *obj;
}
bool& getBool() {
assert(isBool());
return boo;
Expand Down Expand Up @@ -378,7 +398,7 @@ struct Value {
return curr;
}

void stringify(std::ostream& os, bool pretty = false);
void stringify(std::ostream& os, bool pretty = false, int indent = 0);

// String operations

Expand Down
57 changes: 56 additions & 1 deletion test/gtest/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using JSONTest = ::testing::Test;

TEST_F(JSONTest, Stringify) {
TEST_F(JSONTest, RoundtripString) {
// TODO: change the API to not require a copy
auto input = "[\"hello\",\"world\"]";
auto* copy = strdup(input);
Expand All @@ -14,3 +14,58 @@ TEST_F(JSONTest, Stringify) {
EXPECT_EQ(ss.str(), input);
free(copy);
}

static void
checkOutput(json::Value::Ref ref, std::string expected, bool pretty = false) {
std::stringstream ss;
ref->stringify(ss, pretty);
EXPECT_EQ(ss.str(), expected);
}

static void checkPrettyOutput(json::Value::Ref ref, std::string expected) {
checkOutput(ref, expected, true);
}

TEST_F(JSONTest, StringifyArray) {
auto array = json::Value::makeArray();
array->push_back(json::Value::make(42));
array->push_back(json::Value::make("1337"));
array->push_back(json::Value::make()); // null
checkOutput(array, "[42,\"1337\",null]");
checkPrettyOutput(array, R"([
42,
"1337",
null
])");
}

TEST_F(JSONTest, StringifyObject) {
auto object = json::Value::makeObject();
object["foo"] = json::Value::make(42);
object["bar"] = json::Value::make("1337");
checkOutput(object, "{\"foo\":42,\"bar\":\"1337\"}");
checkPrettyOutput(object, R"({
"foo": 42,
"bar": "1337"
})");
}

TEST_F(JSONTest, StringifyNesting) {
auto array = json::Value::makeArray();
auto object = json::Value::makeObject();
auto array1 = json::Value::makeArray();
auto object1 = json::Value::makeObject();
array->push_back(object);
object["body"] = array1;
array1->push_back(object1);
object1["value"] = json::Value::make(42);
checkPrettyOutput(array, R"([
{
"body": [
{
"value": 42
}
]
}
])");
}
Loading