Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions docs/content/supported_formats/json.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,60 @@ Note that `std::cout` is also an ostream, so this works as well:
rfl::json::write(person, std::cout) << std::endl;
```

## Read and write flags

`rfl::json` provides `inline constexpr` flag constants (combinable with `|`) so you do not need to reference underlying `YYJSON_*` constants directly.

### Read flags (`rfl::json::read` and `rfl::json::load`)

| Flag | Description |
| --- | --- |
| `rfl::json::allow_comments` | Allow C-style single-line (`//`) and multi-line (`/* */`) comments |
| `rfl::json::allow_trailing_commas` | Allow a single trailing comma at the end of an object or array |
| `rfl::json::jsonc` | Shorthand for `allow_comments \| allow_trailing_commas` (JSONC) |
| `rfl::json::allow_bom` | Allow and skip a leading UTF-8 BOM *(requires yyjson >= 0.11.0)* |
| `rfl::json::stop_when_done` | Stop after the first JSON value and ignore trailing content |
| `rfl::json::read_allow_inf_and_nan` | Allow `Inf`, `Infinity`, and `NaN` numbers and literals |
| `rfl::json::read_allow_invalid_unicode` | Allow invalid UTF-8 byte sequences in string values |
| `rfl::json::allow_ext_number` | Allow hex numbers, leading/trailing decimal points, and leading `+` *(requires yyjson >= 0.12.0)* |
| `rfl::json::allow_ext_escape` | Allow extended string escape sequences *(requires yyjson >= 0.12.0)* |
| `rfl::json::allow_ext_whitespace` | Allow extended whitespace characters *(requires yyjson >= 0.12.0)* |
| `rfl::json::allow_single_quoted_str` | Allow single-quoted strings *(requires yyjson >= 0.12.0)* |
| `rfl::json::allow_unquoted_key` | Allow unquoted object keys *(requires yyjson >= 0.12.0)* |
| `rfl::json::json5` | Allow JSON5 syntax *(requires yyjson >= 0.12.0)* |

Example reading JSONC:

```cpp
const auto result = rfl::json::read<Person>(
jsonc_string, rfl::json::allow_comments | rfl::json::allow_trailing_commas);

// Or using the convenience alias:
const auto result2 = rfl::json::read<Person>(jsonc_string, rfl::json::jsonc);
```

### Write flags (`rfl::json::write`, `rfl::json::save`, and `rfl::json::to_schema`)

| Flag | Description |
| --- | --- |
| `rfl::json::pretty` | Pretty-print with 4-space indentation |
| `rfl::json::pretty_two_spaces` | Pretty-print with 2-space indentation (overrides `pretty`) |
| `rfl::json::newline_at_end` | Append a trailing `\n` to the output *(requires yyjson >= 0.9.0)* |
| `rfl::json::escape_unicode` | Escape non-ASCII characters as `\uXXXX` for ASCII-only output |
| `rfl::json::escape_slashes` | Escape `/` as `\/` |
| `rfl::json::write_allow_inf_and_nan` | Write `Inf`/`NaN` as `Infinity`/`NaN` literals (non-standard) |
| `rfl::json::inf_and_nan_as_null` | Write `Inf`/`NaN` as `null` (overrides `write_allow_inf_and_nan`) |
| `rfl::json::write_allow_invalid_unicode` | Allow invalid UTF-8 byte sequences when encoding strings |

Example combining write flags:

```cpp
const std::string json_string = rfl::json::write(
person, rfl::json::pretty_two_spaces | rfl::json::newline_at_end);
```

> Note: `read_allow_inf_and_nan` / `write_allow_inf_and_nan` and `read_allow_invalid_unicode` / `write_allow_invalid_unicode` are prefixed with `read_` / `write_` because the underlying read and write flag bit values differ. Unsafe/incompatible read flags (`YYJSON_READ_INSITU`, `YYJSON_READ_NUMBER_AS_RAW`, `YYJSON_READ_BIGNUM_AS_RAW`) are intentionally not exposed (`YYJSON_READ_INSITU` is rejected at runtime).

## Custom constructors

One of the great things about C++ is that it gives you control over
Expand Down
55 changes: 55 additions & 0 deletions include/rfl/json/read.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,61 @@
namespace rfl {
namespace json {

/// Stop after parsing the first JSON value instead of issuing an error if
/// trailing content follows.
inline constexpr yyjson_read_flag stop_when_done = YYJSON_READ_STOP_WHEN_DONE;

/// Allow a single trailing comma at the end of an object or array.
inline constexpr yyjson_read_flag allow_trailing_commas =
YYJSON_READ_ALLOW_TRAILING_COMMAS;

/// Allow C-style single-line (//) and multi-line (/* */) comments.
inline constexpr yyjson_read_flag allow_comments = YYJSON_READ_ALLOW_COMMENTS;

/// Allow Inf/NaN numbers and literals (e.g. 1e999, NaN, inf, -Infinity).
inline constexpr yyjson_read_flag read_allow_inf_and_nan =
YYJSON_READ_ALLOW_INF_AND_NAN;

/// Allow invalid UTF-8 byte sequences when parsing string values.
inline constexpr yyjson_read_flag read_allow_invalid_unicode =
YYJSON_READ_ALLOW_INVALID_UNICODE;

#if defined(YYJSON_VERSION_HEX) && YYJSON_VERSION_HEX >= 0x000B00
/// Allow and skip a leading UTF-8 BOM before parsing.
/// Requires yyjson >= 0.11.0.
inline constexpr yyjson_read_flag allow_bom = YYJSON_READ_ALLOW_BOM;
#endif

/// Convenience flag for JSONC (comments and trailing commas).
inline constexpr yyjson_read_flag jsonc =
allow_comments | allow_trailing_commas;

#if defined(YYJSON_VERSION_HEX) && YYJSON_VERSION_HEX >= 0x000C00
/// Allow extended number formats (hexadecimal, leading/trailing decimal point,
/// leading plus sign). Requires yyjson >= 0.12.0.
inline constexpr yyjson_read_flag allow_ext_number =
YYJSON_READ_ALLOW_EXT_NUMBER;

/// Allow extended escape sequences in strings. Requires yyjson >= 0.12.0.
inline constexpr yyjson_read_flag allow_ext_escape =
YYJSON_READ_ALLOW_EXT_ESCAPE;

/// Allow extended whitespace characters. Requires yyjson >= 0.12.0.
inline constexpr yyjson_read_flag allow_ext_whitespace =
YYJSON_READ_ALLOW_EXT_WHITESPACE;

/// Allow strings enclosed in single quotes. Requires yyjson >= 0.12.0.
inline constexpr yyjson_read_flag allow_single_quoted_str =
YYJSON_READ_ALLOW_SINGLE_QUOTED_STR;

/// Allow unquoted object keys. Requires yyjson >= 0.12.0.
inline constexpr yyjson_read_flag allow_unquoted_key =
YYJSON_READ_ALLOW_UNQUOTED_KEY;

/// Convenience flag for JSON5. Requires yyjson >= 0.12.0.
inline constexpr yyjson_read_flag json5 = YYJSON_READ_JSON5;
#endif

using InputObjectType = typename Reader::InputObjectType;
using InputVarType = typename Reader::InputVarType;

Expand Down
33 changes: 31 additions & 2 deletions include/rfl/json/write.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef RFL_JSON_WRITE_HPP_
#define RFL_JSON_WRITE_HPP_

#include <stdexcept>
#if __has_include(<yyjson.h>)
#include <yyjson.h>
#else
Expand All @@ -19,9 +18,39 @@
namespace rfl {
namespace json {

/// Convenient alias for the YYJSON pretty flag
/// Write JSON pretty-printed with 4-space indentation.
inline constexpr yyjson_write_flag pretty = YYJSON_WRITE_PRETTY;

/// Write JSON pretty-printed with 2-space indentation.
/// Overrides `pretty` if both are set.
inline constexpr yyjson_write_flag pretty_two_spaces =
YYJSON_WRITE_PRETTY_TWO_SPACES;

/// Escape non-ASCII Unicode characters as `\uXXXX` for ASCII-only output.
inline constexpr yyjson_write_flag escape_unicode = YYJSON_WRITE_ESCAPE_UNICODE;

/// Escape forward slashes `/` as `\/`.
inline constexpr yyjson_write_flag escape_slashes = YYJSON_WRITE_ESCAPE_SLASHES;

/// Write Inf and NaN numbers as `Infinity` and `NaN` literals (non-standard).
inline constexpr yyjson_write_flag write_allow_inf_and_nan =
YYJSON_WRITE_ALLOW_INF_AND_NAN;

/// Write Inf and NaN numbers as `null`.
/// Overrides `write_allow_inf_and_nan` if both are set.
inline constexpr yyjson_write_flag inf_and_nan_as_null =
YYJSON_WRITE_INF_AND_NAN_AS_NULL;

/// Allow invalid UTF-8 byte sequences when encoding string values.
inline constexpr yyjson_write_flag write_allow_invalid_unicode =
YYJSON_WRITE_ALLOW_INVALID_UNICODE;

#if defined(YYJSON_VERSION_HEX) && YYJSON_VERSION_HEX >= 0x000900
/// Append a trailing newline `\n` at the end of the JSON output.
/// Requires yyjson >= 0.9.0.
inline constexpr yyjson_write_flag newline_at_end = YYJSON_WRITE_NEWLINE_AT_END;
#endif

/// Returns a JSON string representation of the object.
/// Uses compile-time reflection to serialize a C++ object to JSON format.
/// @tparam Ps Processors to apply during serialization (transforms the data)
Expand Down
Loading
Loading