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
- Error handling through
do-try-catch
mechanism - Handles Optionals
- Supports Keypaths
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
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.
let value: String = try Banana.load(file: "simple") <~~ keyPath("path.to.key")
[
{
"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)
- Carthage Support
- CocoaPods Support
- SwiftPM Support
- OS X, iOS Targets
- Watch, TvOS Targets
Found a bug? Want a new feature? Please feel free to report any issue or raise a Pull Request.