Search code examples
c++jsonstringcoutrapidjson

How to print or to_string() a nested JSON with unknown structure using Rapidjson


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;
}


Solution

  • 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;
    }