C++ Pass JSON object to string - jsoncpp

1.7k views Asked by At

I have a code that pass a json data to a function by pointer of void. This code works with a json string.

#include <iostream>
#include <string>

struct funcInputs{
  bool bVolume;
  std::string data;

  void read() {  
    std::cout<<" userdata: "<<data<<std::endl;  
  };
};
void test(void *input)  {

   //Read input
   funcInputs *inputVar = (reinterpret_cast< funcInputs *> (input));
   inputVar->read();

 }
int main()
{

   std::string s= "{\"lowerThreshold\":200,\"upperThreshold\":1080}";
   funcInputs arg ={true,s};
   test(reinterpret_cast<void *>(&arg));
}

It prints the result:

userdata: {"lowerThreshold":200,"upperThreshold":1080}

Now if my json is an object, like this:

{"lowerThreshold":200,"upperThreshold":1080}

While passing json object to string, I used the below code

  Json::StreamWriterBuilder builder;
  builder.settings_["indentation"] = "";
  std::string s = Json::writeString(builder, jsonObj);
  funcInputs arg ={true,s};
  test(reinterpret_cast<void *>(&arg));

And this give me a bad result:

userdata: ���hreshold":200,"upperThreshold":1080}

Can you help me correct this error?

0

There are 0 answers