GithubHelp home page GithubHelp logo

jsonmapper's Introduction

JSONMapper

JSON mapping help for Swift

##Installation JSONMapper can be added to your project using Cocoapods 0.36 (beta) by adding the following line to your Podfile:

pod 'JSONMapper', '~> 0.0.1'

TODO

  • more thorough testing

Example

Model

import Foundation
import UIKit

struct Tweet: Mappable {
    let user: User
    let text: String
    let screenName: String
    let createdAt: Date
    let favorited: Bool
    let language: Language?
    let urlItems: [URLItem]
    let replyToStatus: Int?
    
    enum Language: String, Mappable {
        case english = "en"
        case french = "fr"
        case spanish = "es"
    }
    
    enum Keys: String, Key {
        case user
        case text
        case createdAt
        case favorited
        case language = "lang"
        case urlItems = "entities.urls"
        case replayToStatusId = "in_reply_to_status_id"
    }
    
    init(mapper: Mapper) throws {
        let container = try mapper.keyedMapperValue()
        
        language = container.decode(forKeyPath: Keys.language)
        user = try container.decodeValue(forKeyPath: Keys.user)
        screenName = try container.decodeValue(forKeyPath: ["user", "screen_name"])
        text = try container.decodeValue(forKeyPath: Keys.text)
        createdAt = try container.decodeValue(forKeyPath: Keys.createdAt)
        favorited = try container.decodeValue(forKeyPath: Keys.favorited)
        urlItems = try container.decodeValue(forKeyPath: Keys.urlItems, decoder: JSONDecoder())
        replyToStatus = container.decode(forKeyPath: Keys.replayToStatusId)
    }
}

struct User: Mappable {
    let name: String
    let idString: String
    let id: Int
    let createdAt: Date
    let url: URL?
    let urlItems: [URLItem]
    let defaultProfile: Bool
    let followersCount: Int
    let description: String
    let backgroundColor: UIColor?
    
    enum Keys: String, Key {
        case idString = "id_str"
        case createdAt = "created_at"
        case defaultProfile = "default_profile"
        case followersCount = "followers_count"
        case description
        case backgroundColor = "profile_background_color"
    }
    
    init(mapper: Mapper) throws {
        let container = try mapper.keyedMapperValue()
        
        name = try container.decodeValue(forKeyPath: "name")
        idString = try container.decodeValue(forKeyPath: Keys.idString)
        id = try container.decodeValue(forKeyPath: "id")
        createdAt = try container.decodeValue(forKeyPath: Keys.createdAt)
        urlItems = try container.decodeValue(forKeyPath: ["entities", "description", "urls"], decoder: JSONDecoder())
        defaultProfile = try container.decodeValue(forKeyPath: Keys.defaultProfile)
        followersCount = try container.decodeValue(forKeyPath: Keys.followersCount)
        url = container.decode(forKeyPath: "url")
        description = try container.decodeValue(forKeyPath: Keys.description)
        
        backgroundColor = container.transform(forKeyPath: Keys.backgroundColor) { value in
            return UIColor.fromHex(hex: value)
        }
    }
}

struct URLItem: Decodable {
    let displayURL: String
    let expandedURL: URL
    let url: URL
    let indices: [Int]
    
    enum CodingKeys: String, CodingKey {
        case displayURL = "display_url"
        case expandedURL = "expanded_url"
        case url
        case indices
    }
}

Decoding

import JSON

let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "EEE MMM dd HH:mm:ss Z yyyy"
        
let adapter = JSON.Adapter()
adapter.dateDecodingStrategy = .formatted(dateFormatter)
adapter.keyDecodingStrategy = .convertToSnakeCase
        
let data: Data = // data from a local file, network, etc
let tweets: [Tweet] = try self.adapter.decode(data: data)

print(tweets)

jsonmapper's People

Contributors

asurin avatar jlalvarez18 avatar plackemacher avatar

Stargazers

 avatar

Watchers

 avatar  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.