kickstarter/Argo

Name: Argo

Owner: Kickstarter

Description: Functional JSON parsing library for Swift

Created: 2016-01-26 16:45:47.0

Updated: 2017-02-10 05:25:09.0

Pushed: 2016-03-04 16:43:25.0

Homepage: https://thoughtbot.com/open-source

Size: 998

Language: Swift

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Argo Carthage compatible

Argo is a library that lets you extract models from JSON or similar structures in a way that's concise, type-safe, and easy to extend. Using Argo, you won't need to write validation code to ensure that incoming data is of the right type, or to make sure required data fields aren't turning up empty. Argo uses Swift's expressive type system to do that for you, and reports back explicit failure states in case it doesn't find what you've told it to expect.

Argo is the Greek word for swift and the name of the ship used by Jason, son of Aeson, of the Argonauts. Aeson is the JSON parsing library in Haskell that inspired Argo, much like Aeson inspired his son Jason.

NOTE: For Swift 1.2 support, use the versions tagged 1.x.x. For Swift 1.1 support, use the versions tagged 0.3.x. You can find those in the releases.

Installation
Carthage

Add the following to your Cartfile:

ub "thoughtbot/Argo"

Then run carthage update.

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

CocoaPods

Add the following to your Podfile:

'Argo'

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

frameworks!

Then run pod install with CocoaPods 0.36 or newer.

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 Argo.framework for your application target.

Usage tl;dr:

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

rt Argo
rt Curry

ct User {
t id: Int
t name: String
t email: String?
t role: Role
t companyName: String
t friends: [User]


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



herever you receive JSON data:

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

et j: AnyObject = json {
t user: User? = decode(j)

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.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.