Skip to content

Commit ead8381

Browse files
kripkenradekdoulik
authored andcommitted
JSON: Add simple printing and creation (WebAssembly#6265)
1 parent f3853de commit ead8381

5 files changed

Lines changed: 63 additions & 0 deletions

File tree

src/support/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ set(support_SOURCES
88
dfa_minimization.cpp
99
file.cpp
1010
istring.cpp
11+
json.cpp
1112
path.cpp
1213
safe_integer.cpp
1314
threads.cpp

src/support/json.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2024 WebAssembly Community Group participants
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "support/json.h"
18+
19+
namespace json {
20+
21+
void Value::stringify(std::ostream& os, bool pretty) {
22+
if (isString()) {
23+
// TODO: escaping
24+
os << '"' << getCString() << '"';
25+
} else if (isArray()) {
26+
os << '[';
27+
auto first = true;
28+
for (auto& item : getArray()) {
29+
if (first) {
30+
first = false;
31+
} else {
32+
// TODO pretty whitespace
33+
os << ',';
34+
}
35+
item->stringify(os, pretty);
36+
}
37+
os << ']';
38+
} else {
39+
WASM_UNREACHABLE("TODO: stringify all of JSON");
40+
}
41+
}
42+
43+
} // namespace json

src/support/json.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ struct Value {
5454
Ref& operator[](IString x) { return (*this->get())[x]; }
5555
};
5656

57+
template<typename T> static Ref make(T t) { return Ref(new Value(t)); }
58+
5759
enum Type {
5860
String = 0,
5961
Number = 1,

test/gtest/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ include_directories(../../src/wasm)
44
set(unittest_SOURCES
55
cfg.cpp
66
dfa_minimization.cpp
7+
json.cpp
78
lattices.cpp
89
possible-contents.cpp
910
printing.cpp

test/gtest/json.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "support/json.h"
2+
#include "gtest/gtest.h"
3+
4+
using JSONTest = ::testing::Test;
5+
6+
TEST_F(JSONTest, Stringify) {
7+
// TODO: change the API to not require a copy
8+
auto input = "[\"hello\",\"world\"]";
9+
auto* copy = strdup(input);
10+
json::Value value;
11+
value.parse(copy);
12+
std::stringstream ss;
13+
value.stringify(ss);
14+
EXPECT_EQ(ss.str(), input);
15+
free(copy);
16+
}

0 commit comments

Comments
 (0)