irccd  3.0.3
Public Member Functions | List of all members
irccd::json_util::deserializer Class Reference

Convenient JSON object parser. More...

#include <json_util.hpp>

Inheritance diagram for irccd::json_util::deserializer:

Public Member Functions

 deserializer (const nlohmann::json &obj)
 
template<typename Type >
auto get (const std::string &key) const noexcept -> std::optional< Type >
 
template<typename Type , typename DefaultValue >
auto optional (const std::string &key, DefaultValue &&def) const noexcept -> std::optional< Type >
 

Detailed Description

Convenient JSON object parser.

This class helps destructuring insecure JSON input by returning optional values if they are not present or invalid.

Constructor & Destructor Documentation

◆ deserializer()

irccd::json_util::deserializer::deserializer ( const nlohmann::json &  obj)

Constructor.

Parameters
objthe JSON object

Member Function Documentation

◆ get()

template<typename Type >
auto irccd::json_util::deserializer::get ( const std::string &  key) const -> std::optional<Type>
inlinenoexcept

Get a value from the document object.

Parameters
keythe property key
Returns
the value or std::nullopt if not found or not convertible

◆ optional()

template<typename Type , typename DefaultValue >
auto irccd::json_util::deserializer::optional ( const std::string &  key,
DefaultValue &&  def 
) const -> std::optional<Type>
inlinenoexcept

Get an optional value from the document object.

If the value is undefined, the default value is returned. Otherwise, if the value is not in the given type, std::nullopt is returned.

Parameters
keythe property key
defthe default value if property is undefined
Returns
the value, std::nullopt or def

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