rapidjson的坑

參考

https://my.oschina.net/zi6xuan/blog/746103

https://blog.csdn.net/wwwasw/article/details/76473165

查看源碼註釋得知:

情形一:
string strValue("abc");
rapidjson::Value v;
v.SetString(strValue.c_str(), strValue.length(), doc.GetAllocator());  //這個函數是拷貝字符串的
//! Set this value as a string by copying from source string.

v.SetString(strValue.c_str(), strValue.length();//這個函數是不拷貝字符串的
//! Set this value as a string without copying source string.

注意事項:
string strTest("abc");
jsonArray.PushBack(StringRef(strTest.c_str(),doc.GetAllocator());//一定要注意strTest的生命週期
StringBuffer buffer;
#ifdef _DEBUG
            PrettyWriter<StringBuffer> writer(buffer);
#else
            Writer<StringBuffer> writer(buffer);
#endif
           doc.Accept(writer); //序列化時,strTest必須沒有被釋放。如果確保正確,使用拷貝

使用方法
string strValue("abc");
rapidjson::Value v;
v.SetString(strValue.c_str(), strValue.length(), doc.GetAllocator());  //這個函數是拷貝字符串的

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章