C Object Serialization
Posted : admin On 30.01.2020Serialization means turning your object into binary data. While deserialization means recreating an object from the data. When serializing you are pushing bytes into an uint8t vector. When unserializing you are reading bytes from an uint8t vector. C# Serialization. In C#, serialization is the process of converting object into byte stream so that it can be saved to memory, file or database. The reverse process of serialization is called deserialization. Serialization is internally used in remote applications. C# SerializableAttribute. Serialization is the process of bringing an object into a form that it can be written on stream. It's the process of converting the object into a form so that it can be stored on a file, database, or memory; or, it can be transferred across the network.
I'd like a C library that can serialize my data structures to disk, and then load them again later. It should accept arbitrarily nested structures, possibly with circular references.
I presume that this tool would need a configuration file describing my data structures. The library is allowed to use code generation, although I'm fairly sure it's possible to do this without it.
Note I'm not interested in data portability. I'd like to use it as a cache, so I can rely on the environment not changing.
Thanks.
Results
Someone suggested Tpl which is an awesome library, but I believe that it does not do arbitrary object graphs, such as a tree of Nodes that each contain two other Nodes.
Another candidate is Eet, which is a project of the Enlightenment window manager. Looks interesting but, again, seems not to have the ability to serialize nested structures.
10 Answers
Check out tpl. From the overview:
Tpl is a library for serializing C data. The data is stored in its natural binary form. The API is small and tries to stay 'out of the way'. Compared to using XML, tpl is faster and easier to use in C programs. Tpl can serialize many C data types, including structures.
Robert GambleRobert GambleI know you're asking for a library. If you can't find one (::boggle::, you'd think this was a solved problem!), here is an outline for a solution:
You should be able to write a code generator[1] to serialize trees/graphs without (run-time) pre-processing fairly simply.
You'll need to parse the node structure (typedef
handling?), and write the included data values in a straight ahead fashion, but treat the pointers with some care.
For pointer to other objects (i.e.
char *name;
) which you know are singly referenced, you can serialize the target data directly.For objects that might be multiply refernced and for other nodes of your tree you'll have to represent the pointer structure. Each object gets assigned a serialization number, which is what is written out in-place of the pointer. Maintain a translation structure between current memory position and serialization number. On encountering a pointer, see if it is already assigned a number, if not, give it one and queue that object up for serialization.
Reading back also requires a node-#/memory-location translation step, and might be easier to do in two passes: regenerate the nodes with the node numbers in the pointer slots (bad pointer, be warned) to find out where each node gets put, then walk the structure again fixing the pointers.
I don't know anything about tpl, but you might be able to piggy-back on it.
The on-disk/network format should probably be framed with some type information. You'll need a name-mangling scheme.
Hunter winalign software install directions for beginners. [1] ROOT uses this mechanism to provide very flexible serialization support in C++.
Late addition: It occurs to me that this is not always as easy as I implied above. Consider the following (contrived and badly designed) declaration:
and assume that we initalize out struct saved_setup
items so that mask_name
points at mask_list[foo].mask_name
.
When we go to serialize the data, what do we do with struct saved_setup.mask_name
?
You will need to take care in designing your data structures and/or bring some case-specific intelligence to the serialization process.
dmckeedmckeeThis is my solution. It uses my own implementation of malloc, free and mmap, munmap system calls. Follow the given example codes. Ref: http://amscata.blogspot.com/2013/02/serialize-your-memory.html
In my approach I create a char array as my own RAM space. Then there are functions for allocate the memory and free them. After creating the data structure, by using mmap
, I write the char array to a file.
Whenever you want to load it back to the memory there is a function which used munmap
to put the data structure again to the char array. Since it has virtual addresses for your pointers, you can re use your data structure. That means, you can create data structure, save it, load it, again edit it, and save it again.
You can take a look on eet. A library of the enlightenment project to store C data types (including nested structures). Although nearly all libs of the enlightenment project are in pre-alpha state, eet is already released. I'm not sure, however, if it can handle circular references. Probably not.
quinmarsquinmarsyou should checkout gwlib. the serializer/deserializer is extensive. and there are extensive tests available to look at. http://gwlib.com/
gngrwzrdgngrwzrdI'm assuming you are talking about storing a graph structure, if not then disregard..
If your storing a graph, I personally think the best idea would be implementing a function that converts your graph into an adjacency matrix. You can then make a function that converts an adjacency matrix to your graph data structure.
This has three benefits (that may or may not matter in your application):
- adjacency matrix are a very natural way to create and store a graph
- You can create an adjacency matrix and import them into your applications
- You can store and read your data in a meaningful way.
I used this method during a CS project and is definitely how I would do it again.
You can read more about adjacency matrix here: http://en.wikipedia.org/wiki/Modified_adjacency_matrix
Jonathan LefflerAnother option is Avro C, an implementation of Apache Avro in C.
Jeff HammerbacherJeff HammerbacherHere is an example using the Binn library (my creation):
If you don't want to use strings as keys you can use a binn_map which uses integers as keys.
There is also support for lists, and all these structures can be nested:
In theory YAML should do what you want http://code.google.com/p/yaml-cpp/
Please let me know if it works for you.
AndyLAndyLNot the answer you're looking for? Browse other questions tagged cserialization or ask your own question.
The concept of Serialization and deserialization is used whenever data pertaining to objects have to be sent from one application to another. Serialization is used to export application data into a file. The destination application then uses deserialization to extract the data from the application for further use.
Serialization is a concept in which C# class objects are written or serialized to files. Let' say you had a C# class called Tutorial. And the class has 2 properties of ID and Tutorials name.
Serializing can be used to directly write the data properties of the Tutorial class to a file. Deserialization is used to read the data from the file and construct the Tutorial object again.
Let's look at an example of how we can achieve this.
In our example, we are going to perform the below high-level steps in the code
- Create a class called Tutorial which has 2 properties, namely ID, and Name
- We will then create an object from the class and assign a value of '1' to the ID property and a value of '.Net' to the name property.
- We will then use serialization to serialize the above object to a file called Example.txt
- Finally, we will use deserialization to deserialize the object from the file and display the values in the Console.
Enter the below code in the program.cs file of the console application.
Step 1) The first step is to add the class which will be used for serialization
Code Explanation:-
- The class which needs to be serialized needs to have the [Serializable] attribute. This is a keyword in C#. This keyword is then attached to the Tutorial class. If you don't mention this attribute, you will get an error when you try to serialize the class.
- Next is the definition of the class which will be serialized. Here we are defining a class called 'Tutorial' and providing 2 properties, one is 'ID' and the other is 'Name.'
Step 2) In this step, first we will create the object of the Tutorial class and serialize it to the file called Example.txt
Code Explanation:-
- First, we create an object of the Tutorial class. We then assign the value of '1' to ID and '.net' to the name property.
- We then use the formatter class which is used to serialize or convert the object to a binary format. The data in the file in serialization is done in binary format. Next, we create a file stream object. The file stream object is used to open the file Example.txt for writing purposes. The keywords FileMode.Create and FileMode.Write is used to specifically mention that the file should be opened for writing purposes.
- Finally, we use the Serialize method to transfer the binary data to the file. We then close the stream, since the write operation is complete.
Serialization In Java
Step 3) Finally to ensure that the data is present in the file, we use deserialization to deserialize the object from the file.
Code Explanation:-
- We create the object 'stream' to open the file Example.txt in reading only mode.
- We then use the formatter class which is used to deserialize the object, which is stored in the Example.txt file. The object returned is set to the object objnew.
- Finally, we display the properties of the object 'objnew' to the console using the 'ID' and 'name' properties.
Python Object Serialization
When the above code is set, and the project is run using Visual Studio, you will get the below output.
Output:-
You can see from the above output that the values from the file were deserialized properly and displayed in the console.
Object C Online
Summary
Serialization is used to write class objects to files.
De-Serialization is used to recover the objects from the file.