Mapping and Parsing JSON Automagically with Alamofire and EVReflection

Seyhun AKYÜREK
Seyhun Akyurek
Published in
3 min readJan 20, 2017

I’d often using Alamofire for HTTP networking, it is popular networking library written in Swift. So, it is the extremely powerful library and easy to use but while doing networking operations I often need abstraction layer to encapsulate Alamofire instead calling it directly.

I did use Moya as for abstraction in my past project so basically, it provides a Manager class to define a clear usage of endpoints, request parameters etc. So I did create my own Manager singleton to manage this stuff.

While designing models, we need to implement mapping for JSON values for each request, response cycle for my models, but it will be the pain if response JSON struct is changed often (requirements can change quickly, can transform request/responses quickly)

I had previous experience use of Reflection in Swift, so I did give a chance to try EVReflection in my new project.

What is EVReflection?

EvReflection is a Swift helper library with reflection functions with support for NSCoding, Printable, Hashable, Equatable, and JSON.

EVReflection test case from it is own repository
  • Parsing objects based on NSObject to a dictionary.
  • Parsing a dictionary back to an object.
  • Creating a class from a string value and get the string value for a class.
  • Support NSCoding methods encodeWithCoder and decodeObjectWithCoder
  • Supporting Printable, Hashable and Equatable while using all properties. (Support for Set in Swift 1.2)
  • Parse an object to a JSON string and parse a JSON string to an object

clipped from EVReflection

Implementing Request/Response Model

First, we need to setup request/response models that inherit from EVReflection (EVObject) base class. BaseRequest for all requests, BaseResponse for all responses in the application and also ErrorResponse to parse error JSON structs if any present (If receive error response object to use of for Alerts, notifications etc.)

So, if error payload included in JSON to mapping and it needs to be parsed.

We can pass extra defaults like application’s current version, build number or any other data too.

Then, in the sample below we can simply declare our Request/Response models inheriting from BaseRequest/BaseResponse classes. So, model inherited from EVObject it will do mapping automatically.

Also, we can set default values via instantiation of class if you need to use some values as request/response

Finally, defining a method to take request params (BaseRequest) and completion handler responses (BaseResponse) gives us completion handler. (Also we can do network status checks etc in here.)

Benefits

  • We don’t need to write mapping for models or use some mapping libraries for JSON responses (as well as requests)
  • We ensured our Request/Response cycle is well defined, we exactly know what is going to as request and we know what is the response should do.
  • We did check for network status using Reachability (you can do your own implementation)
  • We ensured it is validated response via Alamofire’s validate() method. It automatically validates status code within 200...299 range, and that the Content-Type header of the response matches the Accept header of the request if one is provided.
  • We have our AccountResponse (BaseResponse) and error if there as the response
  • We used AlamofireJSONToObjects library to which converts JSON response data into Swift objects using EVReflection (as Alamofire extension).
  • We have clean, testable implementation and it is reusable to support different API requests.

Here is the sample method to create a request

As you can see responseObject is wrapped by AlamofireJSONToObjects, it is using generic Result oriented approach.

Finally, in completion block, we have accountsResponse model object transformed from JSON magically.

That’s all for now 🎉

--

--

Seyhun AKYÜREK
Seyhun Akyurek

Application Team Leader Mobile Channels in Commercial Bank Of Dubai. Enterprise Design Thinking Co-Creator/Practitioner, Blogger