Skip to content

Commit

Permalink
Merge pull request #910 from jvk75/master
Browse files Browse the repository at this point in the history
Added "map from file" methods
  • Loading branch information
tristanhimmelman authored Oct 30, 2017
2 parents d1b8536 + f6e2631 commit 0f81d99
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions Sources/Mapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,44 @@ public final class Mapper<N: BaseMappable> {
}
}

extension Mapper {
// MARK: Functions that create model from JSON file

/// JSON file to Mappable object
/// - parameter JSONfile: Filename
/// - Returns: Mappable object
public func map(JSONfile: String) -> N? {
if let path = Bundle.main.path(forResource: JSONfile, ofType: nil) {
do {
let JSONString = try String(contentsOfFile: path)
do {
return self.map(JSONString: JSONString)
}
} catch {
return nil
}
}
return nil
}

/// JSON file to Mappable object array
/// - parameter JSONfile: Filename
/// - Returns: Mappable object array
public func mapArray(JSONfile: String) -> [N]? {
if let path = Bundle.main.path(forResource: JSONfile, ofType: nil) {
do {
let JSONString = try String(contentsOfFile: path)
do {
return self.mapArray(JSONString: JSONString)
}
} catch {
return nil
}
}
return nil
}
}

extension Mapper {

// MARK: Functions that create JSON from objects
Expand Down

0 comments on commit 0f81d99

Please sign in to comment.