< Previous PageNext Page > Hide TOC

Serializations

Serialization converts Objective-C types to and from an architecture-independent byte stream. In contrast to archiving, basic serialization does not record the data type of the values nor the relationships between them; only the values themselves are recorded. It is your responsibility to deserialize the data in the proper order. Several convenience classes, however, do provide the ability to serialize property lists, recording their structure along with their values.

Contents:

Property Lists
Deprecated Classes and Methods


Property Lists

In Cocoa, property lists are defined as simple object graphs consisting of nothing but the following types: NSDictionary, NSArray, NSString, NSDate, NSNumber, Java number objects (such as Integer), and NSData. Several classes are defined that can serialize these property list objects directly. These classes define special formats for the data stream which record the contents of the objects and their hierarchical relationship.

For more details on property lists, see the Programming Topic Property List Programming Guide.

NSPropertyListSerialization

The NSPropertyListSerialization class provides the serialization methods that convert property list objects to and from either an XML or an optimized binary format. This class can also read the old-style OpenStep string format, although numbers and dates are interpreted as strings. Container objects are always created immutable when deserializing in Java, but can be mutable or immutable when deserializing in Objective-C.

The NSPropertyListSerialization class object provides the interface to the serialization process; you don’t create instances of NSPropertyListSerialization.

Issues with Property List Serialization

Property list serialization does not preserve the full class identity of the objects, only its general kind—a dictionary, an array, and so on. As a result, if a property list is serialized and then deserialized, the objects in the resulting property list might not be of the same class as the objects in the original property list. In particular, when a property list is serialized, the mutability of the container objects (NSDictionary and NSArray objects) is not preserved. When deserializing, though, you can choose to have all container objects created mutable or immutable.

Serialization also does not track the presence of objects referenced multiple times. Each reference to an object within the property list is serialized separately, resulting in multiple instances when deserialized.

Because serialization does not preserve class information or mutability, nor handles multiple references, coding (as implemented by NSCoder and its subclasses) is the preferred way to make object graphs persistent.

Deprecated Classes and Methods

Note: NSSerializer and NSDeserializer were deprecated in Mac OS X version 10.2 and are obsolete. Use NSPropertyListSerialization instead.

NSData’s and NSMutableData’s serialization methods were deprecated in Mac OS X version 10.2 and are obsolete. Use the NSPropertyListSerialization class instead.

NSSerializer and NSDeserializer

The NSSerializer and NSDeserializer classes, available only in Objective-C, provide mechanisms for converting between a property list and an architecture-independent representation of the property list. The NSSerializer class stores the property list representation in an NSData object that can be written to a file or transmitted to another process or machine. Conversely, the NSDeserializer class converts a representation of a property list (as contained in an NSData object) back into a collection of property list objects in memory. Options to these NSDeserializer methods allow you to specify that container objects (arrays or dictionaries) in the resulting object graph be mutable or immutable; that deserialization begin at the start of the data or from some position within it; or that deserialization occur lazily, so a property list is deserialized only if it is actually going to be accessed.

The NSSerializer and NSDeserializer class objects provide the interface to the serialization process; you don’t create instances of NSSerializer or NSDeserializer. You might create subclasses to modify the representation it creates, for example, to encrypt the data or add authentication information.

Primitive Data

NSData and NSMutableData define the methods serializeDataAt:ofObjCType:context: and deserializeDataAt:ofObjCType:atCursor:context: to serialize and deserialize Objective-C types. These methods can be used to serialize the basic types, such as integers, floats, character strings, and so on, as well as structures and arrays.

These methods do not directly support the serialization of objects, but by implementing the NSObjCTypeSerializationCallBack protocol you can provide a helper object (via the context argument) that can serialize and deserialize the object in a non-object form. For example, an NSString object can be converted to a C string and then serialized; when deserializing, the helper object can read the C string and convert it back into an NSString object. See “Serializing Objects” for details.



< Previous PageNext Page > Hide TOC


© 2002, 2009 Apple Inc. All Rights Reserved. (Last updated: 2009-02-04)


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.