1

Trying to serialize a class I have into a json string. Of course I can just write the strings manually, but I was hoping to utilize a the picojson library.

What I am trying to achieve is, given this class A

class A {
  public:
    int field1;
    string field2;
}

A a;
a.field1 = 1;
a.field2 = "example";

I want to convert this class instance "a" into a picojson object and then call picojson::serialize() to get the json string.

{"field1": 1, "field2": "example"}
1
  • Does this answer your question? Commented Jun 22, 2020 at 17:49

1 Answer 1

4

Well completely of the top of my head, and just by looking at the header file, it seems you need something like this

using namespace picojson;

object o;
o["field1"] = value(static_cast<double>(a.field1));
o["field2"] = value(a.field2);
std::cout << value(o);

or (what you actually asked for)

std::string s = value(o).serialize();
Sign up to request clarification or add additional context in comments.

1 Comment

Well I've now tested the above code, and made one small correction (added the static_cast), so you should be good to go.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.