I'm looking for an efficient C object serialization Mechanism
I'm looking for an efficient way to implement a serialization mechanism in C. I know it would be simple to just store the data in JSON, for example, and then reinitialize everything during the parsing.
But I'm wondering if it is possible (or worth it) to write something that will just take my struct (containing dynamically allocated data), convert all its data and all the pointers into a buffer, so I can place that buffer into a file. Then I would read the file, retrieve the contents, malloc a location of the size of the file contents, and place the contents into that location.
I feel like it is possible to do (convert all the pointers to some kind of local pointing 开发者_如何学Pythonscheme and place all the structures appropriately into a buffer to match those pointers). I'm wondering whether a third party serializer like that exists, or whether it's worth implementing this.
You could look into protocol buffers: http://code.google.com/p/protobuf/, they have decent c tool support
However, in my experience just do it the easiest, most user-readable way (for debugging serialized data) first. Design with the notion in mind that you may need to change it later.
Then if disk or speed constraints absolutely, positively make it so you need to switch over to something better do it. Basically as in (nearly) all things optimize last.
精彩评论