Personally, I would prefer a recording option that allows you to filter out empty / null properties when recording. Thus, you can define your own class, for example, class MyFastWriter : public FastWriter , override printValue to process objectValue and call FastWriter::writeValue for the rest. Unfortunately, the JsonCpp API has defined the printValue member printValue as confidential, so you cannot override it (or even call it) from a custom derived class.
Therefore, I see only three main ways to achieve what you want: (1) Adaptation of the json value before writing, (2) defining your own writer class and copying a lot of code from FastWriter or (3) change the source code of FastWriter .
There is already a correct answer for option (1) provided by Jarod42.
Option (2) and (3) share the main flaw that you copy or modify implementation details that may change in future versions of JsonCpp; But still, if someone knows about the shortcomings that arise when changing or copying the source code of the library, this may be an option. The situation may be that the json value at hand should contain empty properties, it is very large and should be written quite often; it becomes inconvenient to copy the value, changing it for writing only and writing it again and again.
I'm certainly not a friend changing source code; In any case, see the following adapted version of FastWriter::writeValue , which allows you to get the desired result:
void FastWriter::writeValue(const Value& value) { switch (value.type()) { // cases handling the other value.types remain as is... ... // case handling objectValue is adapted: case objectValue: { Value::Members members(value.getMemberNames()); document_ += '{'; // inserted flag indicating that the first element is to be written: bool isFirst = true; for (Value::Members::iterator it = members.begin(); it != members.end(); ++it) { const std::string& name = *it; // inserted to skip empty/null property values if(value[name].empty() || value[name].isNull()) continue; // Replaced: necessary because the first written entry is not necessarily members.begin: // if (it != members.begin()) // document_ += ','; if (!isFirst) document_ += ','; else isFirst = false; // Kept as is... document_ += valueToQuotedStringN(name.data(), static_cast<unsigned>(name.length())); document_ += yamlCompatiblityEnabled_ ? ": " : ":"; writeValue(value[name]); } document_ += '}'; } break; } }
source share