ibireme / yyjson

The fastest JSON library in C
https://ibireme.github.io/yyjson/doc/doxygen/html/
MIT License
3.04k stars 262 forks source link

Some fields are written to json, but there are garbled characters in the fields that generate json. #94

Closed hyhtemple closed 1 year ago

hyhtemple commented 1 year ago

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

    yyjson_mut_doc_set_root(pdoc,root);

    //MessageID
    yyjson_mut_obj_add_str(pdoc,root,"MessageID",struuid.c_str());

    //SourceDeviceID
    yyjson_mut_obj_add_str(pdoc,root,"SourceDeviceID",CpssConfig::GetInstance()->m_sourceDeviceID.c_str());

    //Uri
    yyjson_mut_obj_add_str(pdoc,root,"URI","/VIID/Faces");

    //Method 
    yyjson_mut_obj_add_str(pdoc,root,"Method","POST");

            //........................

    //LocationMarkTime
    if (m_UsedPerson->LocationMarkTime.bUsed)
    {           yyjson_mut_obj_add_str(pdoc,pPersonData,"LocationMarkTime",CFuncLib::TimeToDataTime(m_LocationMarkTime).c_str());
    }   
    //PersonAppearTime
    if (m_UsedPerson->PersonAppearTime.bUsed)
    {           yyjson_mut_obj_add_str(pdoc,pPersonData,"PersonAppearTime",CFuncLib::TimeToDataTime(m_PersonAppearTime).c_str());
    }   

    //PersonDisAppearTime
    if (m_UsedPerson->PersonDisAppearTime.bUsed)
    {           yyjson_mut_obj_add_str(pdoc,pPersonData,"PersonDisAppearTime",CFuncLib::TimeToDataTime(m_PersonDisAppearTime).c_str());
    }   
ibireme commented 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

hyhtemple commented 1 year ago

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.