Menu

Home

Protocol Serialization

C++ serialization of various encoding formats:

Supports two types of serialization: full parsing results in a Document Object Model, and pull parsing is useful for streaming input.

Compatible with Boost.Serialization.

Full Parsing

The full parser transforms the entire input into a data structure. This is done via an input archive (called iarchive.) You can either use a generic tree data structure, such as dynamic-cpp, as the Document Object Model, or you can parse the input directly into your own data structures or the standard containers in C++.

iarchive input(buffer.data(), buffer.data() + buffer.size());
dynamic::var result;
input >> result;


Formatting a full data struture is done with an output archive (called oarchive.)

dynamic::var data;
// Insert some values into data
std::ostringstream result;
stream_oarchive output(result)
output << data;

Pull Parsing

The pull parser (called reader) is based on the Iterator design pattern, where the input is interpreted piece by piece. A simple example is:

std::string buffer = "[42, 43]";
json::reader reader(buffer.data(), buffer.data() + buffer.size());
assert(reader.type() == token::token_array_begin);
reader.next();
std::cout << reader.get_integer() << std::endl;
reader.next();
std::cout << reader.get_integer() << std::endl;
reader.next();
assert(reader.type() == token::token_array_end);
reader.next();
assert(reader.type() == token::token_eof);


Pull parsers are often paired with push formatters (called writer), which will create the output stream piece by piece. This example shows how to recreate the parsed buffer from the previous example:

std::ostringstream output;
output_stream<char> buffer(output);
json::writer writer(buffer);
writer.write_array_begin();
writer.write(42);
writer.write(43);
writer.write_array_end();
std::cout << output.str() << std::endl;


Depedencies

Depends on Boost.Serialization. Test suites depends on Boost.Test.


Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.