Skip to content

Commit

Permalink
Add static types
Browse files Browse the repository at this point in the history
  • Loading branch information
delba committed Dec 7, 2015
1 parent efc950d commit da4bdb6
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 1 deletion.
2 changes: 1 addition & 1 deletion JASON.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='ios' requires-full-environment='true' last-migration='0700'>
<playground version='5.0' target-platform='ios' requires-full-environment='true'>
<timeline fileName='timeline.xctimeline'/>
</playground>
109 changes: 109 additions & 0 deletions Source/JASON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,115 @@ private extension JSON {
var nsArray: NSArray? { return object as? NSArray }
}

// MARK: Static subscripts

public class JSONKeys {}

public class JSONKey<ValueType>: JSONKeys {
public let _key: String

public init(_ key: String) {
self._key = key
}
}

extension JSON {
/// The value as a string or nil if not present/convertible
public subscript(key: JSONKey<String?>) -> String? {
return self[key._key].string
}

/// The value as a string or "" if not present/convertible
public subscript(key: JSONKey<String>) -> String {
return self[key._key].stringValue
}

/// The value as a boolean or nil if not present/convertible
public subscript(key: JSONKey<Bool?>) -> Bool? {
return self[key._key].bool
}

/// The value as a boolean or false if not present/convertible
public subscript(key: JSONKey<Bool>) -> Bool {
return self[key._key].boolValue
}

/// The value as a 64-bit signed integer or nil if not present/convertible
public subscript(key: JSONKey<Int?>) -> Int? {
return self[key._key].int
}

/// The value as a 64-bit signed integer or 0 if not present/convertible
public subscript(key: JSONKey<Int>) -> Int {
return self[key._key].intValue
}

/// The value as a 64-bit floating-point number or nil if not present/convertible
public subscript(key: JSONKey<Double?>) -> Double? {
return self[key._key].double
}

/// The value as a 64-bit floating-point number or 0.0 if not present/convertible
public subscript(key: JSONKey<Double>) -> Double {
return self[key._key].doubleValue
}

/// The value as a 32-bit floating-point number or nil if not present/convertible
public subscript(key: JSONKey<Float?>) -> Float? {
return self[key._key].float
}

/// The value as a 32-bit floating-point number or 0.0 if not present/convertible
public subscript(key: JSONKey<Float>) -> Float {
return self[key._key].floatValue
}

}

extension JSON {
/// The value as an array or nil if not present/convertible
public subscript(key: JSONKey<[AnyObject]?>) -> [AnyObject]? {
return self[key._key].array
}

/// The value as an array or an empty array if not present/convertible
public subscript(key: JSONKey<[AnyObject]>) -> [AnyObject] {
return self[key._key].arrayValue
}

/// The value as an array or nil if not present/convertible
public subscript(key: JSONKey<[JSON]?>) -> [JSON]? {
return self[key._key].jsonArray
}

/// The value as an array or an empty array if not present/convertible
public subscript(key: JSONKey<[JSON]>) -> [JSON] {
return self[key._key].jsonArrayValue
}
}

extension JSON {
/// The value as a dictionary or nil if not present/convertible
public subscript(key: JSONKey<[String: AnyObject]?>) -> [String: AnyObject]? {
return self[key._key].dictionary
}

/// The value as a dictionary or an empty dictionary if not present/convertible
public subscript(key: JSONKey<[String: AnyObject]>) -> [String: AnyObject] {
return self[key._key].dictionaryValue
}

/// The value as a dictionary or nil if not present/convertible
public subscript(key: JSONKey<[String: JSON]?>) -> [String: JSON]? {
return self[key._key].jsonDictionary
}

/// The value as a dictionary or an empty dictionary if not present/convertible
public subscript(key: JSONKey<[String: JSON]>) -> [String: JSON] {
return self[key._key].jsonDictionaryValue
}
}

// MARK: Operators

infix operator <| {
Expand Down
56 changes: 56 additions & 0 deletions Tests/JASONTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,63 @@ import UIKit
import XCTest
import JASON

extension JSONKeys {
static let string = JSONKey<String>("string")
static let optionalString = JSONKey<String?>("optionalString")
static let int = JSONKey<Int>("int")
static let optionalInt = JSONKey<Int?>("optionalInt")
static let double = JSONKey<Double>("double")
static let optionalDouble = JSONKey<Double?>("optionalDouble")
static let float = JSONKey<Float>("float")
static let optionalFloat = JSONKey<Float?>("optionalFloat")
static let bool = JSONKey<Bool>("bool")
static let optionalBool = JSONKey<Bool?>("optionalBool")
static let array = JSONKey<[AnyObject]>("array")
static let optionalArray = JSONKey<[AnyObject]?>("optionalArray")
static let dictionary = JSONKey<[String: AnyObject]>("dictionary")
static let optionalDictionary = JSONKey<[String: AnyObject]>("optionalDictionary")
static let arrayJSON = JSONKey<[JSON]>("arrayJSON")
static let optionalArrayJSON = JSONKey<[JSON]?>("optionalArrayJSON")
static let dictionaryJSON = JSONKey<[String: JSON]>("dictionaryJSON")
static let optinoalDictionaryJSON = JSONKey<[String: JSON]?>("optionalDictionaryJSON")
}


class JASONTests: XCTestCase {

func testStaticSubscripts() {
let json: JSON = [
"string": "string",
"optionalString": "string",
"int": 42,
"optionalInt": 42,
"double": 4.2,
"optionalDouble": 4.2,
"float": 4.2,
"optionalFloat": 4.2,
"bool": true,
"optionalBool": true,
"array": ["string", 42, 4.2, true],
"optionalArray": ["string", 42, 4.2, true],
"dictionary": ["string": 42],
"optionalDictionary": ["string": 42],
]

XCTAssertEqual("string", json[.string])
XCTAssertEqual("string", json[.optionalString])
XCTAssertEqual(42, json[.int])
XCTAssertEqual(42, json[.optionalInt])
XCTAssertEqual(4.2, json[.double])
XCTAssertEqual(4.2, json[.optionalDouble])
XCTAssertEqual(4.2, json[.float])
XCTAssertEqual(4.2, json[.optionalFloat])
XCTAssertEqual(true, json[.bool])
XCTAssertEqual(true, json[.optionalBool])
XCTAssertEqualArrays(["string", 42, 4.2, true], json[.array])
XCTAssertEqualArrays(["string", 42, 4.2, true], json[.optionalArray]!)
XCTAssertEqualDictionaries(["string": 42], json[.dictionary])
XCTAssertEqualDictionaries(["string": 42], json[.optionalDictionary])
}

func testInitWithObject() {
let object: AnyObject = ["name": "Brandon Walsh"]
Expand Down

0 comments on commit da4bdb6

Please sign in to comment.