bhargavg / Banana

[Deprecated] High calorie, easy to digest, JSON mapper in Swift. Just peel and eat.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Deprecated] Banana 🍌 πŸ’ Build Status Carthage compatible

This library is deprecated in favor of Swift 4's Decodable protocol

Banana is a library that allows conversion of parsed JSON into typed objects.

Why another JSON mapper right?

The idea behind creating Banana is to show that JSON mapping is not as complicated.

Simplicity and no-black-magic are the key design principles. The name Banana is chosen to signify this.

If you are interested in how this libary has evolved, please read this blog post series

Features

  • Error handling through do-try-catch mechanism
  • Handles Optionals
  • Supports Keypaths

Understanding Version Numbers

Banana supports both legacy Swift 2.3 and latest Swift 3.0. To achieve this, the following release strategy is followed:

  • Odd number versions for Swift 2.3 releases
  • Even number versions for Swift 3 releases

Installation

To add this library to your project, just add the following to your Cartfile

github "bhargavg/banana"

and carthage update. For full list of command, please refer Carthage documentation.

Note: Make sure you use even version number releases.

Examples

Read a single value

let value: String = try Banana.load(file: "simple") <~~ keyPath("path.to.key")

Mapping to models and back

[
    {
        "x": "hi",
        "y": 5
    },
    {
        "x": "yolo",
        "yo": 6
    }
]
struct Foo {
    let x: String
    let y: Int

    static func fromJSON(json: JSON) throws -> Foo {
        return Foo(
                    x: try get(json, key: "x"),
                    y: try get(json, keys: ["y", "yo"])
                  )
    }

    static func toJSON(foo: Foo) -> JSON {
        return ["x": foo.x, "y": foo.y]
    }
}

let foos: [Foo] = try Banana.load(file: "foos_file") <<~ Foo.fromJSON
print(foos)

let jsonString: String = try foos <<~ Foo.toJSON <~~ Banana.dump(options: [.PrettyPrinted]) <~~ Banana.toString(encoding: NSUTF8StringEncoding)
print(jsonString)

Todo:

  • Carthage Support
  • CocoaPods Support
  • SwiftPM Support
  • OS X, iOS Targets
  • Watch, TvOS Targets

Contribution

Found a bug? Want a new feature? Please feel free to report any issue or raise a Pull Request.

About

[Deprecated] High calorie, easy to digest, JSON mapper in Swift. Just peel and eat.

License:MIT License


Languages

Language:Swift 98.0%Language:Objective-C 2.0%