With NetworkStack
requests you can use two different return data :
Data
JSON
In this example we have four different JSON
serialization services :
Right below we will see about implementation and difference between three of them.
Decodable | JSONCodable | SwiftyJSON | ObjectMapper | |
---|---|---|---|---|
Throwable | ✅ | ✅ | ✅ | |
Transformer | ✅ | ✅ | ||
Serializer | ✅ | ✅ | ✅ | |
Deserializer | ✅ | ✅ | ✅ | ✅ |
Protocol conformance | Decodable | JSONCodable | Mappable or ImmutableMappable | |
Mapper | ✅ | |||
Parser | ✅ | ✅ | ✅ | ✅ |
// decodable protocol conformance
decode(object: JSONObject) throws // deserialize
Parser : you can see here, Decodable extension conformance for our Realm
Video object.
Documentation :
Decodable
// JSONCodable protocol conformance
decode(_ json: Any) throws -> T // deserialize
Transformable : JSONTransformer
is a protocol to create transformers. Two default trnasformers is implement (StringToURL
& StringToDate
).
Parser : you can see here, JSONCodable extension conformance for our Video Realm
object.
Documentation :
JSONCodable
This is a very flexible pod to serialize and deserialize JSON. There is no protocol conformance, but on this example we try to fix limits and usage. So we create Swifty
protocol with conformance :
init(json: JSON) // deserialize
func toJSON() -> JSON // serialize
You can create your own protocol or not.
Transformable : map, flatmap to transform datas. Use everything you want to transform JSON <-> Object
. You can create you own Tranformable protocol if needed.
Parser :
It's more flexible than JSONCodable
and there are specific features like JSON
merging. you can se here, SwiftyJSON extension conformance for our Video Realm
object.
Documentation :
SwiftyJSON
It's a Mapper so it manages your object to map properties. Mapping means that you need to set all properties with var
keyword.
If you use Realm it would be guidelines to set all properties with var
keyword. But as you know, a rule don't come without exceptions, and Realm has own properties like RealmOptional<T>
& List<T>
where the guideline is to set properties with let
keyword. And use respectivily realmOptional.value
and realmlist.append()
to access and modifiy it correctly.
!! NOW !! with ImmutableMappable
protocol we can do something better. You can perform serialization and deserialization by yourself. With this protocol, ObjectMapper
instroduce immutable principle. Right way to use it :
// ImmutableMappable protocol conformance
init(map: Map) throws // deserialize
func mapping(map: Map) // serialize
This protocol is under construction, so use it carefully. And to conclude, this protocol keep the legacy of ObjectMapper
and it is difficult to understand why the mapper denomination exists inside this protocol cause it's now a parser.
Transformable :
TransformType
is a protocol to create transformers. You have multiple default transformers like :
DateFormatterTransform(dateFormatter: DateFormatter)
EnumTransform<T: RawRepresentable>()
NSDecimalNumberTransform()
URLTransform(shouldEncodeURLString: Bool = true)
Parser :
you can see here, ObjectMapper extension conformance for our Video Realm
object.
Documentation :
ObjectMapper
You will see two network layers that have same possibilities.
TargetType
is a centric object with required property that you need to create requests. RequestParameters
is more flexible and you will be able to customize it what you want with few guidelines.
NetworkStack
is full RxSwift
compare to Moya
that you can choose between standard API, RxSwift
and ReactiveSwift
.
Features | NetworkStack | Moya |
---|---|---|
Request with data response | ✅ | ✅ |
Upload request with data response | ✅ | ✅ |
Request with JSON response | ✅ | ✅ |
Upload request with JSON response | ✅ | ✅ |
JWT | ✅ | |
OAuth2 | ✅ | ✅ |
auto retry auth | ✅ | |
auto renew token | ✅ | |
Plugins | ✅ | |
Errors enum | ✅ |