xspyhack / CodableKit

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CodableKit

Build Status codecov

Usage

Shorthands

Sometimes we have to write init(from:) by hand, so let's make this work a little bit easier:

init(from decoder: Decoder) throws {
    let container = try decoder.container(keyedBy: CodingKeys.self)
    
    // self.name = try container.decode(String.self, forKey: .name)
    // Shorthand:
    self.name = try container.decode(.name)
    
    // self.nickname = try container.decodeIfPresent(String.self, forKey: .nickname)
    // Shorthand:
    self.nickname = try container.decodeIfPresent(.nickname)
    
    // self.title = try? container.decode(String.self, forKey: .title)
    // Shorthand:
    self.title = container[.title]
}

AnyCodingKey

When writing init(from:) by hand, we also have to write the annoying CodingKeys. So let's introducing AnyCodingKey, and forget about CodingKeys:

init(from decoder: Decoder) throws {
    let container = try decoder.container() // decoder.container(keyedBy: AnyCodingKey.self)
    
    self.name = try container.decode("name")
    self.nickname = try container.decoderIfPresent("nickname")
    self.title = container["title"]

    // Some tricks:
    self.camelCasePropertyWithDefaultValue 
        = container["camel_case_property_with_default_value"] ?? 42
}

Installation

Swift Package Manager

import PackageDescription

let package = Package(
    name: "Example",
    dependencies: [
        .package(url: "https://github.com/kylinroc/CodableKit.git", from: "0.4.0"),
    ],
    targets: [
        .target(name: "Example", dependencies: ["CodableKit"]),
    ]
)

About

License:MIT License


Languages

Language:Swift 100.0%