diff --git a/search/search_index.json b/search/search_index.json
index 09860993..28f624fc 100644
--- a/search/search_index.json
+++ b/search/search_index.json
@@ -1 +1 @@
-{"config":{"lang":["en"],"separator":"[\\s\\-]+","pipeline":["stopWordFilter"],"fields":{"title":{"boost":1000.0},"text":{"boost":1.0},"tags":{"boost":1000000.0}}},"docs":[{"location":"","title":"Welcome","text":"\ud83d\udcbb 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: - Close integration with containers from the C++ standard library
- Close adherence to C++ idioms
- Out-of-the-box support for JSON
- Simple installation
- Simple extendability to other serialization formats
- Simple extendability to custom classes
- Being one of the fastest serialization libraries in existence, as demonstrated by our benchmarks
"},{"location":"#reflect-cpp","title":"reflect-cpp","text":""},{"location":"#why-do-we-need-this","title":"Why do we need this?","text":"Suppose your C++ program has complex data structures it needs to save and load. Or maybe it needs to interact with some kind of external API. If you do this the traditional way, you will have a lot of boilerplate code. This is annoying and error-prone.
Example
If you are new to reflect-cpp, please refer to our GitHub repository for the latest list of supported serialization formats. You will also find a simple and more comprehensive code example.
reflect-cpp is not just a reflection library, it is for serialization, deserialization and validation through reflection.
That means that you can encode your requirements about the input data in the type system and have them validated upfront. This is why the library also includes algebraic data types like tagged unions and numerous validation routines. Having your requirements encoded in the type system is the most reliable way of ensuring they are met. If your requirements are not met, the user of your software gets a very clear error message. Encoding your requirements in the type system also makes it a lot easier for anyone reading your code.
This increases user experience and developer experience, it makes your code safer (fewer bugs) and more secure (less prone to malicious attacks).
For a more in-depth theoretical discussions of these topics, the following books are warmly recommended:
- Category Theory for Programmers by Bartosz Milewski on GitHub
- Domain Modeling Made Functional by Scott Wlaschin
"},{"location":"#the-team-behind-reflect-cpp","title":"The team behind reflect-cpp","text":"reflect-cpp has been developed by getML (Code17 GmbH), a company specializing in software engineering and machine learning for enterprise applications. reflect-cpp is currently maintained by Patrick Urbanke and Manuel Bellersen, with major contributions coming from the community.
"},{"location":"#related-projects","title":"Related projects","text":"reflect-cpp was originally developed for getml-community, the fastest open-source tool for feature engineering on relational data and time series. If you are interested in Data Science and/or Machine Learning, please check it out.
"},{"location":"#professional-c-support","title":"Professional C++ support","text":"For comprehensive C++ support beyond the scope of GitHub discussions, we\u2019re here to help! Reach out at support@getml.com to discuss any technical challenges or project requirements. We\u2019re excited to support your work as independent software consultants.
"},{"location":"#license","title":"License","text":"reflect-cpp is released under the MIT License.
"},{"location":"as/","title":"rfl::as","text":"rfl::as allows you to cast structs as other structs. This is useful if the fields in B are a subset of the fields in A or we need the fields in A and B to produce C.
This only works for the rfl::Field-syntax.
For example:
struct A {\n std::string f1;\n std::string f2;\n};\n\nstruct B {\n std::string f3;\n std::string f4;\n};\n\nstruct C {\n std::string f1;\n std::string f2;\n std::string f4;\n};\n\nconst auto a = A{.f1 = \"Hello\", .f2 = \"World\"};\n\nconst auto b = B{.f3 = \"Hello\", .f4 = \"World\"};\n\n// Creates c by retrieving f1 and f2 from a and f4 from b. \n// Ignores f3.\nconst auto c = rfl::as<C>(a, b);\n
In this case, f1, f2 and f4 are now copied into the newly created c of type C.
Much like rfl::replace, rfl::as supports move semantics:
auto a = A{.f1 = \"Hello\", .f2 = \"World\"};\n\nauto b = B{.f3 = \"Hello\", .f4 = \"World\"};\n\nconst auto c = rfl::as<C>(std::move(a), std::move(b));\n
In this case, f1, f2 and f4 are now moved into the newly created c of type C. This is a zero-copy operation. f3 is discarded.
rfl::as also supports rfl::Flatten. For instance:
struct A {\n std::string f1;\n std::string f2;\n};\n\nstruct B {\n std::string f3;\n std::string f4;\n};\n\nstruct C {\n rfl::Flatten<A> a;\n rfl::Flatten<B> b;\n int f5;\n};\n\nconst auto a = A{.f1 = \"Hello\", .f2 = \"World\"};\n\nconst auto b = B{.f3 = \"Hello\", .f4 = \"World\"};\n\nconst auto c =\n rfl::as<C>(a, b, rfl::make_field<\"f5\">(5));\n
In this particular instance, rfl::as<C>(...) is basically equivalent to this:
const auto c = C{.a = a, .b = b, .f5 = rfl::make_field<\"f5\">(5)};\n
"},{"location":"backwards_compatability/","title":"Maintaining backwards compatability","text":"If you change your structs, you probably want to be able to interact with data generated by previous versions of your structs. This section explains how you can do that.
For the purpose of this section, any field of type std::optional, std::shared_ptr or std::unique_ptr is an optional field. Any other fields are required.
Please refer to the section on optional fields for details.
"},{"location":"backwards_compatability/#rules","title":"Rules","text":"The following rules apply:
- You can add optional fields.
- You can remove any optional or required fields, if they are no longer needed.
- You can change the field names of any optional fields.
- You can change the order of any existing fields.
- You can not add any required fields.
- You can not change the field names of any required fields.
"},{"location":"backwards_compatability/#api-versioning","title":"API versioning","text":"If you find it difficult to follow the rules as stated above or you cannot guarantee that you will always be able to follow them in the future, you might want to use API versioning.
You can either use rfl::TaggedUnion or externally tagged variants:
struct API_v_1_0 {\n using Tag = rfl::Literal<\"v1.0\">;\n ...\n};\n\nstruct API_v_1_1 {\n using Tag = rfl::Literal<\"v1.1\">;\n ...\n};\n\n...\n\nusing APIs = rfl::TaggedUnion<\"version\", API_v_1_0, API_v_1_1, ...>;\n
struct API_v_1_0 {\n ...\n};\n\nstruct API_v_1_1 {\n ...\n};\n\n...\n\nusing APIs = rfl::Variant<rfl::Field<\"v1.0\", API_v_1_0>, rfl::Field<\"v1.1\", API_v_1_1>, ...>;\n
"},{"location":"benchmarks/","title":"Benchmarks","text":"reflect-cpp conducts continuuous benchmarking across different operating systems, compilers and architectures and publishes the results on its Actions tab. Refer to the benchmarks folder for details.
The benchmarks show that reflect-cpp is not only very convenient, but also one the fastest JSON libraries for C++. It is faster than RapidJSON and about 10 times faster than nlohmann/json. It can be even faster than that, if you choose to use a different format supported by reflect-cpp, such as msgpack.
"},{"location":"bytestring/","title":"rfl::Bytestring","text":"rfl::Bytestring is a simple alias:
namespace rfl {\n\nusing Bytestring = std::basic_string<std::byte>;\n\n}\n
For more information on how to handle std::basic_string please refer to the relevant documentation:
https://en.cppreference.com/w/cpp/string/basic_string
Bytestrings are supported by BSON, CBOR, flexbuffers and msgpack. Textual formats do not support them.
"},{"location":"c_arrays_and_inheritance/","title":"C Arrays and inheritance","text":"reflect-cpp supports fixed-size C arrays and some forms of inheritance, but only as an opt-in.
We have made this design decision, because checking for C arrays and inheritance requires a disproportionate amount of compile time and we believe it is more important to reduce compile time than to support things out-of-the-box that most people won't need anyway. Since there are good alternatives for both of these problems, it is recommended to avoid using C arrays or inheritance altogether.
Note that C arrays are not the same thing as std::array. std::array is always supported and is the recommended alternative.
If you want support for these, you will have to pass the flag -D REFLECTCPP_C_ARRAYS_OR_INHERITANCE during compilation.
"},{"location":"c_arrays_and_inheritance/#c-arrays","title":"C arrays","text":"Suppose you have a struct like this:
struct Person{\n std::string first_name; \n std::string last_name; \n int[5] post_code;\n};\n
In this example, post_code is a C array. The best way to handle this is to simply replace the C array with std::array, like this:
struct Person{\n std::string first_name; \n std::string last_name; \n std::array<int, 5> post_code;\n};\n
However, in some cases, using C arrays is unavoidable. For instance bson_oid_t contains a C array under-the-hood, so if you want to parse a bson_oid_t you will have no choice but to pass the flag mentioned above.
"},{"location":"c_arrays_and_inheritance/#inheritance","title":"Inheritance","text":"reflect-cpp supports some form of inheritance. Because we use structured bindings to retrieve the fields of a struct, there are limitations on what we can do when it comes to inheritance:
Every non-static data member of E must be a direct member of E or the same base class of E. (https://en.cppreference.com/w/cpp/language/structured_binding)
In other words, all of the fields must be inside the same struct.
So, this is fine:
struct Base {\n int x;\n};\n\nstruct Derived : Base {};\n
This is fine as well:
struct Base {};\nstruct Derived : Base {\n int x;\n};\n
But this is not fine, because the fields are spread out over more than one struct:
struct Base {\n int x;\n};\n\nstruct Derived : Base {\n int y;\n};\n
The recommended alternative is to simply use rfl::Flatten, which has no such limitation:
struct Base {\n int x;\n};\n\nstruct Derived {\n rfl::Flatten<Base> base; \n int y;\n};\n
Please refer to the section on rfl::Flatten in this documentation for more information.
If for any reason you want to use inheritance patterns like the ones described above, you will make have to make sure that all of the fields are inside the same struct and also you will also have to pass the compile time flag mentioned above.
"},{"location":"composing_validators/","title":"Composing validators","text":"Sometimes the conditions you want to impose are more complex than a simple minimum or a simple regex pattern. That is why reflect-cpp allows you to compose them using the operators rfl::AnyOf, rfl::AllOf and rfl::OneOf.
rfl::AnyOf requires that at least one of the conditions contained therein be true. It thus works like an OR operator. rfl::AllOf requires that all of the conditions contained therein be true. It thus works like an AND operator. rfl::OneOf requires that exactly one of the conditions contained therein be true. It thus works like a XOR operator.
For instance, if you want to ensure that a person's age is a reasonable number, you can impose two conditions as follows:
using Age = rfl::Validator<int,\n rfl::AllOf<rfl::Minimum<0>, rfl::Maximum<130>>>;\n\nstruct Person {\n rfl::Rename<\"firstName\", std::string> first_name;\n rfl::Rename<\"lastName\", std::string> last_name;\n Age age;\n};\n
However, if you pass more than one constraint to rfl::Validator, they will be automatically connected using rfl::AllOf, so you might as well write:
using Age = rfl::Validator<int, rfl::Minimum<0>, rfl::Maximum<130>>;\n\n...\n
You can also nest composed validators. For instance, in this case the age must either be 0-10 or 40-130.
using Age = rfl::Validator<\n int,\n rfl::AnyOf<rfl::AllOf<rfl::Minimum<0>, rfl::Maximum<10>>,\n rfl::AllOf<rfl::Minimum<40>, rfl::Maximum<130>>>>;\n
You can also compose regex patterns:
using TableOrColumnName = rfl::Validator<\n std::string,\n rfl::AnyOf<rfl::PatternValidator<R\"(^[A-Z]+(?:_[A-Z]+)*$)\", \"TableName\">,\n rfl::PatternValidator<R\"(^[a-z]+(?:_[a-z]+)*$)\", \"ColumnName\">>>;\n
"},{"location":"contributing/","title":"How to contribute","text":""},{"location":"contributing/#setup","title":"Setup","text":"Make sure includes are relative.
We need internal includes to be relative and not depend on any externally set include directory.
That is, for example, if you are within any file in rfl/internal, prefer
#include \"to_ptr_named_tuple.hpp\"\n
over #include \"rfl/internal/to_ptr_named_tuple.hpp\"\n
For further details and reasoning, please refer to #30.