NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

Encoder/Decoder options for document-based apps (Hacking with macOS: SwiftUI Edition)

Forums > Books

In the latest refresh of Hacking with macOS: SwiftUI Edition, the JSONDecoder and JSONEncoder are used in the document-based app Screenable. Is this the best Encoding/Decoding option or just the easiest. My document could be large and complex and like to use something that is disk-space efficient. How much affect does the Encoding/Decoding have on the file size of the document?

   

hi Jim,

certainly, the right answer is "it depends ..." on what you're saving.

using JSON is indeed easy, but one thing that potentially inflates file size is saving many objects of the same type where the length of variable names is a little long.

example: if you have structs/objects with variable names such as lengthOfGame, numberOfPlayers, crowdSize, finalScore, matchWentIntoOvertime, and so forth, every object encoding will have these strings as idenitifers in the JSON. if you have a few thousand of these, that's a problem (not so much for speed of encoding/decoding, but for JSON filelength).

so, do some testing with JSON. but use CodingKeys to control the names actually written to JSON, as in this example that i have used in my code to change longer, readable variable names into condensed identifiers for JSON.

    enum CodingKeys: String, CodingKey {
        case bidLevel = "bLvl"
        case dealerIndex = "dlrIdx"
        case declarerIndex = "dclIdx"
        case doubledState = "dbl"
        case handIndex = "hIdx"
        case playerIDs = "pIDs"
        case previousHandIndex = "pIdx"
        case roundIndex = "rIdx"
        case score = "scr"
        case sessionID = "sID"
        case suit = "st"
        case tricksTaken = "tTakn"
        case vulnerable = "vul"
        case vulnerabilityStyle = "vulStl"
    }

then test again. my recollection is the i was decreasing file size by as much as 40% just with this alone on about 2500 objects. (i suppose changing the coding keys to numbers would help even more.)

hope that helps,

DMG

   

Of course, you don't have to use JSON with Codable. If a different format would work better for your document, you could always implement a custom (de|en)coding format. It's not particularly difficult, but it is a lot of code to implement. See chapter 7 in the Flight School Guide to Swift Codable for an example of implementing a binary format using Codable.

   

Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.