title: Swift: Add property to a struct that gets encoded to JSON
body: <p>I have a Struct for the user's preferences and this object gets encoded and stored as a JSON file and stored for iCloud Document Sync (I found this to be more reliable than NSUbiquitousContainer, even though that's what you're supposed to use for preferences).</p>
  <p>Let's say in v1 of the app the struct looks like this and all users have a JSON file with these 3 properties.</p>
  <pre><code>struct Preferences: Codable {
      var soundsDisabled: Bool = false
      var hapticsDisabled: Bool = false
      var badgeNumberEnabled: Bool = false
  }
  </code></pre>
  <p>What I need to figure out is if there is a way to add a 4th property in v1.1 to this Struct without messing up the existing JSON. Right now if I do add a new property, everything gets reset to the default values and the user's preferences get lost.</p>
  <p>UPDATE: this is how this struct gets encoded to JSON</p>
  <pre><code>extension Storage {
      
      static var preferences: Preferences {
          get {
              guard
                  let data = try? Data(contentsOf: Storage.preferencesFile),
                  let decoded = try? JSONDecoder().decode(Preferences.self, from: data)
              else { return Preferences() }
              return decoded
          }
          set {
              do { try JSONEncoder().encode(newValue).write(to: Storage.preferencesFile, options: .atomic) }
              catch let error {
                  print(&quot;Failed to write Preferences: \(error.localizedDescription)&quot;)
                  MSAnalytics.trackEvent(&quot;Failed to write preferences&quot;, withProperties: [&quot;Error&quot;: error.localizedDescription])
              }
          }
      }
  
  }
  </code></pre>
  <p>Thank you.</p>