Difference between revisions of "W2271 JSON FILES"

From Coder Merlin
Line 6: Line 6:


==JSON in Swift==
==JSON in Swift==
When working with JSON in Swift, we can consider each key as a string, number, boolean, another JSON object, an array, or a null (nothing) value.
There's a very strong use case for considering JSON when working in Swift. Take for example, if you are working on an app that is required to transmit data to and from a webservice via API. Every time you wish to send or receive data to and from this webservice, your Swift code must be encoded into JSON to meet the


Swift has three protocols that makes working with JSON simple and straightforward — '''Decodable''', '''Encodable''', and '''Codable'''. ''Codable'' is a type alias for ''Decodable'' and ''Encodable'' which is done through Swift’s protocol composition.  
developers have access to protocols that allow Swift objects such as a '''String''' , '''Int''' , '''Double''' , '''URL''' , '''Date''' , '''Array''' and '''Dictionary''' to be easily encoded into JSON for sending and receive data from a webservice.  


*Storing data - These keys can be stored JSON can be stored as a mulit-line String in Swift and converted to a Data object (and vice versa). This is done with the Decodable and Encodable protocols.
<syntaxhighlight lang="JSON">
{
    "name": "Josh",
    "age": 30
}
</syntaxhighlight>


*Sending data - These keys can be stored JSON can be stored as a mulit-line String in Swift and converted to a Data object (and vice versa). This is done with the Decodable and Encodable protocols.
Swift offers built-in support for working with JSON through a protocol called codable. When a piece of JSON code is considered codable, developers can freely convert data into usable swift code in a very simple and straightforward manner. Importantly, these protocols come in very handy for sending and storing data with JSON.
<syntaxhighlight lang="JSON">
{
    "name": "Josh",
    "age": 30
}
</syntaxhighlight>


*Retreiving data - These keys can be stored JSON can be stored as a mulit-line String in Swift and converted to a Data object (and vice versa). This is done with the Decodable and Encodable protocols.
* '''Encodable''' - By marking your model type as Encodable, you tell Swift that you want to use this type to convert instances of Person to a JSON object. Most commonly this is used to send data to the server.
<syntaxhighlight lang="JSON">
 
{
* '''Decodable''' - By marking your model type as Decodable, you tell Swift that you want to use this type to convert JSON to Person instances. Most commonly this is used when receiving data from the server.
    "name": "Josh",
 
    "age": 30
*'''Codable''' - As mentioned earlier in this article, Codable is a type alias for both Encodable and Decodable which allows your type to be used to encode and decode JSON. Codable was introduced in Swift 4. Whether you mark your type as Encodable, Decode, or Codable, is up to you. Some may prefer to be explicit with their naming and stick with the Encodable and Decodable, but just mark as Codable unless told otherwise.
}
</syntaxhighlight>


==What is the motivation for learning JSON?==
==What is the motivation for learning JSON?==

Revision as of 06:30, 20 November 2021

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder

What is JSON?[edit]

 This article can be improved by:  Emphasizing in italics important concepts, e.g. open standard method of storing, sending, and retrieving data

JSON or Javascript Object Notation is a text-based language-independent data-interchange format very similar to XML or YAML. Although JSON can be used with almost every major coding language, JSON wouldn’t be correctly classified as a “programming language” per se, rather it’s a text-based open standard method of storing, sending, and retrieving data between a client and server.

JSON is a very important data representation format, as it is widely used to facilitate the majority of web-based API calls and is very common in facilitating communication between servers and clients in a wide array of applications.

JSON in Swift[edit]

There's a very strong use case for considering JSON when working in Swift. Take for example, if you are working on an app that is required to transmit data to and from a webservice via API. Every time you wish to send or receive data to and from this webservice, your Swift code must be encoded into JSON to meet the

developers have access to protocols that allow Swift objects such as a String , Int , Double , URL , Date , Array and Dictionary to be easily encoded into JSON for sending and receive data from a webservice.


Swift offers built-in support for working with JSON through a protocol called codable. When a piece of JSON code is considered codable, developers can freely convert data into usable swift code in a very simple and straightforward manner. Importantly, these protocols come in very handy for sending and storing data with JSON.

  • Encodable - By marking your model type as Encodable, you tell Swift that you want to use this type to convert instances of Person to a JSON object. Most commonly this is used to send data to the server.
  • Decodable - By marking your model type as Decodable, you tell Swift that you want to use this type to convert JSON to Person instances. Most commonly this is used when receiving data from the server.
  • Codable - As mentioned earlier in this article, Codable is a type alias for both Encodable and Decodable which allows your type to be used to encode and decode JSON. Codable was introduced in Swift 4. Whether you mark your type as Encodable, Decode, or Codable, is up to you. Some may prefer to be explicit with their naming and stick with the Encodable and Decodable, but just mark as Codable unless told otherwise.

What is the motivation for learning JSON?[edit]

 This article can be improved by:  Beginning the article with the motivation for learning the topic.

There are several reasons learning JSON can be beneficial for developers, especially those early in their coding career. First off, learning JSON gives developers an easy path to working with a multitude of web-based applications and services that are written in Javascript. Since JSON is a superset of JavaScript, anything you write in JSON can be interpreted by JaveScript compliers, lowering the barrier for entry to working with Javascript-based applications and services. Furthermore, since JSON is a text-only data-interchange format, it is intuitive and the intention of a snippet of JSON can be interpreted easily. Further, nearly every major coding language today has a JSON library allowing JSON to be injected and interpreted by compliers independent of the coding language you wish to write in.

Using JSON with JavaScript[edit]

 This article can be improved by:  Providing initial examples in Swift using the CodeExplorer. While JSON is most certainly used with JavaScript, because this site is primarily focused on Swift, begin the examples there.

Converting JSON to Javascript is quite easy. In fact, there are a series of Javascript functions that can be called to parse JSON into a Javascript object.

With JSON.parse(), for instance, developers can input a JSON string and parse the text into a Javascript object.

'{"name":"Bob", "age":20, "city":"Phoenix"}'

Using the JSON.parse() function, one can simply pass the JSON to the function as follows:

const obj = JSON.parse('{"name":"Bob", "age":20, "city":"Phoenix"}');

By doing so, JSON.parse() will create a Javascript object, in this case, a const type object called obj to how the JSON values in a Javascript JSON format.

Next, using the JSON.stringify() function, the object created via the JSON text using JSON.parse() can be converted to a string.

const obj = {name: "Bob", age: 20, city: "Phoenix"};

Again, results in a const type object with the given values. And now, that object can be converted to a string.

const myJSON = JSON.stringify(obj);