diff --git a/include/json/writer.h b/include/json/writer.h index 2b081a7..4951ff4 100644 --- a/include/json/writer.h +++ b/include/json/writer.h @@ -90,8 +90,15 @@ public: // without a major version bump. /** Configuration of this builder. Available settings (case-sensitive): - - "commentStyle": "None", "Some", or "All" + - "commentStyle": "None" or "All" - "indentation": "" + - "enableYAMLCompatibility": False or True + - slightly change the whitespace around colons + - "dropNullPlaceholders": False or True + - Drop the "null" string from the writer's output for nullValues. + Strictly speaking, this is not valid JSON. But when the output is being + fed to a browser's Javascript, it makes for smaller output and the + browser can handle the output just fine. You can examine 'settings_` yourself to see the defaults. You can also write and read them just like any diff --git a/src/lib_json/json_writer.cpp b/src/lib_json/json_writer.cpp index 27d5f56..372e945 100644 --- a/src/lib_json/json_writer.cpp +++ b/src/lib_json/json_writer.cpp @@ -973,6 +973,8 @@ StreamWriter* StreamWriterBuilder::newStreamWriter() const std::string indentation = settings_["indentation"].asString(); std::string cs_str = settings_["commentStyle"].asString(); + bool eyc = settings_["enableYAMLCompatibility"].asBool(); + bool dnp = settings_["dropNullPlaceholders"].asBool(); CommentStyle::Enum cs = CommentStyle::All; if (cs_str == "All") { cs = CommentStyle::All; @@ -982,10 +984,15 @@ StreamWriter* StreamWriterBuilder::newStreamWriter() const return NULL; } std::string colonSymbol = " : "; - if (indentation.empty()) { + if (eyc) { + colonSymbol = ": "; + } else if (indentation.empty()) { colonSymbol = ":"; } std::string nullSymbol = "null"; + if (dnp) { + nullSymbol = ""; + } std::string endingLineFeedSymbol = ""; return new BuiltStyledStreamWriter( indentation, cs, @@ -996,6 +1003,8 @@ static void getValidWriterKeys(std::set* valid_keys) valid_keys->clear(); valid_keys->insert("indentation"); valid_keys->insert("commentStyle"); + valid_keys->insert("enableYAMLCompatibility"); + valid_keys->insert("dropNullPlaceholders"); } bool StreamWriterBuilder::validate(Json::Value* invalid) const { @@ -1021,6 +1030,8 @@ void StreamWriterBuilder::setDefaults(Json::Value* settings) //! [StreamWriterBuilderDefaults] (*settings)["commentStyle"] = "All"; (*settings)["indentation"] = "\t"; + (*settings)["enableYAMLCompatibility"] = false; + (*settings)["dropNullPlaceholders"] = false; //! [StreamWriterBuilderDefaults] }