Your SlideShare is downloading. ×
Himotoki: A type-safe JSON decoding library #関モバ
Himotoki: A type-safe JSON decoding library #関モバ
Himotoki: A type-safe JSON decoding library #関モバ
Himotoki: A type-safe JSON decoding library #関モバ
Himotoki: A type-safe JSON decoding library #関モバ
Himotoki: A type-safe JSON decoding library #関モバ
Himotoki: A type-safe JSON decoding library #関モバ
Himotoki: A type-safe JSON decoding library #関モバ
Himotoki: A type-safe JSON decoding library #関モバ
Himotoki: A type-safe JSON decoding library #関モバ
Himotoki: A type-safe JSON decoding library #関モバ
Himotoki: A type-safe JSON decoding library #関モバ
Himotoki: A type-safe JSON decoding library #関モバ
Himotoki: A type-safe JSON decoding library #関モバ
Himotoki: A type-safe JSON decoding library #関モバ
Himotoki: A type-safe JSON decoding library #関モバ
Himotoki: A type-safe JSON decoding library #関モバ
Himotoki: A type-safe JSON decoding library #関モバ
Himotoki: A type-safe JSON decoding library #関モバ
Himotoki: A type-safe JSON decoding library #関モバ
Himotoki: A type-safe JSON decoding library #関モバ
Himotoki: A type-safe JSON decoding library #関モバ
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

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

165

Published on

関西モバイルアプリ研究会 #2での発表スライドです。

関西モバイルアプリ研究会 #2での発表スライドです。

Published in: Engineering
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
165
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Himotoki A"type'safe"JSON"decoding"library @ikesyo 関西モバイルアプリ研究会!#2,!2015(05(20!Wed #関モバ
  • 2. @ikesyo いけしょー/池田翔 京都でフリーランスのiOSエンジニア (時々Android)しています 現在はフリュー㈱のモバイル開発の チームにジョイン中。 Swi$もReac)veCocoaも使ってます。
  • 3. Reac%veCocoaのコミッター(Contributor)やってます
  • 4. 閑話休題
  • 5. Swi$でJSON(=>(オブジェクトの マッピングってどうしてますか?
  • 6. 前回 Objec&ve(C時代の定番*=>*Mantle ObjectMapper
  • 7. 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との相性が悪い
  • 8. もっとタイプセーフなJSONマッパーを
  • 9. ということで作りました
  • 10. Himotoki(紐解き)
  • 11. Himotoki • h#ps://github.com/ikesyo/Himotoki • ObjectMapperとは違い、JSONのデコード(デシリアライズ) のみに特化 • Argo>(h#ps://github.com/thoughtbot/Argo)>と同じ方向性 • Argoよりもより簡潔にモデル定義が行えるように • 外部依存なし(今のところは)
  • 12. Himotoki • プロトコルベースのAPI • 継承の必要なし • class%も%struct%も使える。 • let%なプロパティのモデルも安全に扱える。 • 必要なJSONの要素が%nil%であればその時点でデコードを失敗させられる。 • 型推論による簡潔なモデル定義 • Argoのようなカリー化した%create()%メソッドは不要。
  • 13. Decodable public protocol Decodable { typealias DecodedType = Self static func decode(e: Extractor) -> DecodedType? }
  • 14. サンプルコード 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) } }
  • 15. 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" } }
  • 16. サンプルコード 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) }
  • 17. Operators 演算子は以下の6種類 // 以下 T: Decodable <| // T <|? // T? <|| // [T] <||? // [T]? <|-| // [String: T] <|-|? // [String: T]?
  • 18. Himotoki'0.2'is'released'today!
  • 19. Welcome'your'contribu/ons!!'!

×