[JSON] Allow emitting comments in json::OStream
This isn't standard JSON, but is a popular extension.
It will be used to show errors in context, rendering pseudo-json for humans.
Differential Revision: https://reviews.llvm.org/D88103
diff --git a/llvm/unittests/Support/JSONTest.cpp b/llvm/unittests/Support/JSONTest.cpp
index 73fc626..986cf5e 100644
--- a/llvm/unittests/Support/JSONTest.cpp
+++ b/llvm/unittests/Support/JSONTest.cpp
@@ -420,15 +420,19 @@
std::string S;
llvm::raw_string_ostream OS(S);
OStream J(OS, Indent);
+ J.comment("top*/level");
J.object([&] {
J.attributeArray("foo", [&] {
J.value(nullptr);
+ J.comment("element");
J.value(42.5);
J.arrayBegin();
J.value(43);
J.arrayEnd();
});
+ J.comment("attribute");
J.attributeBegin("bar");
+ J.comment("attribute value");
J.objectBegin();
J.objectEnd();
J.attributeEnd();
@@ -437,17 +441,21 @@
return OS.str();
};
- const char *Plain = R"({"foo":[null,42.5,[43]],"bar":{},"baz":"xyz"})";
+ const char *Plain =
+ R"(/*top* /level*/{"foo":[null,/*element*/42.5,[43]],/*attribute*/"bar":/*attribute value*/{},"baz":"xyz"})";
EXPECT_EQ(Plain, StreamStuff(0));
- const char *Pretty = R"({
+ const char *Pretty = R"(/* top* /level */
+{
"foo": [
null,
+ /* element */
42.5,
[
43
]
],
- "bar": {},
+ /* attribute */
+ "bar": /* attribute value */ {},
"baz": "xyz"
})";
EXPECT_EQ(Pretty, StreamStuff(2));