#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<rapidjson::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<typename T> void WriteJson(std::string& key, T&& value) { m_writer.Key(key.c_str()); WriteValue(std::forward<T>(value)); } template<typename T> void WriteJson(const char* key, T&& value) { m_writer.String(key); WriteValue(std::forward<T>(value)); } template<typename T> void WriteArrayContent(T&& value) { WriteValue(std::forward<T>(value)); } const char* GetString()const { return m_buf.GetString(); } private: template<typename V> typename std::enable_if<std::is_same<V, int>::value>::type WriteValue(V value) { m_writer.Int(value); } template<typename V> typename std::enable_if<std::is_same<V, unsigned int>::value>::type WriteValue(V value) { m_writer.Uint(value); } template<typename V> typename std::enable_if<std::is_same<V, int64_t>::value>::type WriteValue(V value) { m_writer.Int64(value); } template<typename V> typename std::enable_if<std::is_floating_point<V>::value>::type WriteValue(V value) { m_writer.Double(value); } template<typename V> typename std::enable_if<std::is_same<V, bool>::value>::type WriteValue(V value) { m_writer.Bool(value); } template<typename V> typename std::enable_if<std::is_pointer<V>::value>::type WriteValue(V value) { m_writer.String(value); } template<typename V> typename std::enable_if<std::is_array<V>::value>::type WriteValue(V value) { m_writer.String(value); } template<typename V> typename std::enable_if<std::is_same<V, std::nullptr_t>::value>::type WriteValue(V value) { m_writer.Null(); } template<typename V> typename std::enable_if<std::is_same<V, 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 <iostream> #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 << json_str << 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<rapidjson::StringBuffer> writer(buffer); document.Accept(writer); std::cout << buffer.GetString() << std::endl; return 0; }