rapidjson 的封装学习

2021-05-01 18:26

阅读:489

标签:生成   ons   null   smo   tps   doc   star   int   obj   

#pragma once
#include "Util.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/document.h"  

NAMESPACEBEGIN(DEF)
//参考 https ://github.com/qicosmos/SmartDB1.03/blob/master/JsonCpp.hpp
//做了部分修改
class RapidJsonWraptor {
	typedef rapidjson::Writer<:stringbuffer> JsonWriter;
public:
	RapidJsonWraptor() :m_writer(m_buf) {}
	~RapidJsonWraptor() {}
	void WriteArrayKey(std::string Key) {
		m_writer.Key(Key.c_str());
	}
	void StartArray() {
		m_writer.StartArray();
	}

	void EndArray() {
		m_writer.EndArray();
	}

	void StartObject()
	{
		m_writer.StartObject();
	}

	void EndObject()
	{
		m_writer.EndObject();
	}

	template
	void WriteJson(std::string& key, T&& value) {
		m_writer.Key(key.c_str());
		WriteValue(std::forward(value));
	}

	template
	void WriteJson(const char* key, T&& value) {
		m_writer.String(key);
		WriteValue(std::forward(value));
	}
	template
	void WriteArrayContent(T&& value) {
		WriteValue(std::forward(value));
	}

	const char* GetString()const {
		return m_buf.GetString();
	}
private:
	template
	typename std::enable_if<:is_same int>::value>::type WriteValue(V value)
	{
		m_writer.Int(value);
	}

	template
	typename std::enable_if<:is_same unsigned int>::value>::type WriteValue(V value)
	{
		m_writer.Uint(value);
	}

	template
	typename std::enable_if<:is_same int64_t>::value>::type WriteValue(V value)
	{
		m_writer.Int64(value);
	}

	template
	typename std::enable_if<:is_floating_point>::value>::type WriteValue(V value)
	{
		m_writer.Double(value);
	}

	template
	typename std::enable_if<:is_same bool>::value>::type WriteValue(V value)
	{
		m_writer.Bool(value);
	}

	template
	typename std::enable_if<:is_pointer>::value>::type WriteValue(V value)
	{
		m_writer.String(value);
	}

	template
	typename std::enable_if<:is_array>::value>::type WriteValue(V value)
	{
		m_writer.String(value);
	}

	template
	typename std::enable_if<:is_same std::nullptr_t>::value>::type WriteValue(V value)
	{
		m_writer.Null();
	}

	template
	typename std::enable_if<:is_same std::string>::value>::type WriteValue(V value)
	{
		m_writer.String(value.c_str());
	}
private:
	rapidjson::StringBuffer m_buf;
	JsonWriter m_writer;
	rapidjson::Document m_doc;
};


NAMESPACEEND

  

// UseRapidJsonSample.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include 
#include string>
#include "JsonStringTool.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/document.h"  

int main()
{
    //生成
    DEF::RapidJsonWraptor jsonw;
    {
        jsonw.StartObject();
        jsonw.WriteJson("int", 1);
        jsonw.WriteJson("bool", false);
        std::string s = "string";
        jsonw.WriteJson(s, s);

        jsonw.WriteArrayKey("arraySample1");
        jsonw.StartArray();
        jsonw.WriteArrayContent("1");
        jsonw.WriteArrayContent("2");
        jsonw.WriteArrayContent("3");
        jsonw.WriteArrayContent(1);
        jsonw.WriteArrayContent(2);
        jsonw.WriteArrayContent(3);
        jsonw.EndArray();
        jsonw.WriteJson("int", 1);
        jsonw.WriteJson("bool", false);
        jsonw.EndObject();
    }

    std::string json_str = jsonw.GetString();
    std::cout  std::endl;

    //修改json_str;
    rapidjson::Document document;
    document.Parse(json_str.c_str());
    rapidjson::Value& new_string = document["string"];
    new_string.SetString("new_string", strlen("new_string"), document.GetAllocator());

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

    return 0;
}

 

rapidjson 的封装学习

标签:生成   ons   null   smo   tps   doc   star   int   obj   

原文地址:http://www.cnblogs.com/itdef/p/7783145.html


评论


亲,登录后才可以留言!