header_utils
Loading...
Searching...
No Matches

Typedefs

using ghassanpl::formats::json::jtype = nlohmann::json::value_t
 Smaller name for nlohmann::json::value_t.
 

Functions

nlohmann::json ghassanpl::formats::json::load_file (std::filesystem::path const &from, std::error_code &ec)
 
nlohmann::json ghassanpl::formats::json::try_load_file (std::filesystem::path const &from, nlohmann::json const &or_json=empty_json)
 
nlohmann::json ghassanpl::formats::json::load_file (std::filesystem::path const &from)
 
void ghassanpl::formats::json::save_file (std::filesystem::path const &to, nlohmann::json const &j, bool pretty=true)
 
nlohmann::json constghassanpl::formats::json::get (nlohmann::json const &g, std::string_view key, jtype type=jtype::discarded)
 Gets the item in the json object g with the key key, or an empty json object if none found.
 
std::string ghassanpl::formats::json::get (nlohmann::json const &g, std::string_view key, std::string_view default_value, jtype type=jtype::discarded)
 Gets the value (converted to a string) in the json object g with the key key, or default_value if none found.
 
template<std::integral T>
ghassanpl::formats::json::get (nlohmann::json const &g, std::string_view key, T default_value, jtype type=jtype::discarded)
 Gets the value (converted to an integer) in the json object g with the key key, or default_value if none found.
 
nlohmann::json constghassanpl::formats::json::get_array (nlohmann::json const &g, std::string_view key)
 Gets the array value in the json object g with the key key, or an empty array if none found.
 
template<typename T >
void ghassanpl::formats::json::field (T &val, nlohmann::json const &g, std::string_view key)
 Sets the value of val to the item in json object g with key key
 
template<typename T >
void ghassanpl::formats::json::field (T &val, nlohmann::json const &g, size_t key)
 Sets the value of val to the item in json array g at index key
 
template<typename T >
bool ghassanpl::formats::json::field_opt (T &val, nlohmann::json const &g, std::string_view key)
 Same as field() but returns if it succeeded, instead of throwing.
 
template<typename VISIT_FUNC >
auto ghassanpl::formats::json::visit (nlohmann::json const &j, VISIT_FUNC &&func)
 Calls func with the actual value inside j; similar to std::visit
 
constexpr const charghassanpl::formats::json::type_name (nlohmann::json::value_t type) noexcept
 

Variables

const nlohmann::json ghassanpl::formats::json::empty_json
 
const nlohmann::json ghassanpl::formats::json::empty_json_array
 
const nlohmann::json ghassanpl::formats::json::empty_json_object
 

Detailed Description

Typedef Documentation

◆ jtype

using ghassanpl::formats::json::jtype = typedef nlohmann::json::value_t

Smaller name for nlohmann::json::value_t.

Definition at line 187 of file json_helpers.h.

Function Documentation

◆ field() [1/2]

template<typename T >
void ghassanpl::formats::json::field ( T &  val,
nlohmann::json const g,
size_t  key 
)
inline

Sets the value of val to the item in json array g at index key

Exceptions
std::runtime_erroron error (invalid index, cannot convert json to `val` type, etc.)

Definition at line 260 of file json_helpers.h.

◆ field() [2/2]

template<typename T >
void ghassanpl::formats::json::field ( T &  val,
nlohmann::json const g,
std::string_view  key 
)
inline

Sets the value of val to the item in json object g with key key

Exceptions
std::runtime_erroron error (no key found, cannot convert json to `val` type, etc.)

Definition at line 238 of file json_helpers.h.

◆ field_opt()

template<typename T >
bool ghassanpl::formats::json::field_opt ( T &  val,
nlohmann::json const g,
std::string_view  key 
)
inline

Same as field() but returns if it succeeded, instead of throwing.

See also
field()

Definition at line 276 of file json_helpers.h.

◆ get() [1/3]

nlohmann::json const & ghassanpl::formats::json::get ( nlohmann::json const g,
std::string_view  key,
jtype  type = jtype::discarded 
)
inline

Gets the item in the json object g with the key key, or an empty json object if none found.

Parameters
typethe value must also be of this type

Definition at line 191 of file json_helpers.h.

◆ get() [2/3]

std::string ghassanpl::formats::json::get ( nlohmann::json const g,
std::string_view  key,
std::string_view  default_value,
jtype  type = jtype::discarded 
)
inline

Gets the value (converted to a string) in the json object g with the key key, or default_value if none found.

Parameters
typethe value must also be of this type

Definition at line 200 of file json_helpers.h.

◆ get() [3/3]

template<std::integral T>
T ghassanpl::formats::json::get ( nlohmann::json const g,
std::string_view  key,
default_value,
jtype  type = jtype::discarded 
)
inline

Gets the value (converted to an integer) in the json object g with the key key, or default_value if none found.

Gets the value (converted to an floating point number) in the json object g with the key key, or default_value if none found.

Parameters
typethe value must also be of this type

Definition at line 210 of file json_helpers.h.

◆ get_array()

nlohmann::json const & ghassanpl::formats::json::get_array ( nlohmann::json const g,
std::string_view  key 
)
inline

Gets the array value in the json object g with the key key, or an empty array if none found.

Definition at line 228 of file json_helpers.h.

◆ load_file() [1/2]

nlohmann::json ghassanpl::formats::json::load_file ( std::filesystem::path const from)
inline

Definition at line 167 of file json_helpers.h.

◆ load_file() [2/2]

nlohmann::json ghassanpl::formats::json::load_file ( std::filesystem::path const from,
std::error_code &  ec 
)
inline

Definition at line 154 of file json_helpers.h.

◆ save_file()

void ghassanpl::formats::json::save_file ( std::filesystem::path const to,
nlohmann::json const j,
bool  pretty = true 
)
inline

Definition at line 179 of file json_helpers.h.

◆ try_load_file()

nlohmann::json ghassanpl::formats::json::try_load_file ( std::filesystem::path const from,
nlohmann::json const or_json = empty_json 
)
inline

Definition at line 160 of file json_helpers.h.

◆ type_name()

constexpr const char * ghassanpl::formats::json::type_name ( nlohmann::json::value_t  type)
constexprnoexcept

Definition at line 314 of file json_helpers.h.

◆ visit()

template<typename VISIT_FUNC >
auto ghassanpl::formats::json::visit ( nlohmann::json const j,
VISIT_FUNC &&  func 
)

Calls func with the actual value inside j; similar to std::visit

Definition at line 296 of file json_helpers.h.

Variable Documentation

◆ empty_json

const nlohmann::json ghassanpl::formats::json::empty_json
inline

Definition at line 150 of file json_helpers.h.

◆ empty_json_array

const nlohmann::json ghassanpl::formats::json::empty_json_array
inline

Definition at line 151 of file json_helpers.h.

◆ empty_json_object

const nlohmann::json ghassanpl::formats::json::empty_json_object
inline

Definition at line 152 of file json_helpers.h.