Step by Step Tutorial: Deserializing JSON using c# and json.net

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] well hello and welcome to another video this one is working with Jason in c-sharp now before we go too much further I do apologize excuse me for my voice of caught a bit of a cold sort of the under the weather but hopefully you can still understand what I've seen some people even said it makes me sound sexy so you be the judge of that although I'm sure that's not what you've come here for okay so onto our user story as a developer that's me or you I want to parse or deserialize and Jason and I don't want to do that so that I can work using object-oriented notation okay so basically you're going to get a JSON string it's probably come to you over the web from an API restful api for example and you want to take that and you want to restructure dory we combine it at the other end or deserialize it into an object format that you can work with our acceptance criteria basically when we're done at the end of the video you should be able to understand what JSON is and have it structured and using c-sharp and Jason don't net you should be able to deserialize jason strings and work with them as objects are ingredients very quickly Windows PC to run Visual Studio Visual Studio itself obviously I use the Community Edition and then Jason editor online is one of many on my injection editing tools or viewing tools I just happen to use that one because I like it so you'll obviously need a web browser and it's a great tool because it allows you to work with some complex Jason structures and navigate through I've I've found this one invaluable when I've been doing some development of java java with there Jason and about 20 minutes of your time the tutorial structure a very straightforward we're going to basically install decent dotnet we'll go through actually I think is to point to a bit mixed up and the second point nurse should really be coming fast so what is Jason why do you use it and for all the alternatives I'll go into it in a bit but if you're really super interested there's lots of excellent resources already oh they are on the net that you can look at this video is primarily targeting working with c-sharp and Jason together and then finally there's some money shot is that where we're going to Dec devise some jason objects using json dotnet which is hopefully fire here so onto a wire flame again of a simple wire frame employee not required but again I just find it helps clarify my thinking so in essence we get two multi-line text boxes the first one at the top of the screen there we're going to paste or law serialized casing into that window will click the deserialize button and then output will be spat out in the text box at the bottom here at any object-oriented is a structured fashion we'll pick a partner attributes and work with them individually you'll see how that see how all that works and I just put a clear debug button on there just to clear the debug window just makes it easier for me when I'm doing the demoing and stuff okay so what what is Jason well it stands for JavaScript object notation and it's effectively an open format used for the transmission of data or object data over the word okay so you make a call tell guys to API you're going to get a lot of information back maybe one object be multiple objects it could be details of people it could be fault tickets doesn't matter what is basically serialized data but in a structured format and it consists of attribute value tails as well as re datatypes okay so an object-oriented concept of attributes that obviously have values and then you can have a breeze within that as well and adjacent object can contain other nested objects so you'll see what I mean by that in a minute when we come into let me come on to some examples and there we go it's time for a bit of an example so in many simple terms this is probably the simplest JSON object that you could think of just about and it basically represents a person in this case a fallen hero of mine someone John Laura and I would still see Sean Connery's the best bond by I had a very soft spot for mantra notice he was the bond that was around when I was growing up and I was a wee boy sore very sad to see him go this year anyway so I thought I'd be able to homage to him so this object represents a person in this case Roger Moore in Jason the start of an object is denoted by a an open current bracket and then the end of the object is closed cuddle bracket and in this case we have for a treat the Belitung tongue twister for attribute value appears in this object and they are comma saturated here as you can see the attribute and then the value R or colon separated but the actual work attribute value appears as a whole lot of comma separated with the exception of the last one which also it doesn't have one so first name dilemma given a dated by double quotes as an attribute and the value in this case is Roger again it's a string value the delimited by double quotes same as last name except a different value in a different attribute name same data type age is a number so as you can see there Roger was 89 many passed away but as it's a number it doesn't require double quotes and then finally a boolean value which can take true or false and and representing whether he's alive or not and unfortunately he is not okay so let's go over to Jason editor on line and paste and add Jason into the left-hand plane with our little arrow and in Tennessee does a Red Cross they are seen as a like a formatting error you click on that button there it is care force the Ellen it's basically see we've got a comma the other should be a semicolon collect the formatting again get rid of that and move over to the right and that the right plane allows you to navigate through and see the structure a bit better bit we've got four attributes and the LC is the object itself so a more complex example now this is introducing the concept of a nested object so again it's basically a repeat of what was on the last screen get the same attributes start the object so on and so forth this time of introduced a new attribute called the dress so it's the same as all the other attributes of Kadir but it's effectively an object attribute it doesn't contain a single element it contains an object of other attribute value appears so again here's a start of a nested object and here in this case we have three attribute value peals representing the address so back in our editor let's paste that JSON in let stayin with no errors that button I'm clicking there it just kind of tie these up and checks for syntax errors as well bills on you next to the object attribute and you can see our high-level object has five attributes the nested object attribute being the fifth one that has self free attribute value pairs as you saw on a previous slide and then finally this is our slightly more complex one I've removed a couple of attributes that were on the previous examples and notably agen is alive and one of the address elements purely for clarity I don't want to clutter the slide too much just taking it out so this is introducing an array okay so you can see here we have a new phone numbers attribute based on lots of the address attribute it doesn't contain a single value it contains an array of objects and arrays in Jason are denoted by square brackets so phone number is an attribute that contains an array of other objects and in this case the object is effectively a phone number represented by type home and mobile and a number okay that they are themselves objects and you can add in as many of those as you like okay back in our editor let's paste that JSON end with no ads tidy up the formatting and push it over to the right-hand plane so we can navigate and you can see our paid and personal object only has four attributes I took out as I said but you can actually in this editor you can actually just go and put them back in so let's do that just to show the attribute can pull it up there we go test the formatting not the good one to do that yet to the back in hierarchical format Academy you can see up tails not get to six attributes no at address I took one out the pull scored so you can see your next of the dress object only has two attributes we can put that in pull scored back in tell you the other formatting push it or to the right and you can see on address object now has three object Knollys here's that are a is get to array items in it we can put a third one in let's put in the work phone number just to prove the point let's say that I find really useful especially when you're working with like large Jason strings it really is invaluable there you go so we get three of II elements now okay let's move on and then just to wrap up I think if memory serves me correct before we go into the cording what are all excuse me what are our alternatives to Jason again a bit a tongue twister Leah XML I'm sure you've heard of that expense for market language I think that was probably learned before Jason this is you know what I was just leaving uni it was the big thing and it was going to solve world hunger never quite transpired that way and I feel just you know that Jason has kind of possibly overtaken it now especially with the the real uptake of RESTful API is they tend to use Jason's the off data transmission protocol yeah more yet another markup language yeah more I've really only come across in terms of it being used as I can regulation final doing a bit of reading I don't know that much about it I believe it's a superset of Jason but again if you want to read up more than that go for your life I'm not going to cover it here after this one I'm there's a bit of our there what's the word I'm looking for contentious one HTML it's not really a data transmission you know alternative to Jason Jason's really in the XML used to transmit data object data hTML is really used to transfer off in HTML pages but theoretically probably use it to transfer data as well but really see any reason why not and then CSV comma separated values look it's probably not going to be used when you were then you would choose to use JSON XML so it's probably not really an alternative but I put that they are just as are kind of useful positioner because I'm sure most people have come across CSV enemy like say let's start coding though okay over to Windows and then let's start Visual Studio good old Visual Studio and okay let's start a new windows project and just plug it Windows farms project windows forms application [Music] just choose where we're going to save it and we'll give it a name so again if you've not watched any of my tutorials before I do go through from the cradle to the grave as at where I do all from scratch so for some of you that might be incredibly painful to watch but that's how I like to do it I just find myself and I'm following tutorials the Astrium a lot of knowledge and sometimes the missteps and it can be quite frustrating and for me it's not the point over tutorials anyway so you should be familiar with this let's just give our form let's rename the let's rename the header to G's and parcel and we're basically going to refer to our a wireframe screen mock but you saw started the tutorial I do genuinely refer to it it's not just something that I do for you know shits and giggles I actually do refer to I find it really useful I think more visually I find and I find them quite helpful so the top good box is going to host a multi-line text box but I just like to put it in a grip box because you can kind of give it a title and contain title in this case raw Jason Ross serialized string and that this will hold the text box we will paste our serialized jason basically string data but before we do that let's add our button or deserialize button give it a name again I start all my buttons of CMD standing foot command and then we'll add a second button we are able basically just clear our second text box when the witches with our open will get spilled too and I just found its nicest to have a button to clear stuff that peels they are rather second their group Vox again a whole dollar deserialized string or objectified json string where the magic is going to happen and we just change the text settings to reflect what it's going to contain our debug output let's just see if that is wild up correctly another things anchored appropriately is and it is that's good so let's let's add our text boxes now and we'll set them to multi-line and we'll give them vertical scroll bars I don't know what is about horizontal scroll bars I just don't I don't like them I don't like text scrolling horizontally probably 70 you're saying that but I just find that I'm losing it you know I think I'm losing it losing my mind maybe and we'll just make sure that's anchored appropriately to the grip box sorry I clicked everything within the grip box and then we'll add our second text box as well and do exactly the same with that this this text box will hold our little hold on output yeah you just come back to where horizontally scrolling text even in notepad I always have two ton word wrap on that's the first thing I do when I build a new Windows PC because it well it used to be anyway what that was hot turned on by default absolutely hate that first little problems I guess so let's see if that's what no I didn't anchor the hidden anchor the text box so let's anchor the text box absolutely if I got that okay looking good let's line up our event handlers for our buttons I like using regions just I can I keep called tidy in a project and the size it's probably a bit of overkill but it's just a good habit to get into if you've worked on some larger code bases the more things like regions the better in my opinion assuming you they're assuming your name them appropriately and given a meaningful name any good things accordingly otherwise they are counterproductive and we will do the same of our deserialize buttons and we will just basically M put some text into our debug window and then we can test that the both buttons are wired up correctly under the clear function works let's clip something in yep cool and it is it sports them down but what you can see it's happening it's just basically writing over so it's not scrolling down it's just yeah it's copying the string in but it's just setting it over itself so let's rectify that I've got a function that I like to use for those of you again I've watched my videos previously you'll be familiar with this function it basically just takes a string and it just scrolls the debug output window and at the same time at rates and string text to the visual studios output window it's fairly simple fairly simple it's incredibly simple but I just find I use it all the time so it takes a string okay now I just like to put it in a try-catch block I don't think it's ever thrown an exception so it's probably a bit of overkill but you never know so in the case where it throws an exception all all the others just then just right out to visual studio sounds Lindell the debug window and we'll just write out what the the exception message wasn't move on with our lives shall we okay there are the new lines because that's the kind of nice people that we are okay so onto the meat and gravy of the actual function itself again we'll just write out whatever string we pass into the debug window just good practice analysis kind of where the magic happens we'll then an update our debug text values to what it was previously plus the new strings and then basically all be then done in your line as well and then all they actually do is just then do a bit of scrolling we just scroll the carrot to the end first of all yes we should get the length of the of a text string and then we just basically scroll of cat to the end okay so let's see the magic walking up we didn't wire up diddly okay let's I always do that I'll write a function and then I got wired up to the button so let's let's make that happen and we're almost there people were all mostly up I've got even got to Jason yet but again you'll thank me in the long run up they want to do that let's see if it works okay yep it was that exciting wasn't that I can imagine you all jumping around in a similar fashion I know I was now that's all wild up no I'm already know to install Jason dotnet okay so go into tools and Visual Studio NuGet package manager manage new get packages for a solution though this is the graphical user interface way to install just go to Browns to see the packages available and there's Jason dotnet right at the top because it's so very popular to install it this we just click on that and follow the instructions it's very simple and alternative is to use the command line do you do exactly the same thing so it's just really a matter of preference and to do that you would type install - package and end it excuse me the name of the package and in this instance it's mutant soft dot Jason how do I know that well yeah Bravo Google reveals all and of course you not know because you've watched this video so just to enter if I can find Mike Russell there we go and that's installed so it just basically installs the package in the ads are reference into our project preferences and we can start using the framework now to deserialize jason okay so let's look at the api that nice and soft provides for us it's really expensive but it can still be a bit confusing so just google json dotnet deserialize and in my internet connection wakes up it will take us to that particular page actually tells really deserializing object and that's effectively what we're going to do in our tutorial but to make it a bit clearer let's just go to the json converts function that we're going to use the DC eli's object function we're going to do they're going to use and basically what it does the one we're going to use is it takes a c-sharp defined class type and it takes ring adjacent string a raw JSON string and it basically DCD Eliza's that string into the object that you specify so if we go back to this example we have an account class a proper C sharp object and then this call here is going to basically take that template and deserialize or raw JSON string against that class and convert it to that and that's effectively what we are going to do okay but let's take a little step back and we'll build up using our JSON examples and we're going to do a bit of a cheat as well to begin with or not even going to provide a c-sharp defined class we're going to cheat a bit and use another option and then we are going to use proper c-sharp classes which is the tricky part actually defining the class templates effectively is the tricky part or well the tricky part that I found enemy and it may be that the quick shortcut that we use in this first bit here may be enough for you to stop watching this video and go away and do what you need to do it depends on your preference anyway let's say let's rate up on DC DL is wrapper function wrapper you know 50 cent et cetera it's basically very simple it's just going to take a raw JSON string and deserialize it or it's going to write an exception and I have to say the the exception handling or that the exception handling the error messaging that this framework gives you is really really good it's very verbose and it tells you exactly what's gone wrong it's really really good so we're just going to make a call to the JSON convert deserialize object method call that showed you just that previously that fails then it will throw an exception and we're just going to write out what that exception is so first off we're going to just create available it's not you know it's not cast to anything it's just one unassigned variable we'll call it J palsun jason pelson oh I thought I forget let's make our using statement or new framework you'll have to do this but it just cut so a lot of typing and it gives us all the all the case and dotnet stuff straight off the back of her having to my mutant soft Jason and here's Oh Jason convert tells you know what it does DCD lies what it does now this is where the magic happens we are going to not give a specified class we are going to use a dynamic placeholder and that basically says as it says there it's resolved at one time okay so we're not passing in a class of any kind and then we're just going to pass in our raw JSON string what does all that mean I hear you cry well let's just let's just write out what we think our object is if you try to DC lies it using this method okay so what we've done we've created an empty variable G person that's not any type of any kind it's just a variable we're using a dynamic option of the deserialize object method call just to take a raw JSON string and find basically what does is it creates a Parsi sharp object based on the jason it creates one on the fly for us dynamically and it's resolved at one time and that may be all that you need to do and you can measure so you can walk off now then and that's you done I wouldn't follow this approach but again it's a simplest approach and I'll just show you it working so let's just let's just show you it working so let's get out Jason this is my North's pad plus plus if I can find it on my second screen with me all right so what we'll do is we'll actually go into Jason online editor I'll just paste the jason then just to check it for correctness we don't actually need to do this to be honest with you but I just want to show you so that's the Jason we're going to give our DC eliezer you can see there's an error we'll fix the error here yeah so that's all good to go all right copy that paste it in here raw Jason just a string D serialize it there you go this DC that works you go over a big deal but basically what it's done is it's actually taking that string and can belted it into an actual C sharp object and the beauty of is we can place then any JSON down into that window assuming it's syntactically correct assuming it's okay it that deserialize function will just convert it into a JSON object for us it's very very powerful and we don't even really need to do the error checking because again if you put in a bad string that one's okay but if you did put in a bad string which we will do in a minute the DCD allowable actually was an ad off a very verbose very descriptive error it's really really great so again your polygon I'm not I still don't really get what we've done here so what we'll actually do is we will output just an individual value of an attribute let's just do that and this is the power this is the power of using an object-oriented notation using a proper object so there's our G person you will notice that there's no one tell a sense drop down it's the compiler is just going to take in our word for it it's not throwing up an error but it's not giving us any help either let's see if it works you should do because it's correct so deserialize the object and it's brought out that attribute follows we've used our object notation to peel out that individual attribute value so let's do another one just to label the point we'll pull out an address attribute and it uses again object-oriented notation there's just one of you know nested eight and deeper again you'll notice it's you know the visual studio environment isn't really giving us any you know intellisense drop-down options that you would usually expect it's just taking our word for it because we're using the dynamic way of deserializing it doesn't know what the object is at this point that only knows at one time and assuming you get the attribute names right that match the name of the json string as we have done here that pulls out correctly deserialized or string what accessing it in an object-oriented to be let's just try that have you get nothing doesn't give us an adder but it just says well you know the suit wasn't an attribute of that name wasn't in the string there's nothing there okay so that may be enough for you maybe you can go for now and do something else but for the rest of you let's let's take a peek back at this the example specifies that you actually should use can use should use a proper c-sharp class as a template it's almost saying here's my c-sharp class I expect when we DC Eli's this jason string that it's going to adhere to this class and when you do that you then have all the intellisense dropdown and all the good stuff that goes with classes so there's clear a very simple class just to hold our first example jason it's just paste it in here as commentary just so we know what it is we need to build them okay so that's obviously not part of our core that's just so we have the Chordata have the jason on the string imma be what we're going to do now is create a c-sharp strongly-typed class as per the action using proper datatypes all that kind of stuff so let's start coding up our c-sharp class or public string first name and it's important to note that the attribute names in our c-sharp class person class should match the attribute name of the jason that we're expecting it's quite important that they match so again public string will do last name in the same way it's just a string and we'll do the same for our age and is a live attribute but we'll change the data type so this again this method you're creating a proper c-sharp class or giving it proper datatypes strongly typed all that kind of stuff all that good stuff that you want my object oriented programming is available to us using this method but again it depends what you're needing to do may be the other the other method is is preferable to you let's just get rid of that for the moment because the chords going to self documenting them builds okay I would expect it should this all what we will do is we'll make a call to do actually leave that the edge one I'll leave that there so you can have a bit of a comparison now basically just copy the jollity record we'll take out the dynamic keyboard and we'll actually put in our proper typed class you probably should change the variable to that class as well but let's just leave that just now let's make the call and just print the object see what happens here's our Jason mmm interesting so not an error but it just prints the actual name of the object it doesn't actually print the contents like the last time and I suspect that something to do with me the new some soft guys have implemented the yeah implemented that method on their objects so not necessary and a super Waldo's will actually drill down into our attributes and there's the difference okay because we use the strongly typed c-sharp class we have access to those attributes and tailor since drop them as you would expect because we were telling it ed so it's a particular type let's paste our JSON then there we go Roger is redundant interrogated the object you know Visual Studio is going to you know knows what the object is and that's why we got all the intellisense dropped and stuff let's just go to that line and actually we use the cord we used previously it should be exactly the same and indeed it is cool what happens for me do this now should it work or doesn't why not because we're trying to access an address object that does not exist in our class yet we've told DC lis method that we are passing out or we expect to deserialize simple JSON calcine we are trying to access an attribute that does not exist in that schema what finds are dynamically could basically throw anything at that because it's resolved at one thing so let's resolve that now let's create another class and what we'll do is we'll just copy all this stuff over I'm not using inheritance and a lot of you're going to be freaking out right now what is inheritance oh you see well I should be using it and I'm not ideal but I will don't worry I will come back and correct it so basically this is a totally separate classes based on our previous class was all the same previous attributes but now we are defining another class within it on address class let's just paste them the end of Jason just so we have a reference okay so we want to create as you can see they were creating a another class but they're not existing class with the attributes in the same way and again we're copying attribute names exactly over all strings okay so you should be starting to get the hang of this no so I found this the hardest thing when I was using this was what do our classes look like in relation to the Jason that we are going to get in order that we can deserialize them correctly and I suspect it's what slips up a lot of people the dynamic option is good that probably doesn't quite develop your understanding enough so there's our address class with those three attributes okay and then what we actually need to remember to do is add our top level a class attribute of type addr but the attribute name has to be exactly the same as adjacent in this case address so fifth object attribute address of type addr let's get rid of that don't need it anymore code self-documenting well okay get rid of that just check the jason for completeness yeah that's okay looks good and let's see if we can look I got rid of that that's good of it little dove solution okay and what we'll do is we will now try and deserialize some jason using that actual object of it just created so we'll just create another one of these lines just again so you can compare with what we did previously the path one should be emerging but here we go or just replacing the class with the Jason Carson complex class let's paste the look didn't want to do that I didn't want to do that either illegal the DCD laser doesn't like it it's working to some extent I thought I'd copied that text let me paste then okay here we go let's see if he can DC Eliza yep there we go so we're getting Roger back let's just uncomment that line that wasn't working previously give us a street address I will just show you the eagle so you can see we have our address attribute with the C child attributes and that that's really the whole point of why we're doing this you know build it and paste the JSON in there we go and we're getting back the address street address form using a strongly typed class and passing it to the DC Eliza so now let's rename our dynamic method we'll run out the line you just created and we'll use the dynamic approach just test in the fat Jason yep it all still works cool using the dynamic approach and what we'll do is copy in our most complex JSON object the theory structure and that and I'll see if the dynamic approach can handle it there you go those are Aviv phone numbers and it does that just you know it prints all out but how do we actually get to those attributes individually like like we have done with the street address unlike we have with the first name well we have to iterate through the array using a form lip basically and let's just start that code now so we're just seeing that we're going to attempt to cycle through our phone numbers and then what again our for loop now again as we're using the dynamic approach we are there's nothing strongly typed and this is why I like that actually providing classes because I find that you're flying a bit blind in the dynamic approach you don't you're not really getting that helping visual studio which is one of the main reasons I like it so much I don't want other developers like using taxpayer 2000 having absolutely zero help I don't get that I need all the help I can get ok so yes we're going to be printing out the phone number and the phone number type [Music] just check yep what's my notepad behind-the-scenes notepad let me look in online editor paste that Jason and just to see the it okay so it's type and number ta okay so again you've got to get those attribute names exactly correct and you're not because you've not got a class with those associated classes defined you have to kind of make sure that it's right in the chord because you're not getting that help or that validation in the code if you're doing it this way but hey it works let's just run that pistol Jason in there we go so using the dynamic method we're posting in all most complex Jason structure and we're pulling an individual a phone number object attributes and naari now as before we're going to comment out our dynamic M line and use of the dynamic keyword and we're going to actually once again create our third and final class jason personally and it's almost complex class that we've created and are basically again are not using inheritance just yet we're just going to copy an entire cord from our previous most complex class and expand upon it okay and i'm doing this just to keep keep the code more readable and so you can see how it's developing over time so again in a similar way that we created the address subclass for now creating a phone number subclass a full nun subclass and we're giving it to and attributes type and number both of which are strings and again like with the address we're going to have to add it as an actual attribute of our main adjacent person every class so we have six class attributes in total just checking let's collect okay so here we go let's put it in as our sixth attribute until for none and the add natural a treat name is four numbers so the type is for none the name of the attributes four numbers again it's got to match exactly it's the same pattern that you've seen before nothing new nothing new to see he'll move along great great all done looking good my phone's just thinking I'm speaking to so let's say that's what that weird noise was so let's make a third and final actual call using that class and deserialize json against that class and we should be able to reuse the cord of already created for our dynamic the dynamic approach that we used but we're getting an error why is that to be not define something correctly so let's just go back here yep so that is the phone number attribute and a G person object but it's not liking it what's what's wrong what do we have to do in our class to make it work it's a very very simple thing all we actually have to do is make this enumerable type and I'm just choosing a list in this case so that's effectively telling us it's an array of phone numbers like a PC here it's no re the address was a single nested object but the phone number four numbers are an array of objects I'm not sure what limit would be in Jason that's an interesting question to ask so let's just paste in or jason and see how we get on okay we have success okay so I did the promise a bit about inheritance it's not really what this tutorial is about but it's really important and I just thought I wouldn't want to leave the example as I have without at least mentioning it now if you know about it then obviously this is rather pointless if you don't know about it the concept of inheritance is quite simple so what you see on your screen at the moment is how we have implemented our software so forget about the address class and the phone number class for a moment but just think about our three main classes that we created the first one we created at four attributes and then the second one we created was effectively identical to that but it introduced this additional attribute but we copied all the attributes from the first one into the second one and and we repeated the same pattern for the third class it basically had all exactly the same attributes as the previous class and the class before that Jason Carson simple but introduced progressively more and more attributes now that was okay and it does work but what's the problem with that well number one it goes against the kind of concepts of inheritance in object-oriented programming it's kind of a self-fulfilling thing but the practical reason or one of the practical reasons why you would choose not to do this as say for example the JSON string that we are now going to expect to receive the vendor and that provides adjacent to us has decided to combine first name and last name into just a single attribute full name we would then in our software implementation have to change three of our classes in three different places to reflect that change which number one it's a lot of what number two it's prone to error blah blah blah it's just not a good design okay so with inheritance inheritance actually care solves that problem which is what you're seeing on your screen now this is the model if we use inheritance okay so thanks for watching good night now I'm just kidding so yeah this is the model that we would use if we're going to use we're going to implement this and a couple seconds just looks a lot simple does it not yes is and basically what you'll see is we have the same three classes JSON person simple in fact the JSON person simple class is exactly the same that does not change that's what is referred to as our base class what were then going to do and what the ADA was kind of denoting is that our next class the decent person complex class will inherit everything from its Paden from the base class and it can then introduce its one attributes it can introduce address it could introduce anything else likes but the the benefit of that is that we don't have to copy the chords and this was technically it's sort of being copied but it's being inherited really it exists in one place and it's basically referenced I suppose that's a better way of describing it and then when we come on to doing the Jason personally object it's exactly the same concept we just need to inherit from parent class which in tana inherits from the base class so for Jason personally we don't have to inherit twice if you know what I mean we don't have to inherit from Jason Pearson complex and then do a separate inheritance if you like from jason person simple the inheritance is implied all the way up the chain so you only have to inherit from one place and you get everything that has gone before so inheritance is actually a great town for it over decided that was the term that they would use for this concept so yeah let's go on and implement that in our solution map okay so here's our first class very familiar I tell you the second class should I say let's don't remove it we will remark out this four attributes that we're going to inherit leaving us just in this class leaving as just the new implementation so how do we inherit very simple call on and then a name of the class will you'll inherit in from it's not easy so basically seeing this class complex class is inheriting everything from a simple class and we can even peek at the implementation to see what we're going to pull in and bathe in mind that those attributes are remark code so it's just Colin tree I've just left in there for readability and we only have to implement the new stuff and then similarly with our babies class we can take out everything that's gone before we can just remark that out and we only need to keep n include the new stuff which is basically a phone number attribute and the associated class then we just rent that out okay and it's just gonna do stuff that we need to keep it and again we just inherit from the complex classes thing you could actually inherit from the simple class but would mean we wouldn't get the address stuff so you couldn't you couldn't head it from that other class as well but you probably would want to do that so let's just test that out now so we're still using our complex as I got a rebase class and it's just basically showing you that because we've inherited it's still gonna work as it did before it should not change he says yeah there we go so it's all working fine it's just what we wanted okay so just to wrap up a little bit on error handling capabilities so here's a simplest JSON object without error comma instead of a call on and then on line a that's all you get nice football saddles what happens when we run it through our cord and fix up there but we won't fix up here what do we get okay we get quite a nice error so let's play good let's fix it and see what we get so began other day also the Jason is boots correct syntactically it's fine why are we getting an error well it's quite simple in our cord we are trying to access an address element with an adjacent which just simply doesn't exist so all the JSON we supplied was syntactically correct and our classes correctly defined sometimes you may do all that stuff but and it's happened to me you'll get a JSON payload without certain attributes because I can happen especially if it's coming from a third party you can't control what data you're going to get so if we run this through our DCD lizard again that particular error should pass let's do that it does but we're getting another one because again in our cord we're trying to access our via four numbers which we're not providing either so if we provide that JSON which will do it will run fine there you go it will let's try to do that so one thing here is you might have defined your classes correctly make some sample jason or works great and this often happens when you're testing stuff at work scree soon as you put it into production you start getting random edge cases yeah so that that works okay so it's just a bit of a gotcha even though you're Jason is well-formed and your classes are perfectly but you sometimes have to test to make sure that you're actually getting values returned back right so what at the end of another video I really appreciate you taking the time to watch and we have time is valuable so I do appreciate it and just showing you my website on my blog that's just a wordpress blog binary fissile or one one com so on the screen there and basically for every video that I do I always put a blog post up enough quite detailed that has the code as the diagrams has some more detailed explanations and some of my terrible jokes and sense of humor up there so please yeah stop by have a look leave a comment or leave a comment on the YouTube channel always love to hear and feedback it's it's great and it gives me ideas for my next video so when thank you again I'll see you soon you
Info
Channel: Les Jackson
Views: 136,604
Rating: undefined out of 5
Keywords: binarythistle, deserialize, deserialize json, deserialize json c#, deserialize json.net, newtonsoft, deserialise, deserialise json, json c#, step by step, step by step tutorial, json.net tutorial
Id: CjoAYslTKX0
Channel Id: undefined
Length: 56min 20sec (3380 seconds)
Published: Tue Jul 25 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.