Skip to content

Commit

Permalink
Merge pull request #33 from alickbass/dictionaries-support
Browse files Browse the repository at this point in the history
Dictionaries support
  • Loading branch information
Oleksii Dykan authored May 15, 2017
2 parents 03af120 + fe227d2 commit 46dce4a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
25 changes: 24 additions & 1 deletion SwiftyJSONModel/JSONObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public extension JSONObject where PropertyType.RawValue == String {

private func value<T: JSONInitializable>(for keyPath: [PropertyType]) throws -> [T] {
return try value(for: keyPath) {
try $0.arrayValue().enumerated().lazy.map({ index, json in
try $0.arrayValue().lazy.enumerated().map({ index, json in
do {
return try T(json: json)
} catch let error as JSONModelError {
Expand All @@ -98,6 +98,25 @@ public extension JSONObject where PropertyType.RawValue == String {
}
}

// MARK: - Value for keypath - Dictionary
public func value<T: JSONInitializable>(for keyPath: PropertyType...) throws -> [String: T] {
return try value(for: keyPath)
}

private func value<T: JSONInitializable>(for keyPath: [PropertyType]) throws -> [String: T] {
return try value(for: keyPath) {
var result: [String: T] = [:]
try $0.dictionaryValue().forEach({ key, json in
do {
result[key] = try T(json: json)
} catch let error as JSONModelError {
throw JSONModelError.invalidValueFor(key: key, error)
}
})
return result
}
}

// MARK: - Value for keypath - Date
public func value(for keyPath: PropertyType..., with transformer: DateTransformer) throws -> Date {
return try value(for: keyPath, with: transformer)
Expand All @@ -121,6 +140,10 @@ public extension JSONObject where PropertyType.RawValue == String {
return try? value(for: keyPath)
}

public func value<T: JSONInitializable>(for keyPath: PropertyType...) -> [String: T]? {
return try? value(for: keyPath)
}

public func value(for keyPath: PropertyType..., with transformer: DateTransformer) -> Date? {
return try? value(for: keyPath, with: transformer)
}
Expand Down
20 changes: 20 additions & 0 deletions SwiftyJSONModel/JSONTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,26 @@ public extension Array where Element: JSONRepresentable {
}
}

struct JSONDict<T: JSONRepresentable>: JSONRepresentable {
let dict: [String: T]

init(_ dict: [String: T]) {
self.dict = dict
}

var jsonValue: JSON {
var result: [String: JSON] = [:]
dict.forEach({ result[$0] = $1.jsonValue })
return JSON(dictionary: result)
}
}

public extension Dictionary where Key == String, Value: JSONRepresentable {
public var jsonRepresantable: JSONRepresentable {
return JSONDict<Value>(self)
}
}

public extension Date {
public func json(with transformer: DateTransformer) -> String {
return transformer.string(form: self)
Expand Down
2 changes: 2 additions & 0 deletions SwiftyJSONModelTests/JSONExtensionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class JSONExtensionTests: XCTestCase {

let json: [String: JSON] = ["SomeKey": "SomeValue"]
XCTAssertEqual(try! JSON(dictionary: json).dictionaryValue(), json)

XCTAssertEqual(["SomeKey": "SomeValue"].jsonRepresantable.jsonValue, ["SomeKey": "SomeValue"])
}

func testJSONConformanceToJSONProtocols() {
Expand Down
14 changes: 14 additions & 0 deletions SwiftyJSONModelTests/JSONObjectTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ class JSONObjectTests: XCTestCase {
}
}

func testDictionaryValue() {
enum Key: String { case some }
let object = try! JSONObject<Key>(json: ["some": ["one": 1, "two": 2]])

XCTAssertEqual(try! object.value(for: .some), ["one": 1, "two": 2])
XCTAssertNotNil(object.value(for: .some) as [String: Int]?)

let otherObject = try! JSONObject<Key>(json: ["some": ["one": "1", "two": "2"]])

XCTAssertThrowsError(try otherObject.value(for: .some) as [String: Int]) { error in
XCTAssertEqual(error as? JSONModelError, .invalidValueFor(key: Key.some.rawValue, .invalidValueFor(key: "one", .invalidElement)))
}
}

func testJSONObjectOptionalValueForKey() {
let jsonObject = try! JSONObject<Person.PropertyKey>(json: Data.person.jsonValue)
let emptyJsonObject = try! JSONObject<Person.PropertyKey>(json: JSON([String: JSON]()))
Expand Down

0 comments on commit 46dce4a

Please sign in to comment.