WEBVTT 0 00:01.150 --> 00:09.180 Sometimes you want to customize JSON encoding and decoding of a type in this lecture. 1 00:09.200 --> 00:13.870 We're going to take a look at the Marshaler and Unmarshaler interfaces. 2 00:13.910 --> 00:14.720 Let's get started. 3 00:16.960 --> 00:22.150 Here I want to encode the list value into this JSON data (on the right). 4 00:22.150 --> 00:29.380 First of all I'm going to encode the list into JSON using marshal indent 5 00:32.310 --> 00:40.790 it's going to give me the encoding result as a data bytes slice I'm going to save them into variables. 6 00:40.780 --> 00:42.700 Then to check for the error. 7 00:42.720 --> 00:47.400 And lastly I'm going to print out the encoded data. 8 00:47.490 --> 00:52.590 OK before going on further I want to talk about something here. 9 00:52.590 --> 00:55.000 I used the fatal function. 10 00:55.110 --> 01:01.650 This function is equals to first printing the error message and then calling the os exit function. 11 01:01.650 --> 01:08.120 So this is kind of equals to printing the message first and then terminating from the program. 12 01:08.120 --> 01:08.680 Okay. 13 01:08.880 --> 01:16.030 So by using this function I don't have to type these two lines of code. 14 01:16.210 --> 01:17.730 Okay let's give it a try. 15 01:18.980 --> 01:19.560 Hmm. 16 01:19.600 --> 01:26.770 It didn't print anything it's because product doesn't export any of its fields. 17 01:26.770 --> 01:33.130 So I need to export all of these fields like so their first letters are uppercase now. 18 01:33.550 --> 01:37.270 So there should be other problems on the other side of the code. 19 01:37.360 --> 01:39.690 So I'm going to take a look at them. 20 01:39.820 --> 01:47.500 There are some problems in the list type so I'm going to change these letters to uppercase as well. 21 01:47.690 --> 01:56.510 And there are also other problems in the main function I'm gonna change these field names into uppercase. 22 01:56.540 --> 01:57.640 Great. 23 01:57.650 --> 01:59.900 Now let's give it another try. 24 01:59.960 --> 02:00.560 Nice. 25 02:00.560 --> 02:08.310 Now it encodes the list into this JSON data however I don't want the release dates to be printed to 26 02:08.310 --> 02:09.720 be encoded like this. 27 02:09.990 --> 02:14.210 I want them to be integers—to be timestamp values. 28 02:14.220 --> 02:17.460 So how can I do that to do that. 29 02:17.490 --> 02:19.630 I need to use an interface. 30 02:19.650 --> 02:22.500 Let me show you its documentation. 31 02:22.620 --> 02:31.500 So if I implement this method on the Timestamp Type I can customize the behavior of the JSON encoder. 32 02:31.500 --> 02:40.500 So I'm going to copy this method from here and paste it into the timestamp type and I'm going to attach 33 02:40.500 --> 02:50.760 it to the timestamp like this and lastly I'm going to change these result values into named result 34 02:50.780 --> 02:57.100 values because I'm going to use this byte slice inside of the function and I'm going to keep the error 35 02:58.910 --> 03:00.140 all right here. 36 03:00.170 --> 03:08.870 First I need to convert this timestamp into an integer and then I need to put that integer into the 37 03:08.870 --> 03:10.220 data slice. 38 03:10.220 --> 03:13.870 OK to do that there are two functions. 39 03:13.880 --> 03:22.620 The first one is the Unix function which returns us an integer and there is also a strconv append 40 03:22.730 --> 03:24.330 int function. 41 03:24.350 --> 03:28.580 This function takes a bytes slice and an integer number. 42 03:28.590 --> 03:33.350 This should be the ts which is an integer which will be an integer. 43 03:33.440 --> 03:38.160 And then lastly I'm going to give it the decimal based system which is 10. 44 03:38.570 --> 03:43.000 So let me copy this from here and put it here. 45 03:43.190 --> 03:48.830 And for the integer part I'm going to call the Unix method of the timestamp. 46 03:48.830 --> 03:51.560 Remember the timestamp is a time value. 47 03:51.590 --> 03:52.140 Right. 48 03:52.160 --> 03:55.320 So that's why I can call this method here. 49 03:55.370 --> 03:59.460 So it's gonna return me the timestamp as an integer value. 50 04:00.350 --> 04:00.590 OK. 51 04:00.590 --> 04:09.620 Now I need to return it first because this append int function returns us a byte slice and then 52 04:09.740 --> 04:12.810 I'm going to return a nil error because there is no error. 53 04:12.830 --> 04:14.080 OK. 54 04:14.330 --> 04:22.360 So by doing this I am customizing the behavior of this encoder. 55 04:22.400 --> 04:23.610 This is the encoder. 56 04:23.900 --> 04:28.880 OK let me try this. 57 04:28.990 --> 04:29.790 Great. 58 04:29.800 --> 04:34.240 Now the timestamps are integers. 59 04:34.280 --> 04:35.890 Let me show you how does it work. 60 04:36.050 --> 04:41.680 Let's say there is a product value and I'm going to call the marshal indent function on the list value. 61 04:41.690 --> 04:45.080 This is one of the products inside of the list. 62 04:45.080 --> 04:51.930 So first it's going to encode the title and price fields and it's gonna use default encoding. 63 04:52.340 --> 04:56.180 But for the released field it's a timestamp. 64 04:56.180 --> 04:58.010 It has a Marshal JSON method. 65 04:58.700 --> 05:04.790 So the marshal indent function is gonna call this Marshal JSON method and the timestamp is going to 66 05:04.790 --> 05:11.990 do its own stuff and customize this encoding and then returns it as a byte slice to the Marshal indent 67 05:11.990 --> 05:12.980 function. 68 05:12.980 --> 05:20.180 So this way we can customize the behavior of the encoder or in other words the Marshaler 69 05:23.150 --> 05:27.830 not that you understand how to customize the encoding behavior. 70 05:27.830 --> 05:34.290 Now I'm going to show you how to customize the decoding behavior as well so to do that. 71 05:34.490 --> 05:38.110 I'm going to run this program once more here. 72 05:38.270 --> 05:47.450 I'm going to get this JSON data and I'm going to put it into a constant like so. 73 05:47.510 --> 05:55.660 Now I'm going to comment out this code and then I'm going to create a list value and I'm going to try 74 05:55.960 --> 06:00.700 to decode this JSON data into the list value. 75 06:01.510 --> 06:06.330 So to do that I'm going to call the JSON Unmarshal function. 76 06:06.490 --> 06:14.030 I'm going to pass the JSON data and the address of this list and then I'm going to handle the error 77 06:14.580 --> 06:15.480 like so. 78 06:15.480 --> 06:22.040 And lastly I'm going to print out the list that's it. 79 06:22.310 --> 06:26.160 Oops! I need to change this into a bytes slice. 80 06:26.210 --> 06:27.050 Okay. 81 06:27.170 --> 06:29.240 Now let me try it. 82 06:29.310 --> 06:34.770 It doesn't know how to convert these numeric timestamp values. 83 06:34.770 --> 06:37.440 We need to teach it how to do so. 84 06:37.470 --> 06:43.440 So I'm going to go to the timestamp type and I'm going to add another method here. 85 06:43.560 --> 06:49.530 But first let me show you its documentation all right. 86 06:49.560 --> 06:54.800 This unmarshaler interface can customize the behavior of decoding. 87 06:54.800 --> 06:55.240 OK. 88 06:55.350 --> 07:01.890 So I'm going to copy this method from here and I'm going to paste it here then I'm going to attach this 89 07:01.890 --> 07:04.020 method to the timestamp. 90 07:04.030 --> 07:07.800 OK let me give it a name to this byte slice as well. 91 07:07.800 --> 07:14.250 So here I'm going to get all of these values one by one into this method. 92 07:14.310 --> 07:24.630 So let's assume that I get this value OK here I need to set this timestamp receiver into this value 93 07:25.230 --> 07:28.720 but I need to do so as a timestamp value. 94 07:28.890 --> 07:32.620 Right now we're going to receive it as an integer value. 95 07:32.730 --> 07:36.300 So I need to convert this into a timestamp to do that. 96 07:36.330 --> 07:39.350 I'm going to call the timestamp function. 97 07:39.480 --> 07:46.410 I'm going to pass this data to the timestamp function like so I'm passing it as string value because the 98 07:46.410 --> 07:54.770 to timestamp function can only understand how to convert string values to timestamp values and lastly 99 07:55.310 --> 08:02.030 I'm going to change this into a pointer receiver so that I can change its original value like so. 100 08:02.570 --> 08:05.470 And lastly I'm going to return a nil error. 101 08:05.480 --> 08:07.450 OK let me try this. 102 08:07.610 --> 08:08.290 Great. 103 08:08.300 --> 08:09.460 Now it works. 104 08:09.540 --> 08:16.610 The unmarshall function decodes all of these timestamp values into a list value. 105 08:17.470 --> 08:18.150 All right. 106 08:18.200 --> 08:19.130 Congrats. 107 08:19.130 --> 08:20.440 That's all for now. 108 08:20.510 --> 08:26.520 I hope you enjoyed it so far. If you want to take a look at this summary 109 08:26.520 --> 08:28.000 If you want to take a look at this summary you can do so now. 110 08:28.020 --> 08:29.280 See you in the next lecture. 111 08:29.320 --> 08:29.610 Bye bye!