This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
We'll serialize the top ten players to a json file.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
So how did you do?
0:00
Were you able to fix the bug?
0:01
Here's how I did it.
0:03
Multiplying by negative one will
return the opposite of ascending
0:05
which is what we want.
0:09
Let's give it a while Control+F5 and
it worked.
0:10
Now instead of printing
these out to the console
0:15
let's try writing the results to a file.
0:18
We've read from a file
with a stream reader.
0:21
Now we can use a stream writer to
write the results to a text file.
0:23
Instead of just plain text though let's
serialize back into JSON using JSON.net.
0:27
Back in our program class we'll
create another method that's like our
0:33
de-serialized players but
instead serializes them.
0:36
We'll call it public
0:40
static void serialize players to file.
0:45
Then it'll take a list of Player,
called players.
0:55
And we'll also need a file name.
1:02
String fileName.
1:04
Let's copy the code from
the deserialize method.
1:09
It'll mostly be the same but
this time with writers instead of readers.
1:12
Well, we won't need this list of players
anymore, so let's get rid of that.
1:24
And our method is void,
so we can take this out.
1:28
We can change the StreamReader
to a StreamWriter.
1:32
And we'll rename it to writer.
1:38
Then instead of a JSON text reader,
we'll need a JSON text writer.
1:44
And change that to JSON writer, and
we'll need to pass that a writer.
1:51
Okay, we don't need to assign anything so
we can take out the players here.
1:59
And instead of DeSerialize we need
to call the Serialize method.
2:04
Can take out this and
see what Serialize wants.
2:13
It wants a JSON writer And the object.
2:18
So we'll pass it a jsonWriter and
2:22
then the object that we want to
serialize which is our players.
2:28
Now back in our main method, we can call
this method with our topTenPlayers, and
2:32
we'll need a new filename.
2:37
fileName, I'll copy this
Path.Combine up here,
2:40
= Path.Combine,
we'll rename it to topten.json
2:46
Then we'll call SerializePlayerToFile.
2:56
I got a little bit of a typo
let's go to definition.
3:01
I'm a stickler for spelling.
3:08
SerializePlayersToFile and
we need the list of top ten players,
3:11
and then we need our file name.
3:16
Let's give it a whirl.
3:21
F5 to run.
3:23
And there's our top ten players.
3:29
And I'll press F5 again
to get out of debugging.
3:30
Now we can go check our debug directory
to see if our new JSON file is there.
3:34
Can right-click on the project and
say open folder in file explore.
3:39
That's a quick way to get
to the solution directory.
3:44
Then I'll go into bin,
debug and there is our file.
3:48
Let's open it up.
3:54
All right so
let's make this little prettier.
3:58
We can right click here and say Un-minify.
4:00
And there's our top ten players.
4:03
Easy peasy, right?
4:08
Great job.
4:11
Now you know how easy it is to
serialize JSON using JSON.net.
4:12
It makes it easy for developers to package
something up, save it somewhere or
4:17
transfer it to another application, but
4:21
then easily load it back
into memory when we need it.
4:23
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up