github.com/bhargavg/Banana

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


License
MIT

Documentation

Banana 🍌 🐒 Build Status Carthage compatible

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

Installation

Only Carthage is supported as of now. Adding CocoaPods support is in pipeline.

Carthage

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

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.