Call us: +1-415-738-4000

BigMemory C++ Client Serialization

Data that will be shared across languages needs to be serialized. The choice of which serializer to use depends upon your environment. Some planning and consideration needs to go into the choice of a serializer, as there are pros and cons related to each. For example, with a JSON serializer, you can manually annotate the domain object for desired attributes. On the other hand, a Google Protocol Buffer serializer can automatically generate the domain object with the annotation for the serializer for you, however, if you have an existing domain object, then using the automatic generation can be intrusive.

Your serializer/deserializer class should include the following:

  • A list of all the namespaces/packages that will be used.
  • A class definition consisting of the following methods/functions:
    • serializeKey() — accepts the key and returns the platform-neutral format (PNF) Value
    • deserializeKey() — accepts the PNF Value and returns the key
    • serializeValue() — accepts the object value and returns the PNF ValueObject
    • deserializeValue() — accepts the PNF ValueObject and returns the object value

The following serializer/deserializer example demonstrates Google Protocol Buffer serialization:

#include "RecipeProtoBufSerializer.h"

ValueObject RecipeProtoBufSerializer::serializeKey(std::string &deserializedKey) {
    return ValueObject::stringValue(deserializedKey);
}

std::string RecipeProtoBufSerializer::deserializeKey(ValueObject &serializedKey) {
    return serializedKey.getString();
}

CacheValue RecipeProtoBufSerializer::serializeValue(RecipeStructure &deserializedValue) {
    CacheValue value = CacheValue();
    value.setValue(ValueObject::binaryValue(deserializedValue.SerializeAsString()));
    std::map<std::string, ValueObject> nvPairs;
    nvPairs[KEY_NAME] = ValueObject::stringValue(deserializedValue.name());
    value.setNvPairs(nvPairs);
    return value;
}

RecipeStructure RecipeProtoBufSerializer::deserializeValue(CacheValue &serializedValue) {
    RecipeStructure recipe = RecipeProject::RecipeStructure();
    if (serializedValue.isValueSet()) {
        recipe.ParseFromString(serializedValue.getValue().getBinary());
    }
    return recipe;
}