Skip to content

Commit ae3c7a7

Browse files
committed
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.
1 parent f572e8e commit ae3c7a7

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

include/json/writer.h

+8
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ namespace Json {
4040

4141
void enableYAMLCompatibility();
4242

43+
/** \brief Drop the "null" string from the writer's output for nullValues.
44+
* Strictly speaking, this is not valid JSON. But when the output is being
45+
* fed to a browser's Javascript, it makes for smaller output and the
46+
* browser can handle the output just fine.
47+
*/
48+
void dropNullPlaceholders();
49+
4350
public: // overridden from Writer
4451
virtual std::string write( const Value &root );
4552

@@ -48,6 +55,7 @@ namespace Json {
4855

4956
std::string document_;
5057
bool yamlCompatiblityEnabled_;
58+
bool dropNullPlaceholders_;
5159
};
5260

5361
/** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a human friendly way.

src/lib_json/json_writer.cpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,8 @@ Writer::~Writer()
192192
// //////////////////////////////////////////////////////////////////
193193

194194
FastWriter::FastWriter()
195-
: yamlCompatiblityEnabled_( false )
195+
: yamlCompatiblityEnabled_( false ),
196+
dropNullPlaceholders_( false )
196197
{
197198
}
198199

@@ -204,6 +205,13 @@ FastWriter::enableYAMLCompatibility()
204205
}
205206

206207

208+
void
209+
FastWriter::dropNullPlaceholders()
210+
{
211+
dropNullPlaceholders_ = true;
212+
}
213+
214+
207215
std::string
208216
FastWriter::write( const Value &root )
209217
{
@@ -220,7 +228,7 @@ FastWriter::writeValue( const Value &value )
220228
switch ( value.type() )
221229
{
222230
case nullValue:
223-
document_ += "null";
231+
if (!dropNullPlaceholders_) document_ += "null";
224232
break;
225233
case intValue:
226234
document_ += valueToString( value.asLargestInt() );

src/test_lib_json/main.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -1399,6 +1399,23 @@ ValueTest::checkIsEqual( const Json::Value &x, const Json::Value &y )
13991399
JSONTEST_ASSERT( y.compare( x ) == 0 );
14001400
}
14011401

1402+
1403+
struct WriterTest : JsonTest::TestCase
1404+
{
1405+
};
1406+
1407+
1408+
JSONTEST_FIXTURE( WriterTest, dropNullPlaceholders )
1409+
{
1410+
Json::FastWriter writer;
1411+
Json::Value nullValue;
1412+
JSONTEST_ASSERT( writer.write(nullValue) == "null\n" );
1413+
1414+
writer.dropNullPlaceholders();
1415+
JSONTEST_ASSERT( writer.write(nullValue) == "\n" );
1416+
}
1417+
1418+
14021419
int main( int argc, const char *argv[] )
14031420
{
14041421
JsonTest::Runner runner;
@@ -1420,5 +1437,6 @@ int main( int argc, const char *argv[] )
14201437
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareArray );
14211438
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareObject );
14221439
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareType );
1440+
JSONTEST_REGISTER_FIXTURE( runner, WriterTest, dropNullPlaceholders );
14231441
return runner.runCommandLine( argc, argv );
14241442
}

0 commit comments

Comments
 (0)