RapidJSON 的坑--允许Object对象存在相同的key,且key为数字时序列化报异常
测试代码如下:
1 void show(rapidjson::Document& doc) 2 { 3 printf("----------------- for iterator\nMemberCount:%d\n", doc.MemberCount()); 4 for(auto it=doc.MemberBegin();it!=doc.MemberEnd();it++) 5 { 6 if (it->name.IsString()) 7 printf("\"%s\":\"%s\"\n",it->name.GetString(), it->value.GetString()); 8 else 9 printf("%d:\"%s\"\n",it->name.GetInt(), it->value.GetString()); 10 } 11 printf("----------------- for iterator end\n"); 12 } 13 14 void testJson(){ 15 rapidjson::Document doc; 16 doc.SetObject(); 17 rapidjson::Document::AllocatorType& allocator = doc.GetAllocator(); 18 rapidjson::Value key, val; 19 key.SetString("12345"); 20 21 std::string aaa="aaa"; 22 for(int i=0; i<10;i++) 23 { 24 std::string a=aaa+std::to_string(i); 25 val.SetString(a.data(), allocator); 26 doc.AddMember(key, val, allocator); 27 // doc["aaa"]=val; // 不能插入 28 // doc[a.data()]=val; // 不能插入 29 } 30 show(doc); 31 // std::string bbb="bbb"; 32 // for(int i=0; i<10;i++) 33 // { 34 // key.SetInt(12345+i); // 数值的key 在输出 json-str的时候会报异常 35 // val.SetString((bbb+std::to_string(i)).data(), allocator); 36 // doc.AddMember(key, val, allocator); 37 // } 38 // show(doc); 39 // 40 rapidjson::StringBuffer jsonBuffer; 41 rapidjson::Writer<rapidjson::StringBuffer> writer(jsonBuffer); 42 doc.Accept(writer); 43 std::string outJson = jsonBuffer.GetString(); 44 printf("json str:%s\n",outJson.data()); 45 // 46 doc.Clear(); 47 doc.Parse(outJson.data()); 48 printf("----------------- Parse\nMemberCount:%d\n", doc.MemberCount()); 49 printf("doc['12345']=%s\n",doc["12345"].GetString()); 50 printf("doc[12345]=%s\n",doc[12345].GetString()); 51 show(doc); 52 printf("end"); 53 }
标签:GetString,--,doc,key,printf,序列化,12345,rapidjson From: https://www.cnblogs.com/lzpong/p/18406468