web123456

c serialization, serialization and deserialization

Definitions and related concepts

The creation of the Internet has created the need for inter-machine communication, and the two sides of the interconnected communication need to use an agreed protocol, serialization and deserialization are part of the communication protocol. Communication protocols often use a layered model, with different models having different definitions of functionality per layer as well as different levels of granularity, for example:TCP/IP protocol is a four-layer protocol, and theOSI modelHowever, it is a seven-layer protocol model. In the OSI seven-layer protocol model, the main function of the Presentation Layer is to convert an application-layer object into a contiguous binary string or, conversely, to convert a binary string into an application-layer object - these two functions are serialization and deserialization. In general, the application layer of the TCP/IP protocol corresponds to the application, presentation and session layers of the OSI seven-layer protocol model, so the serialization protocol is part of the application layer of the TCP/IP protocol. The explanation of serialization protocol in this paper is mainly based on the OSI seven-layer protocol model.

Serialization: The process of converting a data structure or object into a binary string. In iOS, this is called archiving.

Deserialization: the process of converting a binary string generated during serialization into a data structure or object.

Data structures, objects, and binary strings are not represented in the same way in different computer languages.

Data Structures and Objects: For a fully object-oriented language like Java, everything the engineer manipulates is an object (Object), derived from the instantiation of a class. The closest concept to a data structure in the Java language is POJO (Plain Old Java Object) or Javabean - those classes that have only setter/getter methods. And C binary strings: the binary string generated by serialization refers to a piece of data stored in memory.C strings can be used directly by the transport layer because they are essentially binary strings ending in '0' stored in memory. Inside the Java language, the concept of a binary string is easily confused with String. In fact String is a first class citizen of Java and is a special object (Object). For cross-language communication, the serialized data certainly can't be a special data type of a certain language. In iOS, the object is converted to NSData type, which is after serialization, and NSData to other types of objects is deserialization.

Serialization and Deserialization in iOS

To convert any object to NSData, this object needs to follow a protocol, which is the NSCoding protocol. The code is as follows:

// Each attribute variable is transcoded and serialized separately

- (void)encodeWithCoder:(NSCoder *)aCoder

{

[aCoder encodeObject: forKey:@"username"];

[aCoder encodeObject: forKey:@"FriendlyName"];

[aCoder encodeObject: forKey:@"phoneNum"];

}

//Separately reverse code each attribute variable according to the keyword, and finally return an object of the Student class, deserialized.

- (id)initWithCoder:(NSCoder *)aDecoder

{

if (self = [super init])

{

= [aDecoder decodeObjectForKey:@"username"];

= [aDecoder decodeObjectForKey:@"FriendlyName"];

= [aDecoder decodeObjectForKey:@"phoneNum"];

}

return self;

}

After an object implements the NSCoding protocol, it can be converted to NSData when the object is used outside by using the archive function:.

// Archive mobilization, serialization

NSData *contactsData=[NSKeyedArchiver archivedDataWithRootObject:ContactsArray];

// Deserialize, transform into an object

NSObject *obj=[NSKeyedUnarchiver unarchiveObjectWithFile:path];

NSCoder is an instrumental coding class that encapsulates functions for serializing and deserializing objects, so in reality, we didn't write our own serialization algorithm, we just followed the protocol and let the system call it.

What iOS does with serialization and deserialization

Implementing classes for NSCoding and serializing data has 2 benefits:

1. Serialized data can be stored directly

2. Serialized data is easy to make exact copies of

Serialized data can be stored directly

In iOS, the quicker way to do this is NSUserDefaults, which is stored as follows:

However, it supports a limited number of data types:

NSNumber(NSInteger、float、double),NSString,NSData,NSArray,NSDictionary,BOOL.

[[NSUserDefaults standardUserDefaults] setObject:nickName forKey:UserDefault_NickName];

[[NSUserDefaults standardUserDefaults] synchronize];

They are generally immutable basic types, and will crash when storing other types, such as NSMutableArray.

The solution is as follows:

// Of course, it can't be ignored that if it's a custom object, don't forget the NSCoding protocol.

NSData *contactsData=[NSKeyedArchiver archivedDataWithRootObject:ContactsArray];

[[NSUserDefaults standardUserDefaults] setObject:contactsData forKey:UserDefault_ContactsArray];

[[NSUserDefaults standardUserDefaults] synchronize];

In addition to NSUserDefaults, another way to store NSData can be with archive + address:

[NSKeyedArchiver archiveRootObject:obj toFile:path];

Serialized data is easy to make exact copies of:

Here is a brief description of using NSKeyedArchiver to implement deep copy:

The main method is to convert an object to NSData first, and then NSData is transferred back to the new object:

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:oldContactsArray];

NSMutableArray *newContactsArray = [NSKeyedUnarchiver unarchiveObjectWithData:data];