Coder Social home page Coder Social logo

decodestrategy's Introduction

DecodeStrategy

latest versionlicenseplatform

DecodeStrategy is convenient applying decode strategy if your backend uses weak type language.

中文介紹

Features

  • Decode has default value, if decode failure

  • Decode array ignore, if decode failure

  • Decode array has default value, if decode failure

  • Universal decode with String, Int and Double type

Installation

  1. Manually

    Add the Sources folder to your Xcode project.

  2. CocoaPods

    pod 'DecodeStrategy'
    

Usage

@DecodeHasDefault

  1. You can use EmptyString, ZeroInt, ZeroDouble
struct User: Decodable {
    @DecodeHasDefault<EmptyString>
    var name: String
}

let data = #"{ "name": null }"#.data(using: .utf8)!
let result = try JSONDecoder().decode(User.self, from: data)

print(result.name)
// ""
  1. Or custom your provider
struct User: Decodable {
    struct DefaultName: DecodeDefaultProvider {
        static var defaultValue = "Ohlulu"
    }
    @DecodeHasDefault<DefaultName>
    var name: String
}

let data = #"{ "name": null }"#.data(using: .utf8)!
let result = try JSONDecoder().decode(User.self, from: data)

print(result.name)
// "Ohlulu"

@DecodeUniversal

struct Model: Decodable {
    @DecodeUniversal var intValue: Int
}

let data = #"{ "intValue": "100" }"#.data(using: .utf8)!
let model = try JSONDecoder().decode(Model.self, from: data)

print(model.intValue)
// 100
struct Model: Decodable {
    @DecodeUniversal var strValue: String
}

let data = #"{ "strValue": 100 }"#.data(using: .utf8)!
let model = try JSONDecoder().decode(Model.self, from: data)

print(model.strValue)
// "100"

More example

@DecodeArrayIgnore

struct Model: Decodable {
    @DecodeArrayIgnore var arr: [String]
}

let data = #"{ "arr": ["1", 2, "3", null] }"#.data(using: .utf8)!
let model = try JSONDecoder().decode(Model.self, from: data)

print(model.arr)
// ["1", "3"]

@DecodeArrayHasDefault

struct Model: Decodable {
    struct DefauleString: DecodeDefaultProvider {
        static var defaultValue = "-1"
    }
    @DecodeArrayHasDefault<DefauleString> var arr: [String]
}

let data = #"{ "arr": ["1", 2, "3", null] }"#.data(using: .utf8)!
let model = try JSONDecoder().decode(Model.self, from: data)

print(model.arr)
// ["1", "-1", "3", "-1"]

License

DecodeStrategy is released under the MIT license. See LICENSE for details.

decodestrategy's People

Contributors

ohlulu avatar

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.