Using JSON IN C#! Serialization & Deserialization made easy!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey and welcome to tutorial.cu my name is Yannick and in this video I'm going to show you how you can see realize and deserialize.net objects to and from Json [Music] here I have a console application in.net 7 and I've simply created a class called purchase inside here we got three properties product name date time and the product price just some basic information well to build up that object then we got our program and inside here I just created a new purchase object someone bought orange juice at well right now and the product price is 2.49 whatever dollar euro whatever you can think of so if we now want to bring that purchase into or that object purchase right if we now want to bring that into Json format which means JavaScript object notation we can use the system.txt.json namespace so let's start using system dot text dot Json now inside there we got a lot of apis which we can use to serialize and deserialize objects serialization in that situation means that we bring a.net object like our purchase object here into Json format and deserialization means that we take plain Json and bring that back into a.net object okay so serialization is 2 Json and DC realization is from json2.net object so let's get started and let's bring that purchase object to Json format so Json format is typically string right so let's say this is a Json string or you can call it like purchase Json or whatever you want to name it and then we take the Json serializer that's the API though that's the object that we want to use to serialize and deserialize and you can now already see that we got some methods suggested here for us interesting uh or right now we want to use the serialize method and we can simply open up the parentheses and add the.net object that we want to serialize to Json now in that case I want to take the purchase object for sure and then I want to add a breakpoint right here and start the application just to see the actual output or the actual value of that Json string awesome so our breakpoint now stop the application so let's simply hover above Json string let me open it up here let me take a look at the Json visualizer well it doesn't matter if you take a look at the XML HTML or Json visualizer just open one of them and you can see our object now displayed in Json so we got the product name orange juice daytime right and the product price and everything here is like nicely written down right you can read it easily now if we simply hover above that value you can see that string where it's quite long and if we take a look at the HTML visualizer you can see that well real Json string so it starts with the curly brace and all the properties inside of the Json object I've written down here and the values assigned by a colon so product name is orange juice daytime make sure you can see that it's comma separated right date time and all of that so here we have no pretty print which actually means that it's a little bit harder to read because it's not nicely indented awesome anyways we now have converted our.net object into a string and that's called serialization for sure we can deserialize that but first of all let's take a look at some additional options that you can use for serialization now just a quick reminder for you make sure to subscribe to our channel so that you no longer miss any upcoming.net related videos because as I always say you are interested in becoming a better developer and we can help you with that by delivering awesome videos and if you're interested in some specific.net content then you have questions write them down into the comment section below we are happy to create any kind of video just for you as long as it is c-sharp.net related oh and if you are a c-shock developer and you want to land your first job as a developer or earn more money as a developer check out our c-sharp progress Academy it turns you into a full stack web developer by learning asp.net and angular restful API development Entity framework in SQL software architecture and unit testing and software design patterns in the end after completing this self-paced online course you are a job ready expert so go ahead and take a look at our c-sharp progress Academy you can find the link in the description below or popping up in the top right corner right now okay so let's add some additional options here first of all we can use the serialize method and also specify type so that you already know what type you want to serialize in that case just add the type braces here and add the object you can see like it's not really necessary and it can be simplified but it's important that you know that you can do that now also we can create an options object of type Json serializer options so just for to make it as easy as possible I will use VAR right here let's call it options equals to new Json serializer options you can see that right here now let's just create that and now let's configure it I want to show you that you have a lot of options here so options Dot case sensitive property case sensitive or case property name case insensitive is something that's quite important it really depends like if you later on talk to an API to upload Json to download Json to download any kind of data and turning your Json information to any kind of C sharp object it might be that an external API is using a naming convention that well does not make sense in c-sharp and in that say and in that case you can just simply enable that flag where you say property name case sense where you say property name is case insensitive so let's say in the API the property price inside the Json is written in lowercase whereas in C sharp the property would start with a capital letter and in that scenario you would not be able to successfully deserialize or serialize so you want to set the name case insensitive to true we don't want to do that now but it's important that you know that you can set this option now what we want to do is we want to activate write indented so let's see that right here by default Json serialized without any extra white space right so this is like shorter it saves bandwidth so there are less bytes to send that's great but if you want to read your response just want to show it to you if you want to have that pretty printed right we want to set that to true just as an example now we can put that options object into our serialize method as the second parameter just put it in here and now let's start it again we still get the break point now I want to show you what that in dented is doing so let me just close that here you can now see breakpoint stopped so hover above Json string again click on view and then text visualizer you can now see that this is definitely way more readable right you got that nicely indented we got white spaces and yes this is bigger so there are more bytes so for production version I would remove that but if you're like if you're interested in debugging or you need that pre-printed whysoever go ahead you can activate that and now you can read it very easily anyway if we open up the Json string visualizer here we can explore the Json data doesn't matter if it's indented or not so as I said for production go ahead remove the white spaces don't make it indented because you want to save bandwidth but I just want to show you that serializer options now one of the next things to even increase the performance a little bit more to speed up everything we cannot only put it into like plain Json we can also format it into utf-8 and in that way just stop the application we call serialize you can see let's just scroll through that here serialize to UTF 8 bytes so let's call that let's remove our type here let's also remove the options and let's change the type to buy it to byte array or to VAR as you like but just for making it easier to show I want to switch it to byte array now so let me just start the application again and as I said I will just revert that in a second let's take a look at the Json string now you can see we got 92 bytes so let's open that up you can see here's our information let's click on view and you can see we only have an ie numberable visualizer why because we have an array here with really some plain 8 byte data so as you can tell this is definitely shorter and has less bytes in the entire size than the readable Json right so keep that in mind this is serialized faster so now we have our Json formation non-readable for like for the simple human eye right um and yeah it's faster so it gets serialized faster so if performance matters for your application make sure that you can serialize and deserialize from byte arrays awesome now that said let's just revert that because for our next example it's important that we get to Json string again so let's just add the options and let's just switch back to our serialize method here and let's specify the type awesome so now I got that pretty printed Json information right and now I want to save it into a file so that we can switch over to deserialization to the second part of this video and also let me remind you again if you like this video give it a thumb up it really helps our Channel and we love to create.net content for you so please smash that like button right now so now let's write that file let's simply call file dot write all text because it's a string right now let's provide a name and it's simply call it purchase.json make sure to add that file extension right dot Json and now let's provide as data to write Json string this is what you'd get written inside the file so let's save it let's just start the application again and that should create a purchase dot Json file including our Json serialized object great so let's close the application here that stops IT and if I open up that program folder and I move into the binary debug.net7 I can now see the purchase.json great so I opened it up in the background already so we got product name orange juice daytime and all of that so this is our object saved into Json so we saved a.net object in Json that's awesome and now we can switch it around and read that object right so what we want to do inside of program.cs now let's just assume I will remove it now just to make it really sure we don't have a purchase object anymore okay but we still have that class and that's important you need to know to which type you want to deserialize in our scenario right here so this is Json serializer options so we don't really need that now we can simply remove nearly everything here but I want to keep that right all text for now so that I am able to still see the path so for this serialization we simply turn it around so first of all we want to read our Json file so let's say VAR purchase Json for example let's say equals to fire dot read all text right read all text and we simply have to provide the path and then we'll get all the information and since we know that we have plain Json inside of that document our purchase Json will be that information now what we can do is we can create an instance of our purchase object now let's say purchase let's again create a purchase object and this time it is our Json serializer.d serialize now you have to provide a type we want to deserialize the Json into a purchase object so if that data here that structure is not matching our class we will not be able to successfully deserialize anyways now we can add the parenthesis and add the Json string that we want to convert from or deserialize from and again let me add a breakpoint and start the application and it should definitely work out fine now as you can see here if I have above that you can now see that I have a dotnet object right so this is not related to any kind of Jades now it's a real.net object you can see like that date times opening up but here we get the product name we get the product price you get the daytime and all of that set correctly so it's saved here forever now right until I delete the file and we can read it from here and create a.net object and this is something that you really want to do a lot of times if you're working for example with any kind of nosql database or if you want to save any kind of settings or yeah well Json is really common in web development awesome now if you serialized into a utf-8 array by the array and you want to deserialize from that you will first have to create a utf-8 Json reader put in the byte array information so you can simplify read all bytes and provide that reader inside of the D serialized method right here anyway the concept stays the same so yeah I hope you liked that video and I hope you learned something new definitely check out our C sharp progress Academy if you're serious about your c-sharp developer career make sure to subscribe to our Channel and I see you back in the next video thanks for watching foreign [Music]
Info
Channel: tutorialsEU - C#
Views: 18,453
Rating: undefined out of 5
Keywords: Tutorials, Tutorial, Programming, Course, Learn, Step by step, guide, development, programmer, video course, video tutorial, learn how to, how to, c#, .net, .net core, dotnet, core, code, asp, asp net, c sharp, coding, csharp, asp.net core, asp.net, programming, json, deserialization, c# serialization and deserialization, serialization, c# binary formatter, deserialize json, serialization and deserialization in c#, c# json serialization and deserialization, json deserialization, json c# tutorial
Id: w6M-Bj-tfv4
Channel Id: undefined
Length: 14min 47sec (887 seconds)
Published: Wed Jan 25 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.