All Projects → JohnSundell → Codextended

JohnSundell / Codextended

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

Programming Languages

swift
15916 projects

Labels

Projects that are alternatives of or similar to Codextended

IkigaJSON
A high performance JSON library in Swift
Stars: ✭ 316 (-75.33%)
Mutual labels:  codable
Swiftai
SwiftAI, write Swift code smart. SwiftAI can generate Model class from JSON now. Codable and HandyJSON is supported. More features will be add.
Stars: ✭ 470 (-63.31%)
Mutual labels:  codable
Thsidebar
NSOutlineView and badge
Stars: ✭ 38 (-97.03%)
Mutual labels:  codable
Easystash
🗳Easy data persistence in Swift
Stars: ✭ 303 (-76.35%)
Mutual labels:  codable
Bamboots
Bamboots - Extension 4 Alamofire
Stars: ✭ 434 (-66.12%)
Mutual labels:  codable
Anycodable
Type-erased wrappers for Encodable, Decodable, and Codable values
Stars: ✭ 811 (-36.69%)
Mutual labels:  codable
PreciseDecimal
A Decimal type that plays nicely with literals and Decodable ✨
Stars: ✭ 18 (-98.59%)
Mutual labels:  codable
Core
🌎 Utility package containing tools for byte manipulation, Codable, OS APIs, and debugging.
Stars: ✭ 62 (-95.16%)
Mutual labels:  codable
Xmlcoder
Easy XML parsing using Codable protocols in Swift
Stars: ✭ 460 (-64.09%)
Mutual labels:  codable
Jsontocodable
A generating tool from Raw JSON to Codable (Swift4) text written in Swift4.
Stars: ✭ 33 (-97.42%)
Mutual labels:  codable
Binarycodable
Swift Codable-like interfaces for binary representations.
Stars: ✭ 359 (-71.98%)
Mutual labels:  codable
Userdefaultsstore
Why not use UserDefaults to store Codable objects 😉
Stars: ✭ 416 (-67.53%)
Mutual labels:  codable
Bettercodable
Better Codable through Property Wrappers
Stars: ✭ 953 (-25.6%)
Mutual labels:  codable
Defaultcodable
A convenient way to handle default values with Swift Codable types
Stars: ✭ 297 (-76.81%)
Mutual labels:  codable
Multipart Kit
🏞 Parses and serializes multipart-encoded data with Codable support.
Stars: ✭ 52 (-95.94%)
Mutual labels:  codable
NetworkAgent
This package is meant to make http request of an easy way inspiren in the architecture of Moya package. This package is 100% free of dependencies and works with Combine api + Codable
Stars: ✭ 16 (-98.75%)
Mutual labels:  codable
Codablefirebase
Use Codable with Firebase
Stars: ✭ 635 (-50.43%)
Mutual labels:  codable
Adaptivecardui
Snippets of UI, authored in JSON and rendered with SwiftUI
Stars: ✭ 73 (-94.3%)
Mutual labels:  codable
Swiftprovisioningprofile
Parse iOS mobile provisioning files into Swift models
Stars: ✭ 55 (-95.71%)
Mutual labels:  codable
Url Encoded Form
📝 Parse and serialize url-encoded form data with Codable support.
Stars: ✭ 32 (-97.5%)
Mutual labels:  codable

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! 😀

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].