VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
VoltMod::Json Class Reference

System.Text.Json-style helpers for mapping C++ types to and from JSON. More...

#include <Json.hpp>

Classes

struct  PrettyOpts
 

Static Public Member Functions

template<class T , auto Options = ReadOptions>
static Result< T > ReadFile (std::string_view path)
 Parse path (resolved via ResolvePath) into T.
 
template<class T , auto Options = ReadOptions>
static Result< T > Read (std::string_view text)
 As ReadFile, over text already in hand.
 
template<class T >
static std::string WritePretty (const T &value)
 Serialize value as prettified JSON.
 
template<class T , auto Options = glz::opts{}>
static std::string Write (const T &value)
 Serialize value as compact JSON.
 
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 GetStringByPath (std::string_view jsonText, std::string_view dotPath)
 Descend a dot-separated path (e.g. "data.room.code") in jsonText.
 
static std::string GetString (const glz::generic &root, std::string_view dotPath)
 

Static Public Attributes

static constexpr glz::opts ReadOptions {.comments = true, .error_on_unknown_keys = false}
 
static constexpr glz::opts StrictReadOptions {.comments = true}
 
static constexpr PrettyOpts PrettyOptions {{.prettify = true}, 2}
 

Detailed Description

System.Text.Json-style helpers for mapping C++ types to and from JSON.

Glaze reflects public aggregate members directly: the member name is the JSON key and a missing key keeps the member's C++ initializer. Unknown keys are ignored for compatibility with older settings files. No registration macro is needed.

struct Cfg { std::string host = "localhost"; int port = 5432; };
auto cfg = Json::ReadFile<Cfg>("addons/voltmod/plugins/my-plugin/config.jsonc");
if (!cfg)
Log::Error("{}", cfg.error().Detail);
static Result< T > ReadFile(std::string_view path)
Parse path (resolved via ResolvePath) into T.
Definition Json.hpp:46
void Error(std::format_string< Args... > fmt, Args &&... args)
Definition Log.hpp:97

Definition at line 31 of file Json.hpp.

Member Function Documentation

◆ GetString()

static std::string VoltMod::Json::GetString ( const glz::generic &  root,
std::string_view  dotPath 
)
inlinestatic

As above, for a node already parsed: saves callers a dump-and-reparse round trip.

Definition at line 162 of file Json.hpp.

References ReadFile().

Referenced by GetStringByPath().

◆ GetStringByPath()

static std::string VoltMod::Json::GetStringByPath ( std::string_view  jsonText,
std::string_view  dotPath 
)
inlinestatic

Descend a dot-separated path (e.g. "data.room.code") in jsonText.

Returns
The leaf as a string, or "" when jsonText does not parse, the path is absent, or the leaf is neither a string nor a primitive. A numeric or boolean leaf reads as "42" / "true" without quotes.

Definition at line 151 of file Json.hpp.

References GetString(), ParseDocument(), and ReadFile().

◆ ParseDocument()

static Result< glz::generic > VoltMod::Json::ParseDocument ( std::string_view  text)
inlinestatic

Parse free-form JSON whose shape is not known at compile time.

Definition at line 108 of file Json.hpp.

References VoltMod::Error::Invalid(), and ReadFile().

Referenced by VoltMod::StatusService::BuildText(), GetStringByPath(), and VoltMod::Translations::Load().

◆ Read()

template<class T , auto Options = ReadOptions>
static Result< T > VoltMod::Json::Read ( std::string_view  text)
inlinestatic

As ReadFile, over text already in hand.

Definition at line 64 of file Json.hpp.

References VoltMod::Error::Invalid(), ReadFile(), and VoltMod::T.

◆ ReadFile()

template<class T , auto Options = ReadOptions>
static Result< T > VoltMod::Json::ReadFile ( std::string_view  path)
inlinestatic

Parse path (resolved via ResolvePath) into T.

Returns
ErrorCode::NotFound when the file cannot be opened; ErrorCode::Invalid with a position-aware message for a syntax error, wrong-typed value, or invalid UTF-8.

Definition at line 46 of file Json.hpp.

References VoltMod::Error::Invalid(), VoltMod::ReadAllText(), and ReadFile().

Referenced by VoltMod::InstalledPlugins::Discover(), VoltMod::GameDataTable::GameDataTable(), GetString(), GetStringByPath(), VoltMod::Options< TSettings, TSnapshot >::Load(), ParseDocument(), Read(), ReadFile(), SubstituteTokens(), Write(), VoltMod::WriteGameDataRecord(), and WritePretty().

◆ SubstituteTokens()

static void VoltMod::Json::SubstituteTokens ( glz::generic &  node,
const Tokens &  tokens 
)
inlinestatic

Recursively replace {key} tokens in every string value of node.

Substituting inside the parsed document rather than in its text is what keeps a token value containing " or \ (a player name, say) from producing invalid JSON.

Definition at line 122 of file Json.hpp.

References ReadFile(), SubstituteTokens(), and VoltMod::Strings::SubstituteTokens().

Referenced by SubstituteTokens().

◆ Write()

template<class T , auto Options = glz::opts{}>
static std::string VoltMod::Json::Write ( const T &  value)
inlinestatic

Serialize value as compact JSON.

Definition at line 96 of file Json.hpp.

References VoltMod::Log::Error(), and ReadFile().

Referenced by VoltMod::StatusService::BuildJson().

◆ WritePretty()

template<class T >
static std::string VoltMod::Json::WritePretty ( const T &  value)
inlinestatic

Serialize value as prettified JSON.

For a file a human reviews: one value per line keeps a git diff of a regenerated artifact down to the values that actually changed.

Definition at line 89 of file Json.hpp.

References ReadFile().

Referenced by VoltMod::Schema::WriteDumpFile(), and VoltMod::WriteGameDataRecord().

Member Data Documentation

◆ PrettyOptions

constexpr PrettyOpts VoltMod::Json::PrettyOptions {{.prettify = true}, 2}
staticconstexpr

Definition at line 80 of file Json.hpp.

◆ ReadOptions

constexpr glz::opts VoltMod::Json::ReadOptions {.comments = true, .error_on_unknown_keys = false}
staticconstexpr

JSONC with the tolerant unknown-key behavior used by existing settings files.

Definition at line 35 of file Json.hpp.

◆ StrictReadOptions

constexpr glz::opts VoltMod::Json::StrictReadOptions {.comments = true}
staticconstexpr

JSONC for formats whose schema rejects unknown keys.

Definition at line 37 of file Json.hpp.


The documentation for this class was generated from the following file: