views:

109

answers:

3

I am writing a server-client application in c and need to send some structures back and forth. From what I have learned, it would be a good idea to use some library to serialize the structures as it then rids them off platform dependency.

I read about some xml based and binary based serialization methods, but I am not sure what library to use and what are generally preferred. Can anyone suggest good libraries for the same? Also I need it to be simple and efficient (going well with the rest of my c code).

Any comment would be great a help.Thanks in advance.

+1  A: 

Quick Google search resulted in finding this C implementation of JSON. This would give you reasonable performance (better than XML at least) while keeping it simple.

Taylor Leese
+7  A: 

Well there's google's protobuf (use http://code.google.com/p/protobuf-c/ for pure C, if C++ is ok you can just use http://code.google.com/p/protobuf/)

And there's also tpl, Apache's Avro

And there's another one I've looked at that I can't remember the name of at the moment, IIRC it was even simpler than the ones I already listed since it doesn't require any sort of schema.

(NOTE: these are all serialize to a binary representation)

Spudd86
A: 

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.

R..