Closed hyhtemple closed 1 year ago
yyjson_mut_obj_add_str()
does not copy the input string. If the string is modified before serialization, it will cause your problem. You can use yyjson_mut_obj_add_strcpy()
or yyjson_mut_obj_add_strncpy()
to avoid this problem, for example:
auto timeStr = CFuncLib::TimeToDataTime(m_PersonAppearTime);
yyjson_mut_obj_add_strncpy(pdoc, pPersonData, "PersonAppearTime", timeStr.data(), timeStr.length());
See also: https://github.com/ibireme/yyjson/blob/master/doc/API.md#api-for-string
thank you for you answer my question, now I used another way. by dynamically allocating memory to use the yyjson_mut_obj_add_str.
//ShotTime
if(m_UsedPicData->ShotTime.bUsed)
{
string* pstrShotTime = new string;
vtTimers.push_back(pstrShotTime);
*pstrShotTime = CFuncLib::TimeToDataTime(m_ShotTime,false);
*pstrShotTime += "000";
//string strTemp = ReplaceNullValue(strShortTime);
if (pstrShotTime->empty())
{
yyjson_mut_obj_add_null(pdoc,pSubImageObj,"ShotTime");
}
else
{
yyjson_mut_obj_add_str(pdoc,pSubImageObj,"ShotTime",pstrShotTime->c_str());
}
}
I think it's okay to use your method.
Describe the bug I am working for the project, it needs some fields are wittern to json, but there are garbled characters in the fields that generate json.
Your environment
Additional context here is the generate json, please look at the PersonAppearTime field, I don't know what's wrong is it? //=================================================================================
"URI": "/VIID/Person", "Method": "POST", "X-Statistics": "TraceId=7f27da55bb6e4031bd9078e96494dab9;Camera=34040000111310100055,20220914171604000;CPSS=000905123456785,20220914171604656,20220914171604855,90;", "Content": { "PersonListObject": { "PersonObject": [ { "PersonID": "22020422999999999999999999999999999999999999999", "DeviceID": "34040000111310100055", "InfoKind": 3, "SourceID": "22020422001321001366022020091514153701019", "LeftTopX": 1663, "LeftTopY": 653, "RightBtmX": 1733, "RightBtmY": 753, "LocationMarkTime": "20220914171604000", "PersonAppearTime": "\u0005\u0005\u0000\u0000\u0000\u0000\u0000\u0000�h�\u0000\u0000\u0000\u0000\u0000�", "PersonDisAppearTime": "20220914171604000", "IDType": "990", "IDNumber": "19", //=================================================================================
the code: yyjson_mut_doc pdoc = yyjson_mut_doc_new(NULL); vector<string> vtBufs; yyjson_mut_val *root = yyjson_mut_obj(pdoc);