From f25d449d5f3ce8833034daf762615dd3c0c31ee3 Mon Sep 17 00:00:00 2001 From: Tomasz Wlostowski Date: Mon, 24 Oct 2022 00:21:10 +0200 Subject: [PATCH] JSON_SETTINGS: added direct serialization to a string (FormatAsString) and parsing from explicitly provided file (bypassing the migration and settings framework). Used for router regression tests. --- common/settings/json_settings.cpp | 43 +++++++++++++++++++++++++++++++ include/settings/json_settings.h | 5 ++++ 2 files changed, 48 insertions(+) diff --git a/common/settings/json_settings.cpp b/common/settings/json_settings.cpp index db4100fd20..4ee7305251 100644 --- a/common/settings/json_settings.cpp +++ b/common/settings/json_settings.cpp @@ -471,6 +471,49 @@ bool JSON_SETTINGS::SaveToFile( const wxString& aDirectory, bool aForce ) } +const std::string JSON_SETTINGS::FormatAsString() const +{ + LOCALE_IO dummy; + + std::stringstream buffer; + buffer << std::setw( 2 ) << *m_internals << std::endl; + + return buffer.str(); +} + + +bool JSON_SETTINGS::LoadFromRawFile( const wxString& aPath ) +{ + try + { + wxFFileInputStream fp( aPath, wxT( "rt" ) ); + wxStdInputStream fstream( fp ); + + if( fp.IsOk() ) + { + *static_cast( m_internals.get() ) = + nlohmann::json::parse( fstream, nullptr, + /* allow_exceptions = */ true, + /* ignore_comments = */ true ); + } + else + { + return false; + } + } + catch( nlohmann::json::parse_error& error ) + { + wxLogTrace( traceSettings, wxT( "Json parse error reading %s: %s" ), aPath, error.what() ); + + return false; + } + + // Now that we have new data in the JSON structure, load the params again + Load(); + return true; +} + + std::optional JSON_SETTINGS::GetJson( const std::string& aPath ) const { nlohmann::json::json_pointer ptr = m_internals->PointerFromString( aPath ); diff --git a/include/settings/json_settings.h b/include/settings/json_settings.h index 702d8c200a..ba34831118 100644 --- a/include/settings/json_settings.h +++ b/include/settings/json_settings.h @@ -227,6 +227,11 @@ c * @return true if the file was saved */ static bool SetIfPresent( const nlohmann::json& aObj, const std::string& aPath, unsigned int& aTarget ); + + const std::string FormatAsString() const; + + bool LoadFromRawFile( const wxString& aPath ); + protected: /**