#pragma once #include #define RAPIDJSON_HAS_STDSTRING 1 #include "rapidjson/document.h" #include "rapidjson/encodings.h" #include "rapidjson/stringbuffer.h" #include "rapidjson/filereadstream.h" #include "rapidjson/filewritestream.h" #include "rapidjson/encodedstream.h" #include "rapidjson/prettywriter.h" #ifdef AECRXBASE_EXPORT #define AECDLLIMPEXP __declspec(dllexport) #else #define AECDLLIMPEXP __declspec(dllimport) #endif namespace SHJson { using namespace rapidjson; typedef GenericDocument > WDocument; typedef GenericValue > WValue; typedef const GenericValue > WCValue; typedef GenericArray Arr; typedef GenericArray CArr; typedef WValue::ConstMemberIterator MCIt; typedef WValue::MemberIterator MIt; typedef WValue::ConstValueIterator VCIt; typedef WValue::ValueIterator VIt; typedef GenericStringBuffer > WStringBuffer; typedef PrettyWriter> WPrettyWriter; typedef Writer> WWriter; AECDLLIMPEXP WDocument::AllocatorType &getJsonAllocator(); AECDLLIMPEXP std::wstring write2Json(const WValue &v, bool pretty = false); AECDLLIMPEXP WDocument &string2Json(LPCTSTR json, WDocument &doc); AECDLLIMPEXP bool json2File(LPCTSTR path, const WValue &v, bool pretty = false); AECDLLIMPEXP bool file2Json(LPCTSTR path, WDocument &doc); AECDLLIMPEXP WValue& addMember(WValue &object, LPCTSTR name, WValue &value); AECDLLIMPEXP WValue& addMember(WValue &object, LPCTSTR name, const int &value); AECDLLIMPEXP WValue& addMember(WValue &object, LPCTSTR name, const unsigned &value); AECDLLIMPEXP WValue& addMember(WValue &object, LPCTSTR name, const __int64 &value); AECDLLIMPEXP WValue& addMember(WValue &object, LPCTSTR name, const unsigned __int64 &value); AECDLLIMPEXP WValue& addMember(WValue &object, LPCTSTR name, const bool &value); AECDLLIMPEXP WValue& addMember(WValue &object, LPCTSTR name, const double &value); AECDLLIMPEXP WValue& addMember(WValue &object, LPCTSTR name, const float &value); AECDLLIMPEXP WValue& addMember(WValue &object, LPCTSTR name, LPCTSTR value); AECDLLIMPEXP WValue& pushBack(WValue &arr, WValue &value); AECDLLIMPEXP WValue& pushBack(WValue &arr, const int &value); AECDLLIMPEXP WValue& pushBack(WValue &arr, const unsigned &value); AECDLLIMPEXP WValue& pushBack(WValue &arr, const __int64 &value); AECDLLIMPEXP WValue& pushBack(WValue &arr, const unsigned __int64 &value); AECDLLIMPEXP WValue& pushBack(WValue &arr, const bool &value); AECDLLIMPEXP WValue& pushBack(WValue &arr, const double &value); AECDLLIMPEXP WValue& pushBack(WValue &arr, const float &value); AECDLLIMPEXP WValue& pushBack(WValue &arr, LPCTSTR value); class AECDLLIMPEXP SimpleRead { public: enum Error { eSuccess = 0, eParseError, eNotObject, eNotArray, eNoMember, eArrayOver, eCanNotValue, }; public: SimpleRead(LPCTSTR pszJson); SimpleRead(Error err):_v(_doc), _err(err) { _doc.SetNull(); } SimpleRead(WValue &v):_v(v), _err(eSuccess) { _doc.SetNull(); }; SimpleRead& operator [] (LPCTSTR key) const; // 对象读取,类似 std::map[key] SimpleRead& operator [] (int idx) const; // 数组对象按照索引读取 template T value() const { static char buffer[sizeof(T)]; if (_err != eSuccess) { return new (buffer) T(); } if (_v.Is()) return _v.Get(); else { _err = eCanNotValue; return new (buffer) T(); } } public: WValue &_v; WDocument _doc; mutable Error _err; }; }