- Forward


Serialization
An Introduction


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Serialization
Back SMYC Forward
  • Defined:
    • The process of creating a "frozen" representation of a "live" (i.e., in-memory) object
  • Uses:
    • Copy objects
    • Provide persistence
    • Transport objects over a communications channel
The Process
Back SMYC Forward

A UML Statechart

serialization
A First Attempt
Back SMYC Forward
  • Serialization Redefined:
    • The creation of a series of bytes from a live object
  • A Natural Way to Proceed:
    • Concatenate the representations (either text or binary) of the various constituents of an object
Complications
Back SMYC Forward
  • Methods:
    • A "frozen" object must somehow contain both its properties and its behaviors and it is not immediately obvious how to represent the behaviors.
  • References:
    • The memory that is pointed to when the object is "frozen" may not even exist when the object is "thawed". Hence, the referenced object must be included in the "frozen" representation, not just the address.
    • It is not enough to just include the referenced object in the "frozen" representation. The references have to be managed intelligently.
Serialization of References
Back SMYC Forward
  • One Difficult Situation:
    • Suppose objectA and objectB both reference objectC, and that both objectA and objectB are serialized (e.g., two Student objects both reference the same Course object)
  • The Problem:
    • If the serialization of objectA includes the contents of objectC and the serialization of objectB includes the contents of objectC a problem may arise when they are deserialized. In particular, if the deserialization algorithm is not careful then objectA and objectB will reference different "copies" of objectC.
Serialization of References (cont.)
Back SMYC Forward

An Illustration

serialization2
Serialization of References (cont.)
Back SMYC Forward
  • Another Difficult Situation:
    • Suppose that objectD references objectE and that objectE references objectD (e.g., the Person class has an attribute named partner that is a reference to a Person)
  • The Problem:
    • Serializing objectD requires you to serialize objectE, but serializing objectE requires you to serialize objectD. Hence, we have to be very careful during the serialization process not to wind up with an infinite recursion.
Serialization of References (cont.)
Back SMYC Forward

An Illustration

serialization3
There's Always More to Learn
Back -