WEBVTT 00:01.410 --> 00:02.160 Welcome back. 00:02.910 --> 00:07.270 In this lecture, you're going to learn how to encode truck values to Jason. 00:07.680 --> 00:11.670 For example, imagine that these two boxes are programs. 00:12.310 --> 00:15.560 One is on Mars and the other is on Earth. 00:16.110 --> 00:21.560 One is written with JavaScript and the other one is written with the goal language. 00:21.870 --> 00:23.790 So how can they communicate? 00:24.390 --> 00:26.490 The Mars program has a message object. 00:26.790 --> 00:28.830 It knows how to encode the message. 00:28.830 --> 00:31.700 Object to Jason LACHSA Engo. 00:31.740 --> 00:33.160 This is called martialing. 00:33.720 --> 00:37.380 So now the program can send the Jason data to the other one. 00:38.010 --> 00:45.990 The Earth program knows how to decode the Jason data so it receives decode and translates the data into 00:45.990 --> 00:49.890 abstract value from the one it can work on the track value. 00:50.130 --> 00:54.230 By the way, in go decoding is also called a martialing. 00:54.540 --> 01:02.120 These two programs can easily encode and decode Jason data because Jason is just a structured string 01:02.130 --> 01:02.700 value. 01:04.270 --> 01:06.040 Why might you want to use Jasem? 01:07.460 --> 01:09.050 First, it is human readable. 01:09.290 --> 01:10.730 You can read it pretty easily. 01:10.910 --> 01:14.030 Second, it is very easy for computers to pass it. 01:14.330 --> 01:16.780 By the way, don't worry, you don't need to do the parsing. 01:16.820 --> 01:19.790 Go comes with its own JSON parser. 01:20.060 --> 01:25.250 And the last reason is Jason is one of the most commonly accepted data exchange format. 01:25.520 --> 01:27.230 So it is widely supported. 01:27.560 --> 01:28.040 All right. 01:28.160 --> 01:29.770 That was a brief explanation. 01:30.290 --> 01:34.100 For more information about Jason, please check out the links in the resources. 01:34.520 --> 01:39.900 OK, now let's say you have a list of users and you want to encode them to Jason data. 01:40.700 --> 01:42.800 Let's first start by declaring a user. 01:44.030 --> 01:51.380 Each user has a username and password, so I'm going to declare two fields like so each user has a set 01:51.380 --> 01:52.500 of permissions as well. 01:52.940 --> 01:55.490 I think it's better to put them in a map like. 01:55.520 --> 01:58.150 So you will see how I'm going to use it in a minute. 01:58.970 --> 02:02.000 Now, I'm going to create a couple of users in a slice like. 02:02.010 --> 02:04.040 So here is the first one. 02:06.650 --> 02:11.870 For the permissions, I'm going to say, Neal, because this user doesn't have any permissions. 02:12.260 --> 02:17.450 By the way, I don't use the field names because this is a small program, so there is no harm doing 02:17.450 --> 02:17.750 so. 02:18.350 --> 02:19.730 OK, here is another user. 02:25.500 --> 02:26.790 And here is the last one. 02:32.880 --> 02:38.460 Typing the map types like so is cumbersome, so I'm going to declare a new map type LACHSA. 02:40.120 --> 02:46.210 Now I can remove the map type from the permissions field, it becomes an anonymous field, as you can 02:46.210 --> 02:49.150 see, anonymous fields don't have to be struck types. 02:49.880 --> 02:51.960 OK, now let's go down here. 02:51.970 --> 02:55.320 I'm going to change the map types to permissions type like so. 02:55.780 --> 02:57.490 OK, better now. 02:57.490 --> 03:00.150 It's time to encode the users to Jason. 03:00.580 --> 03:04.330 To do that, I'm going to use the Marshall function from the Jassam package. 03:05.020 --> 03:12.400 Dysfunction in courts to give them value to Jason interface here means any type so the function can 03:12.400 --> 03:14.080 accept any types of data. 03:14.320 --> 03:18.330 I'll talk more about the empty interface type in the upcoming sections. 03:18.800 --> 03:22.180 OK, now I'm going to pass the user to the function. 03:23.590 --> 03:30.220 As you can see, the marshal function returns to Bisley's because Jason is a string formate and it also 03:30.220 --> 03:32.590 returns an error, let's say the. 03:34.310 --> 03:36.380 And I'm going to handle the error, as usual. 03:40.670 --> 03:43.940 Now it's time to print the encoded JSON data like so. 03:46.010 --> 03:46.620 Oops. 03:46.670 --> 03:47.540 Where is my data? 03:48.230 --> 03:53.890 This problem happens because the Jason package only includes the export fields. 03:54.590 --> 03:57.250 So let's take a look at the fields of the users track. 03:57.620 --> 04:00.810 They all start with lowercase letters, right? 04:01.130 --> 04:03.350 This means they are on export. 04:03.350 --> 04:06.290 It feels so other packages cannot see them. 04:06.710 --> 04:10.730 It is easy to export a field so the jasen package can see it. 04:11.060 --> 04:16.160 All you need to do is changing their first letters to uppercase Lexar. 04:17.730 --> 04:19.200 Voila, now it works. 04:19.800 --> 04:26.550 By the way, the output doesn't look good, this format, it's using the Marshall in and function lachsa. 04:27.240 --> 04:31.590 This function works exactly like the Marshall function, but it prints the Jason. 04:32.400 --> 04:33.420 Now it looks better. 04:34.230 --> 04:37.540 By the way, there are two more inputs to the function, as you can see here. 04:37.920 --> 04:40.610 Let me change them so you can understand what they do. 04:42.490 --> 04:48.570 As you can see, the first parameter adds the prefix to each line and the other one is used for the 04:48.580 --> 04:56.170 intonation it in dance like soap, because Jason is a hierarchical data format here, the square braces 04:56.170 --> 04:57.130 define a list. 04:57.370 --> 04:58.630 Think of it as a slice. 04:59.170 --> 05:01.700 Each curly brace defines the adjacent object. 05:02.170 --> 05:03.740 Think of it as a strike value. 05:04.450 --> 05:07.600 These are the fields and these are the values. 05:08.630 --> 05:15.200 OK, let's also export the permissions field, it's an anonymous field, so I'm going to make it a named 05:15.200 --> 05:15.970 field instead. 05:17.820 --> 05:23.910 Here it is, as you can see, my values are also can be encoded as Jassam objects. 05:24.880 --> 05:31.900 Let's say for security purposes, you don't want to encode the password field, but you want other packages 05:31.900 --> 05:36.250 to see this field, so how can you prevent encoding it? 05:37.030 --> 05:40.270 Well, you can add a field tag like so. 05:42.900 --> 05:49.800 Also now there is no password data in the jassam, now, let's say you need to encode the fields with 05:49.800 --> 05:57.930 different names to do that so you can use a field tag again like so here I say in this field as username, 05:58.620 --> 06:02.190 I'm also going to encode the permissions field as Perm's. 06:03.570 --> 06:04.300 Excellent. 06:05.250 --> 06:11.640 There is one more problem, though, the first user doesn't have any permissions, so the marshals are 06:11.640 --> 06:15.030 in course it as not to prevent that. 06:15.030 --> 06:20.610 You can add and omit MTA option like so this field tag does two things. 06:21.030 --> 06:28.560 First, it changes the encoded name to Perm's and it prevents encoding the field if it has zero value. 06:29.750 --> 06:34.910 As you can see now, it doesn't include the permissions for the first user, but if you don't want to 06:34.910 --> 06:40.310 change its name but only admit it, if it's empty, then you just need to remove the name, but you 06:40.310 --> 06:41.840 still need to keep the comma. 06:42.260 --> 06:46.250 Otherwise, the Jassam package will encode the field as Ormet empty. 06:48.110 --> 06:52.730 Now, the field name is permissions again, but the first user doesn't have one. 06:55.370 --> 06:58.370 Now, let's say the Jason to tofile lachsa. 07:00.510 --> 07:06.600 The greater than sun means get the output of the program and write it into the file, you are going 07:06.600 --> 07:08.310 to use this file in the next lecture. 07:09.150 --> 07:09.600 All right. 07:09.600 --> 07:12.630 Now let's talk about the field tax a little bit more. 07:13.020 --> 07:16.040 Just like the fields, they are part of the strike type. 07:16.320 --> 07:21.560 So they are declared before the completion and they cannot be changed later on in runtime. 07:21.870 --> 07:22.830 But what are they? 07:23.490 --> 07:26.820 Well, a field tax is a meta data about the field. 07:27.240 --> 07:30.600 It associates a simple shrink value to our field. 07:31.200 --> 07:35.430 Most of the time you're going to use them to control the encoding and decoding. 07:35.910 --> 07:36.840 As an example. 07:36.850 --> 07:38.820 Let's take a look at the permissions field. 07:39.630 --> 07:43.080 As you know, this is the field name and this is the field type. 07:43.410 --> 07:44.820 So this is the field tag. 07:45.150 --> 07:46.500 It's just a string literal. 07:46.890 --> 07:49.050 It doesn't have any meaning on its own. 07:52.250 --> 07:58.790 You want to type of field tack in a real string, literal because usually a field that includes double 07:58.790 --> 07:59.240 chords. 08:01.040 --> 08:06.230 Conventionally, a field tech has a key and a value, for example, this is a kini. 08:07.290 --> 08:14.610 Usually it denotes a package name here, this denotes not only the adjacent package will read this stack, 08:15.000 --> 08:17.710 so the Jasen package only reads a few tag. 08:17.730 --> 08:24.290 If it starts with this Jassam key, each package can interpret the full text differently. 08:24.690 --> 08:28.350 So other than Jason, there are a lot of field tags as well. 08:28.530 --> 08:31.830 For more information, please check out the videos in the resources. 08:33.600 --> 08:40.650 OK, this is the value of the Jason deck, it may contain multiple options conventionally, you need 08:40.650 --> 08:45.380 to type them within double chords and you need to separate them with a comma. 08:46.230 --> 08:51.540 You can use the options to tell the Jason package how to encode and decode a field. 08:52.670 --> 08:58.850 For example, there are two options here, the first one changes the encoded field name and the other 08:58.850 --> 09:02.630 one prevents encoding the field if it has a zero value. 09:03.170 --> 09:06.290 I showed you the examples of them in the coding lecture. 09:06.590 --> 09:07.720 So you know what they do. 09:08.430 --> 09:08.900 Right. 09:09.120 --> 09:11.330 So for now, see in the next lecture by.