Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing json using yyjson_read failed #126

Closed
hyhtemple opened this issue May 25, 2023 · 3 comments
Closed

Parsing json using yyjson_read failed #126

hyhtemple opened this issue May 25, 2023 · 3 comments

Comments

@hyhtemple
Copy link

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
Copy link
Owner

ibireme commented May 25, 2023

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
Copy link
Author

I tried taking YYJSON_WRITE_ESCAPE_UNICODE flag to the yyjson _read and successful

@ibireme
Copy link
Owner

ibireme commented May 26, 2023

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.

@ibireme ibireme closed this as completed May 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants