Created
August 17, 2016 07:48
-
-
Save macbaszii/78c6b193dad3a16261878f943be0df2a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static func ObjectMapperSerializer<T: Mappable>(keyPath: String?, mapToObject object: T? = nil, context: MapContext? = nil) -> ResponseSerializer<T, NSError> { | |
| return ResponseSerializer { request, response, data, error in | |
| guard error == nil else { | |
| return .Failure(error!) | |
| } | |
| guard let _ = data else { | |
| let failureReason = "Data could not be serialized. Input data was nil." | |
| let error = newError(.DataSerializationFailed, failureReason: failureReason) | |
| return .Failure(error) | |
| } | |
| let JSONResponseSerializer = Request.JSONResponseSerializer(options: .AllowFragments) | |
| let result = JSONResponseSerializer.serializeResponse(request, response, data, error) | |
| let JSONToMap: AnyObject? | |
| if let keyPath = keyPath where keyPath.isEmpty == false { | |
| JSONToMap = result.value?.valueForKeyPath(keyPath) | |
| } else { | |
| JSONToMap = result.value | |
| } | |
| if let object = object { | |
| Mapper<T>().map(JSONToMap, toObject: object) | |
| return .Success(object) | |
| } else if let parsedObject = Mapper<T>(context: context).map(JSONToMap){ | |
| return .Success(parsedObject) | |
| } | |
| let failureReason = "ObjectMapper failed to serialize response." | |
| let error = newError(.DataSerializationFailed, failureReason: failureReason) | |
| return .Failure(error) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment