Question: I have a daemon for a scientific instrument that needs to persist about 100 unique variables. Currently, these settings are stored in binary form at explicit addresses, and each is accessed with a unique getter and setter. This is completely unmaintainable. I'd like to group settings together in sensible structures and persist those structures into files. I'll also have default values in case of incomplete files. What library or technique will allow me to save and restore these structures with as few lines of code as possible?

Answer: 
The fastest/easiest way to serialize a C struct is to use X-macros. This lets you define related properties together and re-use elements. This example does not do any of the error checking that should be done but with an implementation less than 30 lines it will be hard to find a shorter one. More elaborate uses are possible like automatically creating getters and setters.

```
#include <stdio.h>

#define SETTINGS_TABLE \
    X(float,  "%f", foo, 2.71818) \
    X(float,  "%f", bar, 0.70711) \
    X(int,    "%i", baz, 42)

#define X(type, fmt, name, default) type name ;
struct Settings { SETTINGS_TABLE };
#undef X

#define X(type, fmt, name, default) default ,
struct Settings settings = { SETTINGS_TABLE };
#undef X

void dump(FILE *f)
{
#define X(type, fmt, name, default) fprintf(f, "%s=" fmt "\n", #name, settings.name);
    SETTINGS_TABLE
#undef X
}

void load(FILE *f)
{
#define X(type, fmt, name, default) fscanf(f, #name "=" fmt, &settings.name);
    SETTINGS_TABLE
#undef X
}

int main(int argc, char *argv[])
{
    FILE *cfg;

    cfg = fopen(argv[1], "r");
    load(cfg);
    fclose(cfg);
    dump(stdout);

    return 0;
}

```
"Shortest" have several meanings

* fast to process
* eating as little as memory as possible
* easy and quick to code

I would suggest to use a simple textual format to serialize them, probably [JSON](http://json.org/) (or maybe [YAML](http://en.wikipedia.org/wiki/YAML) or XML). The advantage of such formats is that they remain somehow human "readable" (at last for a programmer).

There exist a lot of JSON libraries (notably in C and C++): [jansson](http://www.digip.org/jansson/) in C, [jsoncpp](http://jsoncpp.sourceforge.net/) for C++, etc etc...

Does your scientific instrument runs only a tiny 8 bit controller? If yes, you might be constrained by code size...

And I suggest also to *document* the serialized format.