3#if !defined(CPPFIG_HAS_JSON)
5#error "cppfig: JSON support is not enabled. Set CPPFIG_ENABLE_JSON=ON in CMake or add the 'json' vcpkg feature."
8#include <nlohmann/json.hpp>
25 if (json.is_boolean()) {
26 return { json.get<
bool>() };
28 if (json.is_number_integer()) {
29 return { json.get<std::int64_t>() };
31 if (json.is_number_float()) {
32 return { json.get<
double>() };
34 if (json.is_string()) {
35 return { json.get<std::string>() };
37 if (json.is_object()) {
39 for (
const auto& [key, val] : json.items()) {
44 if (json.is_array()) {
59 if (value.IsBoolean()) {
60 return value.Get<
bool>();
62 if (value.IsInteger()) {
63 return value.Get<std::int64_t>();
65 if (value.IsDouble()) {
66 return value.Get<
double>();
68 if (value.IsString()) {
69 return value.Get<std::string>();
71 if (value.IsObject()) {
72 nlohmann::json json = nlohmann::json::object();
73 for (
const auto& [key, val] : value.Items()) {
78 if (value.IsArray()) {
79 return nlohmann::json::array();
98 catch (
const nlohmann::json::parse_error& e) {
109 catch (
const nlohmann::json::parse_error& e) {
122concept HasJsonAdl =
requires(
const T& value, nlohmann::json& json) {
123 { to_json(json, value) };
124 { from_json(json, std::declval<T&>()) };
142 to_json(json, value);
151 from_json(json, result);
161 static auto FromString(std::string_view str) -> std::optional<T>
164 auto json = nlohmann::json::parse(str);
A value-or-error type, similar to std::expected (C++23).
Definition status.h:100
A self-contained, recursive value type for configuration data.
Definition value.h:26
static auto Array() -> Value
Creates an empty array value.
Definition value.h:141
static auto Object() -> Value
Creates an empty object value.
Definition value.h:133
Concept for types that have nlohmann::json ADL serialization.
Definition json.h:122
C++20 compile-time type-safe configuration library.
Definition conf.h:13
auto ValueToJson(const Value &value) -> nlohmann::json
Converts a cppfig::Value to a nlohmann::json value.
Definition json.h:54
auto JsonToValue(const nlohmann::json &json) -> Value
Converts a nlohmann::json value to a cppfig::Value.
Definition json.h:20
auto InvalidArgumentError(std::string message) -> Status
Returns an InvalidArgument error status.
Definition status.h:61
Helper to create ConfigTraits for types with nlohmann::json ADL.
Definition json.h:138
static auto Serialize(const T &value) -> Value
Definition json.h:139
static auto ToString(const T &value) -> std::string
Definition json.h:159
static auto FromString(std::string_view str) -> std::optional< T >
Definition json.h:161
static auto Deserialize(const Value &value) -> std::optional< T >
Definition json.h:146
JSON serializer using nlohmann::json.
Definition json.h:87
static auto Stringify(const Value &data, int indent=4) -> std::string
Converts a Value tree to a formatted JSON string.
Definition json.h:115
static auto Parse(std::istream &is) -> StatusOr< Value >
Parses JSON from an input stream.
Definition json.h:91
static auto ParseString(std::string_view str) -> StatusOr< Value >
Parses JSON from a string.
Definition json.h:104