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.
This commit is contained in:
Aaron Jacobs
2012-03-12 04:53:57 +00:00
parent f572e8e42e
commit ae3c7a7aab
3 changed files with 36 additions and 2 deletions

View File

@@ -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() );

View File

@@ -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 );
}