Skip to content
This repository has been archived by the owner on Dec 10, 2017. It is now read-only.

Commit

Permalink
Add helpers to load and dump JSON files
Browse files Browse the repository at this point in the history
  • Loading branch information
bhargavg committed Apr 16, 2016
1 parent c5572cb commit c79737d
Show file tree
Hide file tree
Showing 21 changed files with 403 additions and 324 deletions.
20 changes: 6 additions & 14 deletions Banana.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
7C2053DD1CBE792A00069051 /* PerfTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C2053DC1CBE792A00069051 /* PerfTests.swift */; };
7C76D4311CBE594A00363423 /* GetTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C76D4301CBE594A00363423 /* GetTests.swift */; };
7C76D4331CBE594A00363423 /* Banana.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7CC8CCA01CBC107200896C9C /* Banana.framework */; };
7C76D43C1CBE5A1A00363423 /* TestUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C76D43B1CBE5A1A00363423 /* TestUtils.swift */; };
7C76D43E1CBE692A00363423 /* PlainObjectTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C76D43D1CBE692A00363423 /* PlainObjectTests.swift */; };
7C76D4401CBE6F6C00363423 /* person.json in Resources */ = {isa = PBXBuildFile; fileRef = 7C76D43F1CBE6F6C00363423 /* person.json */; };
7C76D4421CBE6FA100363423 /* personWithTODOItems.json in Resources */ = {isa = PBXBuildFile; fileRef = 7C76D4411CBE6FA100363423 /* personWithTODOItems.json */; };
7C76D4441CBE700C00363423 /* NestedObjectTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C76D4431CBE700C00363423 /* NestedObjectTests.swift */; };
7CA20E9C1CBCFB8F0098EC8E /* Banana.h in Headers */ = {isa = PBXBuildFile; fileRef = 7CA20E9B1CBCFB8F0098EC8E /* Banana.h */; settings = {ATTRIBUTES = (Public, ); }; };
7CA2DF3B1CBFB3FA00E91622 /* Banana.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7CA2DF3A1CBFB3FA00E91622 /* Banana.swift */; };
7CC8CCB91CBC176600896C9C /* Types.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7CC8CCB61CBC176600896C9C /* Types.swift */; };
7CC8CCBA1CBC176600896C9C /* Operators.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7CC8CCB71CBC176600896C9C /* Operators.swift */; };
7CC8CCBB1CBC176600896C9C /* Functions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7CC8CCB81CBC176600896C9C /* Functions.swift */; };
Expand All @@ -36,12 +36,12 @@
7C76D42E1CBE594A00363423 /* BananaTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = BananaTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
7C76D4301CBE594A00363423 /* GetTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GetTests.swift; sourceTree = "<group>"; };
7C76D4321CBE594A00363423 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
7C76D43B1CBE5A1A00363423 /* TestUtils.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestUtils.swift; sourceTree = "<group>"; };
7C76D43D1CBE692A00363423 /* PlainObjectTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PlainObjectTests.swift; sourceTree = "<group>"; };
7C76D43F1CBE6F6C00363423 /* person.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = person.json; sourceTree = "<group>"; };
7C76D4411CBE6FA100363423 /* personWithTODOItems.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = personWithTODOItems.json; sourceTree = "<group>"; };
7C76D4431CBE700C00363423 /* NestedObjectTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NestedObjectTests.swift; sourceTree = "<group>"; };
7CA20E9B1CBCFB8F0098EC8E /* Banana.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Banana.h; path = Support/Banana.h; sourceTree = SOURCE_ROOT; };
7CA2DF3A1CBFB3FA00E91622 /* Banana.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Banana.swift; path = Sources/Banana.swift; sourceTree = "<group>"; };
7CC8CCA01CBC107200896C9C /* Banana.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Banana.framework; sourceTree = BUILT_PRODUCTS_DIR; };
7CC8CCA51CBC107200896C9C /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; name = Info.plist; path = Support/Info.plist; sourceTree = "<group>"; };
7CC8CCB61CBC176600896C9C /* Types.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Types.swift; path = Sources/Types.swift; sourceTree = "<group>"; };
Expand All @@ -68,18 +68,9 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
7C2053DB1CBE791000069051 /* Utils */ = {
isa = PBXGroup;
children = (
7C76D43B1CBE5A1A00363423 /* TestUtils.swift */,
);
name = Utils;
sourceTree = "<group>";
};
7C76D42F1CBE594A00363423 /* BananaTests */ = {
isa = PBXGroup;
children = (
7C2053DB1CBE791000069051 /* Utils */,
7C76D4301CBE594A00363423 /* GetTests.swift */,
7C76D43D1CBE692A00363423 /* PlainObjectTests.swift */,
7C76D4431CBE700C00363423 /* NestedObjectTests.swift */,
Expand Down Expand Up @@ -126,6 +117,7 @@
7CC8CCB61CBC176600896C9C /* Types.swift */,
7CC8CCB71CBC176600896C9C /* Operators.swift */,
7CC8CCB81CBC176600896C9C /* Functions.swift */,
7CA2DF3A1CBFB3FA00E91622 /* Banana.swift */,
);
name = Sources;
sourceTree = "<group>";
Expand Down Expand Up @@ -244,14 +236,14 @@
7C76D4311CBE594A00363423 /* GetTests.swift in Sources */,
7C76D4441CBE700C00363423 /* NestedObjectTests.swift in Sources */,
7C2053DD1CBE792A00069051 /* PerfTests.swift in Sources */,
7C76D43C1CBE5A1A00363423 /* TestUtils.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
7CC8CC9B1CBC107200896C9C /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
7CA2DF3B1CBFB3FA00E91622 /* Banana.swift in Sources */,
7CC8CCB91CBC176600896C9C /* Types.swift in Sources */,
7CC8CCBA1CBC176600896C9C /* Operators.swift in Sources */,
7CC8CCBB1CBC176600896C9C /* Functions.swift in Sources */,
Expand Down Expand Up @@ -327,7 +319,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.3;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
Expand Down Expand Up @@ -370,7 +362,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.3;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
TARGETED_DEVICE_FAMILY = "1,2";
Expand Down
15 changes: 5 additions & 10 deletions BananaTests/GetTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,16 @@ class GetTests: XCTestCase {
}

func testKeyPath() {
let json = Utils.loadJSON("personWithTODOItems")

do {
let stringKey: String = try get(json) <~~ keyPath("address.home.street")
let rawJSON: [String: AnyObject] = try Banana.load(file: "personWithTODOItems", fileExtension: "json", bundle: NSBundle(forClass: GetTests.self))

let stringKey: String = try rawJSON <~~ keyPath("address.home.street")
let boolKey: Bool = try rawJSON <~~ keyPath("address.office.is_active")

XCTAssertTrue(stringKey == "17/B, Bank Road")
} catch {
XCTFail("Failed with error: \(error)")
}

do {
let boolKey: Bool = try get(json) <~~ keyPath("address.office.is_active")
XCTAssertTrue(boolKey == false)
} catch {
XCTFail("Failed with error: \(error)")
}
}

}
35 changes: 17 additions & 18 deletions BananaTests/NestedObjectTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ import Banana
class NestedObjectTests: XCTestCase {

func testParsing() {
let json = Utils.loadJSON("personWithTODOItems")

do {
let person = try get(json) <~~ Person.init

let person = try Banana.load(file: "personWithTODOItems", fileExtension: "json", bundle: NSBundle(forClass: GetTests.self)) <~~ Person.fromJSON

XCTAssert(person.name == "Bob")
XCTAssert(person.age == 25)
Expand All @@ -34,12 +33,12 @@ class NestedObjectTests: XCTestCase {
let homePinCode: String?
let todoItems: [TodoItem]

init(json: JSON) throws {
name = try get(json, key: "name")
age = try get(json, key: "age")
gender = try get(json, key: "gender") <~~ Gender.init
homePinCode = try? get(json, key: "address") <~~ keyPath("home.pincode")
todoItems = try get(json, key: "todo_items") <<~ TodoItem.init
static func fromJSON(json: JSON) throws -> Person {
return Person(name: try get(json, key: "name"),
age: try get(json, key: "age"),
gender: try get(json, key: "gender") <~~ Gender.parse,
homePinCode: try? get(json, key: "address") <~~ keyPath("home.pincode"),
todoItems: try get(json, key: "todo_items") <<~ TodoItem.fromJSON)
}
}

Expand All @@ -48,24 +47,24 @@ class NestedObjectTests: XCTestCase {
let title: String
let isCompleted: Bool

init(json: JSON) throws {
id = try get(json, key: "id")
title = try get(json, key: "title")
isCompleted = try get(json, key: "is_completed")
static func fromJSON(json: JSON) throws -> TodoItem {
return TodoItem(id: try get(json, key: "id"),
title: try get(json, key: "title"),
isCompleted: try get(json, key: "is_completed"))
}
}

enum Gender {
case Male, Female

init(fromString: String) throws {
switch fromString {
static func parse(value: String) throws -> Gender {
switch value {
case "male":
self = .Male
return .Male
case "female":
self = .Female
return .Female
default:
throw ParseError<String, String>.Custom("Invalid Gender: \(fromString)")
throw BananaError<String, String>.Custom("Invalid Gender: \(value)")
}
}
}
Expand Down
34 changes: 17 additions & 17 deletions BananaTests/PerfTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import Banana
class PerfTests: XCTestCase {

func testPerformanceExample() {
let json = Utils.loadJSON("personWithTODOItems")
let json: [String: AnyObject] = try! Banana.load(file: "personWithTODOItems", fileExtension: "json", bundle: NSBundle(forClass: PerfTests.self))

self.measureBlock {
for _ in 0...1_000 {
let _ = try! get(json) <~~ Person.init
let _ = try! json <~~ Person.parse
}
}
}
Expand All @@ -29,12 +29,12 @@ class PerfTests: XCTestCase {
let homePinCode: String?
let todoItems: [TodoItem]

init(json: JSON) throws {
name = try get(json, key: "name")
age = try get(json, key: "age")
gender = try get(json, key: "gender") <~~ Gender.init
homePinCode = try? get(json, key: "address") <~~ keyPath("home.pincode")
todoItems = try get(json, key: "todo_items") <<~ TodoItem.init
static func parse(json: JSON) throws -> Person {
return Person(name: try get(json, key: "name"),
age: try get(json, key: "age"),
gender: try get(json, key: "gender") <~~ Gender.parse,
homePinCode: try? get(json, key: "address") <~~ keyPath("home.pincode"),
todoItems: try get(json, key: "todo_items") <<~ TodoItem.parse)
}
}

Expand All @@ -43,24 +43,24 @@ class PerfTests: XCTestCase {
let title: String
let isCompleted: Bool

init(json: JSON) throws {
id = try get(json, key: "id")
title = try get(json, key: "title")
isCompleted = try get(json, key: "is_completed")
static func parse(json: JSON) throws -> TodoItem {
return TodoItem(id: try get(json, key: "id"),
title: try get(json, key: "title"),
isCompleted: try get(json, key: "is_completed"))
}
}

enum Gender {
case Male, Female

init(fromString: String) throws {
switch fromString {
static func parse(value: String) throws -> Gender {
switch value {
case "male":
self = .Male
return .Male
case "female":
self = .Female
return .Female
default:
throw ParseError<String, String>.Custom("Invalid Gender: \(fromString)")
throw BananaError<String, String>.Custom("Invalid Gender: \(value)")
}
}
}
Expand Down
26 changes: 12 additions & 14 deletions BananaTests/PlainObjectTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ import Banana
class ParsingTests: XCTestCase {

func testParsing() {
let json = Utils.loadJSON("person")

do {
let person = try get(json) <~~ Person.init
do {
let person = try Banana.load(file: "person", fileExtension: "json", bundle: NSBundle(forClass: GetTests.self)) <~~ Person.fromJSON

XCTAssert(person.name == "Bob")
XCTAssert(person.age == 25)
Expand All @@ -33,25 +31,25 @@ class ParsingTests: XCTestCase {
let gender: Gender
let address: String?

init(json: JSON) throws {
name = try get(json, key: "name")
age = try get(json, key: "age")
gender = try get(json, key: "gender") <~~ Gender.init
address = try? get(json, key: "address")
static func fromJSON(json: JSON) throws -> Person {
return Person(name: try get(json, key: "name"),
age: try get(json, key: "age"),
gender: try get(json, key: "gender") <~~ Gender.parse,
address: try? get(json, key: "address"))
}
}

enum Gender {
case Male, Female

init(fromString: String) throws {
switch fromString {
static func parse(value: String) throws -> Gender {
switch value {
case "male":
self = .Male
return .Male
case "female":
self = .Female
return .Female
default:
throw ParseError<String, String>.Custom("Invalid Gender: \(fromString)")
throw BananaError<String, String>.Custom("Invalid Gender: \(value)")
}
}
}
Expand Down
22 changes: 0 additions & 22 deletions BananaTests/TestUtils.swift

This file was deleted.

39 changes: 5 additions & 34 deletions Example/Sources/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,46 +1,17 @@
//
// AppDelegate.swift
// Example
//
// Created by Bhargav Gurlanka on 4/11/16.
// Copyright © 2016 Bhargav Gurlanka. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
return true
}

func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


func applicationWillResignActive(application: UIApplication) { }
func applicationDidEnterBackground(application: UIApplication) { }
func applicationWillEnterForeground(application: UIApplication){ }
func applicationDidBecomeActive(application: UIApplication) { }
func applicationWillTerminate(application: UIApplication) { }
}

18 changes: 14 additions & 4 deletions Example/Sources/Models/Address.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,20 @@ struct Address {
let street: String
let city: String
let state: String?

/// Mapping from JSON to this model
static func fromJSON(json: JSON) throws -> Address {
return Address(street: try get(json, key: "street"),
city: try get(json, key: "city"),
state: try? get(json, key: "state"))
}

init(json: JSON) throws {
street = try get(json, key: "street")
city = try get(json, key: "city")
state = try? get(json, key: "state")
/// Mapping from this model to JSON
static func toJSON(address: Address) throws -> JSON {
var json = JSON()
json["street"] = address.street
json["city"] = address.city
json["state"] = address.state
return json
}
}
Loading

0 comments on commit c79737d

Please sign in to comment.