libnick 2025.9.1
A cross-platform base for native Nickvision applications.
Loading...
Searching...
No Matches
Nickvision::Helpers::JsonFileBase Class Reference

A base class for thread-safe json data files. More...

#include <jsonfilebase.h>

Inheritance diagram for Nickvision::Helpers::JsonFileBase:
Nickvision::Helpers::IJsonSerializable

Public Member Functions

 JsonFileBase (const std::filesystem::path &path)
 Constructs a JsonFileBase, loading the file from disk.
 
virtual ~JsonFileBase () noexcept=default
 Destructs a JsonFileBase.
 
const std::filesystem::path & getPath () const noexcept
 Gets the path of the json file.
 
Events::Event< Events::EventArgs > & saved () noexcept
 Gets the Saved event.
 
bool save () noexcept
 Saves the config file to disk.
 
boost::json::value toJson () const noexcept override
 Serializes the object to Json.
 
- Public Member Functions inherited from Nickvision::Helpers::IJsonSerializable
virtual ~IJsonSerializable ()=default
 

Protected Member Functions

bool contains (const std::string &key) const noexcept
 Gets whether or not the json object contains a key.
 
template<SupportedJsonValue T>
get (const std::string &key, const T &defaultValue) const noexcept
 Gets a value from the json object.
 
template<SupportedJsonValue T>
void set (const std::string &key, const T &value) noexcept
 Sets a value in the json object.
 

Detailed Description

A base class for thread-safe json data files.

Constructor & Destructor Documentation

◆ JsonFileBase()

Nickvision::Helpers::JsonFileBase::JsonFileBase ( const std::filesystem::path & path)

Constructs a JsonFileBase, loading the file from disk.

Parameters
pathThe path to the json file
Exceptions
std::invalid_argumentThrown if the path is empty

◆ ~JsonFileBase()

virtual Nickvision::Helpers::JsonFileBase::~JsonFileBase ( )
virtualdefaultnoexcept

Destructs a JsonFileBase.

Member Function Documentation

◆ contains()

bool Nickvision::Helpers::JsonFileBase::contains ( const std::string & key) const
protectednoexcept

Gets whether or not the json object contains a key.

◆ get()

template<SupportedJsonValue T>
T Nickvision::Helpers::JsonFileBase::get ( const std::string & key,
const T & defaultValue ) const
inlineprotectednoexcept

Gets a value from the json object.

Template Parameters
TThe type of the value to get
Parameters
keyThe key of the value to get
defaultValueThe default value to return if the key is not found or if the value is not of the expected type
Returns
The value associated with the key

◆ getPath()

const std::filesystem::path & Nickvision::Helpers::JsonFileBase::getPath ( ) const
noexcept

Gets the path of the json file.

Returns
The path of the json file

◆ save()

bool Nickvision::Helpers::JsonFileBase::save ( )
noexcept

Saves the config file to disk.

Returns
True if saved to disk, else false

◆ saved()

Events::Event< Events::EventArgs > & Nickvision::Helpers::JsonFileBase::saved ( )
noexcept

Gets the Saved event.

Returns
The Saved event

◆ set()

template<SupportedJsonValue T>
void Nickvision::Helpers::JsonFileBase::set ( const std::string & key,
const T & value )
inlineprotectednoexcept

Sets a value in the json object.

Template Parameters
TThe type of the value to set
Parameters
keyThe key of the value to set
valueThe value to set

◆ toJson()

boost::json::value Nickvision::Helpers::JsonFileBase::toJson ( ) const
overridevirtualnoexcept

Serializes the object to Json.

Returns
The Json representation of the object

Implements Nickvision::Helpers::IJsonSerializable.


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