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
2 changes: 1 addition & 1 deletion .github/workflows/linux.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
strategy:
fail-fast: false
matrix:
format: ["JSON", "AVRO", "BOOST_SERIALIZATION", "CAPNPROTO", "CBOR", "CEREAL", "FLEXBUFFERS", "MSGPACK", "PARQUET", "TOML", "UBJSON", "XML", "YAML", "benchmarks", "headers"]
format: ["JSON", "AVRO", "BOOST_SERIALIZATION", "CAPNPROTO", "CBOR", "CEREAL", "FLEXBUFFERS", "MSGPACK", "NLOHMANN", "PARQUET", "TOML", "UBJSON", "XML", "YAML", "benchmarks", "headers"]
compiler: [llvm, gcc]
compiler-version: [11, 12, 13, 14, 16, 17, 18]
cxx: [20, 23]
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/windows.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
strategy:
fail-fast: false
matrix:
format: ["JSON", "AVRO", "BOOST_SERIALIZATION", "CAPNPROTO", "CBOR", "CEREAL", "FLEXBUFFERS", "MSGPACK", "PARQUET", "TOML", "UBJSON", "XML", "YAML", "benchmarks"]
format: ["JSON", "AVRO", "BOOST_SERIALIZATION", "CAPNPROTO", "CBOR", "CEREAL", "FLEXBUFFERS", "MSGPACK", "NLOHMANN", "PARQUET", "TOML", "UBJSON", "XML", "YAML", "benchmarks"]
name: "windows-msvc (${{ matrix.format }})"
concurrency:
group: "windows-${{ github.ref }}-${{ github.job }}-${{ matrix.format }}"
Expand Down Expand Up @@ -76,7 +76,7 @@ jobs:
strategy:
fail-fast: false
matrix:
format: ["JSON", "BOOST_SERIALIZATION", "CAPNPROTO", "CBOR", "FLEXBUFFERS", "MSGPACK", "PARQUET", "TOML", "UBJSON", "XML", "YAML"]
format: ["JSON", "BOOST_SERIALIZATION", "CAPNPROTO", "CBOR", "FLEXBUFFERS", "MSGPACK", "NLOHMANN", "PARQUET", "TOML", "UBJSON", "XML", "YAML"]
name: "windows-msvc-shared (${{ matrix.format }})"
runs-on: windows-latest
steps:
Expand Down
13 changes: 13 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ option(REFLECTCPP_CEREAL "Enable Cereal support" ${REFLECTCPP_ALL_FORMATS})
option(REFLECTCPP_CSV "Enable CSV support" ${REFLECTCPP_ALL_FORMATS})
option(REFLECTCPP_FLEXBUFFERS "Enable flexbuffers support" ${REFLECTCPP_ALL_FORMATS})
option(REFLECTCPP_MSGPACK "Enable msgpack support" ${REFLECTCPP_ALL_FORMATS})
option(REFLECTCPP_NLOHMANN "Enable nlohmann/json support" ${REFLECTCPP_ALL_FORMATS})
option(REFLECTCPP_PARQUET "Enable parquet support" ${REFLECTCPP_ALL_FORMATS})
option(REFLECTCPP_XML "Enable XML support" ${REFLECTCPP_ALL_FORMATS})
option(REFLECTCPP_TOML "Enable TOML support" ${REFLECTCPP_ALL_FORMATS})
Expand Down Expand Up @@ -82,6 +83,7 @@ if (
REFLECTCPP_CSV OR
REFLECTCPP_FLEXBUFFERS OR
REFLECTCPP_MSGPACK OR
REFLECTCPP_NLOHMANN OR
REFLECTCPP_PARQUET OR
REFLECTCPP_XML OR
REFLECTCPP_TOML OR
Expand Down Expand Up @@ -158,6 +160,10 @@ if (REFLECTCPP_USE_VCPKG)
list(APPEND VCPKG_MANIFEST_FEATURES "msgpack")
endif()

if (REFLECTCPP_NLOHMANN OR REFLECTCPP_CHECK_HEADERS)
list(APPEND VCPKG_MANIFEST_FEATURES "nlohmann")
endif()

if (REFLECTCPP_PARQUET OR REFLECTCPP_CHECK_HEADERS)
list(APPEND VCPKG_MANIFEST_FEATURES "parquet")
endif()
Expand Down Expand Up @@ -403,6 +409,13 @@ if (REFLECTCPP_MSGPACK OR REFLECTCPP_CHECK_HEADERS)
target_link_libraries(reflectcpp PUBLIC $<IF:$<TARGET_EXISTS:msgpack-c>,msgpack-c,$<IF:$<TARGET_EXISTS:msgpackc>,msgpackc,msgpack-c>>)
endif ()

if (REFLECTCPP_NLOHMANN OR REFLECTCPP_CHECK_HEADERS)
if (NOT TARGET nlohmann_json::nlohmann_json)
find_package(nlohmann_json 3.11.3 CONFIG REQUIRED)
endif ()
target_link_libraries(reflectcpp PUBLIC nlohmann_json::nlohmann_json)
endif ()

if (REFLECTCPP_PARQUET OR REFLECTCPP_CHECK_HEADERS)
if (NOT TARGET Arrow)
find_package(Arrow CONFIG REQUIRED)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ The following table lists the serialization formats currently supported by refle
| CSV | [Apache Arrow](https://arrow.apache.org/) | >= 21.0.0 | Apache 2.0 | Tabular textual format |
| flexbuffers | [flatbuffers](https://github.com/google/flatbuffers) | >= 23.5.26 | Apache 2.0 | Schema-less version of flatbuffers, binary format |
| msgpack | [msgpack-c](https://github.com/msgpack/msgpack-c) | >= 6.0.0 | BSL 1.0 | JSON-like binary format |
| nlohmann/json| [nlohmann/json](https://github.com/nlohmann/json) | >= 3.11.3 | MIT | Header-only DOM adapter (`nlohmann::json`, `ordered_json`) |
| parquet | [Apache Arrow](https://arrow.apache.org/) | >= 21.0.0 | Apache 2.0 | Tabular binary format |
| TOML | [toml++](https://github.com/marzer/tomlplusplus) | >= 3.4.0 | MIT | Textual format with an emphasis on readability |
| UBJSON | [jsoncons](https://github.com/danielaparker/jsoncons)| >= 0.176.0 | BSL 1.0 | JSON-like binary format |
Expand Down
5 changes: 5 additions & 0 deletions conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class ReflectCppConan(ConanFile):
"with_csv": [True, False],
"with_flatbuffers": [True, False],
"with_msgpack": [True, False],
"with_nlohmann": [True, False],
"with_parquet": [True, False],
"with_toml": [True, False],
"with_ubjson": [True, False],
Expand All @@ -62,6 +63,7 @@ class ReflectCppConan(ConanFile):
"with_csv": False,
"with_flatbuffers": False,
"with_msgpack": False,
"with_nlohmann": False,
"with_parquet": False,
"with_toml": False,
"with_ubjson": False,
Expand Down Expand Up @@ -96,6 +98,8 @@ def requirements(self):
self.requires("flatbuffers/24.3.25", transitive_headers=True)
if self.options.with_msgpack:
self.requires("msgpack-c/6.0.0", transitive_headers=True)
if self.options.with_nlohmann:
self.requires("nlohmann_json/3.11.3", transitive_headers=True)
if self.options.with_toml:
self.requires("tomlplusplus/3.4.0", transitive_headers=True)
if self.options.with_xml:
Expand Down Expand Up @@ -136,6 +140,7 @@ def generate(self):
tc.cache_variables["REFLECTCPP_CSV"] = self.options.with_csv
tc.cache_variables["REFLECTCPP_FLEXBUFFERS"] = self.options.with_flatbuffers
tc.cache_variables["REFLECTCPP_MSGPACK"] = self.options.with_msgpack
tc.cache_variables["REFLECTCPP_NLOHMANN"] = self.options.with_nlohmann
tc.cache_variables["REFLECTCPP_PARQUET"] = self.options.with_parquet
tc.cache_variables["REFLECTCPP_TOML"] = self.options.with_toml
tc.cache_variables["REFLECTCPP_XML"] = self.options.with_xml
Expand Down
1 change: 1 addition & 0 deletions docs/content/supported_formats/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Human-readable formats for configuration, data exchange, and legacy integration.

{{< cards >}}
{{< card link="json" title="JSON" icon="book-open" subtitle="Out-of-the-box support via yyjson. Standard for web APIs and data interchange." >}}
{{< card link="nlohmann" title="nlohmann/json" icon="book-open" subtitle="Header-only direct DOM interop with nlohmann::json and nlohmann::ordered_json." >}}
{{< card link="yaml" title="YAML" icon="book-open" subtitle="Textual format with an emphasis on readability. Widely used in Docker, Kubernetes, and CI/CD configuration." >}}
{{< card link="toml" title="TOML" icon="book-open" subtitle="Textual format with an emphasis on readability. Popular for application configuration in the Rust ecosystem." >}}
{{< card link="csv" title="CSV" icon="book-open" subtitle="Tabular textual format. The universal interchange format for spreadsheets, databases, and ETL pipelines." >}}
Expand Down
71 changes: 71 additions & 0 deletions docs/content/supported_formats/nlohmann.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
breadcrumbs: true
---

# nlohmann/json

For `nlohmann::json` interoperability, include `<rfl/nlohmann.hpp>` and enable `-DREFLECTCPP_NLOHMANN=ON` when building reflect-cpp (or link `nlohmann_json::nlohmann_json` directly — the adapter is header-only and requires `nlohmann/json >= 3.11.3`). It has no dependency on `yyjson` and works with `-DREFLECTCPP_JSON=OFF`.

## Reading and writing (`nlohmann::json` and `nlohmann::ordered_json`)

Convert reflected structs directly to and from `nlohmann::basic_json` without string round-trips:

```cpp
#include <rfl/nlohmann.hpp>

struct Person {
std::string first_name;
rfl::Validator<int, rfl::Minimum<0>> age;
std::optional<std::string> nickname;
};

const Person src{.first_name = "Ada", .age = 36, .nickname = "countess"};

// Serialize to nlohmann::json (supports reflect-cpp processors)
const nlohmann::json j = rfl::nlohmann::write<rfl::SnakeCaseToCamelCase>(src);

// Deserialize without throwing (errors are returned via rfl::Result)
const rfl::Result<Person> res =
rfl::nlohmann::read<Person, rfl::SnakeCaseToCamelCase, rfl::NoExtraFields>(j);

// Preserve struct field order with nlohmann::ordered_json
const nlohmann::ordered_json oj = rfl::nlohmann::write<nlohmann::ordered_json>(src);
```

Note that `nlohmann::ordered_json` uses `nlohmann::ordered_map` (backed by a contiguous sequence), so key lookup and insertion are $O(N)$ per field rather than $O(\log N)$ as with `nlohmann::json`.

## Opt-in `nlohmann::adl_serializer`

`<rfl/nlohmann.hpp>` always declares `rfl::nlohmann::enable_adl_serializer<T>`. When C++ exceptions are enabled and `JSON_NOEXCEPTION` is not defined, it also defines the opt-in `nlohmann::adl_serializer` specialization. Specialize `rfl::nlohmann::enable_adl_serializer<T>` to `std::true_type` to opt specific types into `nlohmann::json` conversions without affecting any other types:

```cpp
struct User {
const std::string name;
rfl::Validator<int, rfl::Minimum<0>> age;
};

template <>
struct rfl::nlohmann::enable_adl_serializer<User> : std::true_type {};

nlohmann::json j = User{.name = "Grace", .age = 42};
User u = j.get<User>(); // throws nlohmann::json::other_error (a nlohmann::json::exception) on error
```

Because `adl_serializer` uses the returning 1-argument `from_json` overload, `j.get<T>()` works cleanly with `rfl::Validator`, `const` fields, and non-default-constructible structs.

## Dynamic `nlohmann::json` fields in reflected structs

`nlohmann::json` and `nlohmann::ordered_json` (and other `nlohmann::basic_json` specializations) can be embedded directly as fields inside reflected structs and serialized across `rfl::nlohmann`, `rfl::json`, `rfl::generic`, and other formats:

```cpp
struct Event {
std::string kind;
nlohmann::json payload;
nlohmann::ordered_json metadata;
};
```

- `rfl::Bytestring` and `rfl::Vectorstring` serialize as RFC 4648 Base64 strings and accept either Base64 strings or native `nlohmann::json::binary_t` values on read.
- When writing `nlohmann::basic_json` values containing `binary_t` to other formats, binary formats (CBOR, msgpack, flexbuffers, UBJSON, BSON, etc.) receive native `rfl::Bytestring` values, `rfl::json` Base64-encodes `rfl::Bytestring`, and text/generic formats without `Bytestring` support fall back to Base64 strings.
- Cross-`basic_json` conversions (`nlohmann::json` ↔ `nlohmann::ordered_json`) preserve exact integer signedness (`uint64_t`/`int64_t`), booleans, strings, binary payloads, and key order.
- Mirroring `rfl::Generic` in `rfl/xml/Parser.hpp`, writing `nlohmann::basic_json` fields to XML is supported, while reading XML into `nlohmann::basic_json` is rejected at compile time because XML is untyped without a schema.
12 changes: 12 additions & 0 deletions include/rfl/nlohmann.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef RFL_NLOHMANN_HPP_
#define RFL_NLOHMANN_HPP_

#include "../rfl.hpp"
#include "nlohmann/Parser.hpp"
#include "nlohmann/Reader.hpp"
#include "nlohmann/Writer.hpp"
#include "nlohmann/adl_serializer.hpp"
#include "nlohmann/read.hpp"
#include "nlohmann/write.hpp"

#endif
Loading