Skip to content
This repository has been archived by the owner on Feb 2, 2023. It is now read-only.

Commit

Permalink
Work on updated API. #8
Browse files Browse the repository at this point in the history
  • Loading branch information
calebd committed Nov 19, 2015
1 parent ebfac53 commit f0a421c
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 45 deletions.
87 changes: 56 additions & 31 deletions Alexander/JSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,50 +25,42 @@ public struct JSON {

public var object: AnyObject

public subscript(index: Int) -> JSON? {
let array = object as? [AnyObject]
return (array?[index]).map({ JSON(object: $0) })
}

public subscript(key: String) -> JSON? {
let dictionary = object as? [String: AnyObject]
return (dictionary?[key]).map({ JSON(object: $0) })
}

public var string: String? {
return object as? String
public var array: [JSON]? {
return arrayValue?.map({ JSON(object: $0) })
}

public var dictionary: [String: JSON]? {
return (object as? [String: AnyObject])?.mapValues({ JSON(object: $0) })
return dictionaryValue?.mapValues({ JSON(object: $0) })
}

public var array: [JSON]? {
return (object as? [AnyObject])?.map({ JSON(object: $0) })
public var arrayValue: [AnyObject]? {
return object as? [AnyObject]
}

public var int: Int? {
return object as? Int
public var dictionaryValue: [String: AnyObject]? {
return object as? [String: AnyObject]
}

public var double: Double? {
return object as? Double
public subscript(index: Int) -> JSON? {
let array = object as? [AnyObject]
return (array?[index]).map({ JSON(object: $0) })
}

public var bool: Bool? {
return object as? Bool
public subscript(key: String) -> JSON? {
let dictionary = object as? [String: AnyObject]
return (dictionary?[key]).map({ JSON(object: $0) })
}

public var url: NSURL? {
return string.flatMap({ NSURL(string: $0) })
public var stringValue: String? {
return object as? String
}

public var timeInterval: NSTimeInterval? {
return object as? NSTimeInterval
public var intValue: Int? {
return object as? Int
}

public var date: NSDate? {
return timeInterval.map({ NSDate(timeIntervalSince1970: $0) })
public var boolValue: Bool? {
return object as? Bool
}


Expand All @@ -84,6 +76,14 @@ public struct JSON {
.map(transform)
.flatMap({ $0 })
}

public func decode<T: RawRepresentable>(type: T.Type) -> T? {
return (object as? T.RawValue).flatMap(T.init)
}

public func decodeArray<T: RawRepresentable>(type: T.Type) -> [T]? {
return (object as? [T.RawValue])?.lazy.map(T.init).flatMap({ $0 })
}
}

extension JSON {
Expand Down Expand Up @@ -112,11 +112,36 @@ extension JSON: CustomDebugStringConvertible {
}

extension JSON {
public func decode<T: RawRepresentable>(type: T.Type) -> T? {
return (object as? T.RawValue).flatMap(T.init)
@available(*, deprecated, message = "Use stringValue instead.")
public var string: String? {
return stringValue
}

public func decodeArray<T: RawRepresentable>(type: T.Type) -> [T]? {
return (object as? [T.RawValue])?.lazy.map(T.init).flatMap({ $0 })
@available(*, deprecated, message = "Use intValue instead.")
public var int: Int? {
return intValue
}

public var double: Double? {
return object as? Double
}

@available(*, deprecated, message = "Use boolValue instead.")
public var bool: Bool? {
return boolValue
}

@available(*, deprecated, message = "Use decode(NSURL) instead.")
public var url: NSURL? {
return decode(NSURL)
}

public var timeInterval: NSTimeInterval? {
return object as? NSTimeInterval
}

@available(*, deprecated, message = "Use decode(NSDate) instead.")
public var date: NSDate? {
return decode(NSDate)
}
}
12 changes: 12 additions & 0 deletions Alexander/JSONCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,15 @@ public extension Alexander.JSON {
return decodeArray(T.decode)
}
}

extension NSURL: JSONDecodable {
public static func decode(JSON: Alexander.JSON) -> Self? {
return JSON.stringValue.flatMap(self.init)
}
}

extension NSDate: JSONDecodable {
public static func decode(JSON: Alexander.JSON) -> Self? {
return (JSON.object as? NSTimeInterval).flatMap({ self.init(timeIntervalSince1970: $0) })
}
}
32 changes: 18 additions & 14 deletions Tests/AlexanderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ final class AlexanderTests: XCTestCase {
func testArray() {
let JSON = Alexander.JSON(object: ["1","2", "a", "B", "D"])
XCTAssertEqual(JSON.array?.count, 5)
XCTAssertEqual(JSON[0]?.string, "1")
XCTAssertEqual(JSON[1]?.string, "2")
XCTAssertEqual(JSON[2]?.string, "a")
XCTAssertEqual(JSON[3]?.string, "B")
XCTAssertEqual(JSON[4]?.string, "D")
XCTAssertEqual(JSON[0]?.stringValue, "1")
XCTAssertEqual(JSON[1]?.stringValue, "2")
XCTAssertEqual(JSON[2]?.stringValue, "a")
XCTAssertEqual(JSON[3]?.stringValue, "B")
XCTAssertEqual(JSON[4]?.stringValue, "D")
}

func testDictionary() {
Expand All @@ -37,17 +37,17 @@ final class AlexanderTests: XCTestCase {
let JSON = Alexander.JSON(object: dictionary)

XCTAssertEqual(JSON["double"]?.double, 9823.212)
XCTAssertEqual(JSON["int"]?.int, 42)
XCTAssertEqual(JSON["string"]?.string, "Caleb")
XCTAssertEqual(JSON["bool"]?.bool, true)
XCTAssertEqual(JSON["int"]?.intValue, 42)
XCTAssertEqual(JSON["string"]?.stringValue, "Caleb")
XCTAssertEqual(JSON["bool"]?.boolValue, true)

XCTAssertEqual(JSON["array"]?.array?.count, 2)
XCTAssertEqual(JSON["array"]?[0]?.double, 1234)
XCTAssertEqual(JSON["array"]?[1]?.double, 4.212)

XCTAssertEqual(JSON["object"]?.dictionary?.count, 2)
XCTAssertEqual(JSON["object"]?["double"]?.double, 877.2323)
XCTAssertEqual(JSON["object"]?["string"]?.string, "Jon")
XCTAssertEqual(JSON["object"]?["string"]?.stringValue, "Jon")

XCTAssertNil(JSON["null"])
}
Expand Down Expand Up @@ -89,7 +89,7 @@ final class AlexanderTests: XCTestCase {
var name: String

static func decode(JSON: Alexander.JSON) -> User? {
guard let ID = JSON["id"]?.string, let name = JSON["name"]?.string else {
guard let ID = JSON["id"]?.stringValue, let name = JSON["name"]?.stringValue else {
return nil
}
return User(ID: ID, name: name)
Expand All @@ -114,7 +114,7 @@ final class AlexanderTests: XCTestCase {
var name: String

static func decode(JSON: Alexander.JSON) -> User? {
guard let ID = JSON["id"]?.string, let name = JSON["name"]?.string else {
guard let ID = JSON["id"]?.stringValue, let name = JSON["name"]?.stringValue else {
return nil
}
return User(ID: ID, name: name)
Expand Down Expand Up @@ -148,14 +148,18 @@ final class AlexanderTests: XCTestCase {

func testURLHelpers() {
let JSON = Alexander.JSON(object: "https://www.hodinkee.com")
let URL = JSON.url
XCTAssertNotNil(URL)

guard let URL = JSON.decode(NSURL) else {
XCTFail()
return
}

XCTAssertEqual(URL, NSURL(string: "https://www.hodinkee.com"))
}

func testDateHelpers() {
let JSON = Alexander.JSON(object: 978307200)
XCTAssertEqual(JSON.timeInterval, NSDate(timeIntervalSinceReferenceDate: 0).timeIntervalSince1970)
XCTAssertEqual(JSON.date, NSDate(timeIntervalSinceReferenceDate: 0))
XCTAssertEqual(JSON.decode(NSDate), NSDate(timeIntervalSinceReferenceDate: 0))
}
}

0 comments on commit f0a421c

Please sign in to comment.