Reading Invalid JSON with System.Text.Json

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] [Applause] welcome back everybody and welcome back James to system. tex. Json stj and everything you ever wanted to know about it okay James you know I know at least what we do everything is always 100% perfect oh absolutely but sometimes in the wide wide world the internet our Json that we get might might not be perfect there might be just some weird stuff in it yes often you know when you're dealing with Json you do expect it to be in the perfect format with the perfect casing and lineup exactly now we' talked about different casing and different missing things from the Json how to handle that but sometimes I've noticed that in Json people like to do wacky stuff put comments or maybe they I always put extra commas and things like that in it and that can be bad pretty much for parsing because again you they're expecting Perfection and when it's not perfect it blows up up yeah I mean I like putting comments I mean sometimes the comments definitely help yeah and but yeah you're right the parser is very strict yeah as you would imagine and I've seen before like where people add comments because the names of the Json uh properties are really crazy like w and a and a US Q or something I don't know what that means so they had a comment in there but system text Json has your cover stj stj let's go ahe and take a look so let's take a look at this Json here and we've been working with this weather data and there's a few things that actually Visual Studio code is telling us are kind of weird like comments are not permitted in Json and hey over here there's a trailing comma in the Json that's not good and this is still kind of somewhat valid Json right I mean you can tell what it wants to convey right it's like commenting my code and just whatever why would we make the Json feel bad yeah let's use it but there's a lot happening here right there's different types of comments with SL slash with SL star a multiple lines that are coming in here now let's just say I go to my program where I have this weather data and I say read all text from this file and deserialize it let me just go and F this puppy and see what happens because again we're expecting that this Json should just kind of ideally work out of the box yeah and no uh oh no it's not valid Json technically it's not valid Json yeah yeah yeah yeah but what if you can't control this right if you just have this file and it's coming from the internet you kind of get get around this you can't go back and say oh please person go change all of your Json and you definitely just can't manually check everything you know exactly maybe if you have an intern yeah it' be quite hard to like read this parse the string and then manipulate I mean just yeah exactly so you can ignore a bunch of this stuff which is kind of cool so we're going to head over back into the code and we're going to write some options here and our good friend new Json serialization op options will come to the rescue M that's the most amazing class in the world yes so first things first there's tons of stuff inside of here that we've talked about already in several videos and put them down here in the down at YouTube but the first one allow trailing commas check this out there it is true a trailing comma is this trailing comma this is a trailing comma there's nothing after right so it's invalid just hanging out trailing just hanging out now we can allow that but we can also say read comment handling uh and this has a disallow skip or allow option in here so this is basically saying you know what do you want it to do and there's different options here to documentation but we basically just want to skip it and say ignore it don't worry about it don't worry about it you it's all good so let's now go ahead and pass this in I'm just going to say options there we go and I'm just going to go ahead and run it again with F5 we have a little break point here so we can read this and take that Json string dilize it back and what do you know T Tada it's all there all of our Json stuff is there and we're totally good to go cool yeah pretty awesome cool cool yeah now go back to our Json all right so now now I've noticed a lot of in Json yeah sometimes like right here we have temperature Celsius 25 yes 25 super super oh it's actually Pleasant huh 77 fahrenheit anyways 25 a lot of times I've noticed though 25 might be in quotes like everything might be a string yes right so if I did something like this for example yeah we don't know we we would think that this is a string because it is kind of a string here but really we know that this is an integer right yeah yeah so uh system Tex Json has us covered as well as you would assume and what's important here in this case right when we're we're looking at the comments we're really just reading it and deserializing it but when when it comes to this you may need to put the data back onto the server right so you might actually need to read and write basically numbers as strings right and we can do that so I'm going to come back over I'm going to save this file so we have it here and um let's just change it so it's not 25 I'm going to change it to 30 right so it's like so now we know it's a different number so I'm playing games right I'm going go back into my program and we're going to add one more option down here which is number handling and there is a bunch in here it's specifically in system text Json serialization so I'm actually going to just bring that up here I'm going to say using system. text. json. serialization and we've been using this a lot with the serialization options like all those little attributes that's where those are so we have strict we have allow reading write as a string allow named Point floating literals right so if you're doing with floating points for example so n for example here what we want specifically is allow reading from string so we're going to allow it to read yeah inside of it right so um let's go ahead and F5 this again and now what we should see is the exact same thing is that we're saying hey this is a number we want to parse that down and sure enough there it goes there it is 30 yep now let's say I didn't have that right I should have just kind of run it in general so if I now hit F5 in this case where I don't have any handling at all um it's not going to know what necessarily to do because um it is a string but we've said in our class that it's an INT so it's invalid so it actually throw an exception here that it can't can't parse it so if we put that back right there now when we run it again it's going to say hey like in this case we'll ensure that you know we can parse those things um as an integer there gotcha all right now because a little bit curious what happens if in the Json It's actually an INT like we got rid of the quotes but we left the um allow reading from string oh yeah so you put this back here so just to make sure that either way works go there so 30 so again if I go back in now that I've saved that file hit F5 again it's going to know that it's a it's AOW reading right it's not it's not a must bear string so it's so now it's gonna know you're just being super safe don't trust the author of this of the string coming in totally so now if you did want to write it you could also pipe that and you could say um um write as string so you could say read and write so you could reuse this options as well so those that's what You' want to do there awesome there you have it easy peasy yeah so all that invalid Json out there whatever people are messing around in there system Json stj know it yeah that's cool that's cool that you can take I love the comments and sometimes you know you get a little funny stuff coming back with trailing commas or whatnot yeah I know that too there you go love it all right well that's going to do it for this system Tex Json video we have tons of videos here on theet channel and of course we'll link to all the documentation below if you like this video give it up thumbs up Jam that subscribe button ring that notification Bell come part of the notification Squad where you get updated every single time we put out a video that's going to do it for this episode of on.net I'm James I'm Matt and thanks for [Music] [Applause] watching
Info
Channel: dotnet
Views: 7,478
Rating: undefined out of 5
Keywords: .NET
Id: ZyLkKI2Ait4
Channel Id: undefined
Length: 8min 31sec (511 seconds)
Published: Wed Jan 03 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.