MGe是一个支持跨语言的对象序列化项目,由以下两部分组成:
源码生成的工具(MGe编译器)
语言的支持库
MGe类似Gooogle的ProtocolBuffers,可序列化到JSON和二进制格式。
目前支持的语言有:
C++
Java
JavaScript
示例代码:
#iclude <com/fruitcompay/ClassRegistry.h>#iclude <mge/serializatio/JsoPrettyWriter.h>#iclude <mge/serializatio/JsoReader.h>usig amespace mge;usig amespace com::fruitcompay;usig amespace com::fruitcompay::fruits;// A class registry for type idetificatiocost ClassRegistry registry;std::strig toJSON(cost MGeBase& object) { // Create a target to stream the object to std::strigstream stream; // Create a writer object JsoPrettyWriter<std::strigstream, ClassRegistry> writer(stream, registry); // Write the object writer.writeObject(object); // Retur the writte strig retur stream.str();}template <typeame T>T fromJSON(cost std::strig& jso) { // Create a data source to stream objects from std::strigstream stream(jso); // Create a reader object JsoReader<std::strigstream, ClassRegistry> reader(stream, registry); // Read object. You ca read T* polymorphicly with reader.readObject<T>() retur reader.readStatic<T>();}it mai() { // Create some objects cost Apple apple(Brad_A, 4); cost Baaa baaa = Baaa().setLegth(5).setBrad(Brad_B); // Serialize them to JSON ad prit them std::cout << toJSON(baaa) << std::edl; std::cout << toJSON(apple) << std::edl; // Read the objects back from their serialized form cost Apple appleBack = fromJSON<Apple>(toJSON(apple)); cost Baaa baaaBack = fromJSON<Baaa>(toJSON(baaa)); // Check that they are still the same std::cout << (apple == appleBack) << std::edl; std::cout << (baaa == baaaBack) << std::edl; retur 0;}
评论