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.
This commit is contained in:
Tomasz Wlostowski 2022-10-24 00:21:10 +02:00
parent cd29dcf6e0
commit f25d449d5f
2 changed files with 48 additions and 0 deletions

View File

@ -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<nlohmann::json*>( 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<nlohmann::json> JSON_SETTINGS::GetJson( const std::string& aPath ) const
{
nlohmann::json::json_pointer ptr = m_internals->PointerFromString( aPath );

View File

@ -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:
/**