Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serialize data in C [closed]

I am writing a server-client application in c and need to send some structures back and forth.

How can I go about serializing the data in a platform independent manner?

like image 734
mmahesh Avatar asked Jul 02 '10 20:07

mmahesh


1 Answers

If you need to worry about compatibility between different versions of the server and client, I'd use Google protocol buffers or JSON (either an existing implementation, or write your own). If the versions will always be in sync, just write your own binary serialization, or even tailor the in-memory structure to be a 'serialized' form and use access functions to read and write it. This basically entails storing numbers as a fixed number of bytes in a fixed-endian order, or some vlc format if you want to get fancy, and using object handles of some sort (simplest is an offset into an array) in place of explicit pointers when one object needs to refer to another.

One really nice side benefit of keeping data in a 'serialized' form internally is that you can work with huge data sets on disk (for example via mmap) without having to write your own complex caching code.

like image 74
R.. GitHub STOP HELPING ICE Avatar answered Sep 27 '22 20:09

R.. GitHub STOP HELPING ICE