Argo:一个库,可让您以简洁、类型安全且易于扩展的方式从 JSON 或类似结构中提取模型

阿尔戈迦太基兼容 评论者 猎犬

Argo 是一个库,可让您以简洁、类型安全且易于扩展的方式从
JSON 或类似结构中提取模型。使用 Argo,您无需
编写验证代码来确保传入的数据类型正确,或
确保必填数据字段不会显示为空。Argo 使用 Swift 的
表达式类型系统来为你做到这一点,并报告明确的失败
状态,以防它没有找到你告诉它期望的内容。

Argo是希腊语中的雨燕,也是Argonauts的Aeson的儿子
Jason使用的船名。Aeson 是 Haskell 中的 JSON 解析库,它
启发了 Argo,就像 Aeson 启发了他的儿子 Jason 一样。

版本兼容性

请注意,我们积极推进新版本
的 Swift。因此,我们强烈建议不要指向 ,
而是使用我们提供的版本之一
mastermaster

以下是当前的 Swift 兼容性细分:

迅捷版 阿尔戈版本
4.X 主人
3.X 4.X
2.2, 2.3 3.X
2.0, 2.1 2.X
1.2 - 2.0 1.X
1.1 0.3.X

Installation

Carthage

Add the following to your Cartfile:

github "thoughtbot/Argo"

Then run .carthage update

Follow the current instructions in Carthage's README
for up to date installation instructions.

Note that if you are using newer versions of Argo, you will need to link both
and into your app.
Argo.frameworkRunes.framework

CocoaPods

Add the following to your Podfile:

pod 'Argo'
Ruby

You will also need to make sure you're opting into using frameworks:

use_frameworks!
Ruby

Then run with CocoaPods 0.36 or newer.pod install

Git Submodules

I guess you could do it this way if that's your thing.

Add this repo as a submodule, and add the project file to your workspace. You
can then link against for your application target.
Argo.framework

You will need to do the same for Runes if you are using newer versions of
Argo.

Usage tl;dr:

Please note: the example below requires an additional, external module named
Curry which lets us use the
function to curry .
curryUser.init

It also imports Runes, which is a dependency of Argo in newer versions. If
you are using an older version of Argo, you might not need that import.

import Argo
import Curry
import Runes

struct User {
  let id: Int
  let name: String
  let email: String?
  let role: Role
  let companyName: String
  let friends: [User]
}

extension User: Decodable {
  static func decode(_ json: JSON) -> Decoded<User> {
    return curry(User.init)
      <^> json <| "id"
      <*> json <| "name"
      <*> json <|? "email" // Use ? for parsing optional values
      <*> json <| "role" // Custom types that also conform to Decodable just work
      <*> json <| ["company", "name"] // Parse nested objects
      <*> json <|| "friends" // parse arrays of objects
  }
}

// Wherever you receive JSON data:

let json: Any? = try? NSJSONSerialization.JSONObjectWithData(data, options: [])

if let j: Any = json {
  let user: User? = decode(j)
}
Swift

For more information, see the Documentation

Contributing

See the CONTRIBUTING document. Thank you, contributors!

License

Argo is Copyright (c) 2015 thoughtbot, inc. It is free software, and may be
redistributed under the terms specified in the LICENSE file.

About

thoughtbot

Argo is maintained and funded by thoughtbot, inc. The names and logos for
thoughtbot are trademarks of thoughtbot, inc.

We love open source software! See our other projects or look at
our product case studies and hire us to help build your iOS app.

GitHub

https://github.com/thoughtbot/Argo