Sometimes you want to customize JSON encoding and decoding of a type in this lecture.
We're going to take a look at the Marshaler and Unmarshaler interfaces.
Let's get started.
Here I want to encode the list value into this JSON data (on the right).
First of all I'm going to encode the list into JSON using marshal indent
it's going to give me the encoding result as a data bytes slice I'm going to save them into variables.
Then to check for the error.
And lastly I'm going to print out the encoded data.
OK before going on further I want to talk about something here.
I used the fatal function.
This function is equals to first printing the error message and then calling the os exit function.
So this is kind of equals to printing the message first and then terminating from the program.
Okay.
So by using this function I don't have to type these two lines of code.
Okay let's give it a try.
Hmm.
It didn't print anything it's because product doesn't export any of its fields.
So I need to export all of these fields like so their first letters are uppercase now.
So there should be other problems on the other side of the code.
So I'm going to take a look at them.
There are some problems in the list type so I'm going to change these letters to uppercase as well.
And there are also other problems in the main function I'm gonna change these field names into uppercase.
Great.
Now let's give it another try.
Nice.
Now it encodes the list into this JSON data however I don't want the release dates to be printed to
be encoded like this.
I want them to be integers—to be timestamp values.
So how can I do that to do that.
I need to use an interface.
Let me show you its documentation.
So if I implement this method on the Timestamp Type I can customize the behavior of the JSON encoder.
So I'm going to copy this method from here and paste it into the timestamp type and I'm going to attach
it to the timestamp like this and lastly I'm going to change these result values into named result
values because I'm going to use this byte slice inside of the function and I'm going to keep the error
all right here.
First I need to convert this timestamp into an integer and then I need to put that integer into the
data slice.
OK to do that there are two functions.
The first one is the Unix function which returns us an integer and there is also a strconv append
int function.
This function takes a bytes slice and an integer number.
This should be the ts which is an integer which will be an integer.
And then lastly I'm going to give it the decimal based system which is 10.
So let me copy this from here and put it here.
And for the integer part I'm going to call the Unix method of the timestamp.
Remember the timestamp is a time value.
Right.
So that's why I can call this method here.
So it's gonna return me the timestamp as an integer value.
OK.
Now I need to return it first because this append int function returns us a byte slice and then
I'm going to return a nil error because there is no error.
OK.
So by doing this I am customizing the behavior of this encoder.
This is the encoder.
OK let me try this.
Great.
Now the timestamps are integers.
Let me show you how does it work.
Let's say there is a product value and I'm going to call the marshal indent function on the list value.
This is one of the products inside of the list.
So first it's going to encode the title and price fields and it's gonna use default encoding.
But for the released field it's a timestamp.
It has a Marshal JSON method.
So the marshal indent function is gonna call this Marshal JSON method and the timestamp is going to
do its own stuff and customize this encoding and then returns it as a byte slice to the Marshal indent
function.
So this way we can customize the behavior of the encoder or in other words the Marshaler
not that you understand how to customize the encoding behavior.
Now I'm going to show you how to customize the decoding behavior as well so to do that.
I'm going to run this program once more here.
I'm going to get this JSON data and I'm going to put it into a constant like so.
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
to decode this JSON data into the list value.
So to do that I'm going to call the JSON Unmarshal function.
I'm going to pass the JSON data and the address of this list and then I'm going to handle the error
like so.
And lastly I'm going to print out the list that's it.
Oops! I need to change this into a bytes slice.
Okay.
Now let me try it.
It doesn't know how to convert these numeric timestamp values.
We need to teach it how to do so.
So I'm going to go to the timestamp type and I'm going to add another method here.
But first let me show you its documentation all right.
This unmarshaler interface can customize the behavior of decoding.
OK.
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
method to the timestamp.
OK let me give it a name to this byte slice as well.
So here I'm going to get all of these values one by one into this method.
So let's assume that I get this value OK here I need to set this timestamp receiver into this value
but I need to do so as a timestamp value.
Right now we're going to receive it as an integer value.
So I need to convert this into a timestamp to do that.
I'm going to call the timestamp function.
I'm going to pass this data to the timestamp function like so I'm passing it as string value because the
to timestamp function can only understand how to convert string values to timestamp values and lastly
I'm going to change this into a pointer receiver so that I can change its original value like so.
And lastly I'm going to return a nil error.
OK let me try this.
Great.
Now it works.
The unmarshall function decodes all of these timestamp values into a list value.
All right.
Congrats.
That's all for now.
I hope you enjoyed it so far. If you want to take a look at this summary
you can do so now.
See you in the next lecture.
Bye bye!