MySQL 9.3.0
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
shcore::json Namespace Reference

Namespaces

namespace  anonymous_namespace{utils_json.cc}
 

Typedefs

using JSON = rapidjson::Document
 

Functions

JSON parse (std::string_view json)
 Parses a JSON string. More...
 
JSON parse_object_or_throw (std::string_view json)
 Parses a JSON string, input is expected to be a JSON object. More...
 
std::string required (const JSON &json, const char *name, bool allow_empty=false)
 Fetches a string value of a required field. More...
 
uint64_t required_uint (const JSON &json, const char *name)
 Fetches an unsigned integer value of a required field. More...
 
std::optional< std::string > optional (const JSON &json, const char *name, bool allow_empty=false)
 Fetches a string value of an optional field. More...
 
std::optional< uint64_t > optional_uint (const JSON &json, const char *name)
 Fetches an unsigned integer value of an optional field. More...
 
std::string to_string (const JSON &json)
 Converts a JSON object to a string. More...
 
std::string to_pretty_string (const JSON &json)
 Converts a JSON object to a nicely formatted string. More...
 

Typedef Documentation

◆ JSON

using shcore::json::JSON = typedef rapidjson::Document

Function Documentation

◆ optional()

std::optional< std::string > shcore::json::optional ( const JSON json,
const char *  name,
bool  allow_empty = false 
)

Fetches a string value of an optional field.

Parameters
jsonJSON object.
nameName of the field.
allow_emptyDon't throw if value is an empty string.
Returns
string value if JSON contains the specified field
Exceptions
std::runtime_errorif value is not a string

◆ optional_uint()

std::optional< uint64_t > shcore::json::optional_uint ( const JSON json,
const char *  name 
)

Fetches an unsigned integer value of an optional field.

Parameters
jsonJSON object.
nameName of the field.
Returns
unsigned integer value if JSON contains the specified field
Exceptions
std::runtime_errorif value is not an unsigned integer

◆ parse()

JSON shcore::json::parse ( std::string_view  json)

Parses a JSON string.

NOTE: this does not throw exceptions, parsed document can be potentially invalid.

Parameters
jsonJSON to be parsed.
Returns
parsed document

◆ parse_object_or_throw()

JSON shcore::json::parse_object_or_throw ( std::string_view  json)

Parses a JSON string, input is expected to be a JSON object.

Parameters
jsonJSON to be parsed.
Returns
parsed document
Exceptions
std::runtime_errorif parsing fails, or result is not a JSON object.

◆ required()

std::string shcore::json::required ( const JSON json,
const char *  name,
bool  allow_empty = false 
)

Fetches a string value of a required field.

Parameters
jsonJSON object.
nameName of the field.
allow_emptyDon't throw if value is an empty string.
Returns
string value
Exceptions
std::runtime_errorif field does not exist or its value is not a string

◆ required_uint()

uint64_t shcore::json::required_uint ( const JSON json,
const char *  name 
)

Fetches an unsigned integer value of a required field.

Parameters
jsonJSON object.
nameName of the field.
Returns
unsigned integer value
Exceptions
std::runtime_errorif field does not exist or its value is not an unsigned integer

◆ to_pretty_string()

std::string shcore::json::to_pretty_string ( const JSON json)

Converts a JSON object to a nicely formatted string.

Parameters
jsonA JSON object
Returns
String representation of a JSON object.

◆ to_string()

std::string shcore::json::to_string ( const JSON json)

Converts a JSON object to a string.

Parameters
jsonA JSON object
Returns
String representation of a JSON object.