107 lines
3.5 KiB
C++
107 lines
3.5 KiB
C++
#pragma once
|
|
|
|
#include <string>
|
|
|
|
#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<UTF16<> > WDocument;
|
|
typedef GenericValue<UTF16<> > WValue;
|
|
typedef const GenericValue<UTF16<> > WCValue;
|
|
typedef GenericArray<false, WValue> Arr;
|
|
typedef GenericArray<true, WValue> CArr;
|
|
typedef WValue::ConstMemberIterator MCIt;
|
|
typedef WValue::MemberIterator MIt;
|
|
typedef WValue::ConstValueIterator VCIt;
|
|
typedef WValue::ValueIterator VIt;
|
|
typedef GenericStringBuffer<UTF16<> > WStringBuffer;
|
|
typedef PrettyWriter<WStringBuffer, UTF16<>> WPrettyWriter;
|
|
typedef Writer<WStringBuffer, UTF16<>> 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 <typename T>
|
|
T value() const
|
|
{
|
|
static char buffer[sizeof(T)];
|
|
|
|
if (_err != eSuccess)
|
|
{
|
|
return new (buffer) T();
|
|
}
|
|
|
|
if (_v.Is<T>())
|
|
return _v.Get<T>();
|
|
else
|
|
{
|
|
_err = eCanNotValue;
|
|
return new (buffer) T();
|
|
}
|
|
}
|
|
public:
|
|
WValue &_v;
|
|
WDocument _doc;
|
|
mutable Error _err;
|
|
};
|
|
}
|
|
|