简体   繁体   中英

Quick'n'dirty way to prototype some serialization in C++?

I need to do a prototype that involves some serialization in C++. It is a quick'n'dirty prototype, so I don't need to solve the problem generally, provide good error checking, or anything like that. But at the same time, I do need to be able to serialize strings of arbitrary length and with arbitrary charcters.

Are there some best practices for how to whip up a quick data serialization in C++? Normally I'd just have output records into a text file with one record per line, but my strings may have new lines in them.

You could consider using JSON , notably thru JsonCpp . You could also use libs11n , a full fledged, template friendly, C++ serialization framework. (If you want a C library for Json, consider jansson ).

You might also consider using old XDR or ASN1 technology.

For a quick & dirty prototype, I do recommend the JsonCpp library mentioned above. And using JSON in that case is useful, since it is a textual, nearly-human-friendly, format.

Later you could even perhaps consider going to MongoDb which has a Json-like model.

Checkout serialization with boost:

http://www.boost.org/doc/libs/1_51_0/libs/serialization/doc/index.html

Not dirty at all but definitely quick.

If you do not mind binary data, for each string dump a length (cast to a char*) and then the value of the string to file. It is very easy to read back. POD structs can also be dumped directly by casting to a char*

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM