1 00:00:00,180 --> 00:00:03,160 The .NET framework has a couple of JsonSerializers. 2 00:00:03,160 --> 00:00:07,310 But we're going to use a third party serializer called Json.NET. 3 00:00:07,310 --> 00:00:09,760 It's open sourced and easy to use. 4 00:00:09,760 --> 00:00:14,240 We'll be using the NuGet package manager to install it in our application. 5 00:00:14,240 --> 00:00:17,638 If you wanna learn more about NuGet at Treehouse check out a link in the notes. 6 00:00:17,638 --> 00:00:24,050 First we'll right-click on the SoccerStats project and choose, Manage NuGet Packages. 7 00:00:25,440 --> 00:00:30,405 Then, under Browse, Json.net is pretty popular, so it's right here. 8 00:00:30,405 --> 00:00:33,660 Newtonsoft.Json. 9 00:00:33,660 --> 00:00:37,962 Here it's chosen the latest stable version to install, that should be fine for us. 10 00:00:37,962 --> 00:00:46,260 Click OK and then Finished. 11 00:00:46,260 --> 00:00:48,320 Let's check the references to see if it's in there. 12 00:00:49,450 --> 00:00:51,410 Newtonsoft.Json. 13 00:00:51,410 --> 00:00:56,011 Before we get started let's check out the documentation on how to use Json.net. 14 00:00:56,011 --> 00:00:59,064 We'll search Json.Net. 15 00:00:59,064 --> 00:01:00,730 There it is. 16 00:01:02,320 --> 00:01:07,130 Let's see, documentation, there it, Json.NET Documentation. 17 00:01:08,380 --> 00:01:10,910 All right, getting started. 18 00:01:10,910 --> 00:01:13,130 Serializing and deserializing JSON. 19 00:01:14,380 --> 00:01:15,930 That's what we want. 20 00:01:15,930 --> 00:01:19,680 Since we're creating objects from JSON, we'll be deserializing. 21 00:01:21,130 --> 00:01:24,190 Let's see, the quickest method of converting between JSON text and 22 00:01:24,190 --> 00:01:26,650 a .NET object is using the JsonSerializer. 23 00:01:27,820 --> 00:01:29,110 Sounds good to me. 24 00:01:29,110 --> 00:01:34,630 We'll skip over JsonConvert and head right here to JsonSerializer. 25 00:01:34,630 --> 00:01:37,190 For more control over how an object is serialized, 26 00:01:37,190 --> 00:01:40,140 the JsonSerializer can be used directly. 27 00:01:40,140 --> 00:01:42,060 The JsonSerializer is able to read and 28 00:01:42,060 --> 00:01:47,041 write JSON text directly to a stream via JsonTextReader and JsonTextWriter. 29 00:01:48,100 --> 00:01:50,620 So it sounds like will need a stream. 30 00:01:50,620 --> 00:01:52,140 And we've got a code sample here. 31 00:01:54,310 --> 00:01:57,650 Looks like we'll need to instantiate a JsonSerializer object. 32 00:01:58,970 --> 00:02:00,830 These properties here look like settings. 33 00:02:00,830 --> 00:02:02,630 We'll ignore those for now. 34 00:02:02,630 --> 00:02:03,880 There's a stream here. 35 00:02:05,320 --> 00:02:08,940 This is using a stream writer because it's serializing. 36 00:02:08,940 --> 00:02:11,960 We'll need the opposite of that which is stream reader. 37 00:02:11,960 --> 00:02:14,790 Then we'll need a JSON reader instead of a JSON writer. 38 00:02:16,690 --> 00:02:18,680 Then it calls the Serialize method. 39 00:02:18,680 --> 00:02:20,760 I bet there's a Deserialize method too. 40 00:02:22,330 --> 00:02:25,030 Let's see if we can find the Deserialize method in the docs. 41 00:02:26,610 --> 00:02:29,390 See this looks like a link to the JsonSerializer. 42 00:02:31,450 --> 00:02:33,520 All right, let's go to the Methods. 43 00:02:36,290 --> 00:02:37,470 Here it is. 44 00:02:37,470 --> 00:02:39,240 Looks like there are four overloads. 45 00:02:41,110 --> 00:02:45,060 These last three methods say that they deserialize into an instance of 46 00:02:45,060 --> 00:02:46,130 the specified type. 47 00:02:47,160 --> 00:02:50,180 This first one attempts without knowing the type so 48 00:02:50,180 --> 00:02:51,500 it would have no idea how to do it. 49 00:02:52,740 --> 00:02:55,540 Let's click on this one that takes a JSON reader and a type. 50 00:02:56,540 --> 00:03:02,400 It returns an object, so we'd have to cast it to our players if we wanted to use it. 51 00:03:02,400 --> 00:03:02,930 Let's go back. 52 00:03:06,670 --> 00:03:09,450 This one looks a little different, with a T in the brackets. 53 00:03:11,670 --> 00:03:13,970 This is telling us that it's generic. 54 00:03:13,970 --> 00:03:16,200 The T represents the type parameter and 55 00:03:16,200 --> 00:03:19,520 you can see here that also the return type is a T. 56 00:03:19,520 --> 00:03:24,290 This means that the method will return the deserialized object as type T. 57 00:03:24,290 --> 00:03:27,840 In the next video we'll be using this method to deserialize our players 58 00:03:27,840 --> 00:03:28,550 JSON file