I want a way to serialize and deserialize Objects to JSON, as automatic as possible.

Serialize:

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.

An example should run like that:

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"

Or like that:

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"

How can I pull this off easily?

推荐答案

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

我也有同样的 idea ,我用GCC XML项目来获取这个信息.它输出描述类 struct 的XML数据. 我已经建立了一个项目,我正在解释这page条中的一些要点:

序列化很容易,但我们必须处理复杂的数据 struct 实现(例如std::string、std::map),这些实现使用分配的缓冲区.

例如,可以按如下方式序列化:

    // 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 this:

    // 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 ;

Ouptuts:

>:~/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$ 

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

Json相关问答推荐

两种情况下过滤的JOLT规范

如何使用JQ有条件 Select 值

替换字符串中特殊字符的Jolt变换

如何使用JQ将JSON字符串替换为解析后的类似功能?

Flutter -控制器问题-JSON API

使用自定义类型在Golang中解析JSON数组

419(未知状态)使用laravel处理PUT请求

在 PowerShell 中通过 aws cli 创建 cloudwatch alert 时出现字符串解析错误

错误解析错误:意外令牌:在我的 .eslintrc.json 文件中.为什么?

如何将该 JSON 解析为 Kotlin 类?

通过sql查询读取嵌套Json

使用 System.Text.Json 序列化记录成员

Swift:如何从字典中删除空值?

现代浏览器一次可以处理多少个 HTML 元素?

反序列化大型 json 对象的 JsonMaxLength 异常

在 JavaScript 中从 Json 数据中删除反斜杠

在 JSON.stringify() 的输出中隐藏空值

android - 在 adb logcat 输出中格式化 json 字符串

使用绝对或相对路径在 curl 请求中发送 json 文件

如何将 mysqli 结果转换为 JSON?