1

The Programming Language : C++

Environment : Linux Ubuntu

Compiler : gcc (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0

I am parsing a nested JSON with unknown structure ( each key can be a json document ) we don't know the key names and values. RapidJson can parse it but i can not find away to print it or copy it to an string as a string object

I try to Google it and can't find any answer. just the similar one is the link below :

iterate and retrieve nested object in JSON using rapidjson

but in this case they use the key name (not my case) ( my main assumption is that I dont know the json structure including the key names)

Now i have this type of JSON and want to print it or to_string() it.

How can i do this ? Is it possible with RapidJson?

This my sample code : main.cpp

#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include <iostream>

using namespace rapidjson;

void toJson1(Document &oJson)
{
  oJson.SetObject();
  oJson.AddMember("id", rapidjson::Value(1), oJson.GetAllocator());
  const char* json1 = "{\"a\":1,\"b\":\"c\"}";
  Document d1;
  d1.Parse(json1);
  oJson.AddMember("test", d1, oJson.GetAllocator());
  return;
}

void toJson(Document &oJson)
{
  oJson.SetObject();
  oJson.AddMember("id", rapidjson::Value(1), oJson.GetAllocator());
  Document d1;
  toJson1(d1);
  oJson.AddMember("test", d1, oJson.GetAllocator());
  return;
}

int main() {

   Document d;

   toJson(d);

   StringBuffer buffer;
   Writer<StringBuffer> writer(buffer);
   d.Accept(writer);

   std::cout << buffer.GetString() << std::endl;
   return 0;
}

this code is the same as above functionality but it works fine, using the function and passing the object by reference made it corrupted

#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include <iostream>
int main() {

   Document d,d1;

   d.SetObject();
   d.AddMember("id", rapidjson::Value(1), d.GetAllocator()); 

   d1.SetObject();
   d1.AddMember("id", rapidjson::Value(1), d1.GetAllocator()); 

   const char* json1 = "{\"a\":1,\"b\":\"c\"}";
   Document d2;
   d2.Parse(json1);
   d1.AddMember("test", d2, d1.GetAllocator());
   d.AddMember("test", d1, d.GetAllocator());

    StringBuffer buffer;
    Writer<StringBuffer> writer(buffer);
    d.Accept(writer);

    std::cout << buffer.GetString() << std::endl;
    return 0;
}

Mohsen
  • 59
  • 1
  • 8
  • The RapidJSON document type is pretty flexible. It has methods like `IsArray` or `IsObject`, etc to determine the type of the object. And it has methods to iterate arrays and object members once you know the type of the object: https://rapidjson.org/md_doc_tutorial.html . Is all you want to do is reserialize it as JSON? Or do you want to print it in some structured way? – parktomatomi Dec 17 '19 at 06:45
  • 2
    i read the tutorial, but i have a problem with nested JSON object with unknown members, i will create the sample C++ code and will share here to describe my exact error, it takes about 30 min, the main code is very large and i need to create a sample to identify the exact problem. – Mohsen Dec 17 '19 at 06:55
  • You can iterate over GenericObject using either begin/end or MemberBegin/MemberEnd to get all key/value pairs of object node. – Konstantin Stupnik Dec 17 '19 at 07:35
  • i added the sample code that RapidJson will corrupted, while using this structure of code. How can i fix this ? – Mohsen Dec 17 '19 at 08:08

1 Answers1

0

Finally with many tries i found the solution.

in each function we should reuse the Allocator of the outer document

the corrected version of the code above is :

main.cpp


#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include <iostream>

using namespace rapidjson;

void toJson1(Document &oJson)
{
   oJson.SetObject();
   oJson.AddMember("id", rapidjson::Value(1), oJson.GetAllocator()); 
   const char* json1 = "{\"a\":1,\"b\":\"c\"}";
   Document d1(&oJson.GetAllocator());
    d1.Parse(json1);
   oJson.AddMember("test", d1, oJson.GetAllocator());
   return;
}

void toJson(Document &oJson)
{
   oJson.SetObject();
   oJson.AddMember("id", rapidjson::Value(1), oJson.GetAllocator()); 
   Document d1(&oJson.GetAllocator());
    toJson1(d1);

   oJson.AddMember("test", d1, oJson.GetAllocator());
   return;
}

int main() {

    Document d;

    toJson(d);

    StringBuffer buffer;
    Writer<StringBuffer> writer(buffer);
    d.Accept(writer);

    std::cout << buffer.GetString() << std::endl;
    return 0;
}

Mohsen
  • 59
  • 1
  • 8