Writing crucial data to the disk as TEXT is always dangerous. Any anonymous user can open the text file and easily read your data. With Object Serialization, you can reduce this danger to a certain extent. You can write any complex object directly to a filestream without converting values of individual properties into a text. If you use C-like escape-sequences for your string data, be sure to always use the same number of hex digits after 'x' and 'u'. I typically use 2 and 4 digits respectively. I typically use 2 and 4 digits respectively.
Suppose you want to serialize a “shape” object, where Shape is an abstract class with derived classes Rectangle, Ellipse, Line, Text, etc. You would declare a pure virtual function serialize(std::ostream&) const within class Shape, and make sure the first thing done by each override is to write out the class’s identity. JSON (JavaScript Object Notation) is an efficient data encoding format that enables fast exchanges of small amounts of data between client browsers and AJAX-enabled Web services. This article demonstrates how to serialize.NET type objects into JSON-encoded data and then deserialize data in the JSON format back into instances of.NET types.
Active1 year, 5 months ago
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.
Daniel Lucraft
Daniel LucraftDaniel Lucraft
5,68644 gold badges2929 silver badges3131 bronze badges
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.
![]()
87.2k2020 gold badges136136 silver badges135135 bronze badges
I 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:
Free adobe illustrator cs5.1 serial number. 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.
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.
[1] ROOT uses this mechanism to provide very flexible serialization support in C++.
C# Serialize Object
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.
dmckee
![]()
82.1k2121 gold badges119119 silver badges213213 bronze badges
This 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.
Amith ChinthakaAmith Chinthaka
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.
quinmarsquinmars
7,25366 gold badges2929 silver badges3939 bronze badges
plan9assemblerplan9assembler
you should checkout gwlib. the serializer/deserializer is extensive. and there are extensive tests available to look at. http://gwlib.com/
gngrwzrdgngrwzrd
3,34522 gold badges2929 silver badges4949 bronze badges
I'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):
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 Leffler
595k9999 gold badges718718 silver badges10741074 bronze badges
mmattaxmmattax
15.9k3737 gold badges108108 silver badges139139 bronze badges
Another option is Avro C, an implementation of Apache Avro in C.
Jeff HammerbacherJeff Hammerbacher
3,43322 gold badges2626 silver badges3434 bronze badges
Here 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:
Bernardo RamosBernardo Ramos
In theory YAML should do what you want http://code.google.com/p/yaml-cpp/
Please let me know if it works for you.
AndyLAndyL
9,6661313 gold badges3939 silver badges6969 bronze badges
Not the answer you're looking for? Browse other questions tagged cserialization or ask your own question.Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |