A Codable extension to decode arrays and to catch & log all decoding failures

Overview

SafeDecoder

SafeDecoder Language: Swift 4.2

A Codable extension to decode arrays and to catch & log all decoding failures

Features

SafeDecoder makes two improvements for Codable models

  • When decoding arrays it can skip over invalid objects, allowing your app to show just valid objects
  • It can also collect all the decoding errors and send them to your logging class or service

How to install

Add this to your CocoaPods Podfile

pod 'SafeDecoder'

How to decode arrays

To decode arrays safely, in you model's init(from decoder:) just call container.decodeArray() and pass in the model type in your array

import SafeDecoder

struct MyModel: Decodable {

    var myArray: [CGRect]

    enum CodingKeys: String, CodingKey {
        case myArray
    }

    public init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)

        // to parse an array without re-thowing an error, call decodeArray()
        myArray = try container.decodeArray(CGRect.self, forKey: .myArray)
    }
}

How to decode safely

If you want to collect all decoding errors for logging, implement your own logging code here

SafeDecoder.logger = { error, typeName in
    // replace with a call to your own logging library/service
    print(error)
}

Then in your model's init(from decoder:) call decoder.safeContainer(), then call container.decodeSafe() or container.decodeArraySafe()

SafeDecoder will collect all errors and log them before throwing the exception

import SafeDecoder

struct MyModel: Decodable {

    var myId: String
    var myArray: [CGRect]

    enum CodingKeys: String, CodingKey {
        case myId
        case myArray
    }

    public init(from decoder: Decoder) throws {
        var container = try decoder.safeContainer(keyedBy: CodingKeys.self)

        let _myId = try container.decodeSafe(String.self, forKey: .myId)
        let _myArray = try container.decodeArraySafe(CGRect.self, forKey: .myArray)

        guard
            let myId = _myId,
            let myArray = _myArray
            else {
                // this reference can be your object identifier to help find the issue with your data
                throw container.getErrors(modelType: MyModel.self, reference: _myId)
        }

        self.myId = myId
        self.myArray = myArray
    }
}

Get these while stocks last :)

An elegant solution for keeping views visible when the keyboard is being shown https://github.com/IdleHandsApps/IHKeyboardAvoiding

Button styles that are centralied and reusable, and hooked up to InterfaceBuilder https://github.com/IdleHandsApps/DesignableButton

An extension to easily set your UINavigationBar transparent and hide the shadow https://github.com/IdleHandsApps/UINavigationBar-Transparent

Author

It'd be great to hear about any cool apps that are using SafeDecoder

License

Distributed under the MIT License

You might also like...
๐ŸŒ A zero-dependency networking solution for building modern and secure iOS, watchOS, macOS and tvOS applications.
๐ŸŒ A zero-dependency networking solution for building modern and secure iOS, watchOS, macOS and tvOS applications.

A zero-dependency networking solution for building modern and secure iOS, watchOS, macOS and tvOS applications. ๐Ÿš€ TermiNetwork was tested in a produc

StatusBarOverlay will automatically show a
StatusBarOverlay will automatically show a "No Internet Connection" bar when your app loses connection, and hide it again. It supports apps which hide the status bar and The Notch

StatusBarOverlay StatusBarOverlay will automatically show a "No Internet Connection" bar when your app loses connection, and hide it again. It support

Socket.io iOS and OSX Client compatible with v1.0 and later
Socket.io iOS and OSX Client compatible with v1.0 and later

SocketIO-Kit โš ๏ธ This project is no longer maintained. Please use the official framework Socket.IO-Client-Swift. SocketIO-Kit is a Socket.io iOS client

RestKit is a framework for consuming and modeling RESTful web resources on iOS and OS X

RestKit RestKit is a modern Objective-C framework for implementing RESTful web services clients on iOS and Mac OS X. It provides a powerful object map

Lightweight Networking and Parsing framework made for iOS, Mac, WatchOS and tvOS.
Lightweight Networking and Parsing framework made for iOS, Mac, WatchOS and tvOS.

NetworkKit A lightweight iOS, Mac and Watch OS framework that makes networking and parsing super simple. Uses the open-sourced JSONHelper with functio

Bonjour networking for discovery and connection between iOS, macOS and tvOS devices.
Bonjour networking for discovery and connection between iOS, macOS and tvOS devices.

Merhaba Bonjour networking for discovery and connection between iOS, macOS and tvOS devices. Features Creating Service Start & Stop Service Stop Brows

QwikHttp is a robust, yet lightweight and simple to use HTTP networking library for iOS, tvOS and watchOS

QwikHttp is a robust, yet lightweight and simple to use HTTP networking library. It allows you to customize every aspect of your http requests within a single line of code, using a Builder style syntax to keep your code super clean.

Impervious is a privacy and security-focused browser with native DANE support and a decentralized p2p light client.

Impervious iOS The first browser with support for native DNS-Based Authentication of Named Entities (DANE) with true downgrade protection, and the fir

Beacon is a privacy and security-focused browser with native DANE support and a decentralized p2p light client.

Beacon iOS The first browser with support for native DNS-Based Authentication of Named Entities (DANE) with true downgrade protection, and the first b

Owner
Idle Hands Apps
c/- Fraser Scott-Morrison
Idle Hands Apps
NSURLSession network abstraction layer, using Codable and Decodable for response and Encodable for request. โš™๏ธ๐Ÿš€

SONetworking NSURLSession network abstraction layer, using Codable and Decodable for response and Encodable for request. Project Folder and File Struc

Ahmad AlSofi 4 Jan 28, 2022
A network extension app to block a user input URI. Meant as a network extension filter proof of concept.

URIBlockNE A network extension app to block a user input URI. Meant as a network extension filter proof of concept. This is just a research effort to

Charles Edge 5 Oct 17, 2022
๐ŸŒธ Powerful Codable API requests builder and manager for iOS.

This lib is about network requests with blackjack, roulette and craps! Using it you will be able to convert your massive API layer code into an awesom

CodyFire 251 Jan 8, 2023
a NSURLCache subclass for handling all web requests that use NSURLRequest

EVURLCache What is this? This is a NSURLCache subclass for handeling all web requests that use NSURLRequest. (This includes UIWebView) The EVURLCache

Edwin Vermeer 296 Dec 18, 2022
A toolkit for Network Extension Framework

NEKit NEKit is deprecated. It should still work but I'm not intent on maintaining it anymore. It has many flaws and needs a revamp to be a high-qualit

zhuhaow 2.8k Jan 2, 2023
Bamboots - Extension 4 Alamofire

Bamboots is a network request framework based on Alamofire , aiming at making network request easier for business development. Protocols Features Requ

mmoaay 442 Dec 22, 2022
Extension URLRequest With Swift

JHURLRequest extension URLRequest Example func test1() { let url = "https://httpbin.org/get" let dic: [String: Any] = [ "name": "Lilei

HaoCold 1 Dec 16, 2021
Alamofire extension for serialize NSData to SwiftyJSON

Alamofire-SwiftyJSON An extension to make serializing Alamofire's response with SwiftyJSON easily. โš ๏ธ To use with Swift 3.x please ensure you are usin

SwiftyJSON 1.4k Dec 22, 2022
An Alamofire extension which converts JSON response data into swift objects using EVReflection

AlamofireJsonToObjects ?? This is now a subspec of EVReflection and the code is maintained there. ?? You can install it as a subspec like this: use_fr

Edwin Vermeer 161 Sep 29, 2022
Server-side Swift. The Perfect core toolset and framework for Swift Developers. (For mobile back-end development, website and API development, and moreโ€ฆ)

Perfect: Server-Side Swift ็ฎ€ไฝ“ไธญๆ–‡ Perfect: Server-Side Swift Perfect is a complete and powerful toolbox, framework, and application server for Linux, iO

PerfectlySoft Inc. 13.9k Jan 6, 2023