Serialization - A Crash Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this is your six one two TV welcome aboard whole YouTube now today we're gonna do another one of those episodes where we go really in depth into one programming concept I know maybe this kind of video doesn't work for everyone who subscribe to this channel but bear with me for those of you who are into programming well the concepts discussed here and things you'll be using in your everyday programming life but there might be a time where you actually meet this stuff so why are we talking about today well today we're going to take a look at what is known as serialization and what I'm going to do is I'm going to try and break this down to be as basic as I possibly can so in programming we tend to have these things called data structures they can be as simple as arrays which is just a collection of information in a list like form or it can end up with really complicated things like nested arrays arrays in arrays or things like dictionaries we have an attribute and a value in passed so that's all well and good right while we are actually writing the code we can always find a way to actually read and access these individual pieces of information the difficulty doesn't come about here the difficulty comes about when one a I don't know maybe save this to a file or transmit it somehow and here's why there's a problem now when you actually have your data structure while running your program essentially what happens is all this information is loaded into memory so let's take for example this particular array now you're expected to be stored in memory maybe something like this and then if you want to look at the first element all I have to do is save this and your computer knows where to find this information but then the problem comes about when I actually want to send this entire rate someone else I have to somehow translate this into some information that can be sent sequentially over a connection that is our key problem and you can imagine that for this the solution is probably pretty trivial but what we do then for something more complicated and this leads us in very nicely to talking about basic serialization so let's go back to the example that we just saw now I'm sure you can have some ideas about your how you want actually send this over connection and the solution is pretty simple I have to do is say okay the first element contains this then the second element contains this and so on and in fact that is exactly the approach to serialization and its most basic a civilization function will take in your data structure and sort of flatten it out into a string the string can then be sent sequentially over a connection and the recipient will then be serialize it and reconstruct an object that in a nutshell is the entire process for serialization so what about more complicated data structures I mean let's say we want to serialize a data structure which looks like this in the memory how do we start we definitely couldn't do it by eye because we have no clue how the individual items are related to each other instead we're going to have to trace all the dependencies and form a little hierarchy before we can even get started on a serialization so let's try this by first inspecting these tree lines we know that our data structure has three main fields the first few links us to a name so we simply fill it in the second few links us to a number however this number is part of a greater collection therefore we have to trace this to completion and note it all down as values belonging to the phone numbers field the third field links us to another data structure which in turn has its own tree fields we know these fields down and trace them further to get their values we can only begin converting everything to a string after we are done with this luckily for us once we have things at this stage the process is straightforward I'm now going to flatten this all out into a single string by simply reading the structure from top to bottom every time there is a nested structure I denote it with additional brackets the square brackets denote an array which is a simple list of items whereas the curly braces denote a dictionary where all entries are named in fact the syntax I'm using here is a formal syntax called JSON or in this later in the episode so when you're done with your serialization you realize that the string you've produced is now very complicated and ugly and it contains all these different nested arrays now it doesn't matter a whole lot if your serialize string isn't very readable gets ultimately at the be serialization and all you have to do is to use the same approach just in a reverse autumn and you will be able to construct your original data structure back and that in a nutshell is the concept behind serialization so throughout the duration of all my explanations I have been using sending information over a line as an example obviously that's not the only application of civilization as mentioned earlier if you want to write a data structure to a file you're gonna have to do that as well or if you want to make a deep copy of a data structure that means you want to duplicate a data structure and all its contents into a new variable you can have to do serialization as well to extract all the pertinent data that is involved with this data structure so all right now that we've taken a look at the concepts behind serialization as well as some of its applications let's jump in to take a look at some popular representations of serialized data the first one we've already seen is called JSON and stands for JavaScript object notation you've seen both forms of it both expanded for readability as well as compacted into one line both these representations are valid and in fact represent the exact same data structure in JSON all elements are comma separated arrays are denoted with square brackets rest objects which contain attribute value paths are denoted by curly braces we can tell the depth of an embedded object by Counting how many unclosed brackets or braces exist before it basically all you're doing is you're using your curly braces to actually be notes the level of nesting of your various items other serialization formats include yml and XML the look of the result produced by these two serialization methods actually are closer to morph markup language kind of syntax yml is actually pretty interesting in a sense that the number of spaces behind every item is the information used to determine how much nesting is there and what belongs under what so this is one of the more unusual languages in which the number of spaces actually do matter XML will be more similar to JSON in the sense that whatever is nested will belong within a set of the limiters so it's clear what belongs under what so yeah these are just a few techniques that are often used for the purpose of serialization these are definitely not the only ways to do things in fact as long as you're able to somehow represent the contents of a data structure as a string and as long as this string can be deserialized back into a data structure then you've gotten the job done no matter how you do it anyway that basically wraps it up for this episode on serialization I hope it's not too dry apple you've learned something interesting from this but yeah that's pretty much all I have for you today until next time you're watching is your 6 months of TV hello thanks so much for watching hope you enjoyed this video and if you did remember that I teach it every like favorite and comment you give me if you like to see more from me in the future don't forget to subscribe to this channel and for more updates outside of youtube don't forget to follow me on twitter X or 6 1 2 TV once again thank you very much watching and I'll see you guys next time you
Info
Channel: 0612 TV w/ NERDfirst
Views: 89,044
Rating: 4.9507165 out of 5
Keywords: Serialization (File Format Genre), JSON, XML, YAML, Serialization, data structure, data, structure, representation
Id: uS37TujnLRw
Channel Id: undefined
Length: 8min 13sec (493 seconds)
Published: Tue May 20 2014
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.