Welcome back.
In this lecture, you're going to learn how to encode truck values to Jason.
For example, imagine that these two boxes are programs.
One is on Mars and the other is on Earth.
One is written with JavaScript and the other one is written with the goal language.
So how can they communicate?
The Mars program has a message object.
It knows how to encode the message.
Object to Jason LACHSA Engo.
This is called martialing.
So now the program can send the Jason data to the other one.
The Earth program knows how to decode the Jason data so it receives decode and translates the data into
abstract value from the one it can work on the track value.
By the way, in go decoding is also called a martialing.
These two programs can easily encode and decode Jason data because Jason is just a structured string
value.
Why might you want to use Jasem?
First, it is human readable.
You can read it pretty easily.
Second, it is very easy for computers to pass it.
By the way, don't worry, you don't need to do the parsing.
Go comes with its own JSON parser.
And the last reason is Jason is one of the most commonly accepted data exchange format.
So it is widely supported.
All right.
That was a brief explanation.
For more information about Jason, please check out the links in the resources.
OK, now let's say you have a list of users and you want to encode them to Jason data.
Let's first start by declaring a user.
Each user has a username and password, so I'm going to declare two fields like so each user has a set
of permissions as well.
I think it's better to put them in a map like.
So you will see how I'm going to use it in a minute.
Now, I'm going to create a couple of users in a slice like.
So here is the first one.
For the permissions, I'm going to say, Neal, because this user doesn't have any permissions.
By the way, I don't use the field names because this is a small program, so there is no harm doing
so.
OK, here is another user.
And here is the last one.
Typing the map types like so is cumbersome, so I'm going to declare a new map type LACHSA.
Now I can remove the map type from the permissions field, it becomes an anonymous field, as you can
see, anonymous fields don't have to be struck types.
OK, now let's go down here.
I'm going to change the map types to permissions type like so.
OK, better now.
It's time to encode the users to Jason.
To do that, I'm going to use the Marshall function from the Jassam package.
Dysfunction in courts to give them value to Jason interface here means any type so the function can
accept any types of data.
I'll talk more about the empty interface type in the upcoming sections.
OK, now I'm going to pass the user to the function.
As you can see, the marshal function returns to Bisley's because Jason is a string formate and it also
returns an error, let's say the.
And I'm going to handle the error, as usual.
Now it's time to print the encoded JSON data like so.
Oops.
Where is my data?
This problem happens because the Jason package only includes the export fields.
So let's take a look at the fields of the users track.
They all start with lowercase letters, right?
This means they are on export.
It feels so other packages cannot see them.
It is easy to export a field so the jasen package can see it.
All you need to do is changing their first letters to uppercase Lexar.
Voila, now it works.
By the way, the output doesn't look good, this format, it's using the Marshall in and function lachsa.
This function works exactly like the Marshall function, but it prints the Jason.
Now it looks better.
By the way, there are two more inputs to the function, as you can see here.
Let me change them so you can understand what they do.
As you can see, the first parameter adds the prefix to each line and the other one is used for the
intonation it in dance like soap, because Jason is a hierarchical data format here, the square braces
define a list.
Think of it as a slice.
Each curly brace defines the adjacent object.
Think of it as a strike value.
These are the fields and these are the values.
OK, let's also export the permissions field, it's an anonymous field, so I'm going to make it a named
field instead.
Here it is, as you can see, my values are also can be encoded as Jassam objects.
Let's say for security purposes, you don't want to encode the password field, but you want other packages
to see this field, so how can you prevent encoding it?
Well, you can add a field tag like so.
Also now there is no password data in the jassam, now, let's say you need to encode the fields with
different names to do that so you can use a field tag again like so here I say in this field as username,
I'm also going to encode the permissions field as Perm's.
Excellent.
There is one more problem, though, the first user doesn't have any permissions, so the marshals are
in course it as not to prevent that.
You can add and omit MTA option like so this field tag does two things.
First, it changes the encoded name to Perm's and it prevents encoding the field if it has zero value.
As you can see now, it doesn't include the permissions for the first user, but if you don't want to
change its name but only admit it, if it's empty, then you just need to remove the name, but you
still need to keep the comma.
Otherwise, the Jassam package will encode the field as Ormet empty.
Now, the field name is permissions again, but the first user doesn't have one.
Now, let's say the Jason to tofile lachsa.
The greater than sun means get the output of the program and write it into the file, you are going
to use this file in the next lecture.
All right.
Now let's talk about the field tax a little bit more.
Just like the fields, they are part of the strike type.
So they are declared before the completion and they cannot be changed later on in runtime.
But what are they?
Well, a field tax is a meta data about the field.
It associates a simple shrink value to our field.
Most of the time you're going to use them to control the encoding and decoding.
As an example.
Let's take a look at the permissions field.
As you know, this is the field name and this is the field type.
So this is the field tag.
It's just a string literal.
It doesn't have any meaning on its own.
You want to type of field tack in a real string, literal because usually a field that includes double
chords.
Conventionally, a field tech has a key and a value, for example, this is a kini.
Usually it denotes a package name here, this denotes not only the adjacent package will read this stack,
so the Jasen package only reads a few tag.
If it starts with this Jassam key, each package can interpret the full text differently.
So other than Jason, there are a lot of field tags as well.
For more information, please check out the videos in the resources.
OK, this is the value of the Jason deck, it may contain multiple options conventionally, you need
to type them within double chords and you need to separate them with a comma.
You can use the options to tell the Jason package how to encode and decode a field.
For example, there are two options here, the first one changes the encoded field name and the other
one prevents encoding the field if it has a zero value.
I showed you the examples of them in the coding lecture.
So you know what they do.
Right.
So for now, see in the next lecture by.