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."
9#include <nlohmann/json.hpp>
26 if (json.is_boolean()) {
27 return { json.get<
bool>() };
29 if (json.is_number_integer()) {
30 return { json.get<std::int64_t>() };
32 if (json.is_number_float()) {
33 return { json.get<
double>() };
35 if (json.is_string()) {
36 return { json.get<std::string>() };
38 if (json.is_object()) {
40 for (
const auto& [key, val] : json.items()) {
45 if (json.is_array()) {
47 for (
const auto& element : json) {
61 if (value.IsBoolean()) {
62 return value.Get<
bool>();
64 if (value.IsInteger()) {
65 return value.Get<std::int64_t>();
67 if (value.IsDouble()) {
68 return value.Get<
double>();
70 if (value.IsString()) {
71 return value.Get<std::string>();
73 if (value.IsObject()) {
74 nlohmann::json json = nlohmann::json::object();
75 for (
const auto& [key, val] : value.Items()) {
80 if (value.IsArray()) {
81 nlohmann::json json = nlohmann::json::array();
82 for (
const auto& element : value.Elements()) {
104 catch (
const nlohmann::json::parse_error& e) {
115 catch (
const nlohmann::json::parse_error& e) {
128concept HasJsonAdl =
requires(
const T& value, nlohmann::json& json) {
129 { to_json(json, value) };
130 { from_json(json, std::declval<T&>()) };
148 to_json(json, value);
157 from_json(json, result);
167 static auto FromString(std::string_view str) -> std::optional<T>
170 auto json = nlohmann::json::parse(str);
A value-or-error type, similar to std::expected (C++23).
Definition status.h:102
A self-contained, recursive value type for configuration data.
Definition value.h:32
static auto Array() -> Value
Creates an empty array value.
Definition value.h:147
static auto Object() -> Value
Creates an empty object value.
Definition value.h:139
Concept for types that have nlohmann::json ADL serialization.
Definition json.h:128
C++20 compile-time type-safe configuration library.
Definition conf.h:17
auto ValueToJson(const Value &value) -> nlohmann::json
Converts a cppfig::Value to a nlohmann::json value.
Definition json.h:56
auto JsonToValue(const nlohmann::json &json) -> Value
Converts a nlohmann::json value to a cppfig::Value.
Definition json.h:21
auto InvalidArgumentError(std::string message) -> Status
Returns an InvalidArgument error status.
Definition status.h:63
Helper to create ConfigTraits for types with nlohmann::json ADL.
Definition json.h:144
static auto Serialize(const T &value) -> Value
Definition json.h:145
static auto ToString(const T &value) -> std::string
Definition json.h:165
static auto FromString(std::string_view str) -> std::optional< T >
Definition json.h:167
static auto Deserialize(const Value &value) -> std::optional< T >
Definition json.h:152
JSON serializer using nlohmann::json.
Definition json.h:93
static auto Stringify(const Value &data, int indent=4) -> std::string
Converts a Value tree to a formatted JSON string.
Definition json.h:121
static auto Parse(std::istream &is) -> StatusOr< Value >
Parses JSON from an input stream.
Definition json.h:97
static auto ParseString(std::string_view str) -> StatusOr< Value >
Parses JSON from a string.
Definition json.h:110