In Chapter 1, you learned how to describe and structure a document. You now know that you, as the designer and developer of an app and its documents, control what data is stored, where and how it is stored, and how to identify and reference it.
You can decide that the data will be stored as a sequence of integers or as a single long string, whatever matters to you and the data you will use. In practice, it makes sense to structure the data inside a document if only to be able to access it easily. This chapter shows how to structure the data within a document using JSON encoding. This structure and encoding provides an easy-to-use format for data that relies on Unicode strings that can represent basic types recognized by JSON.
Using JSON Encoding
What matters most for JSON is the fact that the format is text-based (as opposed, for example, to a binary or digital representation) and the fact that each element can be named (as opposed to being identified by location or sequence).
A location- or sequence-based coding style lets you specify the format of each element in the encoding sequence. Knowing the format of an element means that you know how much space it will take up, and this will let you read or write the data using the standard read/write syntax in any programming language.
The disadvantage of sequence- or location-based coding is that if you change the sequence of data elements or the format of a data element, you break any read/write code that you already have. JSON encoding relies on names of data elements rather than their formats or sequence. Thus, you avoid the frequent problem of breaking read/write code when you modify a format of a single data element or when you change the order of the data elements.
Introducing JSON
A string is an ordered collection of Unicode characters.
A number is just that; the most basic JSON number is a double.
A Boolean is true or false.
The final primitive value in JSON is null, an object that has no value.
In JSON, these types can be combined into objects, which are unordered collections of name/value pairs; a JSON array is an ordered collection of name/value pairs.
JSON and Swift
Swift goes beyond the basic JSON types with its JSONSerialization class (part of the Foundation framework). JSONSerialization converts JSON into array and dictionary Swift data types in addition to the basic JSON string, number, and Bool data types.
Note
Swift bridges Boolean and bool (C) types into Bool types. This is handled automatically for you.
Using Swift Structs
JSON is a flexible and easy-to-use notation tool. On the other hand, Swift is designed to be a powerful tool for building apps, particularly those using the model-view-controller (MVC) design pattern, which is more complex than JSON. One area that demonstrates this well is the Swift struct type. You may often declare structs in Swift that you will use throughout your app (or not at all). When you work strictly with JSON, it is uncommon to declare a struct that is not used to store data. This section explains how to create and use Swift structs with JSON.
Swift Struct
What matters here is that the Student struct contains two var elements: name and studentID. Also worth noting is the fact that in this playground the Foundation framework must be imported because it will be used for working with JSON data. The other elements of the struct are standard Swift elements.
Tip
Note that the Swift style is to capitalize names of objects such as structs, so the name of the Student struct is capitalized.
Swift Extension for Coding Keys
Swift Extension for Encoding
Encoding JSON
You’ll need a JSONEncoder object to handle encoding. Such a JSONEncoder object is often named jsonEncoder but you can use any name you want. A JSONEncoder object specifies the container for the encoded data.
In Listing 2-3, there is only one function, encode (to: encoder), and it uses a jsonEncoder object, the container that will contain the encoded data retrieved from the encoder.
The heart of the encode (to: encoder) function consists of the three lines of code that encode the struct elements (name and studentID).
can throw an error.
Tip
If you are debugging this code, set a breakpoint on the try statement so that you can see what causes a problem. Typical problems you may encounter in this code are typos in the key names.
Decoding JSON
Extension for Decoding
Rather than an encode(to: encoder:) function, the heart of this code is init(from decoder: Decoder).
Putting the Encoding and Decoding Together
You can encode and decode the data as you wish. For the purpose of debugging, you can print out encoded data using code such as the following:
You can print out a string showing the JSON code:
You can then reverse the process to print out the decoded data, as you see here:
Note
For debugging, you may want to add the code in this section to your app so that you can use breakpoints to verify the encoding and decoding of the data. Usually once it is working and the keys are correct, you can disable the breakpoints or even remove them.
Summary
This chapter showed you how to encode and decode JSON data to and from Swift structs. Because you will be working with named Swift objects, you don’t have to worry about the sequence or formatting of data.