War on JSON in Swift (Object Mapper vs Codable)
Mapping :
Mapping is an operation where each element of a given set (the domain) is associated with one or more elements of a second set (the range).
Mapping in Swift:
Usually when an App interacts with external API or even sometimes local static data, we actually manipulates different data types like JSON or plist or sometime even some other formats too. We need to map our model class objects with the JSON data so we can model our business processes. In Swift we have following common libraries for parsing and mapping JSON with our model objects:
- JSON Serialisation
- Swifty JSON
- Codable
- ObjectMapper
In this article we will learn about two of the most common parsing and mapping libraries in Swift and will analyze the difference between two.
Object Mapper:
ObjectMapper is a framework than can convert JSON to objects (and viceversa). Parsing and mapping JSON be achieved even more easily, and quickly, using a library like ObjectMapper.
In order to use ObjectMapper we need to implement the Mappable protocol:
public protocol BaseMappable {
mutating func mapping(map: Map) {
}
public protocol Mappable: BaseMappable {
init?(map: Map)
}
For using ObjectMapper:
1. Our objects need to extend from Mappable
2. Our objects need to implement the mapping function in which we will specify which properties of the JSON are assigned to which properties of the object.
3. Properties must be declared as optional variables
Codable:
Codable is a protocol introduced in Swift4 Standard library. It provides three types:
Encodable protocol: It is used for encoding.
Decodable protocol: It is used for decoding.
Codable protocol: It is used for both encoding and decoding.
typealias Codable = Encodable & Decodable
For using Codable:
1. To encode and decode the custom types need to adopt Codable protocol.
2. Custom type must have the Codable type properties.
3. Codable types include data types like Int, Double, String, URL, Data.
4. Other properties like array, dictionary are codable if they are comprised of codable types.
Difference between ObjectMapper and Codable
ObjectMapper
- This is a third party framework.
- Object Mapper has type transformation support.
- Object Mapper is said to be faster than Codable. (or more precisely than JSONEncoder/Decoder)
- Updates are not promised using ObjectMapper with new Swift version releases.
- Adds extra dependency in your project.
- ObjectMapper is protocol that is further inherited from BaseMappable protocol.
- Need to update never versions with CocoaPods or other dependency manager.
- Need to define mapping for every field with key against JSON.
Codable
- This is Swift native solution.
- Codable is probably slower than ObjectMapper because it’s built mapping solution. When you got no coding keys, swift will read through class properties using mirror and then map.
- In Codable we need external library for type transformation.
- Removes third party dependency for parsing and mapping.
- If model object fields are different than JSON keys , we need to define keys as enums using CodingKey protocol.
- Codable is a protocol which is a composition of two other protocols: Encodable & Decodable.
- Auto generates encode and init methods if fields have same name as keys as in JSON, so less code is required.
- As it is native solution new updates are available in Foundation framework.
Example:
Let us say that in our application we have a Customer object, an Address object, a Company object etc. In other words something like this:
We receive JSON from Remote API and populate these model objects for our business logic flow. Let us analyze parsing and mapping these model objects using ObjectMapper and Codable.
Using ObjectMapper:
Using Coadable:
As you can observe that in Address class we have defined custom keys as our zipcode field is different then zipCodeForAddress.
Conclusion:
In my opinion Codable is more optimized and better way to map model objects in Swift and it is Swift native solution and removes third party dependency. ObjectMapper is still more popular due to its better readablitiy, popularity and support for Alamofire as well. In coming times we expect to see more use of Codable in iOS Developers community for parsing and mapping in Swift.