QSerializer icon indicating copy to clipboard operation
QSerializer copied to clipboard

QSerializer Enhancements and Fixes

Open DHR60 opened this issue 8 months ago • 1 comments

Overview of Updates

This PR introduces several improvements to the QSerializer library, enhancing serialization capabilities and user experience. Main changes include:

  1. Conditional definition support for QJsonDocument mode
  2. Code structure optimization and warning reduction
  3. Enhanced std::optional<T> JSON/XML serialization support
  4. Empty value and null reference skipping functionality

Detailed Changes

Serialization Options Enhancement

  • Implemented comprehensive skipping mechanism for empty values, null values, and null literals
  • Added class-level and member-level serialization option controls
  • Introduced shouldSkipMemberEmpty, shouldSkipMemberNull, and shouldSkipMemberNullLiterals methods for fine-grained control

Macro Definition Optimization

  • Refactored QS_DERIVED_META_OBJECT_METHOD and related macros for code consistency
  • Added QS_FIELD_DEFAULT and QS_OBJECT_DEFAULT macros to support default value setting
  • Optimized QSERIALIZABLE macro definition

C++17 Compatibility

  • Added C++17-based conditional compilation logic
  • Utilized inline static member variables for improved implementation efficiency in C++17
  • Maintained backward compatibility with older C++ standards

Optional Type Support

  • Enhanced JSON and XML serialization handling for std::optional<T>
  • Added dedicated macros for optional types: QS_JSON_FIELD_OPT, QS_XML_FIELD_OPT, etc.
  • Implemented optional object type serialization: QS_JSON_OBJECT_OPT, QS_XML_OBJECT_OPT

Other Optimizations

  • Converted QSerializer to a header-only INTERFACE library, improving ease of use
  • Fixed Qt6 support issues
  • Improved static function methods, including adding static fromJson functionality

Usage Example

// Using class-level settings to skip empty values and null
QS_CLASS_BEGIN(MySerializableClass)
    QS_INTERNAL_SKIP_EMPTY_AND_NULL
    
    QS_FIELD(QString, normalString)
    QS_FIELD_OPT(int, optionalInt)
    
    // Member-level settings
    QS_FIELD(QStringList, emptyListToSkip)
    QS_INTERNAL_MEMBER_SKIP_EMPTY(emptyListToSkip)
QS_CLASS_END

DHR60 avatar Mar 27 '25 05:03 DHR60

In this PR, my primary focus is to introduce the QS_FIELD_DEFAULT macro and improve code structure optimization while reducing warnings, as these are key enhancements for the QSerializer library. Other changes, such as QS_FIELD_OPT and related optional type support macros (e.g., QS_FIELD_OPT, QS_INTERNAL_SKIP_EMPTY_AND_NULL), are part of my own requirements. If these optional type features are not needed by the project, I’m happy to open a separate PR to remove them.

Let me know if any adjustments are required!

DHR60 avatar Mar 27 '25 06:03 DHR60