Add option to include required fields in JSON even if value == nullptr and includeNullFields == false.

This commit is contained in:
Benedikt-Alexander Mokroß 2021-10-14 11:05:35 +02:00
parent c80047b6bd
commit cf9530ced5
2 changed files with 8 additions and 1 deletions

View File

@ -155,11 +155,12 @@ void Serializer::serializeObject(Serializer* serializer,
auto dispatcher = static_cast<const oatpp::data::mapping::type::__class::AbstractObject::PolymorphicDispatcher*>(polymorph.valueType->polymorphicDispatcher);
auto fields = dispatcher->getProperties()->getList();
auto object = static_cast<oatpp::BaseObject*>(polymorph.get());
auto config = serializer->getConfig();
for (auto const& field : fields) {
auto value = field->get(object);
if(value || serializer->getConfig()->includeNullFields) {
if (value || config->includeNullFields || (field->info.required && config->honorRequired)) {
(first) ? first = false : stream->writeSimple(",", 1);
serializeString(stream, (p_char8)field->name, std::strlen(field->name));
stream->writeSimple(":", 1);

View File

@ -65,9 +65,15 @@ public:
/**
* Include fields with value == nullptr into serialized json.
* Field will still be included when field-info `required` is set to true and &id:honorRequired is set to true.
*/
bool includeNullFields = true;
/**
* Honor required fields in DTO_FIELD_INFO and include required fields even if they are `value == nullptr`
*/
bool honorRequired = false;
/**
* If `true` - insert string `"<unknown-type>"` in json field value in case unknown field found.
* Fail if `false`.