getml / reflect-cpp

A C++20 library for fast serialization, deserialization and validation using reflection. Supports JSON, BSON, CBOR, flexbuffers, msgpack, TOML, XML, YAML / msgpack.org[C++20]
https://getml.github.io/reflect-cpp/
MIT License
821 stars 65 forks source link

General question #50

Closed domcir closed 6 months ago

domcir commented 6 months ago

Hi Great work! Is it possible to use smart pointers on classes to be serialise? Is it possible to serialise private and protected attributes? Structs are public by default, is a public constructor necessary? Thanks Dom

liuzicheng1987 commented 6 months ago

Hi @domcir ,

Thanks for your questions.

I would refer you to the documentation, where a lot of these questions are covered:

https://github.com/getml/reflect-cpp/tree/main/docs

But to answer your questions directly:

  1. Yes, std::unique_ptr and std::shared_ptr are supported. They are treated as optional fields, though (https://github.com/getml/reflect-cpp/blob/main/docs/optional_fields.md).

  2. No, you cannot serialize private and protected fields. This is because rfl::xxx::write does not have access to these fields. This is something that other programming languages, like Rust or Go, would not allow you to do either. However, you can set up your classes with private fields to be supported by reflect-cpp, like this (https://github.com/getml/reflect-cpp/blob/main/docs/custom_classes.md) or this (https://github.com/getml/reflect-cpp/blob/main/docs/custom_parser.md).

  3. No, you should not have a constructor at all. The struct should be an aggregate (https://github.com/getml/reflect-cpp/blob/main/docs/structs.md).

domcir commented 6 months ago

Thanks for the answers!