From 78034bcd615e739ad51a1b4bc570b6f1927932fc Mon Sep 17 00:00:00 2001 From: George Hazan Date: Wed, 17 Jan 2024 18:05:48 +0300 Subject: json2file + file2json = helpers to load json from file and write it back --- libs/libjson/src/libJSON.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ libs/libjson/src/libjson.def | 2 ++ libs/libjson/src/libjson64.def | 2 ++ libs/libjson/src/stdafx.h | 1 + 4 files changed, 45 insertions(+) (limited to 'libs/libjson/src') diff --git a/libs/libjson/src/libJSON.cpp b/libs/libjson/src/libJSON.cpp index 7cd2255d4a..3dd7544afa 100644 --- a/libs/libjson/src/libJSON.cpp +++ b/libs/libjson/src/libJSON.cpp @@ -458,3 +458,43 @@ LIBJSON_DLL(int) json_equal(JSONNode *node, JSONNode *node2) { JSON_ASSERT_SAFE(node2, JSON_TEXT("null node2 to json_equal"), return false;); return *node == *node2; } + +///////////////////////////////////////////////////////////////////////////////////////// + +LIBJSON_DLL(bool) file2json(const wchar_t *pwszFilename, JSONNode &root) +{ + if (!pwszFilename) + return false; + + int fileId = _wopen(pwszFilename, _O_BINARY | _O_RDONLY); + if (fileId == -1) + return false; + + size_t dwFileLength = _filelength(fileId), dwReadLen; + ptrA szBuf((char *)mir_alloc(dwFileLength + 1)); + dwReadLen = _read(fileId, szBuf, (unsigned)dwFileLength); + _close(fileId); + if (dwFileLength != dwReadLen) + return false; + + szBuf[dwFileLength] = 0; + + root = JSONNode::parse(szBuf); + if (!root) + return false; + + return true; +} + +LIBJSON_DLL(bool) json2file(const JSONNode &root, const wchar_t *pwszFilename) +{ + auto szBody = root.write_formatted(); + + int fileId = _wopen(pwszFilename, _O_CREAT | _O_TRUNC | _O_WRONLY, _S_IREAD | _S_IWRITE); + if (fileId == -1) + return false; + + int cbLen = _write(fileId, szBody.c_str(), (unsigned)szBody.length()); + _close(fileId); + return cbLen == (int)szBody.length(); +} diff --git a/libs/libjson/src/libjson.def b/libs/libjson/src/libjson.def index debf2a9ff5..5cbfc3394a 100644 --- a/libs/libjson/src/libjson.def +++ b/libs/libjson/src/libjson.def @@ -211,3 +211,5 @@ json_write_formatted @191 ??9JSONNode@@QBE_N_K@Z @208 NONAME ??6@YGAAVJSONNode@@AAV0@ABUSINT64_PARAM@@@Z @209 NONAME ??0JSONNode@@QAE@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PB_W@Z @210 NONAME +?file2json@@YG_NPB_WAAVJSONNode@@@Z @211 NONAME +?json2file@@YG_NABVJSONNode@@PB_W@Z @212 NONAME diff --git a/libs/libjson/src/libjson64.def b/libs/libjson/src/libjson64.def index c46a354d53..5ed328af4b 100644 --- a/libs/libjson/src/libjson64.def +++ b/libs/libjson/src/libjson64.def @@ -211,3 +211,5 @@ json_write_formatted @191 ??9JSONNode@@QEBA_N_K@Z @208 NONAME ??6@YAAEAVJSONNode@@AEAV0@AEBUSINT64_PARAM@@@Z @209 NONAME ??0JSONNode@@QEAA@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PEB_W@Z @210 NONAME +?file2json@@YA_NPEB_WAEAVJSONNode@@@Z @211 NONAME +?json2file@@YA_NAEBVJSONNode@@PEB_W@Z @212 NONAME diff --git a/libs/libjson/src/stdafx.h b/libs/libjson/src/stdafx.h index e9d52cfe52..5e22df85fd 100644 --- a/libs/libjson/src/stdafx.h +++ b/libs/libjson/src/stdafx.h @@ -32,6 +32,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include #include #include +#include #include #include #include -- cgit v1.2.3