Skip to content
Closed
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ The resulting JSON string looks like this:
{"firstName":"Homer","lastName":"Simpson","age":45}
```

For large datasets or live streams, `rfl::json::StreamReader` and `rfl::json::StreamWriter` also support record-level JSON streaming (NDJSON, RFC 7464 JSON Text Sequences, and JSON arrays) — see the [JSON documentation](https://munich-data-engineering.github.io/reflect-cpp/supported_formats/json/#record-level-streaming).

Or you can use another format, such as YAML.

```cpp
Expand Down
30 changes: 30 additions & 0 deletions docs/content/supported_formats/json.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,36 @@ Note that `std::cout` is also an ostream, so this works as well:
rfl::json::write(person, std::cout) << std::endl;
```

## Record-level streaming

For large datasets or live streams, `rfl::json::StreamReader` and `rfl::json::StreamWriter` read and write one record at a time without loading the entire document into memory. Three framing formats are supported via `rfl::json::StreamFormat`:

- `StreamFormat::ndjson` — Newline-delimited JSON (`{...}\n{...}\n`, default)
- `StreamFormat::json_seq` — RFC 7464 JSON Text Sequences (`\x1e{...}\n`)
- `StreamFormat::json_array` — Top-level JSON array (`[{...},{...}]`)

```cpp
// Writing records incrementally
rfl::json::StreamWriter writer(my_ostream, rfl::json::StreamFormat::ndjson);
for (const auto& person : people) {
writer.write(person).value();
}
writer.finish().value();
writer.flush().value();

// Reading records incrementally (C++20 input_range or next())
rfl::json::StreamReader<Person> reader(my_istream, rfl::json::StreamFormat::ndjson);
for (auto&& res : reader) {
if (res) {
process(*res);
} else {
log_error(res.error().what());
}
}
```

`StreamReader` also accepts a `std::string_view` directly (zero-copy), supports processors such as `rfl::SnakeCaseToCamelCase` and `rfl::NoExtraFields`, enforces a configurable `max_record_bytes` cap (64 MiB default, `0` for unlimited), and recovers from per-record syntax errors in `ndjson` (advancing to the next line) and `json_seq` (resyncing to the next `\x1e`). `StreamWriter::write()`, `flush()`, and `finish()` return `rfl::Result<rfl::Nothing>`; pretty-printing flags are rejected for `ndjson` and `json_seq`.

## Custom constructors

One of the great things about C++ is that it gives you control over
Expand Down
1 change: 1 addition & 0 deletions include/rfl/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "json/load.hpp"
#include "json/read.hpp"
#include "json/save.hpp"
#include "json/stream.hpp"
#include "json/to_schema.hpp"
#include "json/write.hpp"

Expand Down
Loading