Extensions giving Swift's Codable API type inference super powers 🦸‍♂️🦹‍♀️

Overview

Codextended

Swift Package Manager Mac + Linux Twitter: @johnsundell

Welcome to Codextended — a suite of extensions that aims to make Swift’s Codable API easier to use by giving it type inference-powered capabilities and conveniences. It’s not a wrapper, nor is it a brand new framework, instead it augments Codable directly in a very lightweight way.

Codable is awesome!

No third-party serialization framework can beat the convenience of Codable. Since it’s built in, it can both leverage the compiler to automatically synthesize all serialization code needed in many situations, and it can also be used as a common bridge between multiple different modules — without having to introduce any shared dependencies.

However, once some form of customization is needed — for example to transform parts of the decoded data, or to provide default values for certain keys — the standard Codable API starts to become really verbose. It also doesn’t take advantage of Swift’s robust type inference capabilities, which produces a lot of unnecessary boilerplate.

That’s what Codextended aims to fix.

Examples

Here are a few examples that demonstrate the difference between using “vanilla” Codable and the APIs that Codextended adds to it. The goal is to turn all common serialization operations into one-liners, rather than having to set up a ton of boilerplate.

🏢 Top-level API

Codextended makes a few slight tweaks to the top-level API used to encode and decode values, making it possible to leverage type inference and use methods on the actual values that are being encoded or decoded.

🍨 With vanilla Codable:

// Encoding
let encoder = JSONEncoder()
let data = try encoder.encode(value)

// Decoding
let decoder = JSONDecoder()
let article = try decoder.decode(Article.self, from: data)

🦸‍♀️ With Codextended:

// Encoding
let data = try value.encoded()

// Decoding
let article = try data.decoded() as Article

// Decoding when the type can be inferred
try saveArticle(data.decoded())

🔑 Overriding the behavior for a single key

While Codable is amazing as long as the serialized data’s format exactly matches the format of the Swift types that’ll use it — as soon as we need to make just a small tweak, things quickly go from really convenient to very verbose.

As an example, let’s just say that we want to provide a default value for one single property (without having to make it an optional, which would make it harder to handle in the rest of our code base). To do that, we need to completely manually implement our type’s decoding — like below for the tags property of an Article type.

🍨 With vanilla Codable:

struct Article: Codable {
    enum CodingKeys: CodingKey {
        case title
        case body
        case footnotes
        case tags
    }

    var title: String
    var body: String
    var footnotes: String?
    var tags: [String]

    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        title = try container.decode(String.self, forKey: .title)
        body = try container.decode(String.self, forKey: .body)
        footnotes = try container.decodeIfPresent(String.self, forKey: .footnotes)
        tags = (try? container.decode([String].self, forKey: .tags)) ?? []
    }
}

🦸‍♂️ With Codextended:

struct Article: Codable {
    var title: String
    var body: String
    var footnotes: String?
    var tags: [String]

    init(from decoder: Decoder) throws {
        title = try decoder.decode("title")
        body = try decoder.decode("body")
        footnotes = try decoder.decodeIfPresent("footnotes")
        tags = (try? decoder.decode("tags")) ?? []
    }
}

Codextended includes decoding overloads both for CodingKey-based values and for string literals, so that we can pick the approach that’s the most appropriate/convenient for each given situation.

📆 Using date formatters

Codable already comes with support for custom date formats through assigning a DateFormatter to either a JSONEncoder or JSONDecoder. However, requiring each call site to be aware of the specific date formats used for each type isn’t always great — so with Codextended, it’s easy for a type itself to pick what date format it needs to use.

That’s really convenient when working with third-party data, and we only want to customize the date format for some of our types, or when we want to produce more readable date strings when encoding a value.

🍨 With vanilla Codable:

struct Bookmark: Codable {
    enum CodingKeys: CodingKey {
        case url
        case date
    }

    struct DateCodingError: Error {}

    static let dateFormatter = makeDateFormatter()

    var url: URL
    var date: Date

    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        url = try container.decode(URL.self, forKey: .url)

        let dateString = try container.decode(String.self, forKey: .date)

        guard let date = Bookmark.dateFormatter.date(from: dateString) else {
            throw DateCodingError()
        }

        self.date = date
    }

    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(url, forKey: .url)

        let dateString = Bookmark.dateFormatter.string(from: date)
        try container.encode(dateString, forKey: .date)
    }
}

🦹‍♀️ With Codextended:

struct Bookmark: Codable {
    static let dateFormatter = makeDateFormatter()

    var url: URL
    var date: Date

    init(from decoder: Decoder) throws {
        url = try decoder.decode("url")
        date = try decoder.decode("date", using: Bookmark.dateFormatter)
    }

    func encode(to encoder: Encoder) throws {
        try encoder.encode(url, for: "url")
        try encoder.encode(date, for: "date", using: Bookmark.dateFormatter)
    }
}

Again, we could’ve chosen to use a CodingKeys enum above to represent our keys, rather than using inline strings.

Mix and match

Since Codextended is 100% implemented through extensions, you can easily mix and match it with “vanilla” Codable code within the same project. It also doesn’t change what makes Codable so great — the fact that it often doesn’t require any manual code at all, and that it can be used as a bridge between frameworks.

All it does is give Codable a helping hand when some form of customization is needed.

Installation

Since Codextended is implemented within a single file, the easiest way to use it is to simply drag and drop it into your Xcode project.

But if you wish to use a dependency manager, you can either use the Swift Package Manager by declaring Codextended as a dependency in your Package.swift file:

.package(url: "https://github.com/JohnSundell/Codextended", from: "0.1.0")

For more information, see the Swift Package Manager documentation.

You can also use CocoaPods by adding the following line to your Podfile:

pod "Codextended"

Contributions & support

Codextended is developed completely in the open, and your contributions are more than welcome.

Before you start using Codextended in any of your projects, it’s highly recommended that you spend a few minutes familiarizing yourself with its documentation and internal implementation (it all fits in a single file!), so that you’ll be ready to tackle any issues or edge cases that you might encounter.

To learn more about the principles used to implement Codextended, check out “Type inference-powered serialization in Swift” on Swift by Sundell.

This project does not come with GitHub Issues-based support, and users are instead encouraged to become active participants in its continued development — by fixing any bugs that they encounter, or improving the documentation wherever it’s found to be lacking.

If you wish to make a change, open a Pull Request — even if it just contains a draft of the changes you’re planning, or a test that reproduces an issue — and we can discuss it further from there.

Hope you’ll enjoy using Codextended! 😀

Comments
  • Added EncodeTransformable & DecodeTransformable

    Added EncodeTransformable & DecodeTransformable

    Hey John 🙌,

    First of all great job with Codextended 👍

    This PR refactored the encode and decode API when using an DateFormatter to an protocol-oriented solution, allowing to pass any kind of EncodeTransformable or DecodeTransformable implementation in order to transform / convert / manipulate the value.

    public typealias Transformable = EncodeTransformable & DecodeTransformable
    
    public protocol EncodeTransformable {
        associatedtype EncodeSourceType
    
        associatedtype EncodeTargetType: Encodable
    
        func transformToEncodable(value: EncodeSourceType) throws -> EncodeTargetType
    }
    
    public protocol DecodeTransformable {
        associatedtype DecodeSourceType: Decodable
    
        associatedtype DecodeTargetType
    
        func transformFromDecodable(value: DecodeSourceType) throws -> DecodeTargetType
    }
    

    The DateFormatter has already been adopted to the Transformable protocol which allows to use the encode and decode API when passing an DateFormatter as there was no change.

    extension DateFormatter: Transformable {
        public func transformToEncodable(value: Date) throws -> String {
            return self.string(from: value)
        }
        
        public func transformFromDecodable(value: String) throws -> Date? {
            return self.date(from: value)
        }   
    }
    

    Even nicer developers can now pass any kind of EncodeTransformable or DecodeTransformable to the encode and decode API. For example a simple IntToStringTransformable.

    struct Article: Codable {
        var id: String
        var body: String
        var date: Date?
    
        init(from decoder: Decoder) throws {
            id = try container.decode("id", using: IntToStringTransformable())
            body = try container.decode("body")
            date = try container.decode("date", using: DateFormatter())
        }
    }
    
    struct IntToStringTransformable: Transformable {
        func transformFromDecodable(value: Int) throws -> String {
            return .init(value)
        }
        
        func transformToEncodable(value: String) throws -> Int? {
            return Int(value)
        }
    }
    

    Additional thoughts: The current Transformable implementation for the DateFormatter will return an optional Date, based on the date(from:) API of the DateFormatter itself, when invoking the transformFromDecodable API.

    Before a DecodingError has been thrown when the Date is nil. I was not a 100% sure if throwing an Error or returning an optional Date is better.

    So I chose the second option so that the developer can decide if either to provide a default value or manually thrown an DecodingError.

    I'm excited about your feedback ✌️

    opened by SvenTiigi 3
  • Implemented decodeArraySafely

    Implemented decodeArraySafely

    Hi John! I really enjoyed the philosophy of this library and I'm happy to have these improvements to Codable in the open.

    One of the frustrations that i always have to workaround in Codable is having a way to safely decode an array of objects safely ignoring the ones that fail to decode, instead of blowing up the entire array as the default system does.

    So trying to decode { "numbers": [ 1, 2, "3", 4 ] } to let numbers: [Int] won't fail, instead you will receive an array of [1, 2, 4].

    I thought that this may be a common enough use case to include it in the library so I spend some time porting it and trying to match the style of this code. I also wrote test to make sure the normal case (all elements are correct) works and that the case of elements failing also works. (There are 2 assertions in the same test, if you prefer to split them feel free to tell me).

    Suffices to say that if you don't consider this to be inline with the library's intend feel free to close it 😉

    opened by alexito4 2
  • Add properties to AnyDateFormatter

    Add properties to AnyDateFormatter

    Why

    Publish supports the use of a custom DateFormatter for decoding dates. It would be nice to allow users to also use ISO8601DateFormatter as the custom date formatter.

    See previous discussion on the issue here: Publish PR #77.

    Problem

    AnyDateFormatter does not provide all the properties that Publish needs in order to use it as a replacement for DateFormatter.

    Solution

    Add two properties to the AnyDateFormatter protocol to allow us to replace DateFormatter in Publish.

    • timeZone: TimeZone!
    • dateFormat: String!

    dateFormat is readonly as Publish only needs to read this value for debugging purposes. DateFormatter already implements this requirement, so we only needed to provide a value for ISO8601DateFormatter that will be useful during debugging.

    You can see how these changes would be integrated into Publish by looking at this draft PR. https://github.com/JohnSundell/Publish/pull/109

    opened by addisonwebb 1
  • Support error propagation for optional properties

    Support error propagation for optional properties

    Hi John,

    The key motivation for this addition was to enable propagation of errors, primarily DecodingError.typeMismatch, when attempting to decode values into optional properties. While the existing functionality for optionals, supported by the try? notation, is valid for many use cases, it does not allow for proper alerting when the key is present, but of an incorrect type.

    I am certainly open to any feedback and very much appreciate your consideration.

    opened by eseay 1
  • Just an idea to help you extend Codextended

    Just an idea to help you extend Codextended

    code comes directly from https://github.com/evermeer/Stuff#codable Feel free to use any of it in Codextended. it's not formatted conform your coding style and not tested for incompatibilities with your code.

    Here is a short list of what you could do with this.

    let json = yourEncodableObjectInstance.toJsonString() let data = yourEncodableObjectInstance.toJsonData() let newObject = try? YourCodableObject(json: json) let newObject2 = try? YourCodableObject(data: data) let objectArray = try? [YourCodableObject](json: json) let objectArray2 = try? [YourCodableObject](data: data) let newJson = objectArray.toJsonString() let innerObject = try? TestCodable(json: "{"user":{"id":1,"naam":"Edwin"}}", keyPath: "user") try initialObject.saveToDocuments("myFile.dat") let readObject = try? TestCodable(fileNameInDocuments: "myFile.dat") try objectArray.saveToDocuments("myFile2.dat") let objectArray3 = try? TestCodable

    opened by evermeer 1
  • Add support for ISO-8601-formatted dates

    Add support for ISO-8601-formatted dates

    This change adds support for the common ISO-8601 date format, by making it possible to pass a ISO8601DateFormatter when encoding or decoding a date (on supported platforms).

    opened by JohnSundell 0
  • Add nested decoding

    Add nested decoding

    Hi!

    This allows decoding values from nested containers like this:

    {
        "a": {
            "b": {
                "c": "hello world"
            }
        }
    }
    

    with:

    decoder.decode(["a","b","c"])
    

    I thought of adding a dot syntax (eg: decoder.decodeNested("a.b.c"), but found it unnecessary.

    I'm not sure if CodextendedDecodingError works with Codextended's philosophy...

    It also may be good to add encoding and update readme.

    opened by dtadic 6
Releases(0.3.0)
  • 0.3.0(May 14, 2019)

    This version of Codextended adds an API for decoding optionals while still throwing an error if the decoding failed; named decodeIfPresent (same as vanilla Codable). Thanks to @eseay for implementing this 👍

    Source code(tar.gz)
    Source code(zip)
  • 0.2.0(Apr 10, 2019)

    • PropertyListDecoder is now supported on non-Apple platforms when using Swift 5.1 (thanks @pvieito!)
    • ISO8601DateFormatter can now be used when decoding or encoding dates
    Source code(tar.gz)
    Source code(zip)
  • 0.1.0(Apr 8, 2019)

Owner
John Sundell
I build apps, games and Swift developer tools! Passionate about open source & developer productivity. You can follow me on Twitter @JohnSundell.
John Sundell
✨ Super sweet syntactic sugar for Swift initializers

Then ✨ Super sweet syntactic sugar for Swift initializers. At a Glance Initialize UILabel then set its properties. let label = UILabel().then { $0.t

Suyeol Jeon 4k Jan 4, 2023
Super powerful remote config utility written in Swift (iOS, watchOS, tvOS, OSX)

Mission Control Super powerful remote config utility written in Swift (iOS, watchOS, tvOS, OSX) Brought to you by Have you ever wished you could chang

appculture 113 Sep 9, 2022
BFKit-Swift is a collection of useful classes, structs and extensions to develop Apps faster.

Features • Classes and Extensions Compatibility • Requirements • Communication • Contributing • Installing and Usage • Documentation • Changelog • Exa

Fabrizio Brancati 992 Dec 2, 2022
A handy collection of more than 500 native Swift extensions to boost your productivity.

SwifterSwift is a collection of over 500 native Swift extensions, with handy methods, syntactic sugar, and performance improvements for wide range of

SwifterSwift 12k Jan 7, 2023
A Swift package for rapid development using a collection of micro utility extensions for Standard Library, Foundation, and other native frameworks.

ZamzamKit ZamzamKit is a Swift package for rapid development using a collection of micro utility extensions for Standard Library, Foundation, and othe

Zamzam Inc. 261 Dec 15, 2022
Handy Combine extensions on NSObject, including Set.

Storable Description If you're using Combine, you've probably encountered the following code more than a few times. class Object: NSObject { var c

hcrane 23 Dec 13, 2022
🌤 Swift Combine extensions for asynchronous CloudKit record processing

Swift Combine extensions for asynchronous CloudKit record processing. Designed for simplicity.

Chris Araman 46 Dec 8, 2022
SharkUtils is a collection of Swift extensions, handy methods and syntactical sugar that we use within our iOS projects at Gymshark.

SharkUtils is a collection of Swift extensions, handy methods and syntactical sugar that we use within our iOS projects at Gymshark.

Gymshark 1 Jul 6, 2021
Helpful extensions for iOS app development 🚀

ExtensionKit includes many extensions, from getting the user location with a deterministic Combine API to a shimmer loading animation, to keyboard notification updates, bottom sheet and much much more. Check out the docs below or install the library with SPM to try it out.

Gary Tokman 110 Oct 31, 2022
Extendy - A set of useful string extensions.

Extendy A set of useful string extensions. Requirements iOS 11.0+ Swift 5+ Installation CocoaPods Extendy is available through CocoaPods. To install i

Anton Novichenko 3 Sep 23, 2021
Extensions that allow you to work with optionals

RxOptionals Sometimes it happens that you need to bind to several optional binders or to an optional method (for example, when using weak). To do this

Dane4ka 3 Aug 9, 2021
Useful Swift code samples, extensions, functionalities and scripts to cherry-pick and use in your projects

SwiftyPick ?? ?? Useful Swift code samples, extensions, functionalities and scripts to cherry-pick and use in your projects. Purpose The idea behind t

Manu Herrera 19 May 12, 2022
Useful extensions for my Swift code

UIViewController extensions presentAlert(withTitle title: String, message : String) presentAlertDialog(withTitle title: String, message : String, acti

Bogdan Grafonsky 1 Oct 17, 2021
Steps and files needed to reproduce a CSP bug in Safari Web Extensions

CSP Safari bug repro There appears to be a discrepancy between how Safari handles CSP policies for extension pages compared to how other browsers do s

Brian Birtles 0 Nov 6, 2021
Personally useful Swift Extensions for iOS Development

Useful-Swift-Extensions Personally useful Swift Extensions for iOS Development; cobbled together from a variety of development projects and StackOverf

Nick Arner 5 Dec 13, 2021
Swift extensions for asynchronous CloudKit record processing

⛅️ AsyncCloudKit Swift extensions for asynchronous CloudKit record processing. D

Chris Araman 17 Dec 8, 2022
Easier sharing of structured data between iOS applications and share extensions

XExtensionItem XExtensionItem is a tiny library allowing for easier sharing of structured data between iOS applications and share extensions. It is ta

Tumblr 86 Nov 23, 2022
Extensions for Swift Standard Types and Classes

Cent Cent is a library that extends certain Swift object types using the extension feature and gives its two cents to Swift language. Dollar is a Swif

Ankur Patel 225 Dec 7, 2022
Useful functions and extensions for sorting in Swift

SwiftSortUtils Motivation This library takes a shot at making comparing and sorting in Swift more pleasant. It also allows you to reuse your old NSSor

Daniel Strittmatter 60 Sep 9, 2022