Skip to content

Commit

Permalink
Add init with json to Dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksii Dykan committed Sep 19, 2017
1 parent bbccd5a commit 269ba19
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
12 changes: 1 addition & 11 deletions SwiftyJSONModel/JSONObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,7 @@ public extension JSONObject where PropertyType.RawValue == String {
}

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
}
return try value(for: keyPath) { try Dictionary(json: $0) }
}

// MARK: - Value for keypath - Date
Expand Down
14 changes: 14 additions & 0 deletions SwiftyJSONModel/JSONTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,20 @@ public extension Dictionary where Key == String, Value: JSONRepresentable {
}
}

public extension Dictionary where Key == String, Value: JSONInitializable {
public init(json: JSON) throws {
var result: [String: Value] = [:]
try json.dictionaryValue().forEach({ key, json in
do {
result[key] = try Value(json: json)
} catch let error as JSONModelError {
throw JSONModelError.invalidValueFor(key: key, error)
}
})
self = result
}
}

public extension Date {
public func json(with transformer: DateTransformer) -> String {
return transformer.string(form: self)
Expand Down

0 comments on commit 269ba19

Please sign in to comment.