#include "rapidjson/document.h" // rapidjson's DOM-style API
#include "rapidjson/prettywriter.h" // for stringify JSON
#include "rapidjson/filestream.h" // wrapper of C stream for prettywriter as output
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"
#include <cstdio>
#include <string>
#include <fstream>
//通过RapidJson解析
int parseJson(const char* json)
{
if ( NULL == json || 0 == json[0] )
return -1;
rapidjson::Document doc;
doc.Parse<0>(json);
if (doc.HasParseError()) //解析错误
return -2;
printf("\nParsing to document succeeded.\n");
assert(doc.IsObject()); // Document is a JSON value represents the root of DOM. Root can be either an object or array.
//string类型
if ( doc.HasMember("hello") ) //判断是否有此参数
{
if ( doc["hello"].IsString() ) //判断是否为字符串类型
{
printf("hello = %s\n", doc["hello"].GetString());
}
}
//bool类型
assert(doc["t"].IsBool()); // JSON true/false are bool. Can also uses more specific function IsTrue().
printf("t = %s\n", doc["t"].GetBool() ? "true" : "false");
assert(doc["f"].IsBool());
printf("f = %s\n", doc["f"].GetBool() ? "true" : "false");
//null类型
printf("n = %s\n", doc["n"].IsNull() ? "null" : "?");
//int整型
assert(doc["i"].IsNumber()); // Number is a JSON type, but C++ needs more specific type.
assert(doc["i"].IsInt()); // In this case, IsUint()/IsInt64()/IsUInt64() also return true.
printf("i = %d\n", doc["i"].GetInt()); // Alternative (int)document["i"]
//double类型
assert(doc["pi"].IsNumber());
assert(doc["pi"].IsDouble());
printf("pi = %g\n", doc["pi"].GetDouble());
//数组
{
const rapidjson::Value& a = doc["a"]; // Using a reference for consecutive access is handy and faster.
assert(a.IsArray());
for (unsigned i = 0; i < a.Size(); i++) // rapidjson uses SizeType instead of size_t.
printf("a[%d] = %d\n", i, a[i].GetInt());
// Note:
//int x = a[0].GetInt(); // Error: operator[ is ambiguous, as 0 also mean a null pointer of const char* type.
int y = a[unsigned(0)].GetInt(); // Cast to SizeType will work.
int z = a[0u].GetInt(); // This works too.
}
/////////////////////////////////////////////////////////////////////////////////////////////////
//修改
// Change i to a bigger number
{
uint64_t f20 = 1; // compute factorial of 20
for (uint64_t j = 1; j <= 20; j++)
f20 *= j;
doc["i"] = f20; // Alternate form: document["i"].SetUint64(f20)
assert(!doc["i"].IsInt()); // No longer can be cast as int or uint.
}
// Adding values to array.
{
rapidjson::Value& a = doc["a"]; // This time we uses non-const reference.
rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();
for (int i = 5; i <= 10; i++)
a.PushBack(i, allocator); // May look a bit strange, allocator is needed for potentially realloc. We normally uses the document's.
// Fluent API
a.PushBack("Lua", allocator).PushBack("Mio", allocator);
}
// This version of SetString() just store the pointer to the string.
// So it is for literal and string that exists within value's life-cycle.
{
doc["hello"] = "rapidjson"; // This will invoke strlen()
// Faster version:
// document["hello"].SetString("rapidjson", 9);
}
// This version of SetString() needs an allocator, which means it will allocate a new buffer and copy the the string into the buffer.
rapidjson::Value author;
{
char buffer[10];
int len = sprintf(buffer, "%s %s", "Milo", "Yip"); // synthetic example of dynamically created string.
author.SetString(buffer, len, doc.GetAllocator());
// Shorter but slower version:
// document["hello"].SetString(buffer, document.GetAllocator());
// Constructor version:
// Value author(buffer, len, document.GetAllocator());
// Value author(buffer, document.GetAllocator());
memset(buffer, 0, sizeof(buffer)); // For demonstration purpose.
}
// Variable 'buffer' is unusable now but 'author' has already made a copy.
doc.AddMember("author", author, doc.GetAllocator());
assert(author.IsNull()); // Move semantic for assignment. After this variable is assigned as a member, the variable becomes null.
////////////////////////////////////////////////////////////////////////////
//// 4. Stringify JSON
printf("\nModified JSON with reformatting:\n");
rapidjson::FileStream f(stdout);
rapidjson::PrettyWriter<rapidjson::FileStream> writer(f);
doc.Accept(writer); // Accept() traverses the DOM and generates Handler events.
////////////////////////////////////////////////////////////////////////////
{
//5 格式为新字符串
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
doc.Accept(writer);
std::string strJson(buffer.GetString(), buffer.Size());
printf("\n\n New JSON:\n%s\n", strJson.c_str());
//6 保存为文件
std::string filepath = ("test.json");
std::ofstream outfile;
outfile.open(filepath.c_str());
if (outfile.fail())
return -3;
outfile << strJson;
outfile.close();
}
return 0;
}
//直接组装Json
int packetJson(void)
{
rapidjson::Document doc;
doc.SetObject();
Value vElem;
vElem.SetArray();
for (int i = 0; i < 5; ++i)
{
rapidjson::Value vElemItem;
vElemItem.SetInt(i);
vElem.PushBack(vElemItem, doc.GetAllocator());
rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();
vElem.PushBack("Lua", allocator).PushBack("Mio", allocator);
}
doc.AddMember("Data", vElem, doc.GetAllocator());
{
doc["hello"] = "rapidjson"; // This will invoke strlen()
// Faster version:
// document["hello"].SetString("rapidjson", 9);
}
{
//5 格式为新字符串
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
doc.Accept(writer);
std::string strJson(buffer.GetString(), buffer.Size());
printf("\n\n New JSON:\n%s\n", strJson.c_str());
//6 保存为文件
std::string filepath = ("test.json");
std::ofstream outfile;
outfile.open(filepath.c_str());
if (outfile.fail())
return -3;
outfile << strJson;
outfile.close();
}
}
#include <conio.h>
void main()
{
const char json[] = " { \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3, 4] } ";
printf("Original JSON:\n %s\n", json);
int ret = parseJson(json);
_getch();
}