mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-11-29 14:26:19 +08:00
897 B
897 B
JSON
For JSON support, you must also include the header <rfl/json.hpp>.
Suppose you have a struct like this:
struct Person {
rfl::Field<"firstName", std::string> first_name;
rfl::Field<"lastName", std::string> last_name;
rfl::Field<"birthday", rfl::Timestamp<"%Y-%m-%d">> birthday;
rfl::Field<"children", std::vector<Person>> children;
};
You can parse JSON strings like this:
const rfl::Result<Person> result = rfl::json::read<Person>(json_string);
A person can be turned into a JSON string like this:
const auto person = Person{...};
const std::string json_string = rfl::json::write(person);
You can also load and save to disc using a very similar syntax:
const rfl::Result<Person> result = rfl::json::load<Person>("/path/to/file.json");
const auto person = Person{...};
rfl::json::save("/path/to/file.json", person);