#include "StdAfx.h" #include "shJson.h" #include "Base64.h" using namespace SHJson; namespace { long getFileSize(FILE *fp) { if (!fp) return 0L; fseek(fp, 0L, SEEK_END); long fSize = ftell(fp); fseek(fp, 0L, 0L); return fSize; } } namespace SHJson { WDocument::AllocatorType &SHJson::getJsonAllocator() { static WDocument g_doc; return g_doc.GetAllocator(); } std::wstring write2Json(const WValue &v, bool pretty) { WStringBuffer buffer; if (pretty) { PrettyWriter, UTF16<>> writer(buffer); v.Accept(writer); } else { Writer, UTF16<>> writer(buffer); v.Accept(writer); } return buffer.GetString(); } WDocument& string2Json(LPCTSTR json, WDocument &doc) { doc.Parse(json); if (doc.HasParseError()) { static WDocument docTmp; return docTmp; } return doc; } bool json2File(LPCTSTR path, const WValue &v, bool pretty) { if (!path) return false; std::wstring ws = write2Json(v, pretty); if (ws.empty()) return false; std::string s = Base64::unicode2Ansii(ws.c_str(), CP_UTF8); CFile f; CFileException e; if (!f.Open(path, CFile::modeCreate | CFile::modeReadWrite, &e)) { CString buffer; e.GetErrorMessage(buffer.GetBuffer(1024), 1024); AfxMessageBox(buffer); buffer.ReleaseBuffer(); return false; } f.Write((char *)s.c_str(), sizeof(char) * s.size()); f.Close(); return true; } bool file2Json(LPCTSTR pszJson, WDocument &doc) { bool brel(false); if (!pszJson) return false; if (_taccess(pszJson, 0x0) != 0x0) return false; FILE *fp(NULL); char *readBuffer(NULL); do { fp = _tfopen(pszJson, _T("rb")); if (!fp) break; long fSize = getFileSize(fp); ++fSize; readBuffer = new char[fSize]; ::memset(readBuffer, 0, sizeof(char) * (fSize)); FileReadStream bis(fp, readBuffer, fSize); AutoUTFInputStream eis(bis); doc.ParseStream >(eis); if (readBuffer) delete[] readBuffer; if (fp) fclose(fp); return !doc.HasParseError(); } while (false); return false; } WValue& addMember(WValue &v, LPCTSTR name, WValue &value) { if (!name) { static WValue vTmp; return vTmp; } if (!v.IsObject()) v.SetObject(); MIt it = v.FindMember(name); if (it != v.MemberEnd()) { it->value.Swap(value); } else { WValue vName; vName.SetString(name, getJsonAllocator()); v.AddMember(vName, value, getJsonAllocator()); } return v; } WValue &addMember(WValue &v, LPCTSTR name, const int &value) { WValue vv(value); return addMember(v, name, vv); } WValue &addMember(WValue &v, LPCTSTR name, const bool &value) { WValue vv(value); return addMember(v, name, vv); } WValue &addMember(WValue &v, LPCTSTR name, const unsigned &value) { WValue vv(value); return addMember(v, name, vv); } WValue &addMember(WValue &v, LPCTSTR name, const __int64 &value) { WValue vv(value); return addMember(v, name, vv); } WValue &addMember(WValue &v, LPCTSTR name, const unsigned __int64 &value) { WValue vv(value); return addMember(v, name, vv); } WValue &addMember(WValue &v, LPCTSTR name, const double &value) { WValue vv(value); return addMember(v, name, vv); } WValue &addMember(WValue &v, LPCTSTR name, const float &value) { WValue vv(value); return addMember(v, name, vv); } WValue &addMember(WValue &v, LPCTSTR name, LPCTSTR value) { WValue vv; vv.SetString(value, getJsonAllocator()); return addMember(v, name, vv); } WValue &pushBack(WValue &arr, WValue &value) { if (!arr.IsArray()) arr.SetArray(); arr.PushBack(value, getJsonAllocator()); return arr; } WValue &pushBack(WValue &arr, const int &value) { WValue vv(value); return pushBack(arr, vv); } WValue &pushBack(WValue &arr, const unsigned &value) { WValue vv(value); return pushBack(arr, vv); } WValue &pushBack(WValue &arr, const __int64 &value) { WValue vv(value); return pushBack(arr, vv); } WValue &pushBack(WValue &arr, const unsigned __int64 &value) { WValue vv(value); return pushBack(arr, vv); } WValue &pushBack(WValue &arr, const bool &value) { WValue vv(value); return pushBack(arr, vv); } WValue &pushBack(WValue &arr, const double &value) { WValue vv(value); return pushBack(arr, vv); } WValue &pushBack(WValue &arr, const float &value) { WValue vv(value); return pushBack(arr, vv); } WValue &pushBack(WValue &arr, LPCTSTR value) { WValue vv; vv.SetString(value, getJsonAllocator()); return pushBack(arr, vv); } }