Merge pull request #494 from bamkrs/alwaysIncludeEmptyCollectionElements

Added `alwaysIncludeEmptyCollectionElements` to `Serializer`
This commit is contained in:
Benedikt-Alexander Mokroß 2021-10-19 13:42:50 +02:00 committed by GitHub
commit 79e7a73043
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -74,6 +74,11 @@ public:
*/
bool alwaysIncludeRequired = false;
/**
* Always include array or map elements, even if their value is `nullptr`.
*/
bool alwaysIncludeNullCollectionElements = false;
/**
* If `true` - insert string `"<unknown-type>"` in json field value in case unknown field found.
* Fail if `false`.
@ -136,7 +141,7 @@ private:
bool first = true;
for(auto& value : *list) {
if(value || serializer->getConfig()->includeNullFields) {
if(value || serializer->getConfig()->includeNullFields || serializer->getConfig()->alwaysIncludeNullCollectionElements) {
(first) ? first = false : stream->writeSimple(",", 1);
serializer->serialize(stream, value);
}
@ -161,7 +166,7 @@ private:
for(auto& pair : *map) {
const auto& value = pair.second;
if(value || serializer->getConfig()->includeNullFields) {
if(value || serializer->getConfig()->includeNullFields || serializer->getConfig()->alwaysIncludeNullCollectionElements) {
(first) ? first = false : stream->writeSimple(",", 1);
const auto& key = pair.first;
serializeString(stream, key->getData(), key->getSize());