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

Trying to get the raw JSON payload of a yyjson_val object #123

Closed eraychumak closed 1 year ago

eraychumak commented 1 year ago

I've went through the documentation numerous times and hit a bit of a roadblock; just to point out, my experience with C++ is still very new so I might be overseeing something entirely, however, I hope to get some support here.

I am trying to get the raw JSON payload as a string of a particular object, however, it keeps saying: 0x0000000000000330 <Error reading characters of string.>

image

The col variable is defined as so:

yyjson_val* col = yyjson_obj_get(location, heading.c_str());

Here's what I've tried so far:

If anyone can guide me with this issue, it would be much appreciated! 😁

Also, if more information is needed, please do ask.

ibireme commented 1 year ago

yyjson does not preserve the original JSON data, so you can't access the raw JSON payload through col->uni.str.

The content of yyjson_val_uni is determined by the type of yyjson_val. For example, col is an array, then col->uni.ofs stores the offset from this array to the next value. You can find more explanations here: https://github.com/ibireme/yyjson/blob/master/doc/DataStructure.md

If you want to print col, you can do this:

const char *str = yyjson_val_write(col, 0, NULL);
// print str
if (str) free(str);
eraychumak commented 1 year ago

Ah, thanks for the insight!