Skip to content

Commit 5a74470

Browse files
committed
enableYAMLCompatibility and dropNullPlaceholders for StreamWriterBuilder
1 parent 07f0e93 commit 5a74470

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

include/json/writer.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,15 @@ class JSON_API StreamWriterBuilder : public StreamWriter::Factory {
9090
// without a major version bump.
9191
/** Configuration of this builder.
9292
Available settings (case-sensitive):
93-
- "commentStyle": "None", "Some", or "All"
93+
- "commentStyle": "None" or "All"
9494
- "indentation": "<anything>"
95+
- "enableYAMLCompatibility": False or True
96+
- slightly change the whitespace around colons
97+
- "dropNullPlaceholders": False or True
98+
- Drop the "null" string from the writer's output for nullValues.
99+
Strictly speaking, this is not valid JSON. But when the output is being
100+
fed to a browser's Javascript, it makes for smaller output and the
101+
browser can handle the output just fine.
95102
96103
You can examine 'settings_` yourself
97104
to see the defaults. You can also write and read them just like any

src/lib_json/json_writer.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,8 @@ StreamWriter* StreamWriterBuilder::newStreamWriter() const
973973

974974
std::string indentation = settings_["indentation"].asString();
975975
std::string cs_str = settings_["commentStyle"].asString();
976+
bool eyc = settings_["enableYAMLCompatibility"].asBool();
977+
bool dnp = settings_["dropNullPlaceholders"].asBool();
976978
CommentStyle::Enum cs = CommentStyle::All;
977979
if (cs_str == "All") {
978980
cs = CommentStyle::All;
@@ -982,10 +984,15 @@ StreamWriter* StreamWriterBuilder::newStreamWriter() const
982984
return NULL;
983985
}
984986
std::string colonSymbol = " : ";
985-
if (indentation.empty()) {
987+
if (eyc) {
988+
colonSymbol = ": ";
989+
} else if (indentation.empty()) {
986990
colonSymbol = ":";
987991
}
988992
std::string nullSymbol = "null";
993+
if (dnp) {
994+
nullSymbol = "";
995+
}
989996
std::string endingLineFeedSymbol = "";
990997
return new BuiltStyledStreamWriter(
991998
indentation, cs,
@@ -996,6 +1003,8 @@ static void getValidWriterKeys(std::set<std::string>* valid_keys)
9961003
valid_keys->clear();
9971004
valid_keys->insert("indentation");
9981005
valid_keys->insert("commentStyle");
1006+
valid_keys->insert("enableYAMLCompatibility");
1007+
valid_keys->insert("dropNullPlaceholders");
9991008
}
10001009
bool StreamWriterBuilder::validate(Json::Value* invalid) const
10011010
{
@@ -1021,6 +1030,8 @@ void StreamWriterBuilder::setDefaults(Json::Value* settings)
10211030
//! [StreamWriterBuilderDefaults]
10221031
(*settings)["commentStyle"] = "All";
10231032
(*settings)["indentation"] = "\t";
1033+
(*settings)["enableYAMLCompatibility"] = false;
1034+
(*settings)["dropNullPlaceholders"] = false;
10241035
//! [StreamWriterBuilderDefaults]
10251036
}
10261037

0 commit comments

Comments
 (0)