A enhanced JSON decoder.

Last update: May 7, 2022

JSONDecoderEx

A enhanced JSON decoder.

Usage

struct User: Codable {
    struct Role: OptionSet, Codable, CustomStringConvertible {
        let rawValue: Int
        static let vip = Self(rawValue: 1 << 0)
        static let manageer = Self(rawValue: 1 << 1)
        var description: String {
            var bg = [String]()
            if self.contains(.vip) { bg.append("vip") }
            if self.contains(.manageer) { bg.append("manager") }
            return "\"\(bg.joined(separator: "|"))\""
        }
    }
    enum Gender: Int, Codable {
        case unknown = 0
        case male
        case woman
    }
    let uid: String
    let nickname: String?
    let role: Role
    let gender: Gender
}

let json = """
         {"uid":"coder","nickname":"coder","role":3}
         """
         
let value = try JSONDecoderEx().decode(User.self, from: json.data(using: .utf8)!)
print(value) 
// User(uid: "coder", nickname: Optional("coder"), role: "vip|manager", gender: User.Gender.unknown)

More Compatibility

extension User.Gender : Unknownable {}

let json = """
         {"uid":1,"nickname":"coder","gender":3}
         """
         
let value = try JSONDecoderEx().decode(User.self, from: json.data(using: .utf8)!)
print(value) 
// User(uid: "1", nickname: Optional("coder"), role: "", gender: User.Gender.unknown)
{keys: ["x"]}">
/// some case decoding required a existing decoder
@propertyWrapper struct UsingCustomCoder<T: NSObject> : Codable {
    
    let wrappedValue: T
    init(wrappedValue: T) {
        self.wrappedValue = wrappedValue
    }
    
    init(from decoder: Decoder) throws {
        let value = try JSONDecoderEx.JSONValue(from: decoder)
        // direct key-value access
        let mode = value["mode"].rawValue as? String
        // bridge to third-party library decoding model
        if (mode == "yymodel") {
            self.wrappedValue = T.yy_model(value.rawValue as? [String: Any])
        } else {
            ...
        }
    }
    
    func encode(to encoder: Encoder) throws {
        ...
    }
}

struct Live {
    ... 
    @UsingCustomCoder var other: OCModel
}

let json = """
         {"other":{"mode":"yymodel","sec":{"keys":["x"]}}}
         """
         
let value = try JSONDecoderEx().decode(Live.self, from: json.data(using: .utf8)!)
print(value.other) 
// {keys: ["x"]}

GitHub

https://github.com/SAGESSE-CN/JSONDecoderEx
You might also like...

AlamofireObjectMappe - An Alamofire extension which converts JSON response data into swift objects using ObjectMapper

AlamofireObjectMapper An extension to Alamofire which automatically converts JSON response data into swift objects using ObjectMapper. Usage Given a U

May 19, 2022

Functional JSON Parser - Linux Ready

Functional JSON Parser - Linux Ready

Functional JSON Parser Feature Linux Ready Type-safe JSON parsing Functional value transformation Easy to parse nested value Dependency free No define

Jan 29, 2022

Argo is a library that lets you extract models from JSON or similar structures in a way that's concise, type-safe, and easy to extend

Argo is a library that lets you extract models from JSON or similar structures in a way that's concise, type-safe, and easy to extend

Argo is a library that lets you extract models from JSON or similar structures in a way that's concise, type-safe, and easy to extend. Using Argo

May 11, 2022

Arrow 🏹 Parse JSON with style

Arrow  🏹 Parse JSON with style

Arrow 🏹 Parse JSON with style

Feb 27, 2022

Elevate is a JSON parsing framework that leverages Swift to make parsing simple, reliable and composable

Elevate is a JSON parsing framework that leverages Swift to make parsing simple, reliable and composable. Elevate should no longer be used for

Mar 1, 2022

Reflection based (Dictionary, CKRecord, NSManagedObject, Realm, JSON and XML) object mapping with extensions for Alamofire and Moya with RxSwift or ReactiveSwift

EVReflection General information At this moment the master branch is tested with Swift 4.2 and 5.0 beta If you want to continue using EVReflection in

May 13, 2022

Freddy - A reusable framework for parsing JSON in Swift.

Freddy - A reusable framework for parsing JSON in Swift.

Why Freddy? Parsing JSON elegantly and safely can be hard, but Freddy is here to help. Freddy is a reusable framework for parsing JSON in Swift. It ha

May 11, 2022

[Deprecated] A shiny JSON parsing library in Swift :sparkles: Loved by many from 2015-2021

[Deprecated] A shiny JSON parsing library in Swift :sparkles: Loved by many from 2015-2021

🚨 Deprecation Notice 🚨 Gloss has been deprecated in favor of Swift's Codable framework. The existing Gloss source is not going away, however updates

May 16, 2022

HandyJSON is a framework written in Swift which to make converting model objects to and from JSON easy on iOS.

HandyJSON To deal with crash on iOS 14 beta4 please try version 5.0.3-beta HandyJSON is a framework written in Swift which to make converting model ob

May 16, 2022
Related tags
JSONNeverDie - Auto reflection tool from JSON to Model, user friendly JSON encoder / decoder, aims to never die
JSONNeverDie - Auto reflection tool from JSON to Model, user friendly JSON encoder / decoder, aims to never die

JSONNeverDie is an auto reflection tool from JSON to Model, a user friendly JSON encoder / decoder, aims to never die. Also JSONNeverDie is a very important part of Pitaya.

Feb 27, 2022
PMJSON provides a pure-Swift strongly-typed JSON encoder/decoder

Now Archived and Forked PMJSON will not be maintained in this repository going forward. Please use, create issues on, and make PRs to the fork of PMJS

Apr 22, 2022
The easy to use Swift JSON decoder
The easy to use Swift JSON decoder

Unbox is deprecated in favor of Swift’s built-in Codable API and the Codextended project. All current users are highly encouraged to migrate to Codable as soon as possible.

May 18, 2022
SwiftyJSON decoder for Codable

SwiftyJSONDecoder 功能 继承自 JSONDecoder,在标准库源码基础上做了改动,与其主要区别如下 使用 SwiftyJSON 解析数据,使用其类型兼容功能 废弃 nonConformingFloatDecodingStrategy 属性设置,Double 及 Float 默认解

Nov 7, 2021
Dogtector: dog breed detection app for iOS using YOLOv5 model combined with Metal based object decoder optimized
Dogtector: dog breed detection app for iOS using YOLOv5 model combined with Metal based object decoder optimized

Dogtector Project description Dogtector is dog breed detection app for iOS using YOLOv5 model combined with Metal based object decoder optimized for u

May 5, 2022
JSEN (JSON Swift Enum Notation) is a lightweight enum representation of a JSON, written in Swift.

JSEN /ˈdʒeɪsən/ JAY-sən JSEN (JSON Swift Enum Notation) is a lightweight enum representation of a JSON, written in Swift. A JSON, as defined in the EC

May 6, 2022
JSON-Practice - JSON Practice With Swift

JSON Practice Vista creada con: Programmatic + AutoLayout Breve explicación de l

Oct 29, 2021
Ss-json - High-performance json parsing in swift

json 0.1.1 swift-json is a pure-Swift JSON parsing library designed for high-per

May 15, 2022
Swift-json - High-performance json parsing in swift

json 0.1.4 swift-json is a pure-Swift JSON parsing library designed for high-per

May 15, 2022
Swift parser for JSON Feed — a new format similar to RSS and Atom but in JSON.

JSONFeed Swift parser for JSON Feed — a new format similar to RSS and Atom but in JSON. For more information about this new feed format visit: https:/

Nov 22, 2021