1
00:00:05,120 --> 00:00:10,369
Before we can start serializing data, we 
need to understand what "serializing" means. 

2
00:00:10,480 --> 00:00:14,468
Once again, I'll check out the fount 
of all human knowledge, the internet. 

3
00:00:14,468 --> 00:00:17,899
Whenever you come across a term 
you don't understand, google it! 

4
00:00:18,000 --> 00:00:22,640
The Wikipedia page on Serialization is a 
good place to start. I won't put the link 

5
00:00:22,640 --> 00:00:26,877
in the resources, you're quite capable 
of searching for things yourself now. 

6
00:00:26,960 --> 00:00:30,670
Here's the Wikipedia 
definition of "serialization": 

7
00:00:31,760 --> 00:00:36,480
the process of translating a data structure or 
object state into a format that can be stored 

8
00:00:36,480 --> 00:00:40,560
(for example, in a file or memory 
data buffer) or transmitted 

9
00:00:40,560 --> 00:00:44,560
(for example, over a computer 
network) and reconstructed later 

10
00:00:44,560 --> 00:00:49,200
(possibly in a different computer environment)
The article goes into more detail, 

11
00:00:49,200 --> 00:00:54,480
and provides some history, but that description 
summarizes what we mean by "serialization". 

12
00:00:54,480 --> 00:00:57,840
There are three important 
aspects of that definition. 

13
00:01:00,000 --> 00:01:03,183
into a format that can be 
stored ... or transmitted 

14
00:01:03,280 --> 00:01:06,640
The whole point of serializing something 
is so that you can either store it somehow, 

15
00:01:06,640 --> 00:01:10,160
or send it somewhere else.
For example, we've seen that 

16
00:01:10,160 --> 00:01:14,880
we can't write numbers to a text file.
Instead, we had to write the individual 

17
00:01:14,880 --> 00:01:20,720
characters – the digits – that made up the number.
That's an example of serialization. 

18
00:01:20,720 --> 00:01:25,520
We took a numerical value, and 
serialized it into its individual digits. 

19
00:01:25,520 --> 00:01:32,414
Well technically, Python did that for us, using 
the str method. But the outcome was the same. 

20
00:01:34,240 --> 00:01:38,400
Given a text file, the number could be 
understood by other programming languages, 

21
00:01:38,400 --> 00:01:42,960
on different operating systems. 
It could even be read by humans. 

22
00:01:42,960 --> 00:01:45,680
Python's internal representation 
of a number might be very different 

23
00:01:45,680 --> 00:01:49,055
from the representation used by 
another programming language. 

24
00:01:49,120 --> 00:01:55,677
Sending the individual bits that make up the 
number, to another program, probably won't work. 

25
00:01:56,880 --> 00:02:00,800
and reconstructed later
That's obviously important. 

26
00:02:00,800 --> 00:02:04,320
If we can't deserialize the data, to 
get it back, then there was little 

27
00:02:04,320 --> 00:02:08,960
point serializing it in the first place.
Having said that, sometimes you won't get 

28
00:02:08,960 --> 00:02:15,284
exactly what you stored. We'll see an example 
of that, when we save some tuples as JSON. 

29
00:02:17,280 --> 00:02:19,926
possibly in a different computer environment 

30
00:02:19,926 --> 00:02:24,378
It's common to save data in a format that 
only the saving program can understand. 

31
00:02:24,480 --> 00:02:29,280
Many programs do that, because the data is 
only useful to the program that created it. 

32
00:02:29,280 --> 00:02:33,037
Proprietary data formats can 
also be protected by patents. 

33
00:02:33,120 --> 00:02:37,512
One example of that is the JPEG 
format, widely used for storing images. 

34
00:02:37,600 --> 00:02:41,360
Several claims for patent infringement were 
made against companies using jpeg images on 

35
00:02:41,360 --> 00:02:45,855
their websites, as well as against 
some digital camera manufacturers. 

36
00:02:48,000 --> 00:02:50,863
GIF files were also subject to 
patent restrictions, 

37
00:02:50,863 --> 00:02:55,760
and that led to the PNG format being created.
Fortunately, those patents were 

38
00:02:55,760 --> 00:03:00,400
either overturned or have now expired.
Even so, using a proprietary format is 

39
00:03:00,400 --> 00:03:04,810
going to cause problems, if you want 
to share that data with other systems. 

40
00:03:07,280 --> 00:03:10,960
Serialization standards
If serialized data is going to be 

41
00:03:10,960 --> 00:03:16,400
useful to different programs and computer systems, 
then it needs to follow a well-defined standard. 

42
00:03:16,400 --> 00:03:19,760
JSON is one such standard.
The International Standards 

43
00:03:19,760 --> 00:03:28,079
Organisation (ISO) released the standard 
ISO/IEC 21778:2017 in 2017. 

44
00:03:28,079 --> 00:03:32,525
That standard is consistent with the ECMA-404 standard for JSON. 

45
00:03:32,525 --> 00:03:39,097
That means any valid JSON that you create can be read by
any standards-compliant JSON parser, anywhere in the world. 

46
00:03:41,280 --> 00:03:47,040
JSON is an open standard (no patent issues) 
format for saving and interchanging data. 

47
00:03:47,040 --> 00:03:50,560
JSON is human-readable, as 
numbers, and other objects, 

48
00:03:50,560 --> 00:03:55,520
are serialized to plain text.
Applications can include a JSON parser, 

49
00:03:55,520 --> 00:04:00,684
that takes the JSON text, and parses it 
into a format that the application can use. 

50
00:04:00,800 --> 00:04:05,304
Python includes a json module, and 
that's what we'll be using in this video. 

51
00:04:06,640 --> 00:04:12,560
Back in IntelliJ, create a new Python 
file called simple_json_example.py. 

52
00:04:12,560 --> 00:04:17,200
I've created a list containing a few of the 
computer languages that influenced Python. 

53
00:04:17,200 --> 00:04:19,839
Pause the video while you type 
the list, or you can download 

54
00:04:19,839 --> 00:04:24,960
languages.txt from the resources for this 
video, and paste the list into your file. 

55
00:04:24,960 --> 00:04:31,600
Remember to include the import, on line 1.
The languages list contains eight lists, 

56
00:04:31,600 --> 00:04:35,295
holding the name of the language, 
and the year it was first used. 

57
00:04:35,360 --> 00:04:38,800
Those inner lists might be better 
represented as tuples, but you'll see why 

58
00:04:38,800 --> 00:04:44,240
I haven't used tuples, in the next video.
To serialize Python values to a file, 

59
00:04:44,240 --> 00:04:48,623
we open a text file for writing, 
then use the json.dump function. 

60
00:04:48,720 --> 00:04:53,460
The dump function serializes the data we 
give it, and write the result to the file. 

61
00:05:19,360 --> 00:05:24,320
Ok, we've told it to dump our languages 
list to testfile. We've also specified the 

62
00:05:24,320 --> 00:05:30,000
encoding to be UTF-8 – the JSON standard 
specifies UTF-8 for JSON documents. 

63
00:05:30,000 --> 00:05:32,112
Run the program. 

64
00:05:33,920 --> 00:05:39,915
The file test.json gets created, and we 
can open it in IntelliJ, to examine it. 

65
00:05:40,080 --> 00:05:44,400
The contents look exactly like a 
Python list, containing other lists. 

66
00:05:44,400 --> 00:05:47,200
That's because Python uses 
square brackets for lists, 

67
00:05:47,200 --> 00:05:52,329
and JSON uses square brackets for its arrays.
JSON isn't specific to Python, 

68
00:05:52,329 --> 00:05:56,800
this data just happens to look very similar 
to the Python data that we serialized. 

69
00:05:56,800 --> 00:06:00,146
We'll have a look at the JSON format, shortly. 

70
00:06:00,240 --> 00:06:05,000
Ok, that's the first step. We 
can store our list in a file. 

71
00:06:05,000 --> 00:06:09,040
For it to be useful, we also need 
to be able to read it back in again. 

72
00:06:09,040 --> 00:06:14,654
To do that, we use the load function.
Switch back to simple_json_example, 

73
00:06:14,654 --> 00:06:19,120
to add the code to read the data back in. 
There's no point writing the same data, 

74
00:06:19,120 --> 00:06:23,149
every time we run the program, so 
I'll comment that code out first: 

75
00:07:04,480 --> 00:07:09,200
Run the program.
I printed out the third item in the list, 

76
00:07:09,200 --> 00:07:14,320
on line 20, just to confirm that we are 
dealing with a list here, and not a string. 

77
00:07:14,320 --> 00:07:19,452
So that's good. We serialized our list 
to a file, then read it back in again. 

78
00:07:19,452 --> 00:07:22,640
Dealing with a nested structure 
like this one is easy. 

79
00:07:22,640 --> 00:07:26,960
The json module handles it well, and 
we retain our original structure. 

80
00:07:26,960 --> 00:07:32,400
Sometimes, you may be dealing with objects that 
can't be serialized and deserialized so reliably. 

81
00:07:32,400 --> 00:07:35,600
I'll demonstrate the problem, 
so that you're aware of it. 

82
00:07:35,600 --> 00:07:37,966
See you, in the next video.

