SoFunction
Updated on 2025-03-10

Methods to continue to simplify rapidjson assembly in C++

Rapidjson assembly continues to simplify----- Life is short, I use Rapidjson

The easiest thing to see:

#include <iostream>
#include <>
#include<>
#include <sys/>
#include <sys/>
#include <>
#include<sstream>
// Please download the open source rapidjson yourself#include "rapidjson/"
#include "rapidjson/"
#include "rapidjson/"
#include "rapidjson/"
#include "rapidjson/"
#include "rapidjson/"
using namespace std;
using rapidjson::Document;
using rapidjson::StringBuffer;
using rapidjson::Writer;
using namespace rapidjson;
void test()
{
 Document document;
 ();
 Document::AllocatorType& allocator = ();
 Value object(rapidjson::kObjectType);
 ("age", 29, allocator);
 ("name", "taoge", allocator);
 StringBuffer buffer;
 Writer<StringBuffer> writer(buffer);
 (writer);
 string str = ();
 cout << str << endl;
}
int main(int argc, char *argv[])
{
 test();
 return 0;
}

result:

{"age":29,"name":"taoge"}

Look at the array again:

#include <iostream>
#include <>
#include<>
#include <sys/>
#include <sys/>
#include <>
#include<sstream>
// Please download the open source rapidjson yourself#include "rapidjson/"
#include "rapidjson/"
#include "rapidjson/"
#include "rapidjson/"
#include "rapidjson/"
#include "rapidjson/"
using namespace std;
using rapidjson::Document;
using rapidjson::StringBuffer;
using rapidjson::Writer;
using namespace rapidjson;
void test()
{
 Document document;
 ();
 Document::AllocatorType& allocator = ();
 Value array(rapidjson::kArrayType);
 Value object(rapidjson::kObjectType);
 ("age", 30, allocator);
 ("name", "taoge", allocator);
 (object, allocator);
 ("json", array, allocator);
 StringBuffer buffer;
 Writer<StringBuffer> writer(buffer);
 (writer);
 string str = ();
 cout << str << endl;
}
int main(int argc, char *argv[])
{
 test();
 return 0;
}

result:

{"json":[{"age":30,"name":"taoge"}]}

Let’s take a look at one more:

#include <iostream>
#include <>
#include<>
#include <sys/>
#include <sys/>
#include <>
#include<sstream>
// Please download the open source rapidjson yourself#include "rapidjson/"
#include "rapidjson/"
#include "rapidjson/"
#include "rapidjson/"
#include "rapidjson/"
#include "rapidjson/"
using namespace std;
using rapidjson::Document;
using rapidjson::StringBuffer;
using rapidjson::Writer;
using namespace rapidjson;
void test()
{
 Document document;
 ();
 Document::AllocatorType& allocator = ();
 Value array(rapidjson::kArrayType);
 Value object(rapidjson::kObjectType);
 ("age", 30, allocator);
 ("name", "taoge", allocator);
 (object, allocator);
 ("oh1", array, allocator);
 ("oh2", "hehe", allocator);
 StringBuffer buffer;
 Writer<StringBuffer> writer(buffer);
 (writer);
 string str = ();
 cout << str << endl;
}
int main(int argc, char *argv[])
{
 test();
 return 0;
}

result:

{"oh1":[{"age":30,"name":"taoge"}],"oh2":"hehe"}

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support. If you want to know more about it, please see the relevant links below