Welcome back.
In this lecture, you're going to learn how to get your values back by decoding the JSON data that we
have created in the last lecture.
OK, in the previous lecture, you had saved the output of the encoding to the users JSON file.
So now I'm going to read it into a bit less, then I'm going to decode it back into a user's slice.
First, I'm going to declare a new bytes for the input.
I'm going to use a scanner to read the jassam from the file.
So I'm going to create a new scanner in a loop.
And I'm going to scan for the lines.
Inside the loop, I'm going to add the bites of the next line from the Jason file to the input slice.
Let me show you what the input looks like.
I'm going to run the program by feeding it JSON file LACHSA.
As you can see, the input contains adjacent data.
Now the scanner removes the new lines.
That's why the output looks like so.
But it's not a problem for the adjacent package.
It can read this.
I can add a new line here, Lexar.
Now it looks better, right?
However, as I said, doing so is unnecessary, so I'm not going to add new lines.
You got the jassam, now it's time to decode it.
I'm going to decode the users into the user slice.
So I'm going to declare it like, sup?
Of course, you don't have the USA's track here, let me declare it LACHSA.
E JSON object contains a user name field, so I'm going to declare the name field as a shrink.
The Jason data doesn't contain the password fields, so I won't include them in the truck either eat
JSON object has a purpose object and each Perm's object contains key value pairs.
So I'm going to add a Matfield like so.
OK, it is time to decode the Jason to the users slice to do that.
I'm going to call the Marshall function.
This function is the opposite of the Marshall function.
Remember, the Marshall function encodes a value to Jason and the Marshall function because Jason to
a value.
It takes a bite slice and an empty interface and returns an error, the data parameter expects and encoded
JSON data.
So first, the commercial function will pass the JSON data and it's going to write the result to a variable
that you're going to pass through the V parameter.
So first I'm going to store the error in the air variable and I'm going to handle the error.
Now, I'm going to give it the JSON data, Alexa.
Lastly, I'm going to give it the user's slice here.
The commercial function is going to decode the Jason into the user's slice.
So let's print the result as the.
Whoops, there is an error.
It says the user is not a pointer, this happens because a function cannot change a variable directly.
As you know, the user is a variable, so the function cannot change it.
There is only one way for a function to change a variable.
You need to use a pointer lachsa.
Otherwise the a marginal function cannot store the decoder data in the user.
Let me explain how it works.
Think of this great as computer memory, each cell represents a memory location and each one has a memory
address.
Here is the user's variable.
It is located on the memory location.
Here is a call to the Marshall function.
Now I'm going to pass the memory address of the user's variable to the, um, Marshall function.
This is called the address operator.
It finds the memory location of a value or a variable.
So here it finds the memory address of the user's slice and it gives that address to the Marshall function
LACHSA.
So the function can directly go to the address of the user's variable and change it there.
That's why you need to pass a pointer.
Don't worry, I'm going to talk about pointers extensively in the upcoming sections, so you'd better
understand how it works.
Here is another problem, the Jason package can understand that there are three objects in the Jason
data, but it couldn't mashallah.
But why?
Let's take a look at the Jason here.
The field name is username Howar.
In this truck, the field name is name.
So you need to tell this to the Jason package so it can get data from the user name fields into the
name field of the truck.
To do that, you need to use field tax.
So I'm going to add a field tag like so here I say decode the user name field in the JSON, object to
the name field in the user's track.
Let's take a look at the Jassam.
It stores the permissions data with the person's name.
So I'm going to add another field tag to the permissions field like so.
Ask them how it works.
Let's pray they printed to do that.
I'm going to range over the users and I'm going to print the username.
Next, I'm going to use a switch and I'm going to save the permissions to the variable.
As you know, some users don't have any permissions, so let's check for that first.
Here I'm going to say this user has no power.
OK, good.
Next, I'm going to check for the admin permission Lexar.
Now, I'm going to say, is it just that?
Lastly, I'm going to check for the right permission.
And I'm going to say, Ken writes.
I'm going to put a new line here so the loop will print the next user on the next line.
Looks good.
Lastly, let's move the marshal into the if statement doing so wasn't necessary, but I think it looks
better to me.
So you've learned how to encode and decode JSON data.
You'll also learn about field text and pointers.
This was the last section of the composite types.
You completed all of them.
Congrats, Volda.
Now you deeply know how to represent data in the go programming language.
That's awesome.
All right.
That's all for now.
See you in the next part.