Latest 1.0.5
Homepage https://github.com/Elenionl/ELSwiftJsonParser
License Apache 2.0
Platforms ios 8.0, requires ARC
Frameworks Foundation
Authors

Apps Using
Downloads
CocoaPods
CocoaPods
iOS 9.0

A light-weight but effective tool for converting between Json Dictionary and Swift object.

Install

pod 'ELSwiftJsonParser'
pod install

Use

Model object which can be convert from Dictionary should confirm Protocol JsonDictionaryDecodable.

/// Convert model from dictionary
protocol JsonDictionaryDecodable {
    /// Creating model from dictionary
    ///
    /// - Parameter dictionary: Source dictionary
    /// - Returns: Model
    static func from(dictionary: [String: Any]) -> Self
}

A sample


class AnObject: NSObject, JsonDictionaryDecodable {

    var string: String?
    var int: Int?
    var bool: Bool?
    var double: Double?
    var date: Date?
    var object: AnObject?
    var objectArray: [AnObject] = []
    var arrayOfString: [String] = []

    enum AType {
        case foo
        case bar
        static func from(int: Int) -> AType? {
            switch int {
            case 0:
                return .foo
            case 1:
                return .bar
            default:
                return nil
            }
        }

        func toInt() -> Int {
            switch self {
            case .foo:
                return 0
            case .bar:
                return 1
            }
        }
    }

    var aType: AType?
    var randomlyParse: String?

    required init(from dictionary: [String : Any]) {
        super.init()
        // String
        string = dictionary.string("string")
        // Int
        int = dictionary.int("int")
        // Bool
        bool = dictionary.bool("boolValue")
        // Double
        double = dictionary.double("double")
        // Date
        date = dictionary.date("dateString")
        // Purse object from Dictionary
        object = dictionary.object("object", type: AnObject.self)
        // Purse array of object from array of Dictionary
        objectArray = dictionary.objectArray("objectArray", type: AnObject.self)
        // Array of string
        arrayOfString = dictionary.array("arrayOfString", type: String.self)
        // Purse Enum from Int
        aType = AType.from(int: dictionary.int("aType") ?? -1)
        // Purse as specified
        randomlyParse = dictionary.array("anyDictionaryArray", type: [String: Any].self).first?.dictionary("anyInnerDictionary")?.string("randomParse")
    }

    required override init() {
        super.init()
    }
}

Model object which can be convert to Dictionary should confirm Protocol: JsonDictionaryEncodable.

extension AnObject: JsonDictionaryEncodable {

    func toDictionary() -> [String : Any] {
        var dictionary: [String: Any]  = [:]
        dictionary["string"] = string ?? ""
        dictionary["int"] = int ?? -1
        if let boolValue = bool {
            dictionary["boolValue"] = boolValue
        }
        dictionary["double"] = double ?? 0.0
        dictionary["date"] = (date?.timeIntervalSince1970 ?? 0.0) * 1000
        dictionary["object"] = object?.toDictionary() ?? [:]
        dictionary["objectArray"] = objectArray.map({ (object) -> [String: Any] in
            return object.toDictionary()
        })
        dictionary["arrayOfString"] = arrayOfString
        dictionary["aType"] = aType?.toInt() ?? -1
        return dictionary
    }
}

Author

@Elenionl, [email protected]

License

English: ELSwiftJsonParser is available under the Apache 2.0 license, see the LICENSE file for more information.

Latest podspec

{
    "name": "ELSwiftJsonParser",
    "version": "1.0.5",
    "summary": "A light-weight tool helps to transfrom Json dictionary into model as well as transfrom model into Json dictionary.",
    "description": "Model object which can be convert from Dictionary should confirm Protocol JsonDictionaryDecodable.n``` Swiftn/// Convert model from dictionarynprotocol JsonDictionaryDecodable {n    /// Creating model from dictionaryn    ///n    /// - Parameter dictionary: Source dictionaryn    /// - Returns: Modeln    static func from(dictionary: [String: Any]) -> Selfn}n```nA samplenn```Swiftnclass AnObject: NSObject, JsonDictionaryDecodable {n    n    var string: String?n    var int: Int?n    var bool: Bool?n    var double: Double?n    var date: Date?n    var object: AnObject?n    var objectArray: [AnObject] = []n    var arrayOfString: [String] = []n    n    enum AType {n        case foon        case barn        static func from(int: Int) -> AType? {n            switch int {n            case 0:n                return .foon            case 1:n                return .barn            default:n                return niln            }n        }n        n        func toInt() -> Int {n            switch self {n            case .foo:n                return 0n            case .bar:n                return 1n            }n        }n    }n    n    var aType: AType?n    var randomlyParse: String?n    n    required init(from dictionary: [String : Any]) {n        super.init()n        string = dictionary.string("string")n        int = dictionary.int("int")n        bool = dictionary.bool("boolValue")n        double = dictionary.double("double")n        date = dictionary.date("dateString")n        object = dictionary.object("object", type: AnObject.self)n        objectArray = dictionary.objectArray("objectArray", type: AnObject.self)n        arrayOfString = dictionary.array("arrayOfString", type: String.self)n        aType = AType.from(int: dictionary.int("aType") ?? -1)n        randomlyParse = dictionary.array("anyDictionaryArray", type: [String: Any].self).first?.dictionary("anyInnerDictionary")?.string("randomParse")n    }n    n    required override init() {n        super.init()n    }n}n```nnModel object which can be convert to Dictionary should confirm Protocol: JsonDictionaryEncodable.nn  n``` Swiftnextension AnObject: JsonDictionaryEncodable {n    n    func toDictionary() -> [String : Any] {n        var dictionary: [String: Any]  = [:]n        dictionary["string"] = string ?? ""n        dictionary["int"] = int ?? -1n        if let boolValue = bool {n            dictionary["boolValue"] = boolValuen        }n        dictionary["double"] = double ?? 0.0n        dictionary["date"] = (date?.timeIntervalSince1970 ?? 0.0) * 1000n        dictionary["object"] = object?.toDictionary() ?? [:]n        dictionary["objectArray"] = objectArray.map({ (object) -> [String: Any] inn            return object.toDictionary()n        })n        dictionary["arrayOfString"] = arrayOfStringn        dictionary["aType"] = aType?.toInt() ?? -1n        return dictionaryn    }n}n```",
    "homepage": "https://github.com/Elenionl/ELSwiftJsonParser",
    "license": {
        "type": "Apache 2.0",
        "file": "LICENSE.md"
    },
    "authors": {
        "Hanping Xu": "[email protected]"
    },
    "social_media_url": "https://github.com/Elenionl",
    "platforms": {
        "ios": "8.0"
    },
    "source": {
        "git": "https://github.com/Elenionl/ELSwiftJsonParser.git",
        "tag": "1.0.5"
    },
    "source_files": "ELSwiftJsonParser/*",
    "requires_arc": true,
    "frameworks": "Foundation",
    "swift_version": "4.0"
}

Pin It on Pinterest

Share This