From ae3c7a7aabfb345968a65001ea9bc25f1e2bb405 Mon Sep 17 00:00:00 2001 From: Aaron Jacobs Date: Mon, 12 Mar 2012 04:53:57 +0000 Subject: [PATCH] Made it possible to drop null placeholders from array output. This can be used when it's clear that the consumer is able to deal with this, as web browsers are. Thanks to Yatin Chawathe for the patch. --- include/json/writer.h | 8 ++++++++ src/lib_json/json_writer.cpp | 12 ++++++++++-- src/test_lib_json/main.cpp | 18 ++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/include/json/writer.h b/include/json/writer.h index 38d41e1..46d5ccc 100644 --- a/include/json/writer.h +++ b/include/json/writer.h @@ -40,6 +40,13 @@ namespace Json { void enableYAMLCompatibility(); + /** \brief 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. + */ + void dropNullPlaceholders(); + public: // overridden from Writer virtual std::string write( const Value &root ); @@ -48,6 +55,7 @@ namespace Json { std::string document_; bool yamlCompatiblityEnabled_; + bool dropNullPlaceholders_; }; /** \brief Writes a Value in JSON format in a human friendly way. diff --git a/src/lib_json/json_writer.cpp b/src/lib_json/json_writer.cpp index b44def3..ccf323a 100644 --- a/src/lib_json/json_writer.cpp +++ b/src/lib_json/json_writer.cpp @@ -192,7 +192,8 @@ Writer::~Writer() // ////////////////////////////////////////////////////////////////// FastWriter::FastWriter() - : yamlCompatiblityEnabled_( false ) + : yamlCompatiblityEnabled_( false ), + dropNullPlaceholders_( false ) { } @@ -204,6 +205,13 @@ FastWriter::enableYAMLCompatibility() } +void +FastWriter::dropNullPlaceholders() +{ + dropNullPlaceholders_ = true; +} + + std::string FastWriter::write( const Value &root ) { @@ -220,7 +228,7 @@ FastWriter::writeValue( const Value &value ) switch ( value.type() ) { case nullValue: - document_ += "null"; + if (!dropNullPlaceholders_) document_ += "null"; break; case intValue: document_ += valueToString( value.asLargestInt() ); diff --git a/src/test_lib_json/main.cpp b/src/test_lib_json/main.cpp index c6ab619..3e28085 100644 --- a/src/test_lib_json/main.cpp +++ b/src/test_lib_json/main.cpp @@ -1399,6 +1399,23 @@ ValueTest::checkIsEqual( const Json::Value &x, const Json::Value &y ) JSONTEST_ASSERT( y.compare( x ) == 0 ); } + +struct WriterTest : JsonTest::TestCase +{ +}; + + +JSONTEST_FIXTURE( WriterTest, dropNullPlaceholders ) +{ + Json::FastWriter writer; + Json::Value nullValue; + JSONTEST_ASSERT( writer.write(nullValue) == "null\n" ); + + writer.dropNullPlaceholders(); + JSONTEST_ASSERT( writer.write(nullValue) == "\n" ); +} + + int main( int argc, const char *argv[] ) { JsonTest::Runner runner; @@ -1420,5 +1437,6 @@ int main( int argc, const char *argv[] ) JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareArray ); JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareObject ); JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareType ); + JSONTEST_REGISTER_FIXTURE( runner, WriterTest, dropNullPlaceholders ); return runner.runCommandLine( argc, argv ); }