Version A Small Codable File Without Losing Old Data
A saved JSON file starts with a title. A later release renames that field and adds a favorite flag. The tempting fix is to catch decoding errors and return an empty document. That makes the screen open, but it can also turn the next autosave into data loss.
Treat a decoding failure as a failed load. A migration should understand the old format before it produces the new one.
Put the format version in the file
For a small app-owned document, an integer version and a few concrete types are often enough. This first format stored name at the top level:
{"version": 1, "name": "A saved thought"}
The current format stores a note with a title and favorite state:
{"version": 2, "note": {"title": "A saved thought", "favorite": false}}
The version describes the stored representation. It is independent of the app’s marketing version and build number. Several app releases can use the same format.
Decode the old shape honestly
Apple’s Codable guide explains how types describe their encoded representation. Keep a type for each format you actually support rather than making every field optional until everything decodes.
import Foundation
struct SavedNote: Codable, Equatable {
var title: String
var favorite: Bool
}
private struct Header: Decodable { let version: Int }
private struct V1: Decodable {
let version: Int
let name: String
}
private struct V2: Codable {
let version: Int
let note: SavedNote
}
enum FileVersionError: Error { case unsupported(Int) }
func loadNote(_ data: Data) throws -> SavedNote {
let decoder = JSONDecoder()
switch try decoder.decode(Header.self, from: data).version {
case 1:
let old = try decoder.decode(V1.self, from: data)
return SavedNote(title: old.name, favorite: false)
case 2:
return try decoder.decode(V2.self, from: data).note
case let version:
throw FileVersionError.unsupported(version)
}
}
func encodeNote(_ note: SavedNote) throws -> Data {
try JSONEncoder().encode(V2(version: 2, note: note))
}
The first decoding pass reads only the header. The selected branch then validates the entire shape expected for that version. Missing favorite in version 2 is an error; it is not silently treated as a version 1 document.
The migration assigns false because version 1 had no favorite concept. That is a product decision expressed in one place. A property initializer on the current model is not a general replacement for an explicit decoding or migration rule.
This example requires a version field even in the oldest supported format. If your real first release wrote unversioned files, recognize that specific legacy schema deliberately. Do not classify every missing or malformed version as old data.
Do not overwrite a format you cannot read
Version 99 takes the unsupported path. That matters when a user opens a document with a newer app, then returns to an older installation.
Keep the original bytes and show a recoverable unsupported-format state. Disable editing and autosave for that failed load. Creating a blank editable model next to an error banner is unsafe if the usual save machinery can still replace the source file.
Similarly, malformed JSON and wrong field types are not evidence that the user wanted a reset. Offer recovery, export of the original, or an explicit new document action according to the app’s needs.
Separate migration from replacement
loadNote only transforms bytes into an in-memory value. It does not write anything. Once that succeeds, the app can encode version 2 and use its established atomic replacement path.
If several scenes can edit the same file, format migration does not settle which edit wins. Keep revision checks and file coordination in the persistence layer. An old scene must not overwrite a newer document merely because both scenes can decode version 2.
For an irreversible migration, retaining a recovery copy may be appropriate. Decide its lifetime and storage cost explicitly; accumulating a full backup on every autosave is a different problem.
Test the files users already have
Check representative version 1 fixtures into the test target. Verify the title mapping and chosen favorite default. Then round-trip a version 2 value, reject an unsupported future version, and reject malformed fields in both known versions.
Also test the loader’s surrounding behavior: a failed load must never schedule an autosave. That application-level assertion catches the destructive failure that a decoder unit test alone cannot see.
September 2026 review: the Swift 6 companion passed legacy mapping, current-format round-trip, unsupported-version, and malformed-field checks. It performs no disk replacement; atomic saving and crash recovery require separate persistence tests.