UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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?

3      

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

3      

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.

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.