Krotos Modules 3
Loading...
Searching...
No Matches
krotos::JsonFile Class Reference

#include <JsonFile.h>

Inheritance diagram for krotos::JsonFile:
krotos::Serialiser krotos::SettingsFile

Public Member Functions

 JsonFile (File jsonFile, const Identifier &defaultProperty, const String &defaultFormat)
 Simple helper class for handling json files. It will attempt to create a json file with the given default property and format, or if the given json file already exists, it will attempt to parse it and check it has the default property.
 
var getDefaultProperty () const
 Get the variant for the default property.
 
const String & getDefaultFormat () const
 Gets the underlying default formatted json, this is used as a fallback when an invalid input is passed to the JSON file.
 

Protected Member Functions

void initialise ()
 Parse the file if it exists, or create it if it doesn't. Data is parsed to m_data;.
 
void save ()
 Call save internal.
 
void createJsonFromDefaultFormat ()
 Generate the JSON file using the default format - used in cases where JSON fails to load.
 

Protected Attributes

var m_data
 The parsed json data.
 
File m_jsonFile
 The json file.
 

Private Member Functions

void saveInternal ()
 Save any changes made to m_data back to the Json file.
 

Private Attributes

Identifier m_defaultProperty
 
String m_defaultFormat
 

Constructor & Destructor Documentation

◆ JsonFile()

krotos::JsonFile::JsonFile ( File jsonFile,
const Identifier & defaultProperty,
const String & defaultFormat )

Simple helper class for handling json files. It will attempt to create a json file with the given default property and format, or if the given json file already exists, it will attempt to parse it and check it has the default property.

The json data is parsed to a protected variant object, m_data. This object may be manipulated directly, to make any changes to the data, and saved back to the file using the save() method.

Parameters
jsonFileThe file to create or parse from if it already exists
defaultPropertyThe default property. This should be the first json property you expect to find in your json file. This is used as a simple way to validate this json file is the one we expect it to be. e.g. myJsonData_version
defaultFormatThe default json template this should use. This will be what is written to the file as a fallback if the file does not exist or there are errors in parsing.

Member Function Documentation

◆ createJsonFromDefaultFormat()

void krotos::JsonFile::createJsonFromDefaultFormat ( )
protected

Generate the JSON file using the default format - used in cases where JSON fails to load.

◆ getDefaultFormat()

const String & krotos::JsonFile::getDefaultFormat ( ) const
inline

Gets the underlying default formatted json, this is used as a fallback when an invalid input is passed to the JSON file.

Returns
A const reference to the default format string.

◆ getDefaultProperty()

var krotos::JsonFile::getDefaultProperty ( ) const
inline

Get the variant for the default property.

◆ initialise()

void krotos::JsonFile::initialise ( )
protected

Parse the file if it exists, or create it if it doesn't. Data is parsed to m_data;.

◆ save()

void krotos::JsonFile::save ( )
protected

Call save internal.

◆ saveInternal()

void krotos::JsonFile::saveInternal ( )
private

Save any changes made to m_data back to the Json file.

Member Data Documentation

◆ m_data

var krotos::JsonFile::m_data
protected

The parsed json data.

◆ m_defaultFormat

String krotos::JsonFile::m_defaultFormat
private

◆ m_defaultProperty

Identifier krotos::JsonFile::m_defaultProperty
private

◆ m_jsonFile

File krotos::JsonFile::m_jsonFile
protected

The json file.


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