DEV Community

liuzicheng1987
liuzicheng1987

Posted on

reflect-cpp: Serialization and deserialization through reflection in C++, including automated struct field name detection

Hi everyone,

I am developing an open-source library for serialization, deserialization and validation for C++, similar to Python’s Pydantic or Rust’s serde.

https://github.com/getml/reflect-cpp

To give you a taste:

struct Person {
  std::string first_name;
  std::string last_name;
  int age;
};

const auto homer =
    Person{.first_name = "Homer",
           .last_name = "Simpson",
           .age = 45};

const std::string json_string = rfl::json::write(homer);

auto homer2 = rfl::json::read<Person>(json_string).value();
Enter fullscreen mode Exit fullscreen mode

Which will result in the following JSON string:

{"first_name":"Homer","last_name":"Simpson","age":45}
Enter fullscreen mode Exit fullscreen mode

Way more complex structures are possible, including recursive structs, nested structs, algebraic data types and automatic validation.

If you are interested, check it out. Constructive criticism is very welcome.

Top comments (0)