2

I have the following C++ code to generate a JSON-string:

StringBuffer JSONData;
PrettyWriter<StringBuffer> writer(JSONData);
writer.StartObject();
writer.String("hello");
writer.String("world");
writer.String("t");
writer.Bool(true);
writer.String("f");
writer.Bool(false);
writer.String("n");
writer.Null();
writer.String("i");
writer.Uint(123);
writer.String("pi");
writer.Double(3.1416);
writer.EndObject();

cout << JSONData.GetString() << endl;
//OUTPUTS:
{
    "hello": "world",
    "t": true,
    "f": false,
    "n": null,
    "i": 123,
    "pi": 3.1416,
}


JSONData.Clear();
writer.Reset(JSONData); //Wont compile
writer.StartObject(); //FAILS !Base::hasRoot_
writer.String("hello");
writer.String("world");
writer.String("t");
writer.Bool(true);
writer.EndObject();
cout << JSONData.GetString() << endl;

However, I want to create another JSON-object after I outputted the previous JSON data. This however returns me the error Assertion failed: !Base::hasRoot_. How can I fix this?

TVA van Hesteren
  • 1,031
  • 3
  • 20
  • 47

1 Answers1

2

This link is the RapidJSON header for PrettyWriter. You can find at line 247 the error your encountering. I am no rapidJSON pro, but I think a writer only works for one object, even though your cleared it. I know this is not a complete answer, but thought it would be a hint anyway.

Also, this link could give you hints about how to use PrettyWriter.

You might want to use reset(), RapidJSON documentation could help.

This function reset the writer with a new stream and default settings, in order to make a Writer object reusable for output multiple JSONs.

This very documentation provides the following example :

Writer<OutputStream> writer(os1);
writer.StartObject();
// ...
writer.EndObject();
writer.Reset(os2);
writer.StartObject();
// ...
writer.EndObject();

You could try something like this :

StringBuffer JSONData;
PrettyWriter<StringBuffer> writer(JSONData);
writer.StartObject();
// ...
writer.EndObject();

JSONData.Clear();
writer.Reset(JSONData);
writer.StartObject();
// ...
writer.EndObject();

You could even create another output stream and parse it to the reset, just to be sure..

Community
  • 1
  • 1
Badda
  • 1,329
  • 2
  • 15
  • 40