instead of trying to wrap your head around how it works under the hood, focus on understanding the concept behind serialization. (abstraction! use it!)
here's a simple example in pseudocode.
Code
public Foo {
public Foo () {
// some code here
}
public void doStuff() {
// some code here
}
public void doOtherStuff {
// some code here
}
public File serialize() {
// serializes this Foo into a File and returns the handle to the file
}
public void deserialize(File f) {
// deserializes a File into this
}
}
The contract for serialize and deserialize here is that the behavior of an object that was serialized to a file should be identical to the behavior of an object that was deserialized from the same file. How it's actually done, whether the pointers are the same, where the state is being stored in memory -- is entirely irrelevant. All that ultimately matters is that the behavior of the methods like doStuff() and doOtherStuff() are consistent.
A simple, real-world analogy is that if I have a car that I drive and I tell you to make another one just like it -- it doesn't matter that you make another one with completely different parts under the hood, with completely different connections. If as the driver of the car, I can't tell the difference between the car you made and the car I have, then it really doesn't matter how the second car was actually built.
Also, remember that if you're the implementer of the serialization/deserialization methods, you have complete control over how it's done. You don't have to replay the 200 words that got inputted in order to build a tree. You could, for example, write your serialization method to simply write out the mappings themselves, but you'd have to make sure that the deserialization code you wrote expects that as the input.
In a lot of ways, the fact that serialization/deserialization ultimately writes to some sort of persistent storage is irrelevant. They're just like any other functions that have contracts that depend on each other. For example, if you had code that looked like this:
Code
public B convertAtoB(A a) { }
public A convertBtoA(B b) { }
It ultimately doesn't matter what you do to get from B to A and to get from A to B. All that matters is that convertBtoA(convertAtoB(someA)) returns an A that is behaviorally identical to someA.
This post was edited by irimi on Nov 9 2012 03:37am