ibireme / yyjson

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

Parsing json using yyjson_read failed #126

Closed hyhtemple closed 1 year ago

hyhtemple commented 1 year ago

Recently, I wanted to parse the json by yyjson, when I use yyjson_read function, for example char g_szJson[]={\"MotorVehicleID\":\"220204280513212220560220230525145700000010200001\",\"TollgateID\":\"22020428051211222056\",\"TollgateName\":\"越山路-光华路\",\"LaneNo\":2,\"DeviceID\":\"22020428051321222056\",\"EquipmentType\":\"01\",\"PassTime\":\"2023-05-25 14:57:00.690\",\"NameOfPassedRoad\":\"越山路-光华路\",\"HasPlate\":false,\"PlateNo\":\"00000000\",\"PlateClass\":\"99\",\"PlateColor\":\"7\",\"Speed\":0,\"Direction\":\"1\",\"DrivingStatusCode\":\"01\",\"VehicleClass\":\"X99\",\"VehicleBrand\":\"0\",\"PlateNumber\":1,\"LimitedSpeed\":0,\"MarkedSpeed\":0,\"Pictures\":[{\"Type\":2,\"ShotTime\":\"2023-05-25 14:57:00.690\",\"Fmt\":\"jpg\",\"Ref\":0}],\"IllegalCode\":\"0\",\"IllegalDesc\":\"\",\"PlaceCode\":\"\",\"PlaceName\":\"越山路-光华路\",\"HDirection\":\"2\",\"VehicleClass2\":\"X99\",\"OrgNo\":\"320500000900\"};

int nLen = strlen(g_szJson); // 770 bytes yyjson_read(g_szJson,nLen); // it's ok

int nLen = strlen(g_szJson)+1; //771 bytes yyjson_read(g_szJson,nLen); // it's error

In the same situation, I use Tencent's rapidjson, but the analysis of both situations is normal. I wonder if yyjson can do the same, and how can it be solved?

ibireme commented 1 year ago

yyjson_read requires you to provide the exact length of the JSON data. When you incorrectly add 1 to the length, an extra `\0 character is added to the end of the JSON data. The JSON standard does not allow this character, and yyjson reports it as an error.

hyhtemple commented 1 year ago

I tried taking YYJSON_WRITE_ESCAPE_UNICODE flag to the yyjson _read and successful

ibireme commented 1 year ago

I tried taking YYJSON_WRITE_ESCAPE_UNICODE flag to the yyjson _read and successful

YYJSON_WRITE_ESCAPE_UNICODE should only be used for writing. The flag should be YYJSON_READ_STOP_WHEN_DONE for yyjson_read.