mirror of
https://gitee.com/zyjblog/oatpp.git
synced 2024-12-22 22:16:37 +08:00
Add option to include required fields in JSON even if value == nullptr
and includeNullFields == false
.
This commit is contained in:
parent
c80047b6bd
commit
cf9530ced5
@ -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);
|
||||
|
@ -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`.
|
||||
|
Loading…
Reference in New Issue
Block a user