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

need help with copying text strings (not just reference them) #2275

Open
u1550l opened this issue May 2, 2024 · 0 comments
Open

need help with copying text strings (not just reference them) #2275

u1550l opened this issue May 2, 2024 · 0 comments

Comments

@u1550l
Copy link

u1550l commented May 2, 2024

I have problems with assigning strings to a document.
Obviously this is caused by the copy/link mechanism.

my code:
(note: to keep the code snippet small, I removed all return value checks)

rapidjson::Document myDoc;
rapidjson::Value objValue;

setJsonHeader(std::string &jsonstr)
{
   rapidjson::Document d;
   //>>> this would work: >>> static rapidjson::Document d;

   d.SetObject();
   d.Parse(jsonstr.c_str());


   objValue.SetObject();
   objValue.AddMember("rpc", d["header"]["rpc"], myDoc.GetAllocator());
   objValue.AddMember("uid", d["header"]["uid"], myDoc.GetAllocator());

   myDoc.RemoveAllMembers();
   myDoc.AddMember("header", objValue, myDoc.GetAllocator());
}

before calling the function, jsonstr contains:

{
    "header": {
        "rpc": "hereIsSomeTextString",
        "uid": 42
    },
    ...
}

inside setJsonHeader(), all behaves as expected.
But after calling function setJsonHeader(), myDoc looks like this:

{
    "header": {
        "rpc": " \u0015\u0001¦¦¦\u0000\u0000omeTextString",
        "uid": 42
    }
}

the string is not valid any more.

My current workaround:
use the "static" declaration of ["d"](rapidjson::Document d)
but then the function is no more re-entrant.

My question:
how can I force rapidjson to actualy copy the contents (i.e. std:strings) instead of just linking them?

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

1 participant