9#include <glaze/json.hpp>
35 static constexpr glz::opts
ReadOptions{.comments =
true, .error_on_unknown_keys =
false};
45 template <
class T, auto Options = ReadOptions>
51 return std::unexpected(text.error());
57 return std::unexpected(
Error::Invalid(std::format(
"{}: {}", path, parsed.error().Detail)));
63 template <
class T, auto Options = ReadOptions>
67 if (
auto ec = glz::read<Options>(value, text))
69 return std::unexpected(
Error::Invalid(glz::format_error(ec, text)));
95 template <
class T,
auto Options = glz::opts{}>
96 static std::string
Write(
const T& value)
98 auto written = glz::write<Options>(value);
101 Log::Error(
"Json: failed to serialize: {}", glz::format_error(written.error()));
104 return std::move(*written);
110 glz::generic document{};
111 if (
auto ec = glz::read_json(document, text))
113 return std::unexpected(
Error::Invalid(glz::format_error(ec, text)));
124 if (node.is_string())
128 else if (node.is_object())
130 for (
auto& [key, child] : node.get_object())
135 else if (node.is_array())
137 for (
auto& child : node.get<glz::generic::array_t>())
151 static std::string
GetStringByPath(std::string_view jsonText, std::string_view dotPath)
162 static std::string
GetString(
const glz::generic& root, std::string_view dotPath)
164 const glz::generic* node = &root;
165 for (
size_t start = 0; start <= dotPath.size();)
167 const size_t dot = dotPath.find(
'.', start);
168 const std::string key(
169 dotPath.substr(start, dot == std::string_view::npos ? std::string_view::npos : dot - start));
170 if (!node->is_object() || !node->contains(key))
174 node = &(*node)[key];
175 if (dot == std::string_view::npos)
182 if (node->is_string())
184 return node->get_string();
187 if (node->is_number() || node->is_boolean())
189 if (
auto dumped = node->dump())
191 return std::move(*dumped);
System.Text.Json-style helpers for mapping C++ types to and from JSON.
static std::string WritePretty(const T &value)
Serialize value as prettified JSON.
static constexpr PrettyOpts PrettyOptions
static std::string GetStringByPath(std::string_view jsonText, std::string_view dotPath)
Descend a dot-separated path (e.g. "data.room.code") in jsonText.
static std::string Write(const T &value)
Serialize value as compact JSON.
static Result< T > ReadFile(std::string_view path)
Parse path (resolved via ResolvePath) into T.
static Result< glz::generic > ParseDocument(std::string_view text)
Parse free-form JSON whose shape is not known at compile time.
static void SubstituteTokens(glz::generic &node, const Tokens &tokens)
Recursively replace {key} tokens in every string value of node.
static std::string GetString(const glz::generic &root, std::string_view dotPath)
static constexpr glz::opts ReadOptions
static Result< T > Read(std::string_view text)
As ReadFile, over text already in hand.
static constexpr glz::opts StrictReadOptions
Owns a plugin's settings and republishes them whole on every Load.
static std::string SubstituteTokens(std::string text, const Tokens &tokens)
void Error(std::format_string< Args... > fmt, Args &&... args)
std::expected< T, Error > Result
std::map< std::string, std::string > Tokens
Result< std::string > ReadAllText(std::string_view path)
Read path (resolved via ResolvePath) into a string.
static Error Invalid(std::string detail)
uint8_t indentation_width