C ++ JSON序列化

问题描述:

在问之前,让我说我已经搜索,但我有点新C ++,所以我需要一些帮助。

Before asking, let me say that I have searched, but I am kinda new on C++ so I need some help with that.

我想要一个方法序列化

序列化:
对我来说,理想的方式是,如果我调用在JSONSerialize()实例中,它返回一个字符串,该JSON对象具有对象的所有公共属性name_of_property:value
对于那些原始值,它是直接的,对于对象,它应该尝试调用每个JSONSerialize()或ToString()或类似的东西递归地序列化所有的公共属性。
对于集合,它也应该正确的行为(只是向量/数组将会成功)。

Serialize: For me, the ideal way is that if I call in an instance JSONSerialize() it returns an string with a JSON object that has all the public properties of the object as "name_of_property": "value". For those values that are primitives, it is straightforward, for objects it should try to call on each JSONSerialize() or ToString() or something like that to recursively serialize all the public properties. For collections it should also behave correctly (just vectors/arrays will be ok).

反序列化给定的对象(让我们说一个狗),并调用 JSONDeserialize(json_string),这应该填充所有的公共属性,创建所需的对象,如果属性不是基元,或所需的集合。

Deserialize: Just make an instance of the given object (let's say a dog) and call JSONDeserialize(json_string), and that should fill all the public properties, creating the needed objects in case that the properties are not primitives, or the needed collections.

示例应该像这样运行:

Dog *d1 = new Dog();
d1->name = "myDog";

string serialized = d1->JSONSerialize();

Dog *d2 = new Dog();
d2->JSONDeserialize(serialized);
std::cout << d2->name; // This will print "myDog"

或者像这样:

Dog *d1 = new Dog();
d1->name = "myDog";

string serialized = JSONSerializer.Serialize(d1);

Dog *d2 = JSONSerializer.Deserialize(serialized, Dog);
std::cout << d2->name; // This will print "myDog"

THANKS:))

因为你需要用C / C ++语言进行反思,所以不存在。你需要有一些元数据描述你的类的结构(成员,继承的基类)。目前C / C ++编译器不能自动提供内置二进制文件中的信息。

For that you need reflection in C/C++ language, that doesn't exists. You need to have some meta data describing the structure of your classes (members, inherited base classes). For the moment C/C++ compilers doesn't provide automatically that information in built binaries.

我有同样的想法,我使用 GCC XML 项目以获取此信息。它输出描述类结构的XML数据。
我已经建立了一个项目,我在这个 page

I had the same idea in mind, and I used GCC XML project to get this information. It outputs XML data describing class structures. I have built a project and I'm explaining some key points in this page :

序列化很容易,但我们必须处理复杂的数据结构实现(std :: string,std :: map )与分配的缓冲区播放。
反序列化更复杂,你需要重新构建你的对象与所有的成员,加上引用vtables ...一个痛苦的实现。

Serialization is easy, but we have to deal with complex data structure implementations (std::string, std::map for example) that play with allocated buffers. Deserialization is more complex and you need to rebuild your object with all its members, plus references to vtables ... a painful implementation.

例如,你可以序列化类似:

For example you can serialize like that :

    // Random class initialization
    com::class1* aObject = new com::class1();

    for (int i=0; i<10; i++){
            aObject->setData(i,i);
    }      

    aObject->pdata = new char[7];
    for (int i=0; i<7; i++){
            aObject->pdata[i] = 7-i;
    }
    // dictionary initialization
    cjson::dictionary aDict("./data/dictionary.xml");

    // json transformation
    std::string aJson = aDict.toJson<com::class1>(aObject);

    // print encoded class
    cout << aJson << std::endl ;

要反序列化数据,它的工作原理如下:

To deserialize data it works like that:

    // decode the object
    com::class1* aDecodedObject = aDict.fromJson<com::class1>(aJson);

    // modify data
    aDecodedObject->setData(4,22);

    // json transformation
    aJson = aDict.toJson<com::class1>(aDecodedObject);

    // print encoded class
    cout << aJson << std::endl ;

Oopsuts:

>:~/cjson$ ./main
{"_index":54,"_inner":  {"_ident":"test","pi":3.141593},"_name":"first","com::class0::_type":"type","com::class0::data":[0,1,2,3,4,5,6,7,8,9],"com::classb::_ref":"ref","com::classm1::_type":"typem1","com::classm1::pdata":[7,6,5,4,3,2,1]}
{"_index":54,"_inner":{"_ident":"test","pi":3.141593},"_name":"first","com::class0::_type":"type","com::class0::data":[0,1,2,3,22,5,6,7,8,9],"com::classb::_ref":"ref","com::classm1::_type":"typem1","com::classm1::pdata":[7,6,5,4,3,2,1]}
>:~/cjson$ 

通常这些实现都是编译器依赖(例如ABI规范)工作(GCCXML输出),这样不容易集成到项目。

Usually these implementations are compiler dependent (ABI Specification for example), and requires external description to work (GCCXML output), such are not really easy to integrate to projects.