Skip to content

Commit

Permalink
Added map from file methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Jari Kalinainen committed Oct 20, 2017
1 parent 551395a commit f6e2631
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 f6e2631

Please sign in to comment.