In this new tutorial you'll learn how to decode and encode a json object using the new Encodable and Decodale protocols (defined in the new Codable type), and how to do a custom decoding by doing some data manipulation during the json object extraction. You will see three different examples that will explain you how it is so easy with swift 4 to do the coding and decoding json process.
Swift 4 has introduced the JSONDecoder() and JSONEncoder() functions, used to do the decoding and encoding operation and the new Codable type, that contains the Encodable and Decodable protocols. When you define your custom object, you can use one of these protocol (or the codable one if your struct needs to do both the decoding and encoding process).
Let's define or custom simple struct. In this example we'll use a json array, with different objects (name - String, id - int and an optional one).
This struct will implement the Decodable protocol. When you have to decode your json object into your custom swift 4 struct, you'll just have to call the JSONDecoder() function.
The square brakets are required because we want to decode an array. This function will directly map the json data into your custom struct array. If the json decoding process fails, the final variable will be nil.
Now, let's do some data manipulation during the decoding process. The server send the id as string, but we want to map it into an integer. Moreover we want to fill some optional values if empty with defaults one.
Let's define our custom structs and the Codingkeys string values:
Let's implement now the Decodable protocol:
The init(from decoder:Decoder) function is called during the decoding process. Here you can do the data manipulation.
It's now time of the encoding process. You'll just have to define your struct using the Codable protocol. Then you'll just have to call the JSONEncoder().encode function. Swift will then do all the work.
Here you can download the playground example with the tree different examples: