21#ifndef _JSON_SETTINGS_H
22#define _JSON_SETTINGS_H
31#include <nlohmann/json_fwd.hpp>
65 JSON_SETTINGS( aFilename, aLocation, aSchemaVersion, true, true, true )
69 bool aCreateIfMissing,
bool aCreateIfDefault,
bool aWriteFile );
92 nlohmann::json&
At(
const std::string& aPath );
93 bool Contains(
const std::string& aPath )
const;
107 virtual bool Store();
114 virtual bool LoadFromFile(
const wxString& aDirectory =
"" );
121 virtual bool SaveToFile(
const wxString& aDirectory =
"",
bool aForce =
false );
134 std::optional<nlohmann::json>
GetJson(
const std::string& aPath )
const;
143 template<
typename ValueType>
144 std::optional<ValueType>
Get(
const std::string& aPath )
const;
153 template<
typename ValueType>
154 void Set(
const std::string& aPath, ValueType aVal );
202 static bool SetIfPresent(
const nlohmann::json& aObj,
const std::string& aPath,
211 static bool SetIfPresent(
const nlohmann::json& aObj,
const std::string& aPath,
bool& aTarget );
219 static bool SetIfPresent(
const nlohmann::json& aObj,
const std::string& aPath,
int& aTarget );
227 static bool SetIfPresent(
const nlohmann::json& aObj,
const std::string& aPath ,
228 unsigned int& aTarget );
245 std::function<
bool(
void)> aMigrator );
254 template<
typename ValueType>
255 bool fromLegacy( wxConfigBase* aConfig,
const std::string& aKey,
const std::string& aDest );
264 const std::string& aDest );
273 const std::string& aDest );
277 return wxT(
"json" );
282 return wxEmptyString;
292 template<
typename ResultType>
293 static ResultType
fetchOrDefault(
const nlohmann::json& aJson,
const std::string& aKey,
294 ResultType aDefault = ResultType() );
343template<> std::optional<wxString>
JSON_SETTINGS::Get(
const std::string& aPath )
const;
345template<>
void JSON_SETTINGS::Set<wxString>(
const std::string& aPath, wxString aVal );
349void to_json( nlohmann::json& aJson,
const wxString& aString );
351void from_json(
const nlohmann::json& aJson, wxString& aString );
bool fromLegacyString(wxConfigBase *aConfig, const std::string &aKey, const std::string &aDest)
Translates a legacy wxConfig string value to a given JSON pointer value.
virtual wxString getFileExt() const
std::optional< ValueType > Get(const std::string &aPath) const
Fetches a value from within the JSON document.
std::optional< nlohmann::json > GetJson(const std::string &aPath) const
Fetches a JSON object that is a subset of this JSON_SETTINGS object, using a path of the form "key1....
bool fromLegacy(wxConfigBase *aConfig, const std::string &aKey, const std::string &aDest)
Translates a legacy wxConfig value to a given JSON pointer value.
wxString m_filename
The filename (not including path) of this settings file (inicode)
SETTINGS_MANAGER * m_manager
A pointer to the settings manager managing this file (may be null)
bool Contains(const std::string &aPath) const
SETTINGS_LOC GetLocation() const
bool LoadFromRawFile(const wxString &aPath)
std::vector< NESTED_SETTINGS * > m_nested_settings
Nested settings files that live inside this one, if any.
virtual bool LoadFromFile(const wxString &aDirectory="")
Loads the backing file from disk and then calls Load()
bool m_createIfDefault
Whether or not the backing store file should be created if all parameters are still at their default ...
void SetLocation(SETTINGS_LOC aLocation)
bool m_writeFile
Whether or not the backing store file should be written.
static bool SetIfPresent(const nlohmann::json &aObj, const std::string &aPath, wxString &aTarget)
Sets the given string if the given key/path is present.
virtual void Load()
Updates the parameters of this object based on the current JSON document contents.
void SetManager(SETTINGS_MANAGER *aManager)
void SetReadOnly(bool aReadOnly)
void ResetToDefaults()
Resets all parameters to default values.
bool fromLegacyColor(wxConfigBase *aConfig, const std::string &aKey, const std::string &aDest)
Translates a legacy COLOR4D stored in a wxConfig string to a given JSON pointer value.
wxString GetFullFilename() const
bool m_createIfMissing
Whether or not the backing store file should be created it if doesn't exist.
bool Migrate()
Migrates the schema of this settings from the version in the file to the latest version.
std::vector< PARAM_BASE * > m_params
The list of parameters (owned by this object)
static ResultType fetchOrDefault(const nlohmann::json &aJson, const std::string &aKey, ResultType aDefault=ResultType())
Helper to retrieve a value from a JSON object (dictionary) as a certain result type.
virtual bool MigrateFromLegacy(wxConfigBase *aLegacyConfig)
Migrates from wxConfig to JSON-based configuration.
const std::string FormatAsString()
void registerMigration(int aOldSchemaVersion, int aNewSchemaVersion, std::function< bool(void)> aMigrator)
Registers a migration from one schema version to another.
nlohmann::json & At(const std::string &aPath)
Wrappers for the underlying JSON API so that most consumers don't need json.hpp All of these function...
JSON_SETTINGS_INTERNALS * Internals()
void ReleaseNestedSettings(NESTED_SETTINGS *aSettings)
Saves and frees a nested settings object, if it exists within this one.
void Set(const std::string &aPath, ValueType aVal)
Stores a value into the JSON document Will throw an exception if ValueType isn't something that the l...
JSON_SETTINGS(const wxString &aFilename, SETTINGS_LOC aLocation, int aSchemaVersion)
bool m_deleteLegacyAfterMigration
Whether or not to delete legacy file after migration.
std::unique_ptr< JSON_SETTINGS_INTERNALS > m_internals
void SetFilename(const wxString &aFilename)
SETTINGS_LOC m_location
The location of this settings file (.
virtual bool SaveToFile(const wxString &aDirectory="", bool aForce=false)
virtual wxString getLegacyFileExt() const
bool m_resetParamsIfMissing
Whether or not to set parameters to their default value if missing from JSON on Load()
void SetLegacyFilename(const wxString &aFilename)
std::map< int, std::pair< int, std::function< bool()> > > m_migrators
A map of starting schema version to a pair of <ending version, migrator function>
int m_schemaVersion
Version of this settings schema.
wxString m_legacy_filename
The filename of the wxConfig legacy file (if different from m_filename)
void AddNestedSettings(NESTED_SETTINGS *aSettings)
Transfers ownership of a given NESTED_SETTINGS to this object.
virtual bool Store()
Stores the current parameters into the JSON document represented by this object Note: this doesn't do...
wxString GetFilename() const
NESTED_SETTINGS is a JSON_SETTINGS that lives inside a JSON_SETTINGS.
const wxChar *const traceSettings
Flag to enable debug output of settings operations and management.
void from_json(const nlohmann::json &aJson, wxString &aString)
@ PROJECT
The settings directory inside a project folder.
@ USER
The main config directory (e.g. ~/.config/kicad/)
@ COLORS
The color scheme directory (e.g. ~/.config/kicad/colors/)
@ NESTED
Not stored in a file, but inside another JSON_SETTINGS.
@ NONE
No directory prepended, full path in filename (used for PROJECT_FILE)
void to_json(nlohmann::json &aJson, const wxString &aString)