From Academic Kids

(Redirected from Marshalling)
See serial for the term in publishing.

In computer science, serialization means to force one-at-a-time access for the purposes of concurrency control, or to encode a data structure as a sequence of bytes. The opposite operation, to extract a data structure from a series of bytes, is deserialization.

This latter form of serialization (which is also referred to as marshalling) involves taking a data structure or object and encoding it into a regular and usually architecture-independent form, suitable for archiving to a file, piping to another application, or, by extension, transmission across a network. Usually the encoding takes the form of a byte stream (a sequence of bytes). When receiving a serialized stream, the encoding process is reversed to get a copy of the original data structure.



Serialization has a number of advantages. Serialization provides:

For some of these features to be useful, architecture independence must be maintained. For example, for maximal use of distribution, a computer running on a different hardware architecture should be able to reliably reconstruct a serialized data stream, regardless of endianness. This means that the simpler and faster procedure of directly copying the memory layout of the data structure, cannot work reliably for all architectures. Serializing the data structure in an architecture independent format means that we do not suffer from the problems of byte ordering, memory layout, or simply different ways of representing data structures in different programming languages.

Serialization however, in some forms, has the disadvantage that since the encoding of the data is serial, merely extracting one part of the data structure that is serialized means that the entire object must be reconstructed or read before this can be done. The serialization capabilities in the Cocoa framework, NSKeyedArchiver, alleviate the problem somewhat, by allowing an object to be archived with each instance variable of the object accessible by using a key.

Even on a single machine, primitive pointer objects are too fragile to save, because the objects to which they point may be reloaded to a different location in memory. To deal with this the serialization process includes a step called unswizzling or pointer unswizzling and the deserialization process includes a step called pointer swizzling.


Serialization, however, breaks the opacity of an abstract data type by potentially exposing private implementation details. To discourage competitors from making compatible products, publishers of proprietary software often keep the details of their programs' serialization formats a trade secret. Some deliberately obfuscate or even encrypt the serialized data.

Yet, interoperability requires that applications be able to understand the serialization of each other. Therefore remote method call architectures such as CORBA define their serialization formats in detail and often provide methods of checking the consistency of any serialized stream when converting it back into an object.

Human-readable serialization

There is a push to redefine, or provide an alternative to the standard serialization protocol that uses XML and produces a human readable encoding, which started in the late 1990s. Such an encoding could be useful for persistent objects that may be read and understood by humans, or communicated to other systems regardless of programming language, but this has the disadvantage of losing the more compact, byte stream based encoding, which is generally more practical.

Programming language support

Several object-oriented programming languages directly support object serialization (or object archival), either by syntactic sugar elements or providing a standard interface for doing so.

Some of these programming languages are Smalltalk, Python, Objective-C, Java, and the .NET family of languages.


In the Objective-C programming language, serialization (most commonly known as archival) is achieved by overriding the write: and read: methods in the Object root class. (NB This is in the GNU runtime variant of Objective-C. In the NeXT-style runtime, the implementation is very similar.)


The following example demonstrates two independent programs, a "sender", who takes the current time (as per time ( in the C standard library), archives it and prints the archived form to the standard output, and a "receiver" which decodes the archived form, reconstructs the time and prints it out.

When compiled, we get a sender program and a receiver program. If we just execute the sender program, we will get out a serialization that looks like:

GNU TypedStream 1D@ÓC°

(with a NULL character after the 1). If we pipe the two programs together, as sender | receiver, we get

received 1089356705

showing the object was serialized, sent, and reconstructed properly.

In essence, the sender and receiver programs could be distributed across a network connection, providing distributed object capabilities.

#import <objc/Object.h>
#import <time.h>
#import <stdio.h>

@interface Sender : Object
   time_t  current_time;

- (id) setTime;
- (time_t) time;
- (id) send;
- (id) read: (TypedStream *) s;
- (id) write: (TypedStream *) s;

#import "Sender.h"

@implementation Sender
- (id) setTime
   //Set the time
   current_time = time(NULL); 
   return self;

- (time_t) time;
   return current_time;

- (id) write: (TypedStream *) stream
    * Write the superclass to the stream.
    * We do this so we have the complete object hierachy,
    * not just the object itself.
   [super write:stream];

    * Write the current_time out to the stream.
    * time_t is typedef for an integer.
    * The second argument, the string "i", specifies the types to write
    * as per the @encode directive.
   objc_write_types(stream, "i", &current_time);
   return self;

- (id) read: (TypedStream *) stream
    * Do the reverse to write: - reconstruct the superclass...
   [super read:stream];

    * And reconstruct the instance variables from the stream...
   objc_read_types(stream, "i", &current_time);
   return self;

- (id) send
   //Convenience method to do the writing. We open stdout as our byte stream
   TypedStream *s = objc_open_typed_stream(stdout, OBJC_WRITEONLY);  

   //Write the object to the stream
   [self write:s];

   //Finish up - close the stream.
#import "Sender.h"

   Sender *s = [Sender new];
   [s setTime];
   [s send];

   return 0;
#import <objc/Object.h>
#import "Sender.h"

@interface Receiver : Object
   Sender *t;

- (id) receive;
- (id) print;
#import "Receiver.h"

@implementation Receiver 

- (id) receive
   //Open stdin as our stream for reading.
   TypedStream *s = objc_open_typed_stream(stdin, OBJC_READONLY);

   //Allocate memory for, and instantiate the object from reading the stream.
   t = [[Sender alloc] read:s];

- (id) print
   fprintf(stderr, "received %d\n", [t time]);

#import "Receiver.h"

   Receiver *r = [Receiver new];
   [r receive];
   [r print];
   return 0;


Java provides automatic serialization which only requires that the object be marked by implementing the Serializable interface. For some not-so-clear reason, the process of serialization is handled in a very idiosyncratic manner. There are no serialization methods defined on the Serializable interface; implementing the interface just marks the class as "okay to serialize." Java then handles serialization internally. The language does though allow the developer to override this by implementing another interface, the Externalizable interface, which includes two special methods used to save and restore the object's state.

The standard encoding method uses a simple translation of the fields into a byte stream. Primitives as well as referenced objects are encoded into the stream. Each object that is referenced by the serialized object must also be serialized and if any in the reference graph is not serializable, then serialization will fail (unless the developer has redefined the serialization for an object and truncates some portion of the reference graph).de:Serialisierung


Academic Kids Menu

  • Art and Cultures
    • Art (
    • Architecture (
    • Cultures (
    • Music (
    • Musical Instruments (
  • Biographies (
  • Clipart (
  • Geography (
    • Countries of the World (
    • Maps (
    • Flags (
    • Continents (
  • History (
    • Ancient Civilizations (
    • Industrial Revolution (
    • Middle Ages (
    • Prehistory (
    • Renaissance (
    • Timelines (
    • United States (
    • Wars (
    • World History (
  • Human Body (
  • Mathematics (
  • Reference (
  • Science (
    • Animals (
    • Aviation (
    • Dinosaurs (
    • Earth (
    • Inventions (
    • Physical Science (
    • Plants (
    • Scientists (
  • Social Studies (
    • Anthropology (
    • Economics (
    • Government (
    • Religion (
    • Holidays (
  • Space and Astronomy
    • Solar System (
    • Planets (
  • Sports (
  • Timelines (
  • Weather (
  • US States (


  • Home Page (
  • Contact Us (

  • Clip Art (
Personal tools