Hendo, and welcome to the Golang programming language course in the previous session, we talked about
Jason Consett and indecision.
We want to talk about the others Jason concept.
Is streaming encoders and decoders, the encoding Jason package provides decoder and encoder types to
support the common operation of reading and writing streams of JASON data.
Now you can see the syntax of decoder function.
It returns a new decoder that reads from R.
The decoder introduces its own buffering and memory data from R beyond the JSON values requested, and
you can see the encoder syntax.
It returns a new encoder that rise to W.
Now let's go to this program to illustrate how to use new decoder and new encoder.
Go to Project Expeller and create a fine like main that go sit.
Package me and define the main function.
First created JSON fine that has three key value pairs named name, age and location.
So create a variable like Jason is three crawling equals back kick and field.
Name Colin
Keene.
H Holland, 20, and location
college in Texas and another key value per name,
Jessica.
Age 30 and location, for example, California.
Now, create a type of i o dot reader using the strings package by calling new reader function and since
Jason Astrium and his arguments for it.
First, define a variable like reader column equals and from the strings package calling new reader
function and set a standard reader as arguments to it.
Now create a writer variable by conning STV out front.
With Piku now defined and variable like writer Cullen equals and invoke SETI out front with the package.
Now, create a new decoder, which reads the data from the reader by shining new decoder function from
JSON package.
So first, define a variable like decoder calling equals and calling new decoder from JSON package.
And says, reader, as argument to.
Now, create a new encoder, which rise the data to the writer by calling new encoder function from
JSON Typekit, so define a variable like encoder calling equals and calling neo encoder functions from
JSON package its new encoder and sits writer, as are humans to it.
Now define it for loop and read the stream of JASON data from an eye or reader, then removes the H
field from each object rise to object to an AI or writer.
So define for
in the JSON, all the keys must be a string.
It means we can use the map time for arbitrary data the encoding JSON package uses map by the key is
string in and efforts to install arbitrary JSON objects.
So we first define a variable like.
Via the map, defined key by the time as strange and value by the time interface.
IMAP by the key string and A. In their face value, the keys are S3 and values are empty interface and
interface type describes an interface with zero methods.
In short, it can access all the types now calling dikkat function to reads the next JSON encoded value
from its input and a source it in the value pointer to by V.
So for call and check together, right?
If at all.
Calling it equals decoder dots.
B quote am Casone, the error not equals mean large dot print.
Pair off and return.
Now, define the range look to remove age fit for define a variable like call and equal range in we.
If key equals equals a
delete.
We come to she.
Now coming and could function to rise the JASON encoding of the to the three, followed by a new line
character.
So for Carl and Jake, together we write Eve error column equals encoder dots and quote Ampersand.
We
are not equals Neal.
Large dot, print and enter.
OK.
Reformat the court, save the project and execute the program.
Go round darts back.
Is this man that go?
OK, now we can see the output location.
Take those name Kane and location Californian named Jessica.
We could remove age feed from Jason value.
In this example, we were able to read a stream of JSON data through a reader, then delete the age
feed value from inside each object and then write the object through.
I ordered Reutter and we could use brand new decoder and new encoder from JSON package.
In order not to prolong the time of decision.
We will finish it and we will continue the content in the next session.
Goodbye.