diff --git a/concepts/custom_classes/index.html b/concepts/custom_classes/index.html index 8ecff346..10a97916 100644 --- a/concepts/custom_classes/index.html +++ b/concepts/custom_classes/index.html @@ -2414,6 +2414,8 @@ that. If your class is more than a trivial, behaviorless struct, you often want 3) It needs to contain a method called reflection that returns said ReflectionType (or a reference thereto).

If you class fulfills these three conditions, then it is fully supported by all serialization and deserialization routines in reflect-cpp.

+

Please be aware that due to limitations of the Avro format, it is a good idea to always have a struct as your +ReflectionType when using Avro to avoid infinite recursions.

If you absolutely do not want to make any changes to your original class, you can implement a custom parser.

Example 1: Using an Impl struct

struct PersonImpl {
diff --git a/generic/index.html b/generic/index.html
index a33316ce..a2c96a4f 100644
--- a/generic/index.html
+++ b/generic/index.html
@@ -2365,6 +2365,7 @@
 

reflect-cpp is intended to be used to directly parse into structs. Doing this is more efficient, more convenient and also safer.

But in some cases, we simply cannot anticipate the complete structure of the data at compile time. For these cases, we have rfl::Generic.

+

Note that generics are not supported for Avro.

rfl::Generic is a convenience wrapper around the following type:

std::variant<bool, int, double, std::string, Object, Array, std::nullopt_t>;
 
diff --git a/index.html b/index.html index 8c4345e7..75f2f847 100644 --- a/index.html +++ b/index.html @@ -447,6 +447,17 @@
-
-


📣 From the Author (01.11.2024): Hi everyone, Patrick (liuzicheng1987) here. With reflect-cpp reaching the 1k-star milestone, we’re excited to roll out an overhauled documentation site at https://rfl.getml.com, giving it a permanent place in our company. Initially developed as an internal tool for our machine learning library, getML, reflect-cpp has grown into something much larger. -
A big thank you to all contributors for helping us reach this point! Your feedback, ideas, and dedication have been invaluable. -
As we look to the project’s future, I would like to hear your thoughts on potential new directions, discuss ideas to expand our user base, or learn more about what you’re building with it. For the next month, I am opening a few slots in my calendar for anyone who wants to connect (link). -
— Best, Patrick -
 

-
-


+

đź’» Source Code: https://github.com/getml/reflect-cpp

reflect-cpp is a C++-20 library for fast serialization, deserialization and validation using reflection, similar to pydantic in Python, serde in Rust, encoding in Go or aeson in Haskell.

reflect-cpp fills an important gap in C++ development. It minimizes boilerplate code and enhances code safety for seamless and efficient data exchange across system components.

-

Design principles for reflect-cpp include:

- +

Design principles for reflect-cpp include:

@@ -2232,6 +2271,45 @@ + + +
  • + + + + Additional requirements for schemaful formats + + + + + +
  • @@ -2434,6 +2512,45 @@ + + +
  • + + + + Additional requirements for schemaful formats + + + + + +
  • @@ -2678,11 +2795,13 @@ You should probably just copy + paste this into your own code and fill in the bl /// Retrieves a particular field from an array. /// Returns an rfl::Error if the index is out of bounds. + /// If your format is schemaful, you do not need this. rfl::Result<InputVarType> get_field_from_array( const size_t _idx, const InputArrayType _arr) const noexcept {...} /// Retrieves a particular field from an object. /// Returns an rfl::Error if the field cannot be found. + /// If your format is schemaful, you do not need this. rfl::Result<InputVarType> get_field_from_object( const std::string& _name, const InputObjectType& _obj) const noexcept {...} @@ -2747,6 +2866,173 @@ of these forms have a method with the following signature:

    Within your implementation of read_object, you must iterate through the object passed to the function and then insert the resulting key-value-pairs into object_reader.read.

    +

    Additional requirements for schemaful formats

    +

    Schemaful formats, like Apache Avro or Cap'n Proto, +are somewhat more complicated than schemaless ones. There are additional factors +to consider which do not apply schemaless formats:

    +
      +
    1. +

      Schemaful formats needs to differentiate between objects, for which + the field names are known at compile time and maps, for which the + field names are not known at compile time. In schemaless formats, there + is no differentiation.

      +
    2. +
    3. +

      Schemaful formats needs an explicit union types. This also means that + many of the problems we have with serializing std::variant which + requires us to develop concepts like rfl::TaggedUnion simply do not + apply to schemaful formats - the problem is already solved.

      +
    4. +
    +

    Additional requirements for schemaful writers

    +

    Any schemaful reader additionally needs to define the following:

    +

    1) An OutputMapType, which must contain key-value pairs. +2) An OutputUnionType, which represents an explicit union.

    +
    struct Writer {
    +  using OutputArrayType = ...;
    +  using OutputMapType = ...;
    +  using OutputObjectType = ...;
    +  using OutputUnionType = ...;
    +  using OutputVarType = ...;
    +
    +  /// Sets an empty map as the root element of the document.
    +  /// Some serialization formats require you to pass the expected size in
    +  /// advance. If you are not working with such a format, you can ignore the
    +  /// parameter `size`. Returns the new array for further modification.
    +  OutputMapType map_as_root(const size_t _size) const noexcept;
    +
    +  /// Sets an empty union as the root element of the document.
    +  OutputUnionType union_as_root() const noexcept;
    +
    +  /// Adds an empty array to an existing map. Returns the new
    +  /// array for further modification.
    +  OutputArrayType add_array_to_map(const std::string_view& _name,
    +                                   const size_t _size,
    +                                   OutputMapType* _parent) const noexcept;
    +
    +  /// Adds an empty array to an existing union.
    +  /// The index refers to the index of the element in the union.
    +  /// Returns the new array for further modification.
    +  OutputArrayType add_array_to_union(const size_t _index, const size_t _size,
    +                                     OutputUnionType* _parent) const noexcept;
    +
    +  /// Adds an empty map to an existing array. Returns the new
    +  /// map for further modification.
    +  OutputMapType add_map_to_array(const size_t _size,
    +                                 OutputArrayType* _parent) const noexcept;
    +
    +  /// Adds an empty map to an existing map. The key or name of the field
    +  /// is signified by `name`. Returns the new map for further modification.
    +  OutputMapType add_map_to_map(const std::string_view& _name,
    +                               const size_t _size,
    +                               OutputMapType* _parent) const noexcept;
    +
    +  /// Adds an empty map to an existing object. The key or name of the field
    +  /// is signified by `name`. Returns the new map for further modification.
    +  OutputMapType add_map_to_object(const std::string_view& _name,
    +                                  const size_t _size,
    +                                  OutputObjectType* _parent) const noexcept;
    +
    +  /// Adds an empty map to an existing union.
    +  /// The index refers to the index of the element in the union.
    +  /// Returns the new map for further modification.
    +  OutputMapType add_map_to_union(const size_t _index, const size_t _size,
    +                                 OutputUnionType* _parent) const noexcept;
    +
    +  /// Adds an empty object to an existing map. The key or name of the field
    +  /// is signified by `name`. Returns the new object for further modification.
    +  OutputObjectType add_object_to_map(const std::string_view& _name,
    +                                     const size_t _size,
    +                                     OutputMapType* _parent) const noexcept;
    +
    +  /// Adds an empty object to an existing union.
    +  /// The index refers to the index of the element in the union.
    +  /// Returns the new object for further modification.
    +  OutputObjectType add_object_to_union(const size_t _index, const size_t _size,
    +                                       OutputUnionType* _parent) const noexcept;
    +
    +  /// Adds an empty union to an existing array. Returns the new
    +  /// union for further modification.
    +  OutputUnionType add_union_to_array(OutputArrayType* _parent) const noexcept;
    +
    +  /// Adds an empty union to an existing map. The key or name of the field
    +  /// is signified by `name`. Returns the new union for further modification.
    +  OutputUnionType add_union_to_map(const std::string_view& _name,
    +                                   OutputMapType* _parent) const noexcept;
    +
    +  /// Adds an empty union to an existing object. The key or name of the field
    +  /// is signified by `name`. Returns the new union for further modification.
    +  OutputUnionType add_union_to_object(const std::string_view& _name,
    +                                      OutputObjectType* _parent) const noexcept;
    +
    +  /// Adds an empty union to an existing union.
    +  /// The index refers to the index of the element in the union.
    +  /// Returns the new union for further modification.
    +  OutputUnionType add_union_to_union(const size_t _index,
    +                                     OutputUnionType* _parent) const noexcept;
    +
    +  /// Adds a null value to a map. Returns an
    +  /// OutputVarType containing the null value.
    +  OutputVarType add_null_to_map(const std::string_view& _name,
    +                                OutputMapType* _parent) const noexcept;
    +
    +  /// Adds a null value to a union. Returns an
    +  /// OutputVarType containing the null value.
    +  OutputVarType add_null_to_union(const size_t _index,
    +                                  OutputUnionType* _parent) const noexcept;
    +
    +  /// Adds a basic value (bool, numeric, string) to an existing map. The key
    +  /// or name of the field is signified by `name`. Returns an
    +  /// OutputVarType containing the new value.
    +  template <class T>
    +  OutputVarType add_value_to_map(const std::string_view& _name, const T& _var,
    +                                 OutputMapType* _parent) const noexcept; 
    +
    +  /// Adds a basic value (bool, numeric, string) to an existing union. The key
    +  /// or name of the field is signified by `name`. Returns an
    +  /// OutputVarType containing the new value.
    +  template <class T>
    +  OutputVarType add_value_to_union(const size_t _index, const T& _var,
    +                                   OutputUnionType* _parent) const noexcept;
    +
    +  /// Signifies to the writer that we do not want to add any further elements to
    +  /// this map. Some serialization formats require this. If you are working
    +  /// with a serialization format that doesn't, just leave the function empty.
    +  void end_map(OutputMapType* _obj) const noexcept; 
    +
    +};
    +
    +

    Additional requirements for schemaful readers

    +

    Any schemaful reader additionally needs to define the following:

    +

    1) An InputMapType, which must contain key-value pairs. +2) An InputUnionType, which represents an explicit union.

    +
    struct Reader {
    +  using InputArrayType = ...;
    +  using InputObjectType = ...;
    +  using InputMapType = ...;
    +  using InputUnionType = ...;
    +  using InputVarType = ...;
    +
    +  /// A schemaful reader needs to differentiate between objects, for which
    +  /// the field names are known at compile time and maps, for which the
    +  /// field names are not known at compile time.
    +  rfl::Result<InputMapType> to_map(const InputVarType& _var) const noexcept;
    +
    +  /// read_map works exactly the same as read_object in schemaless formats.
    +  template <class MapReader>
    +  std::optional<Error> read_map(const MapReader& _map_reader,
    +                                const InputMapType& _map) const noexcept;
    +
    +  /// A schemaful reader needs an explicit union type.
    +  rfl::Result<InputUnionType> to_union(const InputVarType& _var) const noexcept;
    +
    +  /// read_union needs to be able to take an InputUnionType and return the corresponding
    +  /// variant (like std::variant or rfl::Variant). 
    +  template <class VariantType, class UnionReaderType>
    +  rfl::Result<VariantType> read_union(
    +      const InputUnionType& _union) const noexcept;
    +};
    +