-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b4944c4
commit 480f2ee
Showing
97 changed files
with
36,904 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#include "MJson.h" | ||
|
||
class FileGuard | ||
{ | ||
public: | ||
FileGuard() = delete; | ||
~FileGuard() | ||
{ | ||
if (_fp) fclose(_fp); | ||
_fp = nullptr; | ||
} | ||
FileGuard(const char* _path) | ||
{ | ||
if (_fp) fclose(_fp); | ||
_wfopen_s(&_fp, to_wide_string(_path).c_str(), L"rb"); | ||
} | ||
operator FILE* () const | ||
{ | ||
return _fp; | ||
} | ||
private: | ||
FILE* _fp = nullptr; | ||
static std::wstring to_wide_string(const std::string& input) | ||
{ | ||
std::vector<wchar_t> WideString(input.length() * 2); | ||
MultiByteToWideChar( | ||
CP_UTF8, | ||
0, | ||
input.c_str(), | ||
int(input.length()), | ||
WideString.data(), | ||
int(WideString.size()) | ||
); | ||
return WideString.data(); | ||
} | ||
}; | ||
|
||
MJson::MJson(const char* _path) | ||
{ | ||
const auto file = FileGuard(_path); | ||
_document = yyjson_read_file(_path, YYJSON_READ_NOFLAG, nullptr, nullptr); | ||
if (!_document) | ||
throw std::exception("Json Parse Error !"); | ||
root = yyjson_doc_get_root(_document); | ||
} | ||
|
||
MJson::MJson(const std::string& _data, bool _read_from_string) | ||
{ | ||
if (_read_from_string) | ||
_document = yyjson_read(_data.c_str(), _data.length(), YYJSON_READ_NOFLAG); | ||
else | ||
{ | ||
const auto file = FileGuard(_data.c_str()); | ||
_document = yyjson_read_fp(file, YYJSON_READ_NOFLAG, nullptr, nullptr); | ||
} | ||
if (!_document) | ||
throw std::exception("Json Parse Error !"); | ||
root = yyjson_doc_get_root(_document); | ||
} |
Oops, something went wrong.