Giter Club home page Giter Club logo

loosecodable's Introduction

LooseCodable

LooseCodable is an extended type of Codable that performs loose type conversion.

Install

Carthage

github "k-motoyan/LooseCodable"

Swift Package Manager

import PackageDescription

let package = Package(
        .
        .
        .
    dependencies: [
        .package(url: "https://github.com/k-motoyan/LooseCodable", from: "1.0.0"),
    ],
        .
        .
        .
)

Example

There is a User type that conforms to the as following Codable protocol.

struct User: Codable {
    let name: String
    let age: Int
}

However, JSON which can not be mapped to User is sometimes delivered.

{
    "name": "Tom",
    "age": "18"
}

This is an error in Codable as follows.
"Expected to decode Int but found a string/data instead."

You can avoid such problems by using LooseCodable.

For example, User is rewritten as follows...

import LooseCodable

struct User: Codable {
    let name: String

    // Wrap the type of property that may receive unintended data in `LooseCodable`.
    private let _age: LooseCodable<Int>

    // public type is not changed.
    var age: Int {
        return self._age.value
    }

    enum CodingKeys: String, CodingKey {
        case name
        case _age = "age"
    }
}

You can also map to User with problematic JSON.

Active types

LooseCodable corresponds to the following types.

  • Bool
  • Int
  • Double
  • String

License

LooseCodable is released under the BSD 3-Clause license.

loosecodable's People

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.