22
Himotoki A type'safe JSON decoding library @ikesyo 関西モバイルアプリ研究会 #2, 2015(05(20 Wed #関モバ

Himotoki: A type-safe JSON decoding library #関モバ

Embed Size (px)

Citation preview

Page 1: Himotoki: A type-safe JSON decoding library #関モバ

HimotokiA"type'safe"JSON"decoding"library

@ikesyo

関西モバイルアプリ研究会!#2,!2015(05(20!Wed

#関モバ

Page 2: Himotoki: A type-safe JSON decoding library #関モバ

@ikesyo

いけしょー/池田翔京都でフリーランスのiOSエンジニア(時々Android)しています

現在はフリュー㈱のモバイル開発のチームにジョイン中。

Swi$もReac)veCocoaも使ってます。

Page 3: Himotoki: A type-safe JSON decoding library #関モバ
Page 4: Himotoki: A type-safe JSON decoding library #関モバ

Reac%veCocoaのコミッター(Contributor)やってます

Page 5: Himotoki: A type-safe JSON decoding library #関モバ

閑話休題

Page 6: Himotoki: A type-safe JSON decoding library #関モバ

Swi$でJSON(=>(オブジェクトのマッピングってどうしてますか?

Page 7: Himotoki: A type-safe JSON decoding library #関モバ

前回

Objec&ve(C時代の定番*=>*Mantle

ObjectMapper

Page 8: Himotoki: A type-safe JSON decoding library #関モバ
Page 9: Himotoki: A type-safe JSON decoding library #関モバ

ObjectMapperもいいけど……• オプショナルな"var"を使うのがいけてない

class User: Mappable { var username: String? var age: Int? var array: [AnyObject]? var group: Gruop? // 別の`Mappable`な型

required init?(_ map: Map) { mapping(map) }

func mapping(map: Map) { username <- map["username"] age <- map["age"] array <- map["array"] group <- map["group"] }}

class Group: Mappable { ... }

• inoutパラメータでRealmとの相性が悪い

Page 10: Himotoki: A type-safe JSON decoding library #関モバ

もっとタイプセーフなJSONマッパーを

Page 11: Himotoki: A type-safe JSON decoding library #関モバ

ということで作りました

Page 12: Himotoki: A type-safe JSON decoding library #関モバ

Himotoki(紐解き)

Page 13: Himotoki: A type-safe JSON decoding library #関モバ
Page 14: Himotoki: A type-safe JSON decoding library #関モバ

Himotoki• h#ps://github.com/ikesyo/Himotoki

• ObjectMapperとは違い、JSONのデコード(デシリアライズ)のみに特化

• Argo>(h#ps://github.com/thoughtbot/Argo)>と同じ方向性

• Argoよりもより簡潔にモデル定義が行えるように

• 外部依存なし(今のところは)

Page 15: Himotoki: A type-safe JSON decoding library #関モバ

Himotoki• プロトコルベースのAPI

• 継承の必要なし

• class%も%struct%も使える。

• let%なプロパティのモデルも安全に扱える。

• 必要なJSONの要素が%nil%であればその時点でデコードを失敗させられる。

• 型推論による簡潔なモデル定義

• Argoのようなカリー化した%create()%メソッドは不要。

Page 16: Himotoki: A type-safe JSON decoding library #関モバ

Decodable

public protocol Decodable { typealias DecodedType = Self static func decode(e: Extractor) -> DecodedType?}

Page 17: Himotoki: A type-safe JSON decoding library #関モバ

サンプルコードstruct Group: Decodable { let name: String let floor: Int let optional: [String]?

// MARK: Decodable

static func decode(e: Extractor) -> Group? { // 関数(クロージャ)としてのイニシャライザ // `let create: ((name: String, floor: Int, optional: [String]?)) -> Group` と推論される。 let create = { Group($0) }

// リスト全体がオプショナルの引数リストを生成する(22引数まで対応)。 // 途中で非オプショナルな引数に対して `nil` が出てきたら引数リストが `nil` になる。 // その引数リスト全体を `Optional.map()` に渡す。 // この場合 `build(a: String?, b: Int?, c: [String]??) -> (String, Int, [String]?)?` と推論される。 return build( e <| "name", e <| "floor", e <||? "optional" ).map(create) }}

Page 18: Himotoki: A type-safe JSON decoding library #関モバ

Argoだとstruct Group: Decodable { let name: String let floor: Int let optional: [String]?

static func create(name: String)(floor: Int)(optional: [String]?) -> Group { return Gruop(name: name, floor: floor, optional: optional) }

// MARK: Decodable

static func decode(j: JSON) -> Decoded<User> { return Gruop.create <^> j <| "name" <*> j <| "floor" <*> j <||? "optional" }}

Page 19: Himotoki: A type-safe JSON decoding library #関モバ

サンプルコード

func testGroup() { var JSON: [String: AnyObject] = [ "name": "Himotoki", "floor": 12 ]

let g: Group? = decode(JSON) XCTAssert(g != nil) XCTAssert(g?.name == "Himotoki") XCTAssert(g?.floor == 12) XCTAssert(g?.optional == nil)

JSON["name"] = nil let f: Group? = decode(JSON) XCTAssert(f == nil)}

Page 20: Himotoki: A type-safe JSON decoding library #関モバ

Operators

演算子は以下の6種類 // 以下 T: Decodable <| // T <|? // T? <|| // [T] <||? // [T]? <|-| // [String: T] <|-|? // [String: T]?

Page 21: Himotoki: A type-safe JSON decoding library #関モバ

Himotoki'0.2'is'released'today!

Page 22: Himotoki: A type-safe JSON decoding library #関モバ

Welcome'your'contribu/ons!!'!