I was prompted to write this article by an almost nervous breakdown caused by my desire to learn how to communicate with third-party APIs, I was specifically interested in the process of decoding JSON documents! Fortunately, I avoided a nervous breakdown, so now it's time to contribute to the community and try to publish my first article on Habré.
Why are there any problems with such a simple task at all?
To understand where the problems come from, you first need to talk about the toolkit that I used. To decode JSON objects, I used the relatively new synthesized protocol of the Foundation library - odable .
Codable is a built-in protocol that allows you to do encoding into a text format object and decoding from a text format. Codable is the sum of two other protocols: Decodable and Encodable.
It is worth making a reservation that a protocol is called synthesized when some of its methods and properties have a default implementation. Why are such protocols needed? To make it easier to work with signing them, at least by reducing the amount of boilerplate code.
These protocols also allow you to work with composition, not inheritance!
Now let's talk about the problems:
First, like everything new, this protocol is poorly described in Apple's documentation. What do I mean by poorly described? The simplest cases of working with JSON objects are analyzed; the methods and properties of the protocol are very briefly described.
Secondly, the problem of the language barrier. It took hours to figure out how to write a search query. Nerves at this time ran out with lightning speed.
Thirdly, what is googled with simple methods does not tell about complex cases.
Now let's talk about a specific case. The case is this: using the Flickr API, search for N-photos by keyword (keyword: read the search query) and display them on the screen.
At first, everything is standard: we get the key to the API, look for the required REST method in the API documentation, look at the description of the arguments for the request to the resource, compose and send the GET request.
Flickr JSON :
{
"photos":{
"page":1,
"pages":"11824",
"perpage":2,
"total":"23648",
"photo":[
{
"id":"50972466107",
"owner":"191126281@N@7" ,
"secret":"Q6f861f8b0",
"server":"65535",
"farm":66,
"title":"Prompt & Reliable Electrolux Oven Repairs in Gold Coast",
"ispublic":1,
"isfriend":0,
"isfamily":0
},
{
"id":"50970556873",
"owner":"49965961@NG0",
"secret":"21f7a6424b",
"server":"65535",
"farm" 66,
"title":"IMG_20210222_145514",
"ispublic":1,
"isfriend":0,
"isfamily":0
}
]
},
"stat":"ok"
}
, . ? , . ? - , ( , ). - , , , ( ) () Flickr , .
? , GET- , ! : ---. . . JSON- , , . "photo": "id", "secret", "server"
, :
struct Photo {
let id: String
let secret: String
let server: String
}
let results: [Photos] = // ...
"" . , best practices JSON- .
. , . , Codable. , .
{
"id":"50972466107",
"owner":"191126281@N07",
"secret":"06f861f8b0"
}
. , JSON- ( "id", "secret", "server"); , (, ). Decodable, , ( , ). ? , " ". . ( , Data, decode(...) JSONDecoder Data).
:
, , API - jsonplaceholder.typicode.com, JSON-, GET-.
jsonformatter.curiousconcept.com . "" REST , Playground Xcode.
tool - app.quicktype.io - Swift JSON-.
. :
struct Photo: Decodable {
let id: String
let secret: String
let server: String
}
let json = """
{
"id":"50972466107",
"owner":"191126281@N07",
"secret":"06f861f8b0"
}
"""
let data = json.data(using: .utf8)
let results: Photo = try! JSONDecoder().decode(Photo.self, from: data)
, JSON-, "key" : "sometexthere" Decodable String, run-time. Decodable coerce- ( ).
struct Photo: Decodable {
let id: Int
let secret: String
let server: Int
}
let json = """
{
"id":"50972466107",
"owner":"191126281@N07",
"secret":"06f861f8b0"
}
"""
let data = json.data(using: .utf8)
let results: Photo = try! JSONDecoder().decode(Photo.self, from: data)
. ?
{
"id":"50972466107",
"owner":"191126281@N07",
"secret":"06f861f8b0",
"server":"65535",
"farm":66,
"title":"Prompt & Reliable Electrolux Oven Repairs in Gold Coast",
"ispublic":1,
"isfriend":0,
"isfamily":0
}
, Decodable , , " " , . , API , " " , , , , . - "".
. JSON-:
[
{
"id":"50972466107",
"owner":"191126281@N07",
"secret":"06f861f8b0",
"server":"65535",
"farm":66,
"title":"Prompt & Reliable Electrolux Oven Repairs in Gold Coast",
"ispublic":1,
"isfriend":0,
"isfamily":0
},
{
"id":"50970556873",
"owner":"49965961@N00",
"secret":"21f7a6524b",
"server":"65535",
"farm":66,
"title":"IMG_20210222_145514",
"ispublic":1,
"isfriend":0,
"isfamily":0
}
]
( ) . , - !
struct Photo: Decodable {
let id: String
let secret: String
let server: String
}
let json = """
[
{
"id":"50972466107",
"owner":"191126281@N07",
"secret":"06f861f8b0",
"server":"65535",
"farm":66,
"title":"Prompt & Reliable Electrolux Oven Repairs in Gold Coast",
"ispublic":1,
"isfriend":0,
"isfamily":0
},
{
"id":"50970556873",
"owner":"49965961@N00",
"secret":"21f7a6524b",
"server":"65535",
"farm":66,
"title":"IMG_20210222_145514",
"ispublic":1,
"isfriend":0,
"isfamily":0
}
]
"""
let data = json.data(using: .utf8)
let results: [Photo] = try! JSONDecoder().decode([Photo].self, from: data)
, [Photo] - Swift. : - !
, , .
" " Decodable , JSON.
JSON- - , . - , . , JSON- ! , -, Character , JSON- .
JSON - . ? , , : JSON (, ). ? , ( )
Decodable .
, . Decodable generic enum CodingKeys, () , JSON , , , ! , . , , : JSON- snake case , Swift camel case. ?
struct Photo: Decodable {
let idInJSON: String
let secretInJSON: String
let serverInJSON: String
enum CodingKeys: String, CodingKey {
case idInJSON = "id_in_JSON"
case secretInJSON = "secret_in_JSON"
case serverInJSON = "server_in_JSON"
}
}
rawValue CodingKeys , JSON-!
! JSON !
{
"photos":{
"page":1,
"pages":"11824",
"perpage":2,
"total":"23648",
"photo":[
{
"id":"50972466107",
"owner":"191126281@N@7" ,
"secret":"Q6f861f8b0",
"server":"65535",
"farm":66,
"title":"Prompt & Reliable Electrolux Oven Repairs in Gold Coast",
"ispublic":1,
"isfriend":0,
"isfamily":0
},
{
"id":"50970556873",
"owner":"49965961@NG0",
"secret":"21f7a6424b",
"server":"65535",
"farm" 66,
"title":"IMG_20210222_145514",
"ispublic":1,
"isfriend":0,
"isfamily":0
}
]
},
"stat":"ok"
}
:
, : "photos", "stat"
"photos" : "page", "pages", "perpages", "total", "photo"
"photo" - , .
?
. dummy . !
Decodable , CodingKeys! ! : Swift ( !) extension stored properties, computed properties odable/Encodable/Decodable, JSON .
, , : photos photo c
, !
// (1) Photo .
struct Photo: Decodable {
let id: String
let secret: String
let server: String
}
// (2) JSONContainer, .
struct JSONContainer: Decodable {
// (3) photos c "photos" JSON, , , - , photo!
let photos: [Photo]
}
extension JSONContainer {
// (4) CodingKeys .
enum CodingKeys: String, CodingKey {
case photos
// (5) , photos.
// (6) - , PhotosKeys - , photos
enum PhotosKeys: String, CodingKey {
// (7) "photo"
case photoKey = "photo"
}
}
// (8)
init(from decoder: Decoder) throws {
// (9) JSON, , - photos
let container = try decoder.container(keyedBy: CodingKeys.self)
// (10) (nested - ) photos
let photosContainer = try container.nestedContainer(keyedBy: CodingKeys.PhotosKeys.self, forKey: .photos)
// (11)
// (12) photos -, .photoKey (.photoKey.rawValue == "photo")
photos = try photosContainer.decode([Photo].self, forKey: .photoKey)
}
}
That's it, now that the instance of the JSONDecoder. Will call decode () - under the hood it will use our initializer to work with decoding
Summing up, I would like to say that working with the network in iOS development is filled with various "surprises", so if you can add something in the comments - be sure to do it!
Thanks to all!
PS After some time, it was concluded that it is okay to map to the final structure in the code using only the built-in behavior of Codable. The conclusion was made after watching the WWDC session analyzing the work with data received from the network.