Skip to content

Commit

Permalink
Add jsonArray and jsonDictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
delba committed Sep 3, 2015
1 parent 27bac4d commit 741676b
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 16 deletions.
36 changes: 20 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,22 +125,26 @@ These getters come by two, `json.<type>` and `json.<type>Value`, and returns an

If the value can't be converted to the given type, the optional getter will return nil and the non-optional one a default value.

Property | Type | Default value
----------------- | ---------------------- | -------------
`string` | `String?` |
`stringValue` | `String` | `""`
`bool` | `Bool?` |
`boolValue` | `Bool` | `false`
`int` | `Int?` |
`intValue` | `Int` | `0`
`double` | `Double?` |
`doubleValue` | `Double` | `0.0`
`float` | `Float?` |
`floatValue` | `Float` | `0.0`
`array` | `[AnyObject]?` |
`arrayValue` | `[AnyObject]` | `[]`
`dictionary` | `[String: AnyObject]?` |
`dictionaryValue` | `[String: AnyObject]` | `[:]`
Property | Type | Default value
----------------- | ---------------------- | -------------
`string` | `String?` |
`stringValue` | `String` | `""`
`bool` | `Bool?` |
`boolValue` | `Bool` | `false`
`int` | `Int?` |
`intValue` | `Int` | `0`
`double` | `Double?` |
`doubleValue` | `Double` | `0.0`
`float` | `Float?` |
`floatValue` | `Float` | `0.0`
`array` | `[AnyObject]?` |
`arrayValue` | `[AnyObject]` | `[]`
`jsonArray` | `[JSON]?` |
`jsonArrayValue` | `[JSON]` | `[]`
`dictionary` | `[String: AnyObject]?` |
`dictionaryValue` | `[String: AnyObject]` | `[:]`
`jsonDictionary` | `[String: JSON]?` |
`jsonDictionaryValue` | `[String: JSON]` | `[:]`

> You can find more getters on the [`extensions` branch](https://github.com/delba/JASON/tree/extensions)
Expand Down
23 changes: 23 additions & 0 deletions Source/JASON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ extension JSON {
public var dictionaryValue: [String: AnyObject] { return dictionary ?? [:] }
}

extension JSON {
/// The value as a dictionary or nil if not present/convertible
public var jsonDictionary: [String: JSON]? { return dictionary?.reduceValues{ JSON($0) }}
/// The value as a dictionary or an empty dictionary if not present/convertible
public var jsonDictionaryValue: [String: JSON] { return jsonDictionary ?? [:] }
}

// MARK: Array

extension JSON {
Expand All @@ -162,6 +169,13 @@ extension JSON {
public var arrayValue: [AnyObject] { return array ?? [] }
}

extension JSON {
/// The value as an array or nil if not present/convertible
public var jsonArray: [JSON]? { return array?.map{ JSON($0) } }
/// The value as an array or an empty array if not present/convertible
public var jsonArrayValue: [JSON] { return jsonArray ?? [] }
}

// MARK: NSDictionary

private extension JSON {
Expand Down Expand Up @@ -464,3 +478,12 @@ private extension NSArray {
return self[index]
}
}

private extension Dictionary {
func reduceValues <T: Any>(transform: (value: Value) -> T) -> [Key: T] {
return reduce([Key: T]()) { (var dictionary, kv) in
dictionary[kv.0] = transform(value: kv.1)
return dictionary
}
}
}
51 changes: 51 additions & 0 deletions Tests/JASONTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,35 @@ class JASONTests: XCTestCase {
XCTAssertNil(json["name"].dictionary)
XCTAssertEqualDictionaries([String: AnyObject](), json["name"].dictionaryValue)
}

func testJsonDictionary() {
let json: JSON = [
"name": "Brandon Walsh",
"mother": [
"name": "Cindy Walsh"
]
]

var key: String

// Value is present and convertible

key = "mother"
XCTAssertEqual("Cindy Walsh", json[key].jsonDictionary!["name"]!.string!)
XCTAssertEqual("Cindy Walsh", json[key].jsonDictionaryValue["name"]!.stringValue)

// Value is not present

key = "father"
XCTAssert(json[key].jsonDictionary == nil)
XCTAssert(json[key].dictionaryValue.isEmpty)

// Value is not convertible

key = "name"
XCTAssert(json[key].jsonDictionary == nil)
XCTAssert(json[key].jsonDictionaryValue.isEmpty)
}

func testArray() {
let json: JSON = [
Expand All @@ -202,6 +231,28 @@ class JASONTests: XCTestCase {
XCTAssertNil(json["name"].array)
XCTAssertEqualArrays([AnyObject](), json["name"].arrayValue)
}

func testJsonArray() {
let json: JSON = [
"name": "Brandon Walsh",
"friends": ["Steve Sanders", "Dylan McKay"]
]

// Value is present and convertible

XCTAssertEqualArrays(["Steve Sanders", "Dylan McKay"], json["friends"].jsonArray!.map{$0.stringValue})
XCTAssertEqualArrays(["Steve Sanders", "Dylan McKay"], json["friends"].jsonArrayValue.map{$0.stringValue})

// Value is not present

XCTAssert(json["romances"].jsonArray == nil)
XCTAssert(json["romances"].jsonArrayValue.isEmpty)

// Value is not convertible

XCTAssert(json["name"].jsonArray == nil)
XCTAssert(json["name"].jsonArrayValue.isEmpty)
}

func testStringOperators() {
var string: String = "Brandon Walsh"
Expand Down

0 comments on commit 741676b

Please sign in to comment.