Latest 1.0.1
Homepage https://github.com/DimaMishchenko/NativeJSONMapper
License MIT
Platforms ios 9.0
Authors

Swift 4

Simple Swift 4 encoding & decoding for Codable protocol objects.

Installation

CocoaPods:

pod 'NativeJSONMapper'

Usage

Decoding:

Simple Coding object:

struct CodableModel: Codable {
    var foo: String
    var bar: Int

    enum CodingKeys: String, CodingKey {
        case foo = "foo_key"
        case bar = "bar_key"
    }      
}

Mapping:


// json string
let jsonString: Any = "{"foo_key" : "FooBar", "bar_key" : 1}"

// json dictionary
let jsonDictionary: [String : Any] = ["foo_key" : "FooBar", 
                                      "bar_key" : 1]

// json dictionaries array
let jsonDictionariesArray: [[String : Any]] = (0...10).map { ["foo_key" : "FooBar - ($0)", 
                                                              "bar_key" : $0] }

//Mapping

let mapper = NativeJSONMapper()

do {
    // string
    let fromString = try mapper.map(jsonString, to: CodableModel.self)

    // dictionary
    let fromDictionary = try mapper.map(jsonDictionary, to: CodableModel.self)

    // dictionaries array
    let fromDictionariesArray = try mapper.map(jsonDictionariesArray, to: [CodableModel].self)
} catch {
    // handle error
}

Encoding:

let model = CodableModel(foo: "FooBar", bar: 1)

let mapper = NativeJSONMapper()

do {
    let data = try mapper.encode(model)
    let string = try mapper.encodeToString(model)
    let dictionary = try mapper.encodeToDictionary(model)
} catch {
    // handle error
}

Custom decoder:

let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .secondsSince1970

let mapper = NativeJSONMapper(decoder: decoder)
//...

License

NativeJSONMapper is under MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "NativeJSONMapper",
    "version": "1.0.1",
    "summary": "Simple Swift 4 encoding & decoding",
    "description": "Simple Swift 4 encoding & decoding.",
    "homepage": "https://github.com/DimaMishchenko/NativeJSONMapper",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Dima Mishchenko": "[email protected]"
    },
    "source": {
        "git": "https://github.com/DimaMishchenko/NativeJSONMapper.git",
        "tag": "1.0.1"
    },
    "platforms": {
        "ios": "9.0"
    },
    "source_files": "NativeJSONMapper/NativeJSONMapper/*"
}

Pin It on Pinterest

Share This