rapidjson

#include <rapidjson/document.h>
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>
#include <json/json.h>
#include <string.h>
#include <time.h>
using namespace std;

int32_t m_stepCount[2] = { 123, 456 };
int32_t m_peaceReqCount[2] = { 12, 32 };
int32_t m_regretReqCount[2] = { 12, 32 };


const char* jsonStr = "{\"hello\":\"world\", \"t\" : true, \"f\" : false, \"n\" : null, \"i\" : 123, \"pi\" : 3.1416, \"arr\": [1, 2, 3, 4] }";


void Parse()
{
	// 每個 JSON 值都儲存爲 Value 類
	// RapidJSON 的所有公開類型及函數都在 rapidjson 命名空間中。
	rapidjson::Document document;	// Document 類則表示整個 DOM,它存儲了一個 DOM 樹的根 Value
	document.Parse(jsonStr);		// 解析json格式字符串

	// 查詢一下根 Object 中有沒有 "hello" 成員。由於一個 Value 可包含不同類型的值,我們可能需要驗證它的類型,並使用合適的 API 去獲取其值。在此例中,"hello" 成員關聯到一個 JSON String。
	assert(document.HasMember("hello"));	// 是否存在hello成員
	assert(document["hello"].IsString());	// 判斷是否是字符串
	cout << "hello : " << document["hello"].GetString() << endl;// 獲取值

	// JSON True/False 值是以 bool 表示的
	assert(document.HasMember("t"));
	assert(document["t"].IsBool());
	cout << "t : " << document["t"].GetBool() << endl;	// 獲取值

	// JSON Null 值可用 IsNull() 查詢
	assert(document.HasMember("n"));
	assert(document["n"].IsNull());

	// JSON Number 類型表示所有數值。然而,C++ 需要使用更專門的類型。
	assert(document["i"].IsNumber());
	// 在此情況下,IsUint()/IsInt64()/IsUInt64() 也會返回 true
	assert(document["i"].IsInt());
	cout << "i : " << document["i"].GetInt() << endl;	// 獲取值
	// 另一種用法: (int)document["i"]
	assert(document["pi"].IsNumber());
	assert(document["pi"].IsDouble());
	cout << "pi : " << document["pi"].GetDouble() << endl;	// 獲取值

	// JSON Array 包含一些元素。
	// 使用引用來連續訪問,方便之餘還更高效。
	const rapidjson::Value& arr = document["arr"];
	assert(arr.IsArray());
	for (rapidjson::SizeType i = 0; i < arr.Size(); i++) // 使用 SizeType 而不是 size_t,缺省情況下,SizeType 是 unsigned 的 typedef。在多數系統中,Array 最多能存儲 2^32-1 個元素。
		cout << "arr[" << i << "] : " << arr[i].GetInt() << endl;	// 獲取值

	for (rapidjson::Value::ConstValueIterator itr = arr.Begin(); itr != arr.End(); ++itr)
		cout << "arr[" << itr - arr.Begin() << "] : " << itr->GetInt() << endl;	// 獲取值
}

void Create()
{
	// 默認構造函數創建一個 Value 或 Document,它的類型便會是 Null。要改變其類型,需調用 SetXXX() 或賦值操作,
	rapidjson::Document document1;
	document1.SetObject();

	rapidjson::Value v;//NULL
	v.SetInt(10);// v = 10; 簡寫

	//構造函數的各個重載
	//幾個類型也有重載構造函數:
	rapidjson::Value b1(true);    // 調用 Value(bool)
	rapidjson::Value i1(-123);    // 調用 Value(int)
	rapidjson::Value u1(123u);    // 調用 Value(unsigned)
	rapidjson::Value d1(1.5);     // 調用 Value(double)


	// 要重建空 Object 或 Array,可在默認構造函數後使用 SetObject() / SetArray(),或一次性使用 Value(Type):
	rapidjson::Value a(rapidjson::kObjectType);
	rapidjson::Value b(rapidjson::kArrayType);

	// 轉移語義(Move Semantics)
	rapidjson::Value c(123);
	rapidjson::Value d(456);
	d = c;         // c 變成 Null,d 變成數字 123。


	rapidjson::Document document2;
	rapidjson::Value author;
	char buffer1[10];
	int len = sprintf(buffer1, "%s %s", "Milo", "Yip"); // 動態創建的字符串。
	author.SetString(buffer1, len, document2.GetAllocator());
	memset(buffer1, 0, sizeof(buffer1));
	cout << author.GetString() << endl;
	// 清空 buffer 後 author.GetString() 仍然包含 "Milo Yip"




	cout << "1111111111111111111111111111111111111111111111111111111111111111111111" << endl;

	rapidjson::Document::AllocatorType& allocator = document1.GetAllocator();
	rapidjson::Value root(rapidjson::kObjectType);
	document1.AddMember("id", 1, allocator);
	document1.AddMember("num", 2, allocator);


	rapidjson::Value step(rapidjson::kArrayType);
	rapidjson::Value regretReqCount(rapidjson::kArrayType);
	for (int i = 0; i <= 1; i++)
	{
		step.PushBack(m_stepCount[i], allocator);   // 可能需要調用 realloc() 所以需要 allocator
		regretReqCount.PushBack(m_regretReqCount[i], allocator);   // 可能需要調用 realloc() 所以需要 allocator
	}

	document1.AddMember("stepCount", step, allocator);
	document1.AddMember("regretReqCount", regretReqCount, allocator);

	rapidjson::StringBuffer buffer;
	rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
	document1.Accept(writer);
	std::cout << buffer.GetString() << std::endl;

	cout << "1111111111111111111111111111111111111111111111111111111111111111111111" << endl;


	rapidjson::Document::AllocatorType& allocator2 = document2.GetAllocator();
	rapidjson::Value root2(rapidjson::kObjectType);
	root2.AddMember("chairid", 1, allocator2);
	root2.AddMember("redchairid", 2, allocator2);


	rapidjson::Value step1(rapidjson::kArrayType);
	rapidjson::Value regretReqCount1(rapidjson::kArrayType);
	for (int i = 0; i <= 1; i++)
	{
		step1.PushBack(m_stepCount[i], allocator2);   // 可能需要調用 realloc() 所以需要 allocator2
		regretReqCount1.PushBack(m_regretReqCount[i], allocator2);   // 可能需要調用 realloc() 所以需要 allocator2
	}

	root2.AddMember("stepCount", step1, allocator2);
	root2.AddMember("regretReqCount", regretReqCount1, allocator2);


	rapidjson::StringBuffer buffer2;
	rapidjson::Writer<rapidjson::StringBuffer> writer2(buffer2);
	root2.Accept(writer2);
	std::cout << buffer2.GetString() << std::endl;
}
int main()
{
	Parse();
	Create();

	return 0;
}

document.Parse(jsonStr);之後

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章