Boardy
Boardy

Reputation: 36225

Replacing value of a member in rapidjson

I am currently working on a project in C++ using rapidjson.

My program receives some JSON data on a socket which includes some authentication details. I log the incoming message, but I want to hide the password so it can't be seen in the log file. So I am trying to get the JSON object, and replace each character of the string and put this replaced string back into the json object where the password was.

Below is the code that I have:

rapidjson::Document jsonObject;
            jsonObject.Parse(command.c_str());

            string method = jsonObject["method"].GetString();

            if (jsonObject.HasMember("sshDetails"))
            {
                Value& sshDetails = jsonObject["sshDetails"];
                string sshPassword = sshDetails["sshPassword"].GetString();

                for (int i = 0; i < sshPassword.length(); i++)
                {
                    sshPassword[i] = '*';
                }

                rapidjson::Value::Member* sshPasswordMember = sshDetails.FindMember("sshPassword");
                sshPasswordMember->name.SetString(sshPassword.c_str(), jsonObject.GetAllocator());

                //Convert it back to a string
                rapidjson::StringBuffer buffer;
                buffer.Clear();
                rapidjson::Writer<rapidjson::StringBuffer>writer(buffer);
                Document jsonDoc;
                jsonDoc.Accept(writer);
                string jsonString = string(buffer.GetString());

I'm getting an error on the following line:

rapidjson::Value::Member* sshPasswordMember = sshDetails.FindMember("sshPassword");

The error I am getting is:

No suitable conversion function from rapidjson::GenericMemberIterator<false, rapidjson::UTF8<char>, rapidjson::MemoryPoolAllocator<rapidjson::CtrlAllocator>> to rapidjson::GenericMember::UTF8<char>, myProject...SocketProcessor.cpp
rapidjson::MemoryPoolAllocator<rapidjson::CtrlAllocator>>*exists

I took the above from an example on another question on SO which was an accepted answer from rapidjson - change key to another value, so what am I missing.

Upvotes: 2

Views: 9409

Answers (2)

using rapidjson in my project I found out that many of such problems can be omitted by the use of auto instead of specifying the type

Upvotes: -1

Boardy
Boardy

Reputation: 36225

I've managed to find the answer to this with a bit of playing round and luck.

I changed

rapidjson::Value::Member* sshPasswordMember = sshDetails.FindMember("sshPassword");
sshPasswordMember->name.SetString(sshPassword.c_str(), jsonObject.GetAllocator());

to be

rapidjson::Value::MemberIterator sshPasswordMember = sshDetails.FindMember("sshPassword");
sshPasswordMember->value.SetString(sshPassword.c_str(), jsonObject.GetAllocator());

Upvotes: 4

Related Questions