enableYAMLCompatibility and dropNullPlaceholders for StreamWriterBuilder
authorChristopher Dunn <cdunn2001@gmail.com>
Wed, 11 Feb 2015 03:15:43 +0000 (21:15 -0600)
committerChristopher Dunn <cdunn2001@gmail.com>
Wed, 11 Feb 2015 03:28:13 +0000 (21:28 -0600)
include/json/writer.h
src/lib_json/json_writer.cpp

index 2b081a7..4951ff4 100644 (file)
@@ -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":  "<anything>"
+    - "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
index 27d5f56..372e945 100644 (file)
@@ -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<std::string>* 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]
 }