Files
reflect-cpp/docs/json.md
T

40 lines
897 B
Markdown

# JSON
For JSON support, you must also include the header `<rfl/json.hpp>`.
Suppose you have a struct like this:
```cpp
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:
```cpp
const rfl::Result<Person> result = rfl::json::read<Person>(json_string);
```
A person can be turned into a JSON string like this:
```cpp
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:
```cpp
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);
```