Overview

Fudge-Cpp

by Jens Alfke -- (c)2011

Fudge-Cpp is a cross-platform C++ library for reading and writing Fudge, "a binary messaging system designed to scale from ad-hoc messages to long-term persistance." Fudge is somewhat similar in semantics to JSON, but uses a binary encoding that's more compact and faster to parse. It can also use ordinals for field discovery (like protobuf or Thrift), but it doesn't need to use them. In Fudge you are free to use ordinals, names or purely order to determine what the field is. If you do use names, however, you can easily convert Fudge to JSON.

Fudge-Cpp's advantages over the existing Fudge-C library are:

  • Simpler, idiomatic C++ API using classes.
  • Integrates with STL and iostreams.
  • Uses less memory -- in fact, it allocates no heap space at all unless you use STL string accessors. When you access the fields in a message, the objects returned to you are really pointers directly into the existing binary message data. When you write a message, the library writes directly to the output stream without needing any heap space for bookkeeping.

Examples

Writing a simple message with a few fields into an in-memory string object:

std::stringstream out;
Writer writer(out);
writer.addInt(1234);
writer.addString("Fudge!");
writer.end();
std::string fudgeData = out.str();   // binary 'string', not human-readable

Writing a JSON-style dictionary directly to a file:

std::ofstream out("/tmp/fudgetest.dat");
Writer writer(out);
writer.setNextName("key1");
writer.addString("value1");
writer.setNextName("key2");
Message nested = writer.beginMessage();
nested.addDouble(3.14159);
nested.addDouble(2.71828);
nested.end();
writer.end();
out.close();

Parsing the Fudge message generated by the first example:

const Envelope* envelope = Envelope::FromBytes(fudgeData);
Message msg = envelope->message();
Message::iterator i = msg.begin();
int value = i->asInt32();

Writing the above Fudge message as JSON:

WriteJSON(envelope, std::cout);

The test cases in the "test" subdirectory show many examples of using the complete API.