From f076f6fb4632dbcefa9bf80368699f8038a3c236 Mon Sep 17 00:00:00 2001
From: Jon Evans <jon@craftyjon.com>
Date: Thu, 16 Feb 2023 23:15:44 -0500
Subject: [PATCH] Preserve unknown keys in JSON_SETTINGS

Fixes https://gitlab.com/kicad/code/kicad/-/issues/13847
---
 common/settings/json_settings.cpp          | 9 ++++++++-
 include/settings/json_settings_internals.h | 4 ++++
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/common/settings/json_settings.cpp b/common/settings/json_settings.cpp
index 6a4c01d083..5c2e177d7d 100644
--- a/common/settings/json_settings.cpp
+++ b/common/settings/json_settings.cpp
@@ -269,6 +269,9 @@ bool JSON_SETTINGS::LoadFromFile( const wxString& aDirectory )
                                                /* allow_exceptions = */ true,
                                                /* ignore_comments  = */ true );
 
+                // Save whatever we loaded, before doing any migration etc
+                m_internals->m_original = *static_cast<nlohmann::json*>( m_internals.get() );
+
                 // If parse succeeds, check if schema migration is required
                 int filever = -1;
 
@@ -438,10 +441,14 @@ bool JSON_SETTINGS::SaveToFile( const wxString& aDirectory, bool aForce )
     LOCALE_IO dummy;
     bool success = true;
 
+    nlohmann::json toSave = m_internals->m_original;
+
+    toSave.update( m_internals->begin(), m_internals->end(), /* merge_objects = */ true );
+
     try
     {
         std::stringstream buffer;
-        buffer << std::setw( 2 ) << *m_internals << std::endl;
+        buffer << std::setw( 2 ) << toSave << std::endl;
 
         wxFFileOutputStream fileStream( path.GetFullPath(), "wb" );
 
diff --git a/include/settings/json_settings_internals.h b/include/settings/json_settings_internals.h
index d34f46d14c..f7b0d2feb2 100644
--- a/include/settings/json_settings_internals.h
+++ b/include/settings/json_settings_internals.h
@@ -73,6 +73,10 @@ public:
         nlohmann::json::json_pointer root( "" );
         this->nlohmann::json::operator[]( root ) = aOther.nlohmann::json::operator[]( root );
     }
+
+private:
+
+    nlohmann::json m_original;
 };
 
 #endif // KICAD_JSON_SETTINGS_INTERNALS_H