File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff 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 ,
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ include_directories(../../src/wasm)
44set (unittest_SOURCES
55 cfg.cpp
66 dfa_minimization.cpp
7+ json.cpp
78 lattices.cpp
89 possible-contents.cpp
910 printing.cpp
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments