https://github.com/chocolacula/easy_reflection_cpp
Reflection brings the best way to serialize/deserialize json and yaml in C++
https://github.com/chocolacula/easy_reflection_cpp
clang cpp17 json reflection serde serialization yaml
Last synced: 22 days ago
JSON representation
Reflection brings the best way to serialize/deserialize json and yaml in C++
- Host: GitHub
- URL: https://github.com/chocolacula/easy_reflection_cpp
- Owner: chocolacula
- License: apache-2.0
- Created: 2021-03-15T16:00:21.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2025-02-04T19:27:02.000Z (3 months ago)
- Last Synced: 2025-03-30T08:07:32.372Z (29 days ago)
- Topics: clang, cpp17, json, reflection, serde, serialization, yaml
- Language: C++
- Homepage:
- Size: 628 KB
- Stars: 102
- Watchers: 3
- Forks: 8
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Easy Reflection solution for C++
[](https://github.com/chocolacula/reflection_cpp/actions/workflows/cmake.yml)
It parses C++ source code for special attributes. In the most straightforward situation, you only need to mark an object by `[[er::reflect]]` attribute. All other work will be done by the code generation tool and reflection library.
The main idea is to use kinda dynamic typing for some type agnostic operations, like copying or getting the name of a type.
It makes it possible to determine a variable type and do the right job - print, serialize/deserialize.It's generally a proof of concept, created with the idea that it could be used for many years. If you are curious about the details of how it works, you can find them in [DEV article](https://dev.to/chocolacula/how-to-write-reflection-for-c-4527).
## Features
- Linux, MacOS and Windows, x86 and ARM support
- translate enums to string and vice versa
- invoke methods
- support of stl containers like `std::vector`, `std::list`, `std::map`, etc.
- smart pointers support
- native serialization directly to an object and without third parties for:
- **JSON**
- **YAML 1.2** even with anchors, but keep in mind that variables behind anchors have to be the same type.
- binary with **Variable Length Quantity** to reduce number of bytes
- debug printing
- understandable errors## Quick start
Look at [Installation](readme/installation.md) guide and install the solution.
Then define your object and use `[[er::reflect]]`:
```cpp
class [[er::reflect]] Object {
public:
std::string field_str;
int field_int;std::vector field_vector;
}
```And serialize/deserialize it in one shot:
```cpp
#include "generated/reflection.h"
#include "er/serialization/json.h"using namespace serialization;
...
auto str = json::to_string(obj).unwrap();
obj = json::from_string(str).unwrap();
...
```For more details see [How To Use](readme/how_to_use.md).
## Performance
The repository includes `benchmarks` folder, feel free to check it on your own hardware.
JSON is faster than [nlohmann json](https://github.com/nlohmann/json). Serialization is the same fast as [rapid json](https://github.com/Tencent/rapidjson), deserialization is a little faster with `simdjson` parser and more than twice slower without.
YAML is blazingly faster than [yaml-cpp](https://github.com/jbeder/yaml-cpp) if I did the benchmark right.
> **Note:** Other libraries do not always convert string-represented values to `int`, `float`, or `bool` and don't create instances of `std::string` until you call something like `.get()`. **Easy Reflection**, on the other hand, provides ready-made objects with all values within.
![]()
![]()
The ratio of the content length in bytes
## Thanks
JetBrains for Open Source Support