All Projects → Anviking → Decodable

Anviking / Decodable

Licence: mit
[Probably deprecated] Swift 2/3 JSON unmarshalling done (more) right

Programming Languages

swift
15916 projects

Labels

Projects that are alternatives of or similar to Decodable

Ndjson
♨️ Wicked-Fast Streaming 'JSON' ('ndjson') Reader in R
Stars: ✭ 44 (-95.81%)
Mutual labels:  json
Fiscalberry
[JSON ↔ HW] Connect things using JSON API with the fiscalberry websocket server interact easily with any kind of Hardware. Another IoT solution...
Stars: ✭ 44 (-95.81%)
Mutual labels:  json
Shon
A simple tool to convert json or yaml into a shell-compliant data structure.
Stars: ✭ 47 (-95.52%)
Mutual labels:  json
Pg variables
Session wide variables for PostgreSQL
Stars: ✭ 44 (-95.81%)
Mutual labels:  json
Json Normalizer
📃 Provides generic and vendor-specific normalizers for normalizing JSON documents.
Stars: ✭ 45 (-95.71%)
Mutual labels:  json
Jj
JSON Stream Editor (command line utility)
Stars: ✭ 1,033 (-1.62%)
Mutual labels:  json
Oakdex Pokedex
Ruby Gem and Node Package for comprehensive Generation 1-7 Pokedex data, including 809 Pokémon, uses JSON schemas to verify the data
Stars: ✭ 44 (-95.81%)
Mutual labels:  json
Minijson
A lightweight json library (C++)
Stars: ✭ 49 (-95.33%)
Mutual labels:  json
Microblob
Serve millions of JSON documents via HTTP.
Stars: ✭ 45 (-95.71%)
Mutual labels:  json
Univalue
High performance RAII C++ JSON library and universal value object class
Stars: ✭ 46 (-95.62%)
Mutual labels:  json
Excel2json
把Excel表转换成json对象,并保存到一个文本文件中。
Stars: ✭ 1,023 (-2.57%)
Mutual labels:  json
Wheel
关于net nio os cache db rpc json web http udp tcp mq 等多个小工具的自定义实现
Stars: ✭ 45 (-95.71%)
Mutual labels:  json
Node.pas
Asynchronous Event-driven server programming for EMB Delphi, powered by libuv.
Stars: ✭ 45 (-95.71%)
Mutual labels:  json
Graphql Factory
A toolkit for building GraphQL
Stars: ✭ 44 (-95.81%)
Mutual labels:  json
Ansible Config encoder filters
Ansible role used to deliver the Config Encoder Filters.
Stars: ✭ 48 (-95.43%)
Mutual labels:  json
Jl Sql
SQL for JSON and CSV streams
Stars: ✭ 44 (-95.81%)
Mutual labels:  json
Zeison
Small, fast and easy-to-use JSON library for Scala.
Stars: ✭ 45 (-95.71%)
Mutual labels:  json
Generator Http Fake Backend
Yeoman generator for building a fake backend by providing the content of JSON files or JavaScript objects through configurable routes.
Stars: ✭ 49 (-95.33%)
Mutual labels:  json
Resticprofile
Configuration profiles for restic backup
Stars: ✭ 48 (-95.43%)
Mutual labels:  json
Webpub Manifest
📜 A JSON based Web Publication Manifest format used at the core of the Readium project
Stars: ✭ 46 (-95.62%)
Mutual labels:  json

Decodable

Simple and strict, yet powerful object mapping made possible by Swift 2's error handling. Greatly inspired by Argo, but without a bizillion functional operators.

Carthage compatible Cocoapods version Platforms Travis

struct Repository {
    let name: String
    let description: String
    let stargazersCount: Int
    let language: String?
    let sometimesMissingKey: String?
    
    let owner: User // Struct conforming to Decodable
    let defaultBranch: Branch // Struct NOT conforming to Decodable
    
    var fullName: String { return "\(owner.login)/\(name)" }
}

extension Repository: Decodable {
    static func decode(j: Any) throws -> Repository {
        return try Repository(
                    name: j => "nested" => "name", 
                    description: j => "description", 
                    stargazersCount: j => "stargazers_count", 
                    language: j => "language", 
                    sometimesMissingKey: j =>? "sometimesMissingKey",
                    owner: j => "owner", 
                    defaultBranch: Branch(name: j => "default_branch")
                )
    }
}

do {
    let json = try NSJSONSerialization.JSONObjectWithData(data, options: [])
    let repo = try [Repository].decode(json)
} catch {
    print(error)
}

How does it work?

A protocol

public protocol Decodable {
    static func decode(json: Any) throws -> Self
}

A parse-function

public func parse<T>(json: Any, path: [String], decode: (Any throws -> T)) throws -> T

And shameless operator-overloading

The too-many generated overloads, all calling the parse-function, can be found in Overloads.swift. Return types include T?, [T?], [T?]?, Any and [String: T]?. When conditional protocol conformance is supported in Swift this won't be necessary, and automagic decoding of infinitly nested generic types (like [[[[[[[[[A???]]: B]]]?]]?]]) would work.

An overload may look like this:

public func => <T: Decodable>(json: Any, keyPath: KeyPath) throws -> T

KeyPaths

Keypaths can be created from string and array literals as well as with explicit initializers. They can also be joined using the operators => and =>?. =>? is another operator that indicates that nil should be returned if the key to the right is missing.

  • When composing => and =>? operators in the same keypath, the strictness of => is still honoured.
  • Optional key paths (=>?) require an optional return type
let a: KeyPath = "a"
let b: KeyPath = ["a", "b"]
let c: KeyPath = "a" => "b" => "c"
let string: String? = json =>? "key1" => "key2" => "key3"`
                                ^^^^ allowed to be missing

Errors

Errors will be caught and rethrown in the decoding process to backpropagate metadata, like the JSON object that failed decoding, the key path to it, and the root JSON object.

From DecodingError.swift:

public enum DecodingError: ErrorProtocol, Equatable {
    /// Thrown when optional casting from `Any` fails.
    ///
    /// This can happen both when trying to access a key on a object
    /// that isn't a `NSDictionary`, and failing to cast a `Castable`
    /// primitive.
    case typeMismatch(expected: Any.Type, actual: Any.Type, Metadata)
    
    /// Thrown when a given, required, key was not found in a dictionary.
    case missingKey(String, Metadata)
    
    /// Thrown from the `RawRepresentable` extension when
    /// `init(rawValue:)` returned `nil`.
    case rawRepresentableInitializationError(rawValue: Any, Metadata)
    
    /// When an error is thrown that isn't `DecodingError`, it 
    /// will be wrapped in `DecodingError.other` in order to also provide
    /// metadata about where the error was thrown.
    case other(ErrorProtocol, Metadata)
}
let dict: NSDictionary = ["object": ["repo": ["owner": ["id" : 1, "login": "anviking"]]]]

do {
    let username: String = try dict => "object" => "repo" => "owner" => "name"
} catch let error {
    print(error)
}
//
// MissingKeyError at object.repo.owner: name in {
//    id = 1;
//    login = anviking;
// }

Handling Errors

Expressions like j => "key" will throw directly, and catch-statements can be used to create the most complex error handling behaviours. This also means that try? can be used to return nil if anything goes wrong instead of throwing.

For convenience there is an operator, =>?, that only returns nil on missing keys, for APIs that indicate null in that manner, and to aid working with different response formats.

Overload Null Behaviour Missing Key Behavior Type Mismatch Behaviour Errors in subobjects
=> -> T throws throws throws uncaught (throws)
=> -> T? nil throws throws uncaught (throws)
=>? -> T? nil nil throws uncaught (throws)
try? => -> T nil nil nil caught (nil)

Customization

Int, Double,String, Bool, Date (ISO8601), NSArray, and NSDictionary types that conform to DynamicDecodable with the following declaration:

public protocol DynamicDecodable {
    associatedtype DecodedType
    static var decoder: (Any) throws -> DecodedType {get set}
}

This allows Decodable to implement default decoding closures while allowing you to override them as needed.

// Lets extend Bool.decoder so that it accepts certain strings:
Bool.decoder = { json in
    switch json {
    case let str as String where str == "true":
        return true
    case let str as String where str == "false":
        return false
    default:
        return try cast(json)
    }
}

Note that when extending new types to conform to Decodable there is really no point in conforming to DynamicDecodable since you already control the implementation. Also note that the decoder properties are intended as "set once". If you need different behaviour on different occations, please create custom decode functions.

The default Date.decoder uses a ISO8601 date formatter. If you don't want to create your own decode closure there's a helper:

Date.decoder = Date.decoder(using: formatter)

When Decodable isn't enough

Don't be afraid of not conforming to Decodable.

let array = try NSArray.decode(json => "list").map {
    try Contribution(json: $0, repository: repo)
}

Tips

  • You can use Decodable with classes. Just make sure to either call a required initializer on self (e.g self.init) and return Self, or make your class final. ( This might be a problem though)
  • The Decodable-protocol and the =>-operator should in no way make you committed to use them everywhere.

Compatibility

Swift version Compatible tag or branch
Swift 4.0 0.6.0
Swift 3.0 v0.5
Swift 2.3 v0.4.4
Swift 2.2 v0.4.3

Note on Swift 4.0 usage

Due to collisions with the standard library you will have to import ambiguous symbols specifically, in addition to Decodable as a whole.

This means you likely want the following

import Decodable
import protocol Decodable.Decodable

and you can import other symbols, e.g KeyPath, DecodingError, in a simlilar fashion (using import struct and import enum)

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].