Applications communicate with a wide range of APIs using various data encoding formats. Each data encoding format represents syntax coding data that another machine can read, but in a way that is easy to understand for humans.
應用程式使用各種資料編碼格式與各式各樣的 API 通訊。每種資料編碼格式代表一種語法,用來編碼讓另一台機器可以讀取的資料,同時也讓人容易理解。
For instance, if you want to use an API to configure a Cisco router, you must check which data type that API supports. Then, you can start writing a request to be handled by that API that sends an API request to update the router's configuration. An API server comprehends your written code and translates it into instructions suitable for your router to process and create an action.
舉例來說,如果你想使用 API 來設定 Cisco 路由器,就必須先確認該 API 支援哪種資料型態。接著,你就可以開始撰寫由該 API 處理的請求,送出更新路由器設定的 API 請求。API 伺服器會理解你所撰寫的程式碼,並將其轉譯成適合你的路由器處理並執行動作的指令。
You will most likely encounter these common data formats:
你很可能會遇到以下幾種常見的資料格式:
- YAML Ain't Markup Language (YAML)YAML 不是標記語言(YAML Ain't Markup Language,YAML)
- JavaScript Object Notation (JSON)JavaScript 物件表示法(JavaScript Object Notation,JSON)
- eXtensible Markup Language (XML)可延伸標記語言(eXtensible Markup Language,XML)
Common Use Cases
常見使用情境
Take a look at some of the most common use cases of XML, JSON, and YAML. You will notice that their files have the same extension as their name (.xml for XML, .json for JSON and .yaml for YAML).
來看看 XML、JSON、YAML 幾種最常見的使用情境。你會注意到它們的檔案副檔名與其名稱相同(XML 用 .xml、JSON 用 .json、YAML 用 .yaml)。
XML is not as humanly readable as the other two formats. XML is verbose, redundant, and complex to use. It is mostly used to interexchange highly structured data between applications (machine-to-machine communication). XML is widely used in Java programming.
XML 沒有另外兩種格式那麼容易讓人閱讀。XML 冗長、重複且使用起來較複雜。它主要用於在應用程式之間交換高度結構化的資料(機器對機器的通訊)。XML 在 Java 程式設計中廣泛使用。
JSON serves as an alternative to XML because it is often smaller and easier to read. It is mostly used for transmitting data between a server and a web page. The JSON syntax is the same as the JavaScript programming language; therefore, you can easily convert JSON data into JavaScript objects. JSON syntax is also useful for YAML because JSON is a subset of YAML. Parsing JSON files with a YAML parser is therefore intuitive.
JSON 是 XML 的替代方案,因為它通常更精簡、更易讀。它主要用於在伺服器與網頁之間傳輸資料。JSON 語法與 JavaScript 程式語言相同,因此你可以輕鬆將 JSON 資料轉換成 JavaScript 物件。JSON 語法對 YAML 也很有用,因為 JSON 是 YAML 的子集。因此,用 YAML 解析器解析 JSON 檔案是很直觀的。
If you are a non-developer building your first API, YAML is a good choice when choosing which data format to use. YAML is made for people who are starting to write code from scratch. XML and JSON are mostly for your programming code to be more machine-readable; you export YAML into one of these two formats. YAML is used in many configuration files today. Because of its similar indentation styles and use of whitespaces for structure, YAML files resonate with people that know Python.
如果你是非開發人員,正在建置你的第一個 API,那麼在選擇資料格式時,YAML 是不錯的選擇。YAML 是為了讓剛開始從零撰寫程式碼的人使用而設計的。XML 和 JSON 主要是為了讓你的程式碼更容易被機器讀取;你可以將 YAML 匯出成這兩種格式之一。YAML 目前廣泛用於許多設定檔中。由於其縮排風格與使用空白字元來表示結構的方式類似,熟悉 Python 的人會對 YAML 檔案感到熟悉。
Common Characteristics
共同特性
Data formats are the foundation of API. They define the syntax and semantics, including the constraints of working with the API.
資料格式是 API 的基礎。它們定義了語法與語意,包括使用該 API 時的限制條件。
The syntax is a way to represent a specific data format in textual form. Some formats use curly braces or square brackets, and others have tags marking the beginning and end of an element. In some formats, quotation marks or commas are heavily used, while others do not require them. But no matter which syntax is used, each of these data formats has a concept of an object. You can think of an object as a packet of information, an element with characteristics. An object can have one or more attributes attached to it.
語法是以文字形式表示特定資料格式的方式。有些格式使用大括號或方括號,有些則用標籤來標示元素的開始與結束。有些格式大量使用引號或逗號,有些則不需要。但無論使用哪種語法,這些資料格式都有物件(object)的概念。你可以把物件想成一包資訊,一個帶有特性的元素。一個物件可以有一或多個屬性。
Many characteristics will be broken down to the key-value concept, the key and value often being separated by a colon. The key identifies a set of data, and it is often positioned on the left side of the colon. The values are the actual data that you are trying to represent. Usually, the data appears on the right side of the colon.
許多特性都可以拆解成鍵值(key-value)概念,鍵和值通常以冒號分隔。鍵用來識別一組資料,通常位於冒號左側。值則是你想表示的實際資料,通常位於冒號右側。
To extract the meaning of the syntax, it is crucial that you recognize how keys and values are notated when looking at the data format. A key must be a string, while a value could be a string, a number, or a Boolean (for instance, true or false). Other values could be more complicated, containing an array or an entirely new object representing a lot of its own data.
要理解語法的意義,關鍵在於了解該資料格式中鍵和值的標示方式。鍵必須是字串,而值則可以是字串、數字或布林值(例如 true 或 false)。其他值可能更複雜,包含陣列或代表大量自身資料的全新物件。
Another thing to notice when looking at a particular data format is the importance of whitespaces and case sensitivity. Sometimes, whitespaces and case sensitivity could be of high importance, and in others, it could carry no significance whatsoever, as you will get to know through some future examples.
檢視某種資料格式時另一個需要注意的重點,是空白字元與大小寫敏感度的重要性。有時候,空白字元與大小寫敏感度非常重要;有時候則完全無關緊要,之後你會透過一些範例了解這一點。
One of the main points about data formats that you should remember is that you can represent any kind of data in any given format.
關於資料格式,你應該記住的重點之一是:任何一種資料都能以任何一種給定的格式來表示。
The figure shows the three previously mentioned common data formats—YAML, JSON, and XML. These examples provide details about a specific user, providing their name, role, and location.
此圖顯示了先前提到的三種常見資料格式——YAML、JSON 和 XML。這些範例提供了某個特定使用者的詳細資料,包括其姓名、角色及所在位置。
You can quickly recognize that the same data is represented in all three formats, so it comes down to two factors when considering which format to choose:
你可以很快發現,同一份資料以三種格式呈現,因此選擇格式時取決於兩個因素:
- If the system you are working with prefers one format over the other, pick the language's format.如果你所使用的系統偏好某一種格式,就選擇該語言的格式。
- If the system can support any of them, pick the format you are more comfortable working with.如果該系統可以支援任一種格式,就選擇你比較熟悉、用起來比較順手的格式。
In this course, you will be introduced to three of the most common data encoding formats, which are JSON, XML, and YAML:
在本課程中,你將認識三種最常見的資料編碼格式,也就是 JSON、XML 和 YAML:
- JSON: JSON is a heavily used data format. JSON was derived from the JavaScript programming language. Because of that historical background, JavaScript code can easily convert data from a JSON file into native JavaScript objects. JSON also shares some similar characteristics with Python dictionaries, and you will learn some of the ways JSON is used with Python.JSON:JSON 是廣泛使用的資料格式。JSON 源自 JavaScript 程式語言。由於這樣的歷史背景,JavaScript 程式碼可以輕鬆將 JSON 檔案中的資料轉換成原生 JavaScript 物件。JSON 也與 Python 字典(dictionary)有一些相似的特性,你將學到 JSON 與 Python 搭配使用的一些方式。
- XML: XML is another data serialization format broadly used for interchanging information over the Internet within two machines. The beginnings of XML go back to the previous century, with the first version initially defined in 1998. It is very similar to HTML; they are both markup languages, meaning that they indicate which parts of a document are present and not how the data is going to be shown in your system specifically.XML:XML 是另一種廣泛用於在兩台機器之間透過網際網路交換資訊的資料序列化格式。XML 的起源可以追溯到上個世紀,最早的版本於 1998 年定義。它與 HTML 非常相似,兩者都是標記語言,意思是它們標示出文件中哪些部分存在,而非具體規定資料要在你的系統中如何呈現。
- YAML: A popular format you will learn about is YAML. According to yaml.org, YAML is a recursive acronym for YAML Ain't Markup Language, which, as the name suggests, is not a markup language like XML. It is more heavily weighted to be humanly writable and readable with a minimalistic format. However, YAML works the same way as other data formats. Overall, YAML is the most humanly readable of all the formats and, at the same time, is just as easy for programs to use, which is why it is gaining increasing popularity among engineers working with programmability.YAML:你將學到的另一種熱門格式是 YAML。根據 yaml.org 的說法,YAML 是「YAML Ain't Markup Language」的遞迴縮寫,正如其名,它不像 XML 那樣是一種標記語言。它更著重於讓人容易撰寫與閱讀,格式極簡。不過,YAML 運作方式與其他資料格式相同。整體而言,YAML 是所有格式中最容易讓人閱讀的,同時對程式來說也一樣容易使用,這也是它在從事網路程式化工作的工程師之間越來越受歡迎的原因。
Serialization and Deserialization of Data
資料的序列化與反序列化
One important concept you should understand when dealing with data encoding formats is serialization and deserialization. Serialization and deserialization may sound like unfamiliar terms at first, but you perform these two actions in your daily life. Take, for instance, a telephone call between two entities. When talking to another person over the phone, your words have to transform into a series of bits that are then sent over an electronic medium or a wireless signal. Your speech must transform into something understandable to the medium it is traversing. This process is called serialization. On the other side of that telephone call, a receiver has to do the opposite process to extract the meaning from those bits sent over and reconstruct your words. This process is called deserialization.
在處理資料編碼格式時,你應該理解的一個重要概念是序列化(serialization)與反序列化(deserialization)。序列化與反序列化乍聽之下可能是陌生的詞彙,但你在日常生活中其實一直在執行這兩種動作。舉例來說,想想兩方之間的電話通話。當你在電話中與對方交談時,你的話語必須轉換成一連串位元,再透過電子媒介或無線訊號傳送出去。你的語音必須轉換成傳輸媒介能理解的形式。這個過程稱為序列化。在電話的另一端,接收者必須執行相反的過程,從傳來的位元中萃取出意義,重建你所說的話。這個過程稱為反序列化。
Serialization in computer science indicates converting a data structure or an object into a binary or textual format that can be stored and reconstructed later. You want to save an object constructed in your code to a file, giving it permanent storage. This way, you preserve the state of an object. Serialized files could be in YAML, JSON, XML, or any other textual data format or a binary format.
在電腦科學中,序列化是指將資料結構或物件轉換成可以儲存並在之後重建的二進位或文字格式。你想把程式碼中建立的物件儲存到檔案中,讓它有永久儲存空間。透過這種方式,你保留了物件的狀態。序列化後的檔案可以是 YAML、JSON、XML 或其他文字資料格式,也可以是二進位格式。
The serialized file could now be transferred to any other system over the network. The receiving system can open that file and reconstruct it to the original state with all the objects defined inside. This process is the opposite of serialization, and it is called deserialization.
序列化後的檔案現在可以透過網路傳輸到任何其他系統。接收端系統可以開啟該檔案,並將其中定義的所有物件還原成原始狀態。這個過程與序列化相反,稱為反序列化。
Like saving data to files, APIs also require reliable serialization and deserialization to exchange data. Most programming languages, like Python, have existing tools for working with various data formats.
就像將資料儲存到檔案一樣,API 也需要可靠的序列化與反序列化來交換資料。大多數程式語言,例如 Python,都已具備處理各種資料格式的既有工具。
Here is a practical example that you could be facing as a network engineer working with network programmability. You have written some code in Python for configuring switches in your organization. You want to send the data held in your objects to the switch API in a format that it will understand unmistakably. You have to convert that Python data structure into a valid YAML, JSON, or XML format. To do that, you will use the serialization process.
以下是一個實際案例,你身為從事網路程式化工作的網路工程師可能會遇到。你已用 Python 撰寫了一些程式碼,用來設定組織中的交換器。你想將物件中儲存的資料送到交換器的 API,並且要以該 API 能明確理解的格式傳送。你必須將該 Python 資料結構轉換成有效的 YAML、JSON 或 XML 格式。要做到這一點,你會使用序列化的過程。
Other times, you might want to get information about your switch's specific interface through that same API. You would receive a configuration of that switch from the API in YAML, JSON, or XML format. To interpret that file so your Python code could understand it, you would use the deserialization process. You extract the details from a textual file and convert them into valid Python objects. This process is also called data or file parsing.
有時候,你可能想透過同一個 API 取得交換器某個特定介面的資訊。你會從 API 收到該交換器以 YAML、JSON 或 XML 格式呈現的設定。要讓你的 Python 程式碼能解讀該檔案,你會使用反序列化的過程。你從文字檔中萃取出細節,並將其轉換成有效的 Python 物件。這個過程也稱為資料或檔案解析(parsing)。


