From 43b940482721a53bbc780b7f294dfb99f8bc9319 Mon Sep 17 00:00:00 2001 From: Lars Toenning Date: Fri, 9 Feb 2024 14:05:36 +0100 Subject: [PATCH] feat: Add RequiredForJson JSON --- src/blackmisc/metaclass.h | 4 +++- src/blackmisc/mixin/mixinjson.h | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/blackmisc/metaclass.h b/src/blackmisc/metaclass.h index c831a8ce8..d5f663763 100644 --- a/src/blackmisc/metaclass.h +++ b/src/blackmisc/metaclass.h @@ -209,7 +209,8 @@ namespace BlackMisc DisabledForHashing = 1 << 3, //!< Element will be ignored by qHash() DisabledForJson = 1 << 4, //!< Element will be ignored during JSON serialization CaseInsensitiveComparison = 1 << 5, //!< Element will be compared case insensitively (must be a QString) - LosslessMarshalling = 1 << 6 //!< Element marshalling will preserve data at the expense of size + LosslessMarshalling = 1 << 6, //!< Element marshalling will preserve data at the expense of size + RequiredForJson = 1 << 7 //!< Element is required when parsing from JSON }; /*! @@ -229,6 +230,7 @@ namespace BlackMisc constexpr static MetaFlags DisabledForJson {}; constexpr static MetaFlags CaseInsensitiveComparison {}; constexpr static MetaFlags LosslessMarshalling {}; + constexpr static MetaFlags RequiredForJson {}; //! @} //! Return a CMetaMemberList of type deduced from the types of the meta members. diff --git a/src/blackmisc/mixin/mixinjson.h b/src/blackmisc/mixin/mixinjson.h index 2a6540b7a..483fb1e6b 100644 --- a/src/blackmisc/mixin/mixinjson.h +++ b/src/blackmisc/mixin/mixinjson.h @@ -155,7 +155,7 @@ namespace BlackMisc const auto value = json.value(CExplicitLatin1String(member.latin1Name())); if (value.isUndefined()) { - constexpr bool required = false; //! \fixme add RequiredForJson flag in metaclass system + constexpr bool required = decltype(member)::has(MetaFlags()); // QLatin1String used instead of QStringLiteral below since the latter causes an internal compiler bug // in GCC 8 and higher if (required) { throw CJsonException(QLatin1String("Missing required member '%1'").arg(member.latin1Name())); } // cppcheck-suppress knownConditionTrueFalse