Closed Edwardmark closed 6 months ago
@Edwardmark I don't have much experience with cpp, but you are passing the same doc
pointer to the recursive function convertTreeNodeToJSON
, is this a problem?
@nixzhu Thanks for you reply. I want to convert a tree structure to json, so I have to use the same doc pointer. The problem is about the jsonNode or SubNode in the recursion function. I print the value, it shows when the bottom recursion end, the upper function call cannot get their value, print result is null.
@Edwardmark I don't have much experience with cpp, but you are passing the same
doc
pointer to the recursive functionconvertTreeNodeToJSON
, is this a problem?
It is not about cpp, I want to verify if the doc owns its subnode memory when calling the recursion function? And what should I do to fix the issue? Thanks .
@Edwardmark The doc holds memory for all json values, so the recursive call seems good.
There are two string handling issues:
yyjson_mut_str()
doesn't take ownership of the string. If the passed string is modified or freed during use, errors like \u0000
may occur. Consider using yyjson_mut_strcpy()
instead.yyjson_mut_val_write()
needs to be freed after use.If you share the TreeNode
source code, we can better understand and replicate the issue.
@ibireme Thanks for your reply. It is the problem. I change yyjson_mut_str() to yyjson_mut_strcpy() and it worked. And for problem2, how to free the string? Could you give me a example? Thanks.
@ibireme Thanks for your reply. It is the problem. I change yyjson_mut_str() to yyjson_mut_strcpy() and it worked. And for problem2, how to free the string? Could you give me a example? Thanks.
Just call free(json_str)
.
@ibireme Thanks.
I have a TreeNode class which defines a tree structure. And I want to convert it to json using yyjson.
The expected output should be:
But the actual result in main is NULL, and the cout << json_str << endl; in the recursion function outputs:
It seems that the result is destructed when the function call end, so it returns null. So what is the problem? Why the result is null? Could you please give me some hits? Thanks. @TkTech @lundmark @ibireme @nixzhu @fd00 Thanks.