From 0d48d43960cc8bdba0c5de866dc94ce419ec8243 Mon Sep 17 00:00:00 2001 From: ProjectSky Date: Sat, 8 Nov 2025 23:24:45 +0800 Subject: [PATCH 1/8] refactor: rename and update JSON extension components - Renamed `YYJSON` to `JSON` throughout the codebase for consistency - Updated file names and references, including `YYJSONManager` to `JsonManager` - Modified performance benchmark and test scripts to reflect the new naming - Removed deprecated files related to the old `YYJSON` implementation - Updated documentation and README to align with the new JSON extension naming conventions --- AMBuilder | 6 +- PackageScript | 2 +- README.md | 96 +- public/{IYYJSONManager.h => IJsonManager.h} | 847 +++-- pushbuild.txt | 7 - scripting/include/{yyjson.inc => json.inc} | 1195 ++++-- ...{yyjson_perf_test.sp => json_perf_test.sp} | 16 +- scripting/test_json.sp | 2987 +++++++++++++++ scripting/yyjson_test.sp | 566 --- src/{YYJSONManager.cpp => JsonManager.cpp} | 1959 +++++++--- src/JsonManager.h | 402 ++ src/JsonNatives.cpp | 3244 +++++++++++++++++ src/YYJSONManager.h | 264 -- src/extension.cpp | 76 +- src/extension.h | 32 +- src/json_natives.cpp | 2479 ------------- src/smsdk_config.h | 6 +- third_party/yyjson/LICENSE | 0 third_party/yyjson/yyjson.c | 0 third_party/yyjson/yyjson.h | 0 20 files changed, 9753 insertions(+), 4431 deletions(-) rename public/{IYYJSONManager.h => IJsonManager.h} (51%) mode change 100644 => 100755 delete mode 100755 pushbuild.txt rename scripting/include/{yyjson.inc => json.inc} (50%) mode change 100644 => 100755 rename scripting/{yyjson_perf_test.sp => json_perf_test.sp} (82%) mode change 100644 => 100755 create mode 100755 scripting/test_json.sp delete mode 100644 scripting/yyjson_test.sp rename src/{YYJSONManager.cpp => JsonManager.cpp} (57%) mode change 100644 => 100755 create mode 100755 src/JsonManager.h create mode 100755 src/JsonNatives.cpp delete mode 100644 src/YYJSONManager.h delete mode 100755 src/json_natives.cpp mode change 100644 => 100755 third_party/yyjson/LICENSE mode change 100644 => 100755 third_party/yyjson/yyjson.c mode change 100644 => 100755 third_party/yyjson/yyjson.h diff --git a/AMBuilder b/AMBuilder index 3d10770..c4eeb7e 100755 --- a/AMBuilder +++ b/AMBuilder @@ -2,7 +2,7 @@ import os for cxx in builder.targets: - binary = Extension.Library(builder, cxx, 'yyjson.ext') + binary = Extension.Library(builder, cxx, 'json.ext') arch = binary.compiler.target.arch binary.compiler.defines += [ @@ -12,8 +12,8 @@ for cxx in builder.targets: binary.sources += [ 'third_party/yyjson/yyjson.c', 'src/extension.cpp', - 'src/YYJSONManager.cpp', - 'src/json_natives.cpp', + 'src/JsonManager.cpp', + 'src/JsonNatives.cpp', os.path.join(Extension.sm_root, 'public', 'smsdk_ext.cpp'), ] diff --git a/PackageScript b/PackageScript index c5f89c6..8b94de4 100755 --- a/PackageScript +++ b/PackageScript @@ -40,5 +40,5 @@ for cxx_task in Extension.extensions: # Copy include files CopyFiles('scripting/include', 'addons/sourcemod/scripting/include', - [ 'yyjson.inc' ] + [ 'json.inc' ] ) \ No newline at end of file diff --git a/README.md b/README.md index 33ed5ae..dbac652 100755 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ A [SourceMod](http://www.sourcemod.net/) extension that provides comprehensive J Performance test results using [twitter.json](https://github.com/ibireme/yyjson_benchmark/blob/master/data/json/twitter.json) (0.60 MB): ``` -=== YYJSON Performance Benchmark === +=== JSON Performance Benchmark === Test iterations: 100 Data size: 0.60 MB Parse time: 0.025 seconds @@ -27,7 +27,7 @@ Parse operations per second: 3937.93 ops/sec Parse speed: 2371.66 MB/s (2.31 GB/s) Stringify speed: 4505.23 MB/s (4.39 GB/s) Stringify operations per second: 7480.55 ops/sec -=== YYJSON Performance Benchmark End === +=== JSON Performance Benchmark End === ``` Test environment: @@ -37,7 +37,7 @@ Test environment: - Test iterations: 100 - SourceMod Version: 1.13.0.6966 - YYJSON Version: Latest version -- Test script: [yyjson_perf_test.sp](scripting/yyjson_perf_test.sp) +- Test script: [json_perf_test.sp](scripting/json_perf_test.sp) Note: Performance may vary depending on server hardware and load conditions. @@ -58,33 +58,33 @@ ambuild ``` ## Documentation -* [API Reference](https://github.com/ProjectSky/sm-ext-yyjson/blob/main/scripting/include/yyjson.inc) +* [API Reference](https://github.com/ProjectSky/sm-ext-yyjson/blob/main/scripting/include/json.inc) * [Latest Release](https://github.com/ProjectSky/sm-ext-yyjson/releases) ### SourceMod Extension API ```cpp -#include +#include -IYYJSONManager* g_pYYJSONManager = nullptr; +IJsonManager* g_pJsonManager = nullptr; void Ext::SDK_OnAllLoaded() { - SM_GET_LATE_IFACE(YYJSONMANAGER, g_pYYJSONManager); + SM_GET_LATE_IFACE(JSONMANAGER, g_pJsonManager); char error[256]; - YYJSONValue* val = g_pYYJSONManager->ParseJSON("{\"name\":\"John\", \"age\":30}", false, false, 0, error, sizeof(error)); + JsonValue* val = g_pJsonManager->ParseJSON("{\"name\":\"John\", \"age\":30}", false, false, 0, error, sizeof(error)); if (!val) { PrintToServer("Failed to parse JSON: %s", error); return; } - size_t size = g_pYYJSONManager->GetSerializedSize(val); + size_t size = g_pJsonManager->GetSerializedSize(val); char buffer[size]; - g_pYYJSONManager->WriteToString(val, buffer, size); + g_pJsonManager->WriteToString(val, buffer, size); // must release the value after using - g_pYYJSONManager->Release(val); + g_pJsonManager->Release(val); printf("JSON: %s\n", buffer); } @@ -95,7 +95,7 @@ void Ext::SDK_OnAllLoaded() #### Working with Objects ```cpp // Create a JSON object -YYJSONObject obj = new YYJSONObject(); +JSONObject obj = new JSONObject(); obj.SetInt("int", 1); obj.SetInt64("int64", "9223372036854775800"); obj.SetFloat("float", 2.0); @@ -120,7 +120,7 @@ delete obj; #### Working with Arrays ```cpp // Create a JSON array -YYJSONArray arr = new YYJSONArray(); +JSONArray arr = new JSONArray(); arr.PushInt(1); arr.PushInt64("9223372036854775800"); arr.PushFloat(2.0); @@ -146,7 +146,7 @@ delete arr; #### Using JSON Pointer ```cpp // Create nested structures -YYJSONObject obj = new YYJSONObject(); +JSONObject obj = new JSONObject(); obj.PtrSetInt("/a/b/c", 1); delete obj; @@ -172,7 +172,7 @@ delete obj; } */ -YYJSONObject data = YYJSON.Parse("example.json", true); +JSONObject data = JSON.Parse("example.json", true); // Access values using JSON Pointer int value = data.PtrGetInt("/int"); // Returns: 1234 float fValue = data.PtrGetFloat("/arr/1"); // Returns: 1.2344 @@ -183,46 +183,38 @@ delete data; #### Array and Object Iteration ```cpp // Object iteration -YYJSONObject obj = YYJSON.Parse("{\"a\": 1, \"b\": 2, \"c\": 3}"); +JSONObject obj = JSON.Parse("{\"a\": 1, \"b\": 2, \"c\": 3}"); char key[64]; -YYJSON value; -// Method 1: Using Foreach (Recommended) -while (obj.ForeachObject(key, sizeof(key), value)) { - PrintToServer("Key: %s", key); - delete value; -} - -// Method 2: Using ForeachKey (only used for keys) -while (obj.ForeachKey(key, sizeof(key))) { +// Method 1: Using Object Iterator (Recommended) +JSONObjIter iter = new JSONObjIter(obj); +while (iter.Next(key, sizeof(key))) { PrintToServer("Key: %s", key); } +delete iter; -// Method 3: Classic iteration +// Method 2: Classic iteration for (int i = 0; i < obj.Size; i++) { obj.GetKey(i, key, sizeof(key)); - value = obj.GetValueAt(i); + JSON value = obj.GetValueAt(i); delete value; } delete obj; // Array iteration -YYJSONArray arr = YYJSON.Parse("[1, 2, 3, 4, 5]"); -int index; +JSONArray arr = JSON.Parse("[1, 2, 3, 4, 5]"); +JSON value; -// Method 1: Using Foreach (Recommended) -while (arr.ForeachArray(index, value)) { - PrintToServer("Index: %d", index); +// Method 1: Using Array Iterator (Recommended) +JSONArrIter iter = new JSONArrIter(arr); +while ((value = iter.Next()) != null) { + PrintToServer("Index: %d", iter.Index); delete value; } +delete iter; -// Method 2: Using ForeachIndex (only used for index) -while (arr.ForeachIndex(index)) { - PrintToServer("Index: %d", index); -} - -// Method 3: Classic iteration +// Method 2: Classic iteration for (int i = 0; i < arr.Length; i++) { value = arr.Get(i); delete value; @@ -234,7 +226,7 @@ delete arr; #### Array Search Operations ```cpp // Create a test array -YYJSONArray arr = YYJSON.Parse( +JSONArray arr = JSON.Parse( "[42, true, \"hello\", 3.14, \"world\", false, 42]" ); @@ -258,7 +250,7 @@ delete arr; #### Sorting Arrays and Objects ```cpp // Array sorting -YYJSONArray arr = YYJSON.Parse( +JSONArray arr = JSON.Parse( "[3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]", .is_mutable_doc = true ); @@ -272,14 +264,14 @@ arr.Sort(YYJSON_SORT_RANDOM); // Random // [5, 2, 9, 1, 6, 3, 4, 5, 1, 3, 5] (example output) // Mixed type array sorting -YYJSONArray mixed = YYJSON.Parse( +JSONArray mixed = JSON.Parse( "[true, 42, \"hello\", 1.23, false, \"world\"]", .is_mutable_doc = true ); mixed.Sort(); // [false, true, 1.23, 42, "hello", "world"] // Object sorting by keys -YYJSONObject obj = YYJSON.Parse( +JSONObject obj = JSON.Parse( "{\"zebra\": 1, \"alpha\": 2, \"beta\": 3, \"gamma\": 4}", .is_mutable_doc = true ); @@ -302,7 +294,7 @@ delete obj; // Create object from key-value string arrays char pairs[][] = {"name", "test", "type", "demo", "version", "1.0.0"}; -YYJSONObject obj = YYJSONObject.FromStrings(pairs, sizeof(pairs)); +JSONObject obj = JSONObject.FromStrings(pairs, sizeof(pairs)); /* Output: { @@ -314,7 +306,7 @@ YYJSONObject obj = YYJSONObject.FromStrings(pairs, sizeof(pairs)); // Create array from string array char items[][] = {"apple", "banana", "orange"}; -YYJSONArray arr = YYJSONArray.FromStrings(items, sizeof(items)); +JSONArray arr = JSONArray.FromStrings(items, sizeof(items)); delete obj; delete arr; @@ -330,7 +322,7 @@ delete arr; #### Using JSON Pack ```cpp // Create object with mixed types -YYJSON packed = YYJSON.Pack("{s:s,s:i,s:f,s:b,s:n}", +JSON packed = JSON.Pack("{s:s,s:i,s:f,s:b,s:n}", "name", "John", "age", 25, "height", 1.75, @@ -350,7 +342,7 @@ delete packed; */ // Create nested structures -YYJSON nested = YYJSON.Pack("{s:{s:s,s:[iii]}}", +JSON nested = JSON.Pack("{s:{s:s,s:[iii]}}", "user", "name", "John", "scores", 85, 90, 95 @@ -367,7 +359,7 @@ delete nested; */ // Create array with mixed types -YYJSON array = YYJSON.Pack("[sifbn]", +JSON array = JSON.Pack("[sifbn]", "test", 42, 3.14, true ); delete array; @@ -388,10 +380,10 @@ When parsing JSON documents, you can choose whether to create a mutable or immut ```cpp // Create an immutable document (read-only) -YYJSONObject obj = YYJSON.Parse("example.json", true); +JSONObject obj = JSON.Parse("example.json", true); // Create a mutable document (read-write) -YYJSONObject obj = YYJSON.Parse("example.json", true, true); +JSONObject obj = JSON.Parse("example.json", true, true); ``` Immutable documents: @@ -410,7 +402,7 @@ Immutable documents support a variety of read operations: Example of operations with immutable documents: ```cpp // Create an immutable document -YYJSONObject obj = YYJSON.Parse("example.json", true); +JSONObject obj = JSON.Parse("example.json", true); // Reading is allowed int value = obj.GetInt("key"); // Works fine @@ -429,10 +421,10 @@ You can convert between mutable and immutable documents using deep copy: ```cpp // Create an immutable document -YYJSONObject immutable = YYJSON.Parse("example.json", true); +JSONObject immutable = JSON.Parse("example.json", true); // Create a mutable copy -YYJSONObject mutable = immutable.ToMutable(); +JSONObject mutable = immutable.ToMutable(); // Now you can modify the mutable copy mutable.SetInt("key", 123); diff --git a/public/IYYJSONManager.h b/public/IJsonManager.h old mode 100644 new mode 100755 similarity index 51% rename from public/IYYJSONManager.h rename to public/IJsonManager.h index ff5172e..d9c359a --- a/public/IYYJSONManager.h +++ b/public/IJsonManager.h @@ -1,7 +1,8 @@ -#ifndef _INCLUDE_SM_YYJSON_IYYJSONMANAGER_H_ -#define _INCLUDE_SM_YYJSON_IYYJSONMANAGER_H_ +#ifndef _INCLUDE_SM_JSON_IJSONMANAGER_H_ +#define _INCLUDE_SM_JSON_IJSONMANAGER_H_ #include +#include using SourceMod::Handle_t; using SourceMod::HandleType_t; @@ -9,21 +10,24 @@ using SourceMod::SMInterface; using SourcePawn::IPluginContext; // Forward declaration -class YYJSONValue; +class JsonValue; +class JsonArrIter; +class JsonObjIter; -#define SMINTERFACE_YYJSONMANAGER_NAME "IYYJSONManager" -#define SMINTERFACE_YYJSONMANAGER_VERSION 1 -#define YYJSON_PACK_ERROR_SIZE 256 -#define YYJSON_ERROR_BUFFER_SIZE 256 +#define SMINTERFACE_JSONMANAGER_NAME "IJsonManager" +#define SMINTERFACE_JSONMANAGER_VERSION 1 +#define JSON_PACK_ERROR_SIZE 256 +#define JSON_ERROR_BUFFER_SIZE 256 +#define JSON_INT64_BUFFER_SIZE 32 /** * @brief JSON sorting order */ -enum YYJSON_SORT_ORDER +enum JSON_SORT_ORDER { - YYJSON_SORT_ASC = 0, // Ascending order - YYJSON_SORT_DESC = 1, // Descending order - YYJSON_SORT_RANDOM = 2 // Random order + JSON_SORT_ASC = 0, // Ascending order + JSON_SORT_DESC = 1, // Descending order + JSON_SORT_RANDOM = 2 // Random order }; /** @@ -43,29 +47,29 @@ class IPackParamProvider }; /** - * @brief YYJSON Manager Interface + * @brief JSON Manager Interface * * This interface provides complete JSON manipulation capabilities. * It's designed to be consumed by other SourceMod C++ extensions * without requiring them to link against yyjson library. * * @usage - * IYYJSONManager* g_pYYJSONManager = nullptr; + * IJsonManager* g_pJsonManager = nullptr; * * bool YourExtension::SDK_OnAllLoaded() * { - * SM_GET_LATE_IFACE(YYJSONMANAGER, g_pYYJSONManager); + * SM_GET_LATE_IFACE(JSONMANAGER, g_pJsonManager); * } */ -class IYYJSONManager : public SMInterface +class IJsonManager : public SMInterface { public: virtual const char *GetInterfaceName() override { - return SMINTERFACE_YYJSONMANAGER_NAME; + return SMINTERFACE_JSONMANAGER_NAME; } virtual unsigned int GetInterfaceVersion() override { - return SMINTERFACE_YYJSONMANAGER_VERSION; + return SMINTERFACE_JSONMANAGER_VERSION; } public: @@ -79,7 +83,7 @@ class IYYJSONManager : public SMInterface * @param error_size Error buffer size * @return JSON value pointer or nullptr on error */ - virtual YYJSONValue* ParseJSON(const char* json_str, bool is_file, bool is_mutable = false, + virtual JsonValue* ParseJSON(const char* json_str, bool is_file, bool is_mutable = false, uint32_t read_flg = 0, char* error = nullptr, size_t error_size = 0) = 0; /** @@ -88,13 +92,13 @@ class IYYJSONManager : public SMInterface * @param buffer Output buffer * @param buffer_size Buffer size * @param write_flg Write flags (YYJSON_WRITE_FLAG values, default: 0) - * @param out_size Pointer to receive actual size written (including null terminator), optional + * @param out_size Pointer to receive actual size written (including null terminator) optional * @return true on success, false if buffer is too small or on error - * + * * @note The out_size parameter returns the size including null terminator * @note Use GetSerializedSize() with the same write_flg to determine buffer size */ - virtual bool WriteToString(YYJSONValue* handle, char* buffer, size_t buffer_size, + virtual bool WriteToString(JsonValue* handle, char* buffer, size_t buffer_size, uint32_t write_flg = 0, size_t* out_size = nullptr) = 0; /** @@ -106,7 +110,7 @@ class IYYJSONManager : public SMInterface * @param error_size Error buffer size * @return true on success */ - virtual bool WriteToFile(YYJSONValue* handle, const char* path, uint32_t write_flg = 0, + virtual bool WriteToFile(JsonValue* handle, const char* path, uint32_t write_flg = 0, char* error = nullptr, size_t error_size = 0) = 0; /** @@ -116,7 +120,16 @@ class IYYJSONManager : public SMInterface * @return true if values are equal, false otherwise * @note Compares structure and content recursively */ - virtual bool Equals(YYJSONValue* handle1, YYJSONValue* handle2) = 0; + virtual bool Equals(JsonValue* handle1, JsonValue* handle2) = 0; + + /** + * Check if JSON value equals a string + * @param handle JSON value to compare + * @param str String to compare with + * @return true if value is a string and equals the given string, false otherwise + * @note Returns false if handle is null, str is null, or value is not a string + */ + virtual bool EqualsStr(JsonValue* handle, const char* str) = 0; /** * Deep copy a JSON value into a target document @@ -125,35 +138,35 @@ class IYYJSONManager : public SMInterface * @return New JSON value (deep copy) or nullptr on failure * @note The returned value is owned by targetDoc's document context */ - virtual YYJSONValue* DeepCopy(YYJSONValue* targetDoc, YYJSONValue* sourceValue) = 0; + virtual JsonValue* DeepCopy(JsonValue* targetDoc, JsonValue* sourceValue) = 0; /** * Get human-readable type description string * @param handle JSON value * @return Type description string (e.g., "object", "array", "string", "number", "true", "false", "unknown") */ - virtual const char* GetTypeDesc(YYJSONValue* handle) = 0; + virtual const char* GetTypeDesc(JsonValue* handle) = 0; /** * Get the size needed to serialize this JSON value - * + * * @param handle JSON value * @param write_flg Write flags (YYJSON_WRITE_FLAG values, default: 0) * @return Size in bytes (including null terminator) - * + * * @note The returned size depends on the write_flg parameter. - * You MUST use the same flags when calling both GetSerializedSize() - * and WriteToString(). Using different flags will return + * You MUST use the same flags when calling both GetSerializedSize() + * and WriteToString(). Using different flags will return * different sizes and may cause buffer overflow. - * + * * @example * // Correct usage: * auto flags = YYJSON_WRITE_PRETTY; - * size_t size = g_pYYJSONManager->GetSerializedSize(handle, flags); + * size_t size = g_pJsonManager->GetSerializedSize(handle, flags); * char* buffer = new char[size]; - * g_pYYJSONManager->WriteToString(handle, buffer, size, flags); // Use same flags + * g_pJsonManager->WriteToString(handle, buffer, size, flags); // Use same flags */ - virtual size_t GetSerializedSize(YYJSONValue* handle, uint32_t write_flg = 0) = 0; + virtual size_t GetSerializedSize(JsonValue* handle, uint32_t write_flg = 0) = 0; /** * Convert immutable document to mutable @@ -161,7 +174,7 @@ class IYYJSONManager : public SMInterface * @return New mutable JSON value or nullptr if already mutable or on error * @note Creates a deep copy as a mutable document */ - virtual YYJSONValue* ToMutable(YYJSONValue* handle) = 0; + virtual JsonValue* ToMutable(JsonValue* handle) = 0; /** * Convert mutable document to immutable @@ -169,145 +182,143 @@ class IYYJSONManager : public SMInterface * @return New immutable JSON value or nullptr if already immutable or on error * @note Creates a deep copy as an immutable document */ - virtual YYJSONValue* ToImmutable(YYJSONValue* handle) = 0; + virtual JsonValue* ToImmutable(JsonValue* handle) = 0; /** * Get JSON type * @param handle JSON value * @return YYJSON_TYPE value */ - virtual uint8_t GetType(YYJSONValue* handle) = 0; + virtual uint8_t GetType(JsonValue* handle) = 0; /** * Get JSON subtype * @param handle JSON value * @return YYJSON_SUBTYPE value */ - virtual uint8_t GetSubtype(YYJSONValue* handle) = 0; - - // Type checking methods + virtual uint8_t GetSubtype(JsonValue* handle) = 0; /** * Check if value is an array * @param handle JSON value * @return true if value is an array */ - virtual bool IsArray(YYJSONValue* handle) = 0; + virtual bool IsArray(JsonValue* handle) = 0; /** * Check if value is an object * @param handle JSON value * @return true if value is an object */ - virtual bool IsObject(YYJSONValue* handle) = 0; + virtual bool IsObject(JsonValue* handle) = 0; /** * Check if value is an integer (signed or unsigned) * @param handle JSON value * @return true if value is an integer */ - virtual bool IsInt(YYJSONValue* handle) = 0; + virtual bool IsInt(JsonValue* handle) = 0; /** * Check if value is an unsigned integer * @param handle JSON value * @return true if value is an unsigned integer */ - virtual bool IsUint(YYJSONValue* handle) = 0; + virtual bool IsUint(JsonValue* handle) = 0; /** * Check if value is a signed integer * @param handle JSON value * @return true if value is a signed integer */ - virtual bool IsSint(YYJSONValue* handle) = 0; + virtual bool IsSint(JsonValue* handle) = 0; /** * Check if value is a number (integer or real) * @param handle JSON value * @return true if value is a number */ - virtual bool IsNum(YYJSONValue* handle) = 0; + virtual bool IsNum(JsonValue* handle) = 0; /** * Check if value is a boolean (true or false) * @param handle JSON value * @return true if value is a boolean */ - virtual bool IsBool(YYJSONValue* handle) = 0; + virtual bool IsBool(JsonValue* handle) = 0; /** * Check if value is boolean true * @param handle JSON value * @return true if value is boolean true */ - virtual bool IsTrue(YYJSONValue* handle) = 0; + virtual bool IsTrue(JsonValue* handle) = 0; /** * Check if value is boolean false * @param handle JSON value * @return true if value is boolean false */ - virtual bool IsFalse(YYJSONValue* handle) = 0; + virtual bool IsFalse(JsonValue* handle) = 0; /** * Check if value is a floating-point number * @param handle JSON value * @return true if value is a floating-point number */ - virtual bool IsFloat(YYJSONValue* handle) = 0; + virtual bool IsFloat(JsonValue* handle) = 0; /** * Check if value is a string * @param handle JSON value * @return true if value is a string */ - virtual bool IsStr(YYJSONValue* handle) = 0; + virtual bool IsStr(JsonValue* handle) = 0; /** * Check if value is null * @param handle JSON value * @return true if value is null */ - virtual bool IsNull(YYJSONValue* handle) = 0; + virtual bool IsNull(JsonValue* handle) = 0; /** * Check if value is a container (object or array) * @param handle JSON value * @return true if value is a container */ - virtual bool IsCtn(YYJSONValue* handle) = 0; + virtual bool IsCtn(JsonValue* handle) = 0; /** * Check if document is mutable * @param handle JSON value * @return true if document is mutable */ - virtual bool IsMutable(YYJSONValue* handle) = 0; + virtual bool IsMutable(JsonValue* handle) = 0; /** * Check if document is immutable * @param handle JSON value * @return true if document is immutable */ - virtual bool IsImmutable(YYJSONValue* handle) = 0; + virtual bool IsImmutable(JsonValue* handle) = 0; /** * Get the number of bytes read when parsing this document * @param handle JSON value - * @return Number of bytes read during parsing (excluding null terminator), 0 if not from parsing - * + * @return Number of bytes read during parsing (including null terminator) 0 if not from parsing + * * @note This value only applies to documents created from parsing * @note Manually created documents (ObjectInit, CreateBool, etc.) will return 0 - * @note The returned size does not include the null terminator + * @note The returned size includes the null terminator */ - virtual size_t GetReadSize(YYJSONValue* handle) = 0; + virtual size_t GetReadSize(JsonValue* handle) = 0; /** * Create an empty mutable JSON object * @return New mutable JSON object or nullptr on failure */ - virtual YYJSONValue* ObjectInit() = 0; + virtual JsonValue* ObjectInit() = 0; /** * Create a JSON object from key-value string pairs @@ -315,7 +326,7 @@ class IYYJSONManager : public SMInterface * @param count Number of key-value pairs * @return New JSON object or nullptr on failure */ - virtual YYJSONValue* ObjectInitWithStrings(const char** pairs, size_t count) = 0; + virtual JsonValue* ObjectInitWithStrings(const char** pairs, size_t count) = 0; /** * Parse a JSON object from string @@ -326,7 +337,7 @@ class IYYJSONManager : public SMInterface * @return Parsed JSON object or nullptr on error * @note Returns error if root is not an object */ - virtual YYJSONValue* ObjectParseString(const char* str, uint32_t read_flg = 0, + virtual JsonValue* ObjectParseString(const char* str, uint32_t read_flg = 0, char* error = nullptr, size_t error_size = 0) = 0; /** @@ -338,7 +349,7 @@ class IYYJSONManager : public SMInterface * @return Parsed JSON object or nullptr on error * @note Returns error if root is not an object */ - virtual YYJSONValue* ObjectParseFile(const char* path, uint32_t read_flg = 0, + virtual JsonValue* ObjectParseFile(const char* path, uint32_t read_flg = 0, char* error = nullptr, size_t error_size = 0) = 0; /** @@ -346,7 +357,7 @@ class IYYJSONManager : public SMInterface * @param handle JSON object * @return Number of key-value pairs */ - virtual size_t ObjectGetSize(YYJSONValue* handle) = 0; + virtual size_t ObjectGetSize(JsonValue* handle) = 0; /** * Get key name at specific index @@ -355,7 +366,7 @@ class IYYJSONManager : public SMInterface * @param out_key Pointer to receive key string * @return true on success, false if index out of bounds */ - virtual bool ObjectGetKey(YYJSONValue* handle, size_t index, const char** out_key) = 0; + virtual bool ObjectGetKey(JsonValue* handle, size_t index, const char** out_key) = 0; /** * Get value at specific index @@ -363,7 +374,7 @@ class IYYJSONManager : public SMInterface * @param index Index of key-value pair * @return JSON value or nullptr if index out of bounds */ - virtual YYJSONValue* ObjectGetValueAt(YYJSONValue* handle, size_t index) = 0; + virtual JsonValue* ObjectGetValueAt(JsonValue* handle, size_t index) = 0; /** * Get value by key name @@ -371,7 +382,7 @@ class IYYJSONManager : public SMInterface * @param key Key name * @return JSON value or nullptr if key not found */ - virtual YYJSONValue* ObjectGet(YYJSONValue* handle, const char* key) = 0; + virtual JsonValue* ObjectGet(JsonValue* handle, const char* key) = 0; /** * Get boolean value by key @@ -380,7 +391,7 @@ class IYYJSONManager : public SMInterface * @param out_value Pointer to receive boolean value * @return true on success, false if key not found or type mismatch */ - virtual bool ObjectGetBool(YYJSONValue* handle, const char* key, bool* out_value) = 0; + virtual bool ObjectGetBool(JsonValue* handle, const char* key, bool* out_value) = 0; /** * Get float value by key @@ -389,7 +400,7 @@ class IYYJSONManager : public SMInterface * @param out_value Pointer to receive float value * @return true on success, false if key not found or type mismatch */ - virtual bool ObjectGetFloat(YYJSONValue* handle, const char* key, double* out_value) = 0; + virtual bool ObjectGetFloat(JsonValue* handle, const char* key, double* out_value) = 0; /** * Get integer value by key @@ -398,16 +409,16 @@ class IYYJSONManager : public SMInterface * @param out_value Pointer to receive integer value * @return true on success, false if key not found or type mismatch */ - virtual bool ObjectGetInt(YYJSONValue* handle, const char* key, int* out_value) = 0; + virtual bool ObjectGetInt(JsonValue* handle, const char* key, int* out_value) = 0; /** - * Get 64-bit integer value by key + * Get 64-bit integer value by key (auto-detects signed/unsigned) * @param handle JSON object * @param key Key name - * @param out_value Pointer to receive 64-bit integer value + * @param out_value Pointer to receive 64-bit integer value (std::variant) * @return true on success, false if key not found or type mismatch */ - virtual bool ObjectGetInt64(YYJSONValue* handle, const char* key, int64_t* out_value) = 0; + virtual bool ObjectGetInt64(JsonValue* handle, const char* key, std::variant* out_value) = 0; /** * Get string value by key @@ -417,7 +428,7 @@ class IYYJSONManager : public SMInterface * @param out_len Pointer to receive string length * @return true on success, false if key not found or type mismatch */ - virtual bool ObjectGetString(YYJSONValue* handle, const char* key, const char** out_str, size_t* out_len) = 0; + virtual bool ObjectGetString(JsonValue* handle, const char* key, const char** out_str, size_t* out_len) = 0; /** * Check if value at key is null @@ -426,7 +437,7 @@ class IYYJSONManager : public SMInterface * @param out_is_null Pointer to receive result * @return true if key exists, false if key not found */ - virtual bool ObjectIsNull(YYJSONValue* handle, const char* key, bool* out_is_null) = 0; + virtual bool ObjectIsNull(JsonValue* handle, const char* key, bool* out_is_null) = 0; /** * Check if object has a specific key @@ -435,7 +446,7 @@ class IYYJSONManager : public SMInterface * @param use_pointer If true, treat key as JSON pointer * @return true if key exists */ - virtual bool ObjectHasKey(YYJSONValue* handle, const char* key, bool use_pointer) = 0; + virtual bool ObjectHasKey(JsonValue* handle, const char* key, bool use_pointer) = 0; /** * Rename a key in the object @@ -446,7 +457,7 @@ class IYYJSONManager : public SMInterface * @return true on success * @note Only works on mutable objects */ - virtual bool ObjectRenameKey(YYJSONValue* handle, const char* old_key, const char* new_key, bool allow_duplicate) = 0; + virtual bool ObjectRenameKey(JsonValue* handle, const char* old_key, const char* new_key, bool allow_duplicate) = 0; /** * Set value by key (mutable only) @@ -455,7 +466,7 @@ class IYYJSONManager : public SMInterface * @param value JSON value to set * @return true on success */ - virtual bool ObjectSet(YYJSONValue* handle, const char* key, YYJSONValue* value) = 0; + virtual bool ObjectSet(JsonValue* handle, const char* key, JsonValue* value) = 0; /** * Set boolean value by key (mutable only) @@ -464,7 +475,7 @@ class IYYJSONManager : public SMInterface * @param value Boolean value * @return true on success */ - virtual bool ObjectSetBool(YYJSONValue* handle, const char* key, bool value) = 0; + virtual bool ObjectSetBool(JsonValue* handle, const char* key, bool value) = 0; /** * Set float value by key (mutable only) @@ -473,7 +484,7 @@ class IYYJSONManager : public SMInterface * @param value Float value * @return true on success */ - virtual bool ObjectSetFloat(YYJSONValue* handle, const char* key, double value) = 0; + virtual bool ObjectSetFloat(JsonValue* handle, const char* key, double value) = 0; /** * Set integer value by key (mutable only) @@ -482,16 +493,16 @@ class IYYJSONManager : public SMInterface * @param value Integer value * @return true on success */ - virtual bool ObjectSetInt(YYJSONValue* handle, const char* key, int value) = 0; + virtual bool ObjectSetInt(JsonValue* handle, const char* key, int value) = 0; /** - * Set 64-bit integer value by key (mutable only) + * Set 64-bit integer value by key (mutable only, auto-detects signed/unsigned) * @param handle Mutable JSON object * @param key Key name - * @param value 64-bit integer value + * @param value 64-bit integer value (std::variant) * @return true on success */ - virtual bool ObjectSetInt64(YYJSONValue* handle, const char* key, int64_t value) = 0; + virtual bool ObjectSetInt64(JsonValue* handle, const char* key, std::variant value) = 0; /** * Set null value by key (mutable only) @@ -499,7 +510,7 @@ class IYYJSONManager : public SMInterface * @param key Key name * @return true on success */ - virtual bool ObjectSetNull(YYJSONValue* handle, const char* key) = 0; + virtual bool ObjectSetNull(JsonValue* handle, const char* key) = 0; /** * Set string value by key (mutable only) @@ -508,7 +519,7 @@ class IYYJSONManager : public SMInterface * @param value String value * @return true on success */ - virtual bool ObjectSetString(YYJSONValue* handle, const char* key, const char* value) = 0; + virtual bool ObjectSetString(JsonValue* handle, const char* key, const char* value) = 0; /** * Remove key-value pair by key (mutable only) @@ -516,29 +527,29 @@ class IYYJSONManager : public SMInterface * @param key Key name * @return true on success */ - virtual bool ObjectRemove(YYJSONValue* handle, const char* key) = 0; + virtual bool ObjectRemove(JsonValue* handle, const char* key) = 0; /** * Remove all key-value pairs (mutable only) * @param handle Mutable JSON object * @return true on success */ - virtual bool ObjectClear(YYJSONValue* handle) = 0; + virtual bool ObjectClear(JsonValue* handle) = 0; /** * Sort object keys * @param handle Mutable JSON object - * @param sort_mode Sort order (YYJSON_SORT_ASC, YYJSON_SORT_DESC, or YYJSON_SORT_RANDOM) + * @param sort_mode Sort order (see JSON_SORT_ORDER enum) * @return true on success * @note Only works on mutable objects */ - virtual bool ObjectSort(YYJSONValue* handle, YYJSON_SORT_ORDER sort_mode) = 0; + virtual bool ObjectSort(JsonValue* handle, JSON_SORT_ORDER sort_mode) = 0; /** * Create an empty mutable JSON array * @return New mutable JSON array or nullptr on failure */ - virtual YYJSONValue* ArrayInit() = 0; + virtual JsonValue* ArrayInit() = 0; /** * Create a JSON array from string values @@ -546,7 +557,43 @@ class IYYJSONManager : public SMInterface * @param count Number of strings * @return New JSON array or nullptr on failure */ - virtual YYJSONValue* ArrayInitWithStrings(const char** strings, size_t count) = 0; + virtual JsonValue* ArrayInitWithStrings(const char** strings, size_t count) = 0; + + /** + * Create a JSON array from 32-bit integer values + * @param values Array of int32_t values + * @param count Number of values + * @return New JSON array or nullptr on failure + */ + virtual JsonValue* ArrayInitWithInt32(const int32_t* values, size_t count) = 0; + + /** + * Create a JSON array from 64-bit integer string values (auto-detects signed/unsigned) + * @param values Array of int64 string values (can be signed or unsigned) + * @param count Number of values + * @param error Error buffer (optional) + * @param error_size Error buffer size + * @return New JSON array or nullptr on failure + * @note Each string value is parsed and auto-detected as signed or unsigned + */ + virtual JsonValue* ArrayInitWithInt64(const char** values, size_t count, + char* error = nullptr, size_t error_size = 0) = 0; + + /** + * Create a JSON array from boolean values + * @param values Array of boolean values + * @param count Number of values + * @return New JSON array or nullptr on failure + */ + virtual JsonValue* ArrayInitWithBool(const bool* values, size_t count) = 0; + + /** + * Create a JSON array from float values + * @param values Array of double values + * @param count Number of values + * @return New JSON array or nullptr on failure + */ + virtual JsonValue* ArrayInitWithFloat(const double* values, size_t count) = 0; /** * Parse a JSON array from string @@ -557,7 +604,7 @@ class IYYJSONManager : public SMInterface * @return Parsed JSON array or nullptr on error * @note Returns error if root is not an array */ - virtual YYJSONValue* ArrayParseString(const char* str, uint32_t read_flg = 0, + virtual JsonValue* ArrayParseString(const char* str, uint32_t read_flg = 0, char* error = nullptr, size_t error_size = 0) = 0; /** @@ -569,7 +616,7 @@ class IYYJSONManager : public SMInterface * @return Parsed JSON array or nullptr on error * @note Returns error if root is not an array */ - virtual YYJSONValue* ArrayParseFile(const char* path, uint32_t read_flg = 0, + virtual JsonValue* ArrayParseFile(const char* path, uint32_t read_flg = 0, char* error = nullptr, size_t error_size = 0) = 0; /** @@ -577,7 +624,7 @@ class IYYJSONManager : public SMInterface * @param handle JSON array * @return Number of elements */ - virtual size_t ArrayGetSize(YYJSONValue* handle) = 0; + virtual size_t ArrayGetSize(JsonValue* handle) = 0; /** * Get element at specific index @@ -585,21 +632,21 @@ class IYYJSONManager : public SMInterface * @param index Element index * @return JSON value or nullptr if index out of bounds */ - virtual YYJSONValue* ArrayGet(YYJSONValue* handle, size_t index) = 0; + virtual JsonValue* ArrayGet(JsonValue* handle, size_t index) = 0; /** * Get first element in array * @param handle JSON array * @return First JSON value or nullptr if array is empty */ - virtual YYJSONValue* ArrayGetFirst(YYJSONValue* handle) = 0; + virtual JsonValue* ArrayGetFirst(JsonValue* handle) = 0; /** * Get last element in array * @param handle JSON array * @return Last JSON value or nullptr if array is empty */ - virtual YYJSONValue* ArrayGetLast(YYJSONValue* handle) = 0; + virtual JsonValue* ArrayGetLast(JsonValue* handle) = 0; /** * Get boolean value at index @@ -608,7 +655,7 @@ class IYYJSONManager : public SMInterface * @param out_value Pointer to receive boolean value * @return true on success, false if index out of bounds or type mismatch */ - virtual bool ArrayGetBool(YYJSONValue* handle, size_t index, bool* out_value) = 0; + virtual bool ArrayGetBool(JsonValue* handle, size_t index, bool* out_value) = 0; /** * Get float value at index @@ -617,7 +664,7 @@ class IYYJSONManager : public SMInterface * @param out_value Pointer to receive float value * @return true on success, false if index out of bounds or type mismatch */ - virtual bool ArrayGetFloat(YYJSONValue* handle, size_t index, double* out_value) = 0; + virtual bool ArrayGetFloat(JsonValue* handle, size_t index, double* out_value) = 0; /** * Get integer value at index @@ -626,16 +673,16 @@ class IYYJSONManager : public SMInterface * @param out_value Pointer to receive integer value * @return true on success, false if index out of bounds or type mismatch */ - virtual bool ArrayGetInt(YYJSONValue* handle, size_t index, int* out_value) = 0; + virtual bool ArrayGetInt(JsonValue* handle, size_t index, int* out_value) = 0; /** - * Get 64-bit integer value at index + * Get 64-bit integer value at index (auto-detects signed/unsigned) * @param handle JSON array * @param index Element index - * @param out_value Pointer to receive 64-bit integer value + * @param out_value Pointer to receive 64-bit integer value (std::variant) * @return true on success, false if index out of bounds or type mismatch */ - virtual bool ArrayGetInt64(YYJSONValue* handle, size_t index, int64_t* out_value) = 0; + virtual bool ArrayGetInt64(JsonValue* handle, size_t index, std::variant* out_value) = 0; /** * Get string value at index @@ -645,7 +692,7 @@ class IYYJSONManager : public SMInterface * @param out_len Pointer to receive string length * @return true on success, false if index out of bounds or type mismatch */ - virtual bool ArrayGetString(YYJSONValue* handle, size_t index, const char** out_str, size_t* out_len) = 0; + virtual bool ArrayGetString(JsonValue* handle, size_t index, const char** out_str, size_t* out_len) = 0; /** * Check if element at index is null @@ -653,7 +700,7 @@ class IYYJSONManager : public SMInterface * @param index Element index * @return true if element is null */ - virtual bool ArrayIsNull(YYJSONValue* handle, size_t index) = 0; + virtual bool ArrayIsNull(JsonValue* handle, size_t index) = 0; /** * Replace element at index with JSON value (mutable only) @@ -662,7 +709,7 @@ class IYYJSONManager : public SMInterface * @param value JSON value to set * @return true on success */ - virtual bool ArrayReplace(YYJSONValue* handle, size_t index, YYJSONValue* value) = 0; + virtual bool ArrayReplace(JsonValue* handle, size_t index, JsonValue* value) = 0; /** * Replace element at index with boolean (mutable only) @@ -671,7 +718,7 @@ class IYYJSONManager : public SMInterface * @param value Boolean value * @return true on success */ - virtual bool ArrayReplaceBool(YYJSONValue* handle, size_t index, bool value) = 0; + virtual bool ArrayReplaceBool(JsonValue* handle, size_t index, bool value) = 0; /** * Replace element at index with float (mutable only) @@ -680,7 +727,7 @@ class IYYJSONManager : public SMInterface * @param value Float value * @return true on success */ - virtual bool ArrayReplaceFloat(YYJSONValue* handle, size_t index, double value) = 0; + virtual bool ArrayReplaceFloat(JsonValue* handle, size_t index, double value) = 0; /** * Replace element at index with integer (mutable only) @@ -689,16 +736,16 @@ class IYYJSONManager : public SMInterface * @param value Integer value * @return true on success */ - virtual bool ArrayReplaceInt(YYJSONValue* handle, size_t index, int value) = 0; + virtual bool ArrayReplaceInt(JsonValue* handle, size_t index, int value) = 0; /** - * Replace element at index with 64-bit integer (mutable only) + * Replace element at index with 64-bit integer (mutable only, auto-detects signed/unsigned) * @param handle Mutable JSON array * @param index Element index - * @param value 64-bit integer value + * @param value 64-bit integer value (std::variant) * @return true on success */ - virtual bool ArrayReplaceInt64(YYJSONValue* handle, size_t index, int64_t value) = 0; + virtual bool ArrayReplaceInt64(JsonValue* handle, size_t index, std::variant value) = 0; /** * Replace element at index with null (mutable only) @@ -706,7 +753,7 @@ class IYYJSONManager : public SMInterface * @param index Element index * @return true on success */ - virtual bool ArrayReplaceNull(YYJSONValue* handle, size_t index) = 0; + virtual bool ArrayReplaceNull(JsonValue* handle, size_t index) = 0; /** * Replace element at index with string (mutable only) @@ -715,7 +762,7 @@ class IYYJSONManager : public SMInterface * @param value String value * @return true on success */ - virtual bool ArrayReplaceString(YYJSONValue* handle, size_t index, const char* value) = 0; + virtual bool ArrayReplaceString(JsonValue* handle, size_t index, const char* value) = 0; /** * Append JSON value to end of array (mutable only) @@ -723,7 +770,7 @@ class IYYJSONManager : public SMInterface * @param value JSON value to append * @return true on success */ - virtual bool ArrayAppend(YYJSONValue* handle, YYJSONValue* value) = 0; + virtual bool ArrayAppend(JsonValue* handle, JsonValue* value) = 0; /** * Append boolean to end of array (mutable only) @@ -731,7 +778,7 @@ class IYYJSONManager : public SMInterface * @param value Boolean value * @return true on success */ - virtual bool ArrayAppendBool(YYJSONValue* handle, bool value) = 0; + virtual bool ArrayAppendBool(JsonValue* handle, bool value) = 0; /** * Append float to end of array (mutable only) @@ -739,7 +786,7 @@ class IYYJSONManager : public SMInterface * @param value Float value * @return true on success */ - virtual bool ArrayAppendFloat(YYJSONValue* handle, double value) = 0; + virtual bool ArrayAppendFloat(JsonValue* handle, double value) = 0; /** * Append integer to end of array (mutable only) @@ -747,22 +794,22 @@ class IYYJSONManager : public SMInterface * @param value Integer value * @return true on success */ - virtual bool ArrayAppendInt(YYJSONValue* handle, int value) = 0; + virtual bool ArrayAppendInt(JsonValue* handle, int value) = 0; /** - * Append 64-bit integer to end of array (mutable only) + * Append 64-bit integer to end of array (mutable only, auto-detects signed/unsigned) * @param handle Mutable JSON array - * @param value 64-bit integer value + * @param value 64-bit integer value (std::variant) * @return true on success */ - virtual bool ArrayAppendInt64(YYJSONValue* handle, int64_t value) = 0; + virtual bool ArrayAppendInt64(JsonValue* handle, std::variant value) = 0; /** * Append null to end of array (mutable only) * @param handle Mutable JSON array * @return true on success */ - virtual bool ArrayAppendNull(YYJSONValue* handle) = 0; + virtual bool ArrayAppendNull(JsonValue* handle) = 0; /** * Append string to end of array (mutable only) @@ -770,7 +817,124 @@ class IYYJSONManager : public SMInterface * @param value String value * @return true on success */ - virtual bool ArrayAppendString(YYJSONValue* handle, const char* value) = 0; + virtual bool ArrayAppendString(JsonValue* handle, const char* value) = 0; + + /** + * Insert JSON value at specific index (mutable only) + * @param handle Mutable JSON array + * @param index Element index (0 to size, size means append) + * @param value JSON value to insert + * @return true on success + */ + virtual bool ArrayInsert(JsonValue* handle, size_t index, JsonValue* value) = 0; + + /** + * Insert boolean at specific index (mutable only) + * @param handle Mutable JSON array + * @param index Element index + * @param value Boolean value + * @return true on success + */ + virtual bool ArrayInsertBool(JsonValue* handle, size_t index, bool value) = 0; + + /** + * Insert integer at specific index (mutable only) + * @param handle Mutable JSON array + * @param index Element index + * @param value Integer value + * @return true on success + */ + virtual bool ArrayInsertInt(JsonValue* handle, size_t index, int value) = 0; + + /** + * Insert 64-bit integer at specific index (mutable only, auto-detects signed/unsigned) + * @param handle Mutable JSON array + * @param index Element index + * @param value 64-bit integer value (std::variant) + * @return true on success + */ + virtual bool ArrayInsertInt64(JsonValue* handle, size_t index, std::variant value) = 0; + + /** + * Insert float at specific index (mutable only) + * @param handle Mutable JSON array + * @param index Element index + * @param value Float value + * @return true on success + */ + virtual bool ArrayInsertFloat(JsonValue* handle, size_t index, double value) = 0; + + /** + * Insert string at specific index (mutable only) + * @param handle Mutable JSON array + * @param index Element index + * @param value String value + * @return true on success + */ + virtual bool ArrayInsertString(JsonValue* handle, size_t index, const char* value) = 0; + + /** + * Insert null at specific index (mutable only) + * @param handle Mutable JSON array + * @param index Element index + * @return true on success + */ + virtual bool ArrayInsertNull(JsonValue* handle, size_t index) = 0; + + /** + * Prepend JSON value to beginning of array (mutable only) + * @param handle Mutable JSON array + * @param value JSON value to prepend + * @return true on success + */ + virtual bool ArrayPrepend(JsonValue* handle, JsonValue* value) = 0; + + /** + * Prepend boolean to beginning of array (mutable only) + * @param handle Mutable JSON array + * @param value Boolean value + * @return true on success + */ + virtual bool ArrayPrependBool(JsonValue* handle, bool value) = 0; + + /** + * Prepend integer to beginning of array (mutable only) + * @param handle Mutable JSON array + * @param value Integer value + * @return true on success + */ + virtual bool ArrayPrependInt(JsonValue* handle, int value) = 0; + + /** + * Prepend 64-bit integer to beginning of array (mutable only, auto-detects signed/unsigned) + * @param handle Mutable JSON array + * @param value 64-bit integer value (std::variant) + * @return true on success + */ + virtual bool ArrayPrependInt64(JsonValue* handle, std::variant value) = 0; + + /** + * Prepend float to beginning of array (mutable only) + * @param handle Mutable JSON array + * @param value Float value + * @return true on success + */ + virtual bool ArrayPrependFloat(JsonValue* handle, double value) = 0; + + /** + * Prepend string to beginning of array (mutable only) + * @param handle Mutable JSON array + * @param value String value + * @return true on success + */ + virtual bool ArrayPrependString(JsonValue* handle, const char* value) = 0; + + /** + * Prepend null to beginning of array (mutable only) + * @param handle Mutable JSON array + * @return true on success + */ + virtual bool ArrayPrependNull(JsonValue* handle) = 0; /** * Remove element at specific index (mutable only) @@ -778,21 +942,21 @@ class IYYJSONManager : public SMInterface * @param index Element index * @return true on success */ - virtual bool ArrayRemove(YYJSONValue* handle, size_t index) = 0; + virtual bool ArrayRemove(JsonValue* handle, size_t index) = 0; /** * Remove first element (mutable only) * @param handle Mutable JSON array * @return true on success */ - virtual bool ArrayRemoveFirst(YYJSONValue* handle) = 0; + virtual bool ArrayRemoveFirst(JsonValue* handle) = 0; /** * Remove last element (mutable only) * @param handle Mutable JSON array * @return true on success */ - virtual bool ArrayRemoveLast(YYJSONValue* handle) = 0; + virtual bool ArrayRemoveLast(JsonValue* handle) = 0; /** * Remove range of elements (mutable only) @@ -801,14 +965,14 @@ class IYYJSONManager : public SMInterface * @param end_index End index (exclusive) * @return true on success */ - virtual bool ArrayRemoveRange(YYJSONValue* handle, size_t start_index, size_t end_index) = 0; + virtual bool ArrayRemoveRange(JsonValue* handle, size_t start_index, size_t end_index) = 0; /** * Remove all elements (mutable only) * @param handle Mutable JSON array * @return true on success */ - virtual bool ArrayClear(YYJSONValue* handle) = 0; + virtual bool ArrayClear(JsonValue* handle) = 0; /** * Find index of boolean value @@ -816,7 +980,7 @@ class IYYJSONManager : public SMInterface * @param search_value Boolean value to search for * @return Index of first match, or -1 if not found */ - virtual int ArrayIndexOfBool(YYJSONValue* handle, bool search_value) = 0; + virtual int ArrayIndexOfBool(JsonValue* handle, bool search_value) = 0; /** * Find index of string value @@ -824,7 +988,7 @@ class IYYJSONManager : public SMInterface * @param search_value String value to search for * @return Index of first match, or -1 if not found */ - virtual int ArrayIndexOfString(YYJSONValue* handle, const char* search_value) = 0; + virtual int ArrayIndexOfString(JsonValue* handle, const char* search_value) = 0; /** * Find index of integer value @@ -832,7 +996,7 @@ class IYYJSONManager : public SMInterface * @param search_value Integer value to search for * @return Index of first match, or -1 if not found */ - virtual int ArrayIndexOfInt(YYJSONValue* handle, int search_value) = 0; + virtual int ArrayIndexOfInt(JsonValue* handle, int search_value) = 0; /** * Find index of 64-bit integer value @@ -840,7 +1004,15 @@ class IYYJSONManager : public SMInterface * @param search_value 64-bit integer value to search for * @return Index of first match, or -1 if not found */ - virtual int ArrayIndexOfInt64(YYJSONValue* handle, int64_t search_value) = 0; + virtual int ArrayIndexOfInt64(JsonValue* handle, int64_t search_value) = 0; + + /** + * Find index of 64-bit unsigned integer value + * @param handle JSON array + * @param search_value 64-bit unsigned integer value to search for + * @return Index of first match, or -1 if not found + */ + virtual int ArrayIndexOfUint64(JsonValue* handle, uint64_t search_value) = 0; /** * Find index of float value @@ -848,16 +1020,16 @@ class IYYJSONManager : public SMInterface * @param search_value Float value to search for * @return Index of first match, or -1 if not found */ - virtual int ArrayIndexOfFloat(YYJSONValue* handle, double search_value) = 0; + virtual int ArrayIndexOfFloat(JsonValue* handle, double search_value) = 0; /** * Sort array elements * @param handle Mutable JSON array - * @param sort_mode Sort order (YYJSON_SORT_ASC, YYJSON_SORT_DESC, or YYJSON_SORT_RANDOM) + * @param sort_mode Sort order (see JSON_SORT_ORDER enum) * @return true on success * @note Only works on mutable arrays */ - virtual bool ArraySort(YYJSONValue* handle, YYJSON_SORT_ORDER sort_mode) = 0; + virtual bool ArraySort(JsonValue* handle, JSON_SORT_ORDER sort_mode) = 0; /** * Create JSON value from format string and parameters @@ -876,7 +1048,7 @@ class IYYJSONManager : public SMInterface * @return New JSON value or nullptr on error * @note Example: format="{s:i,s:s}" with params ["age", 25, "name", "John"] creates {"age":25,"name":"John"} */ - virtual YYJSONValue* Pack(const char* format, IPackParamProvider* param_provider, + virtual JsonValue* Pack(const char* format, IPackParamProvider* param_provider, char* error = nullptr, size_t error_size = 0) = 0; /** @@ -884,41 +1056,41 @@ class IYYJSONManager : public SMInterface * @param value Boolean value * @return New JSON boolean or nullptr on failure */ - virtual YYJSONValue* CreateBool(bool value) = 0; + virtual JsonValue* CreateBool(bool value) = 0; /** * Create a JSON float value * @param value Float value * @return New JSON float or nullptr on failure */ - virtual YYJSONValue* CreateFloat(double value) = 0; + virtual JsonValue* CreateFloat(double value) = 0; /** * Create a JSON integer value * @param value Integer value * @return New JSON integer or nullptr on failure */ - virtual YYJSONValue* CreateInt(int value) = 0; + virtual JsonValue* CreateInt(int value) = 0; /** - * Create a JSON 64-bit integer value - * @param value 64-bit integer value + * Create a JSON 64-bit integer value (auto-detects signed/unsigned) + * @param value 64-bit integer value (std::variant) * @return New JSON integer64 or nullptr on failure */ - virtual YYJSONValue* CreateInt64(int64_t value) = 0; + virtual JsonValue* CreateInt64(std::variant value) = 0; /** * Create a JSON null value * @return New JSON null or nullptr on failure */ - virtual YYJSONValue* CreateNull() = 0; + virtual JsonValue* CreateNull() = 0; /** * Create a JSON string value * @param value String value * @return New JSON string or nullptr on failure */ - virtual YYJSONValue* CreateString(const char* value) = 0; + virtual JsonValue* CreateString(const char* value) = 0; /** * Get boolean value from JSON @@ -926,7 +1098,7 @@ class IYYJSONManager : public SMInterface * @param out_value Pointer to receive boolean value * @return true on success, false on type mismatch */ - virtual bool GetBool(YYJSONValue* handle, bool* out_value) = 0; + virtual bool GetBool(JsonValue* handle, bool* out_value) = 0; /** * Get float value from JSON @@ -934,7 +1106,7 @@ class IYYJSONManager : public SMInterface * @param out_value Pointer to receive float value * @return true on success, false on type mismatch */ - virtual bool GetFloat(YYJSONValue* handle, double* out_value) = 0; + virtual bool GetFloat(JsonValue* handle, double* out_value) = 0; /** * Get integer value from JSON @@ -942,15 +1114,15 @@ class IYYJSONManager : public SMInterface * @param out_value Pointer to receive integer value * @return true on success, false on type mismatch */ - virtual bool GetInt(YYJSONValue* handle, int* out_value) = 0; + virtual bool GetInt(JsonValue* handle, int* out_value) = 0; /** - * Get 64-bit integer value from JSON + * Get 64-bit integer value from JSON (auto-detects signed/unsigned) * @param handle JSON value - * @param out_value Pointer to receive 64-bit integer value + * @param out_value Pointer to receive 64-bit integer value (std::variant) * @return true on success, false on type mismatch */ - virtual bool GetInt64(YYJSONValue* handle, int64_t* out_value) = 0; + virtual bool GetInt64(JsonValue* handle, std::variant* out_value) = 0; /** * Get string value from JSON @@ -959,7 +1131,7 @@ class IYYJSONManager : public SMInterface * @param out_len Pointer to receive string length * @return true on success, false on type mismatch */ - virtual bool GetString(YYJSONValue* handle, const char** out_str, size_t* out_len) = 0; + virtual bool GetString(JsonValue* handle, const char** out_str, size_t* out_len) = 0; /** * Get value using JSON Pointer @@ -969,7 +1141,7 @@ class IYYJSONManager : public SMInterface * @param error_size Error buffer size * @return JSON value or nullptr on error */ - virtual YYJSONValue* PtrGet(YYJSONValue* handle, const char* path, + virtual JsonValue* PtrGet(JsonValue* handle, const char* path, char* error = nullptr, size_t error_size = 0) = 0; /** @@ -981,7 +1153,7 @@ class IYYJSONManager : public SMInterface * @param error_size Error buffer size * @return true on success, false on error */ - virtual bool PtrGetBool(YYJSONValue* handle, const char* path, bool* out_value, + virtual bool PtrGetBool(JsonValue* handle, const char* path, bool* out_value, char* error = nullptr, size_t error_size = 0) = 0; /** @@ -993,7 +1165,7 @@ class IYYJSONManager : public SMInterface * @param error_size Error buffer size * @return true on success, false on error */ - virtual bool PtrGetFloat(YYJSONValue* handle, const char* path, double* out_value, + virtual bool PtrGetFloat(JsonValue* handle, const char* path, double* out_value, char* error = nullptr, size_t error_size = 0) = 0; /** @@ -1005,19 +1177,19 @@ class IYYJSONManager : public SMInterface * @param error_size Error buffer size * @return true on success, false on error */ - virtual bool PtrGetInt(YYJSONValue* handle, const char* path, int* out_value, + virtual bool PtrGetInt(JsonValue* handle, const char* path, int* out_value, char* error = nullptr, size_t error_size = 0) = 0; /** - * Get 64-bit integer value using JSON Pointer + * Get 64-bit integer value using JSON Pointer (auto-detects signed/unsigned) * @param handle JSON value * @param path JSON Pointer path - * @param out_value Pointer to receive 64-bit integer value + * @param out_value Pointer to receive 64-bit integer value (std::variant) * @param error Error buffer (optional) * @param error_size Error buffer size * @return true on success, false on error */ - virtual bool PtrGetInt64(YYJSONValue* handle, const char* path, int64_t* out_value, + virtual bool PtrGetInt64(JsonValue* handle, const char* path, std::variant* out_value, char* error = nullptr, size_t error_size = 0) = 0; /** @@ -1030,7 +1202,7 @@ class IYYJSONManager : public SMInterface * @param error_size Error buffer size * @return true on success, false on error */ - virtual bool PtrGetString(YYJSONValue* handle, const char* path, const char** out_str, + virtual bool PtrGetString(JsonValue* handle, const char* path, const char** out_str, size_t* out_len, char* error = nullptr, size_t error_size = 0) = 0; /** @@ -1042,7 +1214,7 @@ class IYYJSONManager : public SMInterface * @param error_size Error buffer size * @return true on success, false on error */ - virtual bool PtrGetIsNull(YYJSONValue* handle, const char* path, bool* out_is_null, + virtual bool PtrGetIsNull(JsonValue* handle, const char* path, bool* out_is_null, char* error = nullptr, size_t error_size = 0) = 0; /** @@ -1054,7 +1226,7 @@ class IYYJSONManager : public SMInterface * @param error_size Error buffer size * @return true on success, false on error */ - virtual bool PtrGetLength(YYJSONValue* handle, const char* path, size_t* out_len, + virtual bool PtrGetLength(JsonValue* handle, const char* path, size_t* out_len, char* error = nullptr, size_t error_size = 0) = 0; /** @@ -1066,7 +1238,7 @@ class IYYJSONManager : public SMInterface * @param error_size Error buffer size * @return true on success, false on error */ - virtual bool PtrSet(YYJSONValue* handle, const char* path, YYJSONValue* value, + virtual bool PtrSet(JsonValue* handle, const char* path, JsonValue* value, char* error = nullptr, size_t error_size = 0) = 0; /** @@ -1078,7 +1250,7 @@ class IYYJSONManager : public SMInterface * @param error_size Error buffer size * @return true on success, false on error */ - virtual bool PtrSetBool(YYJSONValue* handle, const char* path, bool value, + virtual bool PtrSetBool(JsonValue* handle, const char* path, bool value, char* error = nullptr, size_t error_size = 0) = 0; /** @@ -1090,7 +1262,7 @@ class IYYJSONManager : public SMInterface * @param error_size Error buffer size * @return true on success, false on error */ - virtual bool PtrSetFloat(YYJSONValue* handle, const char* path, double value, + virtual bool PtrSetFloat(JsonValue* handle, const char* path, double value, char* error = nullptr, size_t error_size = 0) = 0; /** @@ -1102,19 +1274,19 @@ class IYYJSONManager : public SMInterface * @param error_size Error buffer size * @return true on success, false on error */ - virtual bool PtrSetInt(YYJSONValue* handle, const char* path, int value, + virtual bool PtrSetInt(JsonValue* handle, const char* path, int value, char* error = nullptr, size_t error_size = 0) = 0; /** - * Set 64-bit integer value using JSON Pointer (mutable only) + * Set 64-bit integer value using JSON Pointer (mutable only, auto-detects signed/unsigned) * @param handle Mutable JSON value * @param path JSON Pointer path - * @param value 64-bit integer value + * @param value 64-bit integer value (std::variant) * @param error Error buffer (optional) * @param error_size Error buffer size * @return true on success, false on error */ - virtual bool PtrSetInt64(YYJSONValue* handle, const char* path, int64_t value, + virtual bool PtrSetInt64(JsonValue* handle, const char* path, std::variant value, char* error = nullptr, size_t error_size = 0) = 0; /** @@ -1126,7 +1298,7 @@ class IYYJSONManager : public SMInterface * @param error_size Error buffer size * @return true on success, false on error */ - virtual bool PtrSetString(YYJSONValue* handle, const char* path, const char* value, + virtual bool PtrSetString(JsonValue* handle, const char* path, const char* value, char* error = nullptr, size_t error_size = 0) = 0; /** @@ -1137,7 +1309,7 @@ class IYYJSONManager : public SMInterface * @param error_size Error buffer size * @return true on success, false on error */ - virtual bool PtrSetNull(YYJSONValue* handle, const char* path, + virtual bool PtrSetNull(JsonValue* handle, const char* path, char* error = nullptr, size_t error_size = 0) = 0; /** @@ -1149,7 +1321,7 @@ class IYYJSONManager : public SMInterface * @param error_size Error buffer size * @return true on success, false on error */ - virtual bool PtrAdd(YYJSONValue* handle, const char* path, YYJSONValue* value, + virtual bool PtrAdd(JsonValue* handle, const char* path, JsonValue* value, char* error = nullptr, size_t error_size = 0) = 0; /** @@ -1161,7 +1333,7 @@ class IYYJSONManager : public SMInterface * @param error_size Error buffer size * @return true on success, false on error */ - virtual bool PtrAddBool(YYJSONValue* handle, const char* path, bool value, + virtual bool PtrAddBool(JsonValue* handle, const char* path, bool value, char* error = nullptr, size_t error_size = 0) = 0; /** @@ -1173,7 +1345,7 @@ class IYYJSONManager : public SMInterface * @param error_size Error buffer size * @return true on success, false on error */ - virtual bool PtrAddFloat(YYJSONValue* handle, const char* path, double value, + virtual bool PtrAddFloat(JsonValue* handle, const char* path, double value, char* error = nullptr, size_t error_size = 0) = 0; /** @@ -1185,19 +1357,19 @@ class IYYJSONManager : public SMInterface * @param error_size Error buffer size * @return true on success, false on error */ - virtual bool PtrAddInt(YYJSONValue* handle, const char* path, int value, + virtual bool PtrAddInt(JsonValue* handle, const char* path, int value, char* error = nullptr, size_t error_size = 0) = 0; /** - * Add 64-bit integer to array using JSON Pointer (mutable only) + * Add 64-bit integer to array using JSON Pointer (mutable only, auto-detects signed/unsigned) * @param handle Mutable JSON value * @param path JSON Pointer path to array - * @param value 64-bit integer value + * @param value 64-bit integer value (std::variant) * @param error Error buffer (optional) * @param error_size Error buffer size * @return true on success, false on error */ - virtual bool PtrAddInt64(YYJSONValue* handle, const char* path, int64_t value, + virtual bool PtrAddInt64(JsonValue* handle, const char* path, std::variant value, char* error = nullptr, size_t error_size = 0) = 0; /** @@ -1209,7 +1381,7 @@ class IYYJSONManager : public SMInterface * @param error_size Error buffer size * @return true on success, false on error */ - virtual bool PtrAddString(YYJSONValue* handle, const char* path, const char* value, + virtual bool PtrAddString(JsonValue* handle, const char* path, const char* value, char* error = nullptr, size_t error_size = 0) = 0; /** @@ -1220,7 +1392,7 @@ class IYYJSONManager : public SMInterface * @param error_size Error buffer size * @return true on success, false on error */ - virtual bool PtrAddNull(YYJSONValue* handle, const char* path, + virtual bool PtrAddNull(JsonValue* handle, const char* path, char* error = nullptr, size_t error_size = 0) = 0; /** @@ -1231,7 +1403,7 @@ class IYYJSONManager : public SMInterface * @param error_size Error buffer size * @return true on success, false on error */ - virtual bool PtrRemove(YYJSONValue* handle, const char* path, + virtual bool PtrRemove(JsonValue* handle, const char* path, char* error = nullptr, size_t error_size = 0) = 0; /** @@ -1240,7 +1412,7 @@ class IYYJSONManager : public SMInterface * @param path JSON Pointer path * @return JSON value or nullptr if not found */ - virtual YYJSONValue* PtrTryGet(YYJSONValue* handle, const char* path) = 0; + virtual JsonValue* PtrTryGet(JsonValue* handle, const char* path) = 0; /** * Try to get boolean value using JSON Pointer (returns false on failure) @@ -1249,7 +1421,7 @@ class IYYJSONManager : public SMInterface * @param out_value Pointer to receive boolean value * @return true on success, false if not found or type mismatch */ - virtual bool PtrTryGetBool(YYJSONValue* handle, const char* path, bool* out_value) = 0; + virtual bool PtrTryGetBool(JsonValue* handle, const char* path, bool* out_value) = 0; /** * Try to get float value using JSON Pointer (returns false on failure) @@ -1258,7 +1430,7 @@ class IYYJSONManager : public SMInterface * @param out_value Pointer to receive float value * @return true on success, false if not found or type mismatch */ - virtual bool PtrTryGetFloat(YYJSONValue* handle, const char* path, double* out_value) = 0; + virtual bool PtrTryGetFloat(JsonValue* handle, const char* path, double* out_value) = 0; /** * Try to get integer value using JSON Pointer (returns false on failure) @@ -1267,16 +1439,16 @@ class IYYJSONManager : public SMInterface * @param out_value Pointer to receive integer value * @return true on success, false if not found or type mismatch */ - virtual bool PtrTryGetInt(YYJSONValue* handle, const char* path, int* out_value) = 0; + virtual bool PtrTryGetInt(JsonValue* handle, const char* path, int* out_value) = 0; /** - * Try to get 64-bit integer value using JSON Pointer (returns false on failure) + * Try to get 64-bit integer value using JSON Pointer (auto-detects signed/unsigned, returns false on failure) * @param handle JSON value * @param path JSON Pointer path - * @param out_value Pointer to receive 64-bit integer value + * @param out_value Pointer to receive 64-bit integer value (std::variant) * @return true on success, false if not found or type mismatch */ - virtual bool PtrTryGetInt64(YYJSONValue* handle, const char* path, int64_t* out_value) = 0; + virtual bool PtrTryGetInt64(JsonValue* handle, const char* path, std::variant* out_value) = 0; /** * Try to get string value using JSON Pointer (returns false on failure) @@ -1286,9 +1458,9 @@ class IYYJSONManager : public SMInterface * @param out_len Pointer to receive string length * @return true on success, false if not found or type mismatch */ - virtual bool PtrTryGetString(YYJSONValue* handle, const char* path, const char** out_str, size_t* out_len) = 0; + virtual bool PtrTryGetString(JsonValue* handle, const char* path, const char** out_str, size_t* out_len) = 0; - // Note: Iterators are stateful and stored in the YYJSONValue object + // Note: Iterators are stateful and stored in the JsonValue object // Call these functions in a loop until they return false /** @@ -1296,23 +1468,25 @@ class IYYJSONManager : public SMInterface * @param handle JSON object * @param out_key Pointer to receive key string * @param out_key_len Pointer to receive key length (can be nullptr) - * @param out_value Pointer to receive value (creates new YYJSONValue) + * @param out_value Pointer to receive value (creates new JsonValue) * @return true if iteration continues, false if iteration complete * @note Iterator state is maintained in handle. Returns false when iteration completes. + * @deprecated Use JSONObjIter instead for better iterator support */ - virtual bool ObjectForeachNext(YYJSONValue* handle, const char** out_key, - size_t* out_key_len, YYJSONValue** out_value) = 0; + virtual bool ObjectForeachNext(JsonValue* handle, const char** out_key, + size_t* out_key_len, JsonValue** out_value) = 0; /** * Get next index-value pair from array iterator * @param handle JSON array * @param out_index Pointer to receive current index - * @param out_value Pointer to receive value (creates new YYJSONValue) + * @param out_value Pointer to receive value (creates new JsonValue) * @return true if iteration continues, false if iteration complete * @note Iterator state is maintained in handle. Returns false when iteration completes. + * @deprecated Use JSONArrIter instead for better iterator support */ - virtual bool ArrayForeachNext(YYJSONValue* handle, size_t* out_index, - YYJSONValue** out_value) = 0; + virtual bool ArrayForeachNext(JsonValue* handle, size_t* out_index, + JsonValue** out_value) = 0; /** * Get next key from object iterator (key only, no value) @@ -1321,8 +1495,9 @@ class IYYJSONManager : public SMInterface * @param out_key_len Pointer to receive key length (can be nullptr) * @return true if iteration continues, false if iteration complete * @note Iterator state is maintained in handle. Returns false when iteration completes. + * @deprecated Use JSONObjIter instead for better iterator support */ - virtual bool ObjectForeachKeyNext(YYJSONValue* handle, const char** out_key, + virtual bool ObjectForeachKeyNext(JsonValue* handle, const char** out_key, size_t* out_key_len) = 0; /** @@ -1331,30 +1506,290 @@ class IYYJSONManager : public SMInterface * @param out_index Pointer to receive current index * @return true if iteration continues, false if iteration complete * @note Iterator state is maintained in handle. Returns false when iteration completes. + * @deprecated Use JSONArrIter instead for better iterator support */ - virtual bool ArrayForeachIndexNext(YYJSONValue* handle, size_t* out_index) = 0; + virtual bool ArrayForeachIndexNext(JsonValue* handle, size_t* out_index) = 0; /** - * Release a YYJSONValue object + * Release a JsonValue object * External extensions should use this instead of deleting directly - * @param value The YYJSONValue to release + * @param value The JsonValue to release */ - virtual void Release(YYJSONValue* value) = 0; + virtual void Release(JsonValue* value) = 0; /** - * Get the HandleType_t for YYJSON handles + * Get the HandleType_t for JSON handles * External extensions MUST use this method to obtain the handle type - * @return The HandleType_t for YYJSON handles + * @return The HandleType_t for JSON handles */ virtual HandleType_t GetHandleType() = 0; /** - * Read YYJSONValue from a SourceMod handle + * Read JsonValue from a SourceMod handle + * @param pContext Plugin context + * @param handle Handle to read from + * @return JsonValue pointer, or nullptr on error (error will be reported to context) + */ + virtual JsonValue* GetFromHandle(IPluginContext* pContext, Handle_t handle) = 0; + + /** + * Initialize an array iterator (same as ArrIterWith but returns pointer) + * @param handle JSON array value + * @return New array iterator or nullptr on error + */ + virtual JsonArrIter* ArrIterInit(JsonValue* handle) = 0; + + /** + * Create an array iterator with an array + * @param handle JSON array value + * @return New array iterator or nullptr on error + */ + virtual JsonArrIter* ArrIterWith(JsonValue* handle) = 0; + + /** + * Get next element from array iterator + * @param iter Array iterator + * @return JSON value wrapper for next element, or nullptr if iteration complete + */ + virtual JsonValue* ArrIterNext(JsonArrIter* iter) = 0; + + /** + * Check if array iterator has more elements + * @param iter Array iterator + * @return true if has next element, false otherwise + */ + virtual bool ArrIterHasNext(JsonArrIter* iter) = 0; + + /** + * Get current index in array iteration + * @param iter Array iterator + * @return Current index (0-based), or SIZE_MAX if iterator is not positioned + */ + virtual size_t ArrIterGetIndex(JsonArrIter* iter) = 0; + + /** + * Remove current element from array (mutable only) + * @param iter Mutable array iterator + * @return Pointer to removed value, or nullptr on error + */ + virtual void* ArrIterRemove(JsonArrIter* iter) = 0; + + /** + * Initialize an object iterator (same as ObjIterWith but returns pointer) + * @param handle JSON object value + * @return New object iterator or nullptr on error + */ + virtual JsonObjIter* ObjIterInit(JsonValue* handle) = 0; + + /** + * Create an object iterator with an object + * @param handle JSON object value + * @return New object iterator or nullptr on error + */ + virtual JsonObjIter* ObjIterWith(JsonValue* handle) = 0; + + /** + * Get next key from object iterator + * @param iter Object iterator + * @return Key value (yyjson_val* for immutable, yyjson_mut_val* for mutable), or nullptr if iteration complete + */ + virtual void* ObjIterNext(JsonObjIter* iter) = 0; + + /** + * Check if object iterator has more elements + * @param iter Object iterator + * @return true if has next element, false otherwise + */ + virtual bool ObjIterHasNext(JsonObjIter* iter) = 0; + + /** + * Get value by key from object iterator + * @param iter Object iterator + * @param key Key value (yyjson_val* or yyjson_mut_val*) + * @return JSON value wrapper for the value, or nullptr on error + */ + virtual JsonValue* ObjIterGetVal(JsonObjIter* iter, void* key) = 0; + + /** + * Iterates to a specified key and returns the value + * @param iter Object iterator + * @param key Key name string + * @return JSON value wrapper for the value, or nullptr if key not found + * @note This function searches the object using the iterator structure + * @warning This function takes a linear search time if the key is not nearby. + */ + virtual JsonValue* ObjIterGet(JsonObjIter* iter, const char* key) = 0; + + /** + * Get current index in object iteration + * @param iter Object iterator + * @return Current index (0-based), or SIZE_MAX if iterator is not positioned + */ + virtual size_t ObjIterGetIndex(JsonObjIter* iter) = 0; + + /** + * Remove current key-value pair from object (mutable only) + * @param iter Mutable object iterator + * @return Pointer to removed key, or nullptr on error + */ + virtual void* ObjIterRemove(JsonObjIter* iter) = 0; + + /** + * Release an array iterator + * @param iter Iterator to release + */ + virtual void ReleaseArrIter(JsonArrIter* iter) = 0; + + /** + * Release an object iterator + * @param iter Iterator to release + */ + virtual void ReleaseObjIter(JsonObjIter* iter) = 0; + + /** + * Get the HandleType_t for array iterator handles + * @return The HandleType_t for array iterator handles + */ + virtual HandleType_t GetArrIterHandleType() = 0; + + /** + * Get the HandleType_t for object iterator handles + * @return The HandleType_t for object iterator handles + */ + virtual HandleType_t GetObjIterHandleType() = 0; + + /** + * Read JsonArrIter from a SourceMod handle + * @param pContext Plugin context + * @param handle Handle to read from + * @return JsonArrIter pointer, or nullptr on error + */ + virtual JsonArrIter* GetArrIterFromHandle(IPluginContext* pContext, Handle_t handle) = 0; + + /** + * Read JsonObjIter from a SourceMod handle * @param pContext Plugin context * @param handle Handle to read from - * @return YYJSONValue pointer, or nullptr on error (error will be reported to context) + * @return JsonObjIter pointer, or nullptr on error + */ + virtual JsonObjIter* GetObjIterFromHandle(IPluginContext* pContext, Handle_t handle) = 0; + + /** + * Read a JSON number from string + * @param dat The JSON data (UTF-8 without BOM), null-terminator is required + * @param read_flg Read flags (YYJSON_READ_FLAG values, default: 0) + * @param error Error buffer (optional) + * @param error_size Error buffer size + * @param out_consumed Pointer to receive number of characters consumed (optional) + * @return New JSON number value or nullptr on error + * @note The returned value is a mutable number value + */ + virtual JsonValue* ReadNumber(const char* dat, uint32_t read_flg = 0, + char* error = nullptr, size_t error_size = 0, size_t* out_consumed = nullptr) = 0; + + /** + * Write a JSON number to string buffer + * @param handle JSON number value + * @param buffer Output buffer (must be at least 40 bytes for floating-point, 21 bytes for integer) + * @param buffer_size Buffer size + * @param out_written Pointer to receive number of characters written (excluding null terminator) (optional) + * @return true on success, false on error + * @note The buffer must be large enough to hold the number string + */ + virtual bool WriteNumber(JsonValue* handle, char* buffer, size_t buffer_size, + size_t* out_written = nullptr) = 0; + + /** + * Set floating-point number's output format to single-precision + * @param handle JSON floating-point number value + * @param flt true to use single-precision (float), false to use double-precision (double) + * @return true on success, false if handle is not a floating-point number + * @note Only works on floating-point numbers (not integers) + */ + virtual bool SetFpToFloat(JsonValue* handle, bool flt) = 0; + + /** + * Set floating-point number's output format to fixed-point notation + * @param handle JSON floating-point number value + * @param prec Precision (1-15), similar to ECMAScript Number.prototype.toFixed(prec) but with trailing zeros removed + * @return true on success, false if handle is not a floating-point number or prec is out of range + * @note Only works on floating-point numbers (not integers) + * @note This will produce shorter output but may lose some precision */ - virtual YYJSONValue* GetFromHandle(IPluginContext* pContext, Handle_t handle) = 0; + virtual bool SetFpToFixed(JsonValue* handle, int prec) = 0; + + /** + * Directly modify a JSON value to boolean type + * @param handle JSON value to modify (cannot be object or array) + * @param value Boolean value + * @return true on success, false if handle is object or array + * @warning For immutable documents, this breaks immutability. Use with caution. + * @note This modifies the value in-place without creating a new value + */ + virtual bool SetBool(JsonValue* handle, bool value) = 0; + + /** + * Directly modify a JSON value to integer type + * @param handle JSON value to modify (cannot be object or array) + * @param value Integer value + * @return true on success, false if handle is object or array + * @warning For immutable documents, this breaks immutability. Use with caution. + * @note This modifies the value in-place without creating a new value + */ + virtual bool SetInt(JsonValue* handle, int value) = 0; + + /** + * Directly modify a JSON value to 64-bit integer type (auto-detects signed/unsigned) + * @param handle JSON value to modify (cannot be object or array) + * @param value 64-bit integer value (std::variant) + * @return true on success, false if handle is object or array + * @warning For immutable documents, this breaks immutability. Use with caution. + * @note This modifies the value in-place without creating a new value + */ + virtual bool SetInt64(JsonValue* handle, std::variant value) = 0; + + /** + * Directly modify a JSON value to floating-point type + * @param handle JSON value to modify (cannot be object or array) + * @param value Float value + * @return true on success, false if handle is object or array + * @warning For immutable documents, this breaks immutability. Use with caution. + * @note This modifies the value in-place without creating a new value + */ + virtual bool SetFloat(JsonValue* handle, double value) = 0; + + /** + * Directly modify a JSON value to string type + * @param handle JSON value to modify (cannot be object or array) + * @param value String value (will be copied for mutable documents) + * @return true on success, false if handle is object or array or value is null + * @warning For immutable documents, this breaks immutability and does NOT copy the string. Use with caution. + * @warning For immutable documents, the string pointer must remain valid for the document's lifetime + * @note For mutable documents, the string is copied into the document's memory pool + */ + virtual bool SetString(JsonValue* handle, const char* value) = 0; + + /** + * Directly modify a JSON value to null type + * @param handle JSON value to modify (cannot be object or array) + * @return true on success, false if handle is object or array + * @warning For immutable documents, this breaks immutability. Use with caution. + * @note This modifies the value in-place without creating a new value + */ + virtual bool SetNull(JsonValue* handle) = 0; + + /** + * Parse an int64 string value into a variant (int64_t or uint64_t) + * @param value String representation of the integer + * @param out_value Output variant to store the parsed value + * @param error Error buffer (optional) + * @param error_size Error buffer size + * @return true on success, false on parse error + * @note Auto-detects whether to use signed or unsigned based on value range + * @note Negative values are stored as int64_t, large positive values may be stored as uint64_t + */ + virtual bool ParseInt64Variant(const char* value, std::variant* out_value, + char* error = nullptr, size_t error_size = 0) = 0; }; -#endif // _INCLUDE_SM_YYJSON_IYYJSONMANAGER_H_ \ No newline at end of file +#endif // _INCLUDE_SM_JSON_IJSONMANAGER_H_ \ No newline at end of file diff --git a/pushbuild.txt b/pushbuild.txt deleted file mode 100755 index 68709e7..0000000 --- a/pushbuild.txt +++ /dev/null @@ -1,7 +0,0 @@ -some meaningless str -8c68c9448936fdfc437994f46753cdda63318509 -f30621f6c6bb134dcba0812438f47571f7bb7aee -597c28947ff356399c998661f25e44b2 -a1b8e64c0495b82ec41f8f020d3e3cd1 -e33332b9577da63d2ba281a660101493 -560bcf4dc0dd6e3debddad339bf013e6a7d41584 \ No newline at end of file diff --git a/scripting/include/yyjson.inc b/scripting/include/json.inc old mode 100644 new mode 100755 similarity index 50% rename from scripting/include/yyjson.inc rename to scripting/include/json.inc index 1813e82..6d81096 --- a/scripting/include/yyjson.inc +++ b/scripting/include/json.inc @@ -1,94 +1,94 @@ -#if defined _yyjson_included +#if defined _json_included #endinput #endif -#define _yyjson_included +#define _json_included // JSON value types -enum YYJSON_TYPE +enum JSON_TYPE { - YYJSON_TYPE_NONE = 0, // Invalid type - YYJSON_TYPE_RAW = 1, // Raw string (stored as is, used for number/literal) - YYJSON_TYPE_NULL = 2, // null - YYJSON_TYPE_BOOL = 3, // true/false - YYJSON_TYPE_NUM = 4, // Number (integer/real) - YYJSON_TYPE_STR = 5, // String - YYJSON_TYPE_ARR = 6, // Array - YYJSON_TYPE_OBJ = 7 // Object + JSON_TYPE_NONE = 0, // Invalid type + JSON_TYPE_RAW = 1, // Raw string (stored as is, used for number/literal) + JSON_TYPE_NULL = 2, // null + JSON_TYPE_BOOL = 3, // true/false + JSON_TYPE_NUM = 4, // Number (integer/real) + JSON_TYPE_STR = 5, // String + JSON_TYPE_ARR = 6, // Array + JSON_TYPE_OBJ = 7 // Object } // JSON value subtypes -enum YYJSON_SUBTYPE +enum JSON_SUBTYPE { - YYJSON_SUBTYPE_NONE = 0 << 3, // Invalid subtype - YYJSON_SUBTYPE_FALSE = 0 << 3, // Boolean false - YYJSON_SUBTYPE_TRUE = 1 << 3, // Boolean true - YYJSON_SUBTYPE_UINT = 0 << 3, // Unsigned integer - YYJSON_SUBTYPE_SINT = 1 << 3, // Signed integer - YYJSON_SUBTYPE_REAL = 2 << 3, // Real number (float/double) - YYJSON_SUBTYPE_NOESC = 1 << 3 // String without escape character + JSON_SUBTYPE_NONE = 0 << 3, // Invalid subtype + JSON_SUBTYPE_FALSE = 0 << 3, // Boolean false + JSON_SUBTYPE_TRUE = 1 << 3, // Boolean true + JSON_SUBTYPE_UINT = 0 << 3, // Unsigned integer + JSON_SUBTYPE_SINT = 1 << 3, // Signed integer + JSON_SUBTYPE_REAL = 2 << 3, // Real number (float/double) + JSON_SUBTYPE_NOESC = 1 << 3 // String without escape character } // JSON reader flags for parsing behavior -enum YYJSON_READ_FLAG +enum JSON_READ_FLAG { - YYJSON_READ_NOFLAG = 0 << 0, // Default behavior - YYJSON_READ_INSITU = 1 << 0, // Read JSON data in-situ (modify input string) - YYJSON_READ_STOP_WHEN_DONE = 1 << 1, // Stop when done instead of issuing an error if there's additional content - YYJSON_READ_ALLOW_TRAILING_COMMAS = 1 << 2, // Allow trailing commas at the end of arrays/objects - YYJSON_READ_ALLOW_COMMENTS = 1 << 3, // Allow C-style comments (/* */) and line comments (//) - YYJSON_READ_ALLOW_INF_AND_NAN = 1 << 4, // Allow nan/inf number (non-standard JSON) - YYJSON_READ_NUMBER_AS_RAW = 1 << 5, // Read numbers as raw strings - YYJSON_READ_ALLOW_INVALID_UNICODE = 1 << 6, // Allow invalid unicode when parsing string - YYJSON_READ_BIGNUM_AS_RAW = 1 << 7, // Read big numbers as raw strings - YYJSON_READ_ALLOW_BOM = 1 << 8, // Allow BOM (Byte Order Mark) at the beginning of the JSON string - YYJSON_READ_ALLOW_EXT_NUMBER = 1 << 9, // Allow extended number (non-standard JSON) - YYJSON_READ_ALLOW_EXT_ESCAPE = 1 << 10, // Allow extended escape sequences in strings (non-standard) - YYJSON_READ_ALLOW_EXT_WHITESPACE = 1 << 11, // Allow extended whitespace characters (non-standard) - YYJSON_READ_ALLOW_SINGLE_QUOTED_STR = 1 << 12, // Allow strings enclosed in single quotes (non-standard) - YYJSON_READ_ALLOW_UNQUOTED_KEY = 1 << 13, // Allow object keys without quotes (non-standard) - YYJSON_READ_JSON5 = YYJSON_READ_ALLOW_TRAILING_COMMAS | - YYJSON_READ_ALLOW_COMMENTS | - YYJSON_READ_ALLOW_INF_AND_NAN | - YYJSON_READ_ALLOW_EXT_NUMBER | - YYJSON_READ_ALLOW_EXT_ESCAPE | - YYJSON_READ_ALLOW_EXT_WHITESPACE | - YYJSON_READ_ALLOW_SINGLE_QUOTED_STR | - YYJSON_READ_ALLOW_UNQUOTED_KEY // Allow JSON5 format, see: [https://json5.org] + JSON_READ_NOFLAG = 0 << 0, // Default behavior + JSON_READ_INSITU = 1 << 0, // Read JSON data in-situ (modify input string) + JSON_READ_STOP_WHEN_DONE = 1 << 1, // Stop when done instead of issuing an error if there's additional content + JSON_READ_ALLOW_TRAILING_COMMAS = 1 << 2, // Allow trailing commas at the end of arrays/objects + JSON_READ_ALLOW_COMMENTS = 1 << 3, // Allow C-style comments (/* */) and line comments (//) + JSON_READ_ALLOW_INF_AND_NAN = 1 << 4, // Allow nan/inf number (non-standard JSON) + JSON_READ_NUMBER_AS_RAW = 1 << 5, // Read numbers as raw strings + JSON_READ_ALLOW_INVALID_UNICODE = 1 << 6, // Allow invalid unicode when parsing string + JSON_READ_BIGNUM_AS_RAW = 1 << 7, // Read big numbers as raw strings + JSON_READ_ALLOW_BOM = 1 << 8, // Allow BOM (Byte Order Mark) at the beginning of the JSON string + JSON_READ_ALLOW_EXT_NUMBER = 1 << 9, // Allow extended number (non-standard JSON) + JSON_READ_ALLOW_EXT_ESCAPE = 1 << 10, // Allow extended escape sequences in strings (non-standard) + JSON_READ_ALLOW_EXT_WHITESPACE = 1 << 11, // Allow extended whitespace characters (non-standard) + JSON_READ_ALLOW_SINGLE_QUOTED_STR = 1 << 12, // Allow strings enclosed in single quotes (non-standard) + JSON_READ_ALLOW_UNQUOTED_KEY = 1 << 13, // Allow object keys without quotes (non-standard) + JSON_READ_JSON5 = JSON_READ_ALLOW_TRAILING_COMMAS | + JSON_READ_ALLOW_COMMENTS | + JSON_READ_ALLOW_INF_AND_NAN | + JSON_READ_ALLOW_EXT_NUMBER | + JSON_READ_ALLOW_EXT_ESCAPE | + JSON_READ_ALLOW_EXT_WHITESPACE | + JSON_READ_ALLOW_SINGLE_QUOTED_STR | + JSON_READ_ALLOW_UNQUOTED_KEY // Allow JSON5 format, see: [https://json5.org] } // JSON writer flags for serialization behavior -enum YYJSON_WRITE_FLAG +enum JSON_WRITE_FLAG { - YYJSON_WRITE_NOFLAG = 0 << 0, // Default behavior - YYJSON_WRITE_PRETTY = 1 << 0, // Pretty print with indent and newline - YYJSON_WRITE_ESCAPE_UNICODE = 1 << 1, // Escape unicode as \uXXXX - YYJSON_WRITE_ESCAPE_SLASHES = 1 << 2, // Escape '/' as '\/' - YYJSON_WRITE_ALLOW_INF_AND_NAN = 1 << 3, // Write inf/nan number (non-standard JSON) - YYJSON_WRITE_INF_AND_NAN_AS_NULL = 1 << 4, // Write inf/nan as null - YYJSON_WRITE_ALLOW_INVALID_UNICODE = 1 << 5, // Allow invalid unicode when encoding string - YYJSON_WRITE_PRETTY_TWO_SPACES = 1 << 6, // Use 2 spaces for indent when pretty print - YYJSON_WRITE_NEWLINE_AT_END = 1 << 7, // Add newline at the end of output - YYJSON_WRITE_FP_TO_FLOAT = 1 << 27 // Write floating-point numbers using single-precision (float) + JSON_WRITE_NOFLAG = 0 << 0, // Default behavior + JSON_WRITE_PRETTY = 1 << 0, // Pretty print with indent and newline + JSON_WRITE_ESCAPE_UNICODE = 1 << 1, // Escape unicode as \uXXXX + JSON_WRITE_ESCAPE_SLASHES = 1 << 2, // Escape '/' as '\/' + JSON_WRITE_ALLOW_INF_AND_NAN = 1 << 3, // Write inf/nan number (non-standard JSON) + JSON_WRITE_INF_AND_NAN_AS_NULL = 1 << 4, // Write inf/nan as null + JSON_WRITE_ALLOW_INVALID_UNICODE = 1 << 5, // Allow invalid unicode when encoding string + JSON_WRITE_PRETTY_TWO_SPACES = 1 << 6, // Use 2 spaces for indent when pretty print + JSON_WRITE_NEWLINE_AT_END = 1 << 7, // Add newline at the end of output + JSON_WRITE_FP_TO_FLOAT = 1 << 27 // Write floating-point numbers using single-precision (float) } /** Write floating-point number using fixed-point notation - This is similar to ECMAScript Number.prototype.toFixed(prec) but with trailing zeros removed. The prec ranges from 1 to 15 - This will produce shorter output but may lose some precision */ -stock YYJSON_WRITE_FLAG YYJSON_WRITE_FP_TO_FIXED(int n) +stock JSON_WRITE_FLAG JSON_WRITE_FP_TO_FIXED(int n) { - return view_as(n << 28); + return view_as(n << 28); } // Sort order for arrays and objects -enum YYJSON_SORT_ORDER +enum JSON_SORT_ORDER { - YYJSON_SORT_ASC = 0, // Ascending order (default) - YYJSON_SORT_DESC = 1, // Descending order - YYJSON_SORT_RANDOM = 2 // Random order + JSON_SORT_ASC = 0, // Ascending order (default) + JSON_SORT_DESC = 1, // Descending order + JSON_SORT_RANDOM = 2 // Random order } -methodmap YYJSON < Handle +methodmap JSON < Handle { /** * Creates a JSON value using a format string and arguments @@ -114,6 +114,8 @@ methodmap YYJSON < Handle /** * Iterates over the object's key-value pairs * + * @deprecated Use JSONObjIter instead + * * @note Needs to be freed using delete or CloseHandle() * * @param buffer Buffer to copy key name to @@ -123,11 +125,14 @@ methodmap YYJSON < Handle * @return True if there are more elements, false when iteration is complete * @error Invalid handle or handle is not an object */ - public native bool ForeachObject(char[] buffer, int maxlength, YYJSON &value); + #pragma deprecated Use JSONObjIter instead. + public native bool ForeachObject(char[] buffer, int maxlength, JSON &value); /** * Iterates over the array's values * + * @deprecated Use JSONArrIter instead + * * @note Needs to be freed using delete or CloseHandle() * * @param index Variable to store current array index (starting from 0) @@ -136,11 +141,14 @@ methodmap YYJSON < Handle * @return True if there are more elements, false when iteration is complete * @error Invalid handle or handle is not an array */ - public native bool ForeachArray(int &index, YYJSON &value); + #pragma deprecated Use JSONArrIter instead. + public native bool ForeachArray(int &index, JSON &value); /** * Same as ForeachObject, but only iterates over the object's keys * + * @deprecated Use JSONObjIter instead + * * @note Use this when you only need keys (faster than ForeachObject since it doesn't create value handles) * * @param buffer Buffer to copy key name to @@ -149,11 +157,14 @@ methodmap YYJSON < Handle * @return True if there are more elements, false when iteration is complete * @error Invalid handle or handle is not an object */ + #pragma deprecated Use JSONObjIter instead. public native bool ForeachKey(char[] buffer, int maxlength); /** * Same as ForeachArray, but only iterates over the array's indexes * + * @deprecated Use JSONArrIter instead + * * @note Use this when you only need indexes (faster than ForeachArray since it doesn't create value handles) * * @param index Variable to store current array index (starting from 0) @@ -161,6 +172,7 @@ methodmap YYJSON < Handle * @return True if there are more elements, false when iteration is complete * @error Invalid handle or handle is not an array */ + #pragma deprecated Use JSONArrIter instead. public native bool ForeachIndex(int &index); /** @@ -184,12 +196,13 @@ methodmap YYJSON < Handle * * @note On 32-bit operating system, files larger than 2GB may fail to write * - * @param file The JSON file's path. If this path is NULL or invalid, the function will fail and return false. If this file is not empty, the content will be discarded + * @param file The JSON file's path. If this path is null or invalid, the function will fail and return false. + * If this file is not empty, the content will be discarded * @param flag The JSON write options * * @return True on success, false on failure */ - public native bool ToFile(const char[] file, YYJSON_WRITE_FLAG flag = YYJSON_WRITE_NOFLAG); + public native bool ToFile(const char[] file, JSON_WRITE_FLAG flag = JSON_WRITE_NOFLAG); /** * Write a value to JSON string @@ -200,7 +213,126 @@ methodmap YYJSON < Handle * * @return Number of characters written to the buffer (including null terminator) or 0 on failure */ - public native int ToString(char[] buffer, int maxlength, YYJSON_WRITE_FLAG flag = YYJSON_WRITE_NOFLAG); + public native int ToString(char[] buffer, int maxlength, JSON_WRITE_FLAG flag = JSON_WRITE_NOFLAG); + + /** + * Write a JSON number value to string buffer + * + * @note The buffer must be large enough to hold the number string (at least 40 bytes for floating-point, 21 bytes for integer) + * + * @param buffer String buffer to write to + * @param maxlength Maximum length of the string buffer + * @param written Variable to store number of characters written (excluding null terminator) (optional) + * + * @return True on success, false on failure + * @error Invalid handle, handle is not a number, or buffer too small + */ + public native bool WriteNumber(char[] buffer, int maxlength, int &written = 0); + + /** + * Set floating-point number's output format to single-precision + * + * @note Only works on floating-point numbers (not integers) + * @note This affects how the number is serialized when using ToString() + * + * @param flt True to use single-precision (float), false to use double-precision (double) + * + * @return True on success, false if handle is not a floating-point number + * @error Invalid handle or handle is not a floating-point number + */ + public native bool SetFpToFloat(bool flt); + + /** + * Set floating-point number's output format to fixed-point notation + * + * @note Only works on floating-point numbers (not integers) + * @note This is similar to ECMAScript Number.prototype.toFixed(prec) but with trailing zeros removed + * @note This will produce shorter output but may lose some precision + * @note This affects how the number is serialized when using ToString() + * + * @param prec Precision (1-15) + * + * @return True on success, false if handle is not a floating-point number or prec is out of range + * @error Invalid handle, handle is not a floating-point number, or precision out of range (1-15) + */ + public native bool SetFpToFixed(int prec); + + /** + * Directly modify a JSON value to boolean type + * + * @note This modifies the value in-place without creating a new value + * @note For immutable documents, this breaks immutability. Use with caution + * + * @param value Boolean value + * + * @return True on success, false if value is object or array + * @error Invalid handle or handle is object or array + */ + public native bool SetBool(bool value); + + /** + * Directly modify a JSON value to integer type + * + * @note This modifies the value in-place without creating a new value + * @note For immutable documents, this breaks immutability. Use with caution + * + * @param value Integer value + * + * @return True on success, false if value is object or array + * @error Invalid handle or handle is object or array + */ + public native bool SetInt(int value); + + /** + * Directly modify a JSON value to 64-bit integer type + * + * @note This modifies the value in-place without creating a new value + * @note For immutable documents, this breaks immutability. Use with caution + * @note This function auto-detects whether the value is signed or unsigned + * + * @param value 64-bit integer value (as string, can be signed or unsigned) + * + * @return True on success, false if value is object or array + * @error Invalid handle, handle is object or array, or invalid int64 string format + */ + public native bool SetInt64(const char[] value); + + /** + * Directly modify a JSON value to floating-point type + * + * @note This modifies the value in-place without creating a new value + * @note For immutable documents, this breaks immutability. Use with caution + * + * @param value Float value + * + * @return True on success, false if value is object or array + * @error Invalid handle or handle is object or array + */ + public native bool SetFloat(float value); + + /** + * Directly modify a JSON value to string type + * + * @note This modifies the value in-place without creating a new value + * @note For immutable documents, this breaks immutability and does NOT copy the string. Use with caution + * + * @param value String value + * + * @return True on success, false if value is object or array or value is null + * @error Invalid handle or handle is object or array + */ + public native bool SetString(const char[] value); + + /** + * Directly modify a JSON value to null type + * + * @note This modifies the value in-place without creating a new value + * @note For immutable documents, this breaks immutability. Use with caution + * + * @return True on success, false if value is object or array + * @error Invalid handle or handle is object or array + */ + public native bool SetNull(); /** * Parses JSON string or a file that contains JSON @@ -214,10 +346,26 @@ methodmap YYJSON < Handle * * @return JSON handle, false on failure */ - public static native any Parse(const char[] string, bool is_file = false, bool is_mutable_doc = false, YYJSON_READ_FLAG flag = YYJSON_READ_NOFLAG); + public static native any Parse(const char[] string, bool is_file = false, bool is_mutable_doc = false, JSON_READ_FLAG flag = JSON_READ_NOFLAG); + + /** + * Read a JSON number from string + * + * @note Needs to be freed using delete or CloseHandle() + * @note The input string must be null-terminated UTF-8 without BOM + * @note The returned value is a mutable number value + * + * @param dat The JSON data (UTF-8 without BOM), null-terminator is required + * @param flag Read flags (JSON_READ_FLAG values, default: JSON_READ_NOFLAG) + * @param consumed Variable to store number of characters consumed (optional) + * + * @return New JSON number value or null on error + * @error If input data is invalid or number parsing fails + */ + public static native JSON ReadNumber(const char[] dat, JSON_READ_FLAG flag = JSON_READ_NOFLAG, int &consumed = 0); /** - * Creates a deep copy of a JSON value and returns it as a new JSON handle. + * Creates a deep copy of a JSON value and returns it as a new JSON handle * * @note Needs to be freed using delete or CloseHandle() * @note This function is recursive and may cause a stack overflow if the object level is too deep @@ -228,7 +376,7 @@ methodmap YYJSON < Handle * * @return New JSON handle on success, false on failure */ - public static native any DeepCopy(const YYJSON targetDoc, const YYJSON sourceValue); + public static native any DeepCopy(const JSON targetDoc, const JSON sourceValue); /** * Returns the JSON value's type description @@ -238,9 +386,9 @@ methodmap YYJSON < Handle * @param maxlength Maximum length of the string buffer * * @return The return value should be one of these strings: "raw", "null", "string", - * "array", "object", "true", "false", "uint", "sint", "real", "unknown". + * "array", "object", "true", "false", "uint", "sint", "real", "unknown" */ - public static native void GetTypeDesc(const YYJSON value, char[] buffer, int maxlength); + public static native void GetTypeDesc(const JSON value, char[] buffer, int maxlength); /** * Returns whether two JSON values are equal (deep compare) @@ -253,7 +401,17 @@ methodmap YYJSON < Handle * * @return True if they are the same, false otherwise */ - public static native bool Equals(const YYJSON value1, const YYJSON value2); + public static native bool Equals(const JSON value1, const JSON value2); + + /** + * Check if JSON value equals a string + * + * @param value JSON handle + * @param str String to compare with + * + * @return True if value is a string and equals the given string, false otherwise + */ + public static native bool EqualsStr(const JSON value, const char[] str); /** * Creates and returns a boolean value @@ -262,9 +420,9 @@ methodmap YYJSON < Handle * * @param value The boolean value to be set * - * @return JSON handle, NULL on error + * @return JSON handle, null on error */ - public static native YYJSON CreateBool(bool value); + public static native JSON CreateBool(bool value); /** * Creates and returns a float value @@ -273,9 +431,9 @@ methodmap YYJSON < Handle * * @param value The float value to be set * - * @return JSON handle, NULL on error + * @return JSON handle, null on error */ - public static native YYJSON CreateFloat(float value); + public static native JSON CreateFloat(float value); /** * Creates and returns an int value @@ -284,20 +442,21 @@ methodmap YYJSON < Handle * * @param value The int value to be set * - * @return JSON handle, NULL on error + * @return JSON handle, null on error */ - public static native YYJSON CreateInt(int value); + public static native JSON CreateInt(int value); /** * Creates and returns an integer64 value * * @note Needs to be freed using delete or CloseHandle() + * @note This function auto-detects whether the value is signed or unsigned * - * @param value The integer64 value to be set + * @param value The integer64 value to be set (can be signed or unsigned) * - * @return JSON handle, NULL on error + * @return JSON handle, null on error */ - public static native YYJSON CreateInt64(const char[] value); + public static native JSON CreateInt64(const char[] value); /** * Creates and returns a string value @@ -306,67 +465,59 @@ methodmap YYJSON < Handle * * @param value The string value to be set * - * @return JSON handle, NULL on error + * @return JSON handle, null on error */ - public static native YYJSON CreateString(const char[] value); + public static native JSON CreateString(const char[] value); /** * Creates and returns a null value * * @note Needs to be freed using delete or CloseHandle() * - * @return JSON handle, NULL on error + * @return JSON handle, null on error */ - public static native YYJSON CreateNull(); + public static native JSON CreateNull(); /** - * Get boolean value by a JSON Handle - * - * @param value JSON handle + * Get boolean value * * @return Boolean value */ - public static native bool GetBool(const YYJSON value); + public native bool GetBool(); /** - * Get float value by a JSON Handle - * - * @param value JSON handle + * Get float value * * @return float value */ - public static native float GetFloat(const YYJSON value); + public native float GetFloat(); /** - * Get int value by a JSON Handle - * - * @param value JSON handle + * Get int value * * @return int value */ - public static native int GetInt(const YYJSON value); + public native int GetInt(); /** - * Get integer64 value by a JSON Handle - * - * @param value JSON handle - * @param buffer Buffer to copy to - * @param maxlength Maximum size of the buffer - * - * @return True on success, false on failure - */ - public static native bool GetInt64(const YYJSON value, char[] buffer, int maxlength); + * Get integer64 value (auto-detects signed/unsigned) + * + * @param buffer Buffer to copy to + * @param maxlength Maximum size of the buffer + * + * @return True on success, false on failure + */ + public native bool GetInt64(char[] buffer, int maxlength); /** - * Get string value by a JSON Handle + * Get string value * - * @param value JSON handle * @param buffer Buffer to copy to * @param maxlength Maximum size of the buffer * * @return True on success, false on failure */ - public static native bool GetString(const YYJSON value, char[] buffer, int maxlength); + public native bool GetString(char[] buffer, int maxlength); /** * Get JSON Handle serialized size in bytes (including null-terminator) @@ -374,13 +525,13 @@ methodmap YYJSON < Handle * @param flag The JSON write options * * @return Size in bytes (including null terminator) - * + * * @note The returned size depends on the flag parameter. - * You MUST use the same flags when calling both GetSerializedSize() - * and ToString(). Using different flags will return different sizes - * and may cause buffer overflow. + * You MUST use the same flags when calling both GetSerializedSize() + * and ToString(). Using different flags will return different sizes + * and may cause buffer overflow */ - public native int GetSerializedSize(YYJSON_WRITE_FLAG flag = YYJSON_WRITE_NOFLAG); + public native int GetSerializedSize(JSON_WRITE_FLAG flag = JSON_WRITE_NOFLAG); /** * Get value by a JSON Pointer @@ -421,14 +572,14 @@ methodmap YYJSON < Handle public native int PtrGetInt(const char[] path); /** - * Get integer64 value by a JSON Pointer - * - * @param path The JSON pointer string - * @param buffer Buffer to copy to - * @param maxlength Maximum size of the buffer - * - * @return integer64 value referenced by the JSON pointer - */ + * Get integer64 value by a JSON Pointer (auto-detects signed/unsigned) + * + * @param path The JSON pointer string + * @param buffer Buffer to copy to + * @param maxlength Maximum size of the buffer + * + * @return integer64 value referenced by the JSON pointer + */ public native bool PtrGetInt64(const char[] path, char[] buffer, int maxlength); /** @@ -456,7 +607,7 @@ methodmap YYJSON < Handle * * @note For strings: returns string length including null-terminator * @note For arrays/objects: returns number of elements - * @note Returns 0 if value is NULL or type is not string/array/object + * @note Returns 0 if value is null or type is not string/array/object * * @param path The JSON pointer string * @@ -467,19 +618,21 @@ methodmap YYJSON < Handle /** * Set value by a JSON Pointer * - * @note The parent nodes will be created if they do not exist. If the target value already exists, it will be replaced by the new value + * @note The parent nodes will be created if they do not exist. + * If the target value already exists, it will be replaced by the new value * * @param path The JSON pointer string * @param value The value to be set * * @return true if JSON pointer is valid and new value is set, false otherwise */ - public native bool PtrSet(const char[] path, YYJSON value); + public native bool PtrSet(const char[] path, JSON value); /** * Set boolean value by a JSON Pointer * - * @note The parent nodes will be created if they do not exist. If the target value already exists, it will be replaced by the new value + * @note The parent nodes will be created if they do not exist. + * If the target value already exists, it will be replaced by the new value * * @param path The JSON pointer string * @param value The boolean value to be set @@ -491,7 +644,8 @@ methodmap YYJSON < Handle /** * Set float value by a JSON Pointer * - * @note The parent nodes will be created if they do not exist. If the target value already exists, it will be replaced by the new value + * @note The parent nodes will be created if they do not exist. + * If the target value already exists, it will be replaced by the new value * * @param path The JSON pointer string * @param value The float value to be set @@ -503,7 +657,8 @@ methodmap YYJSON < Handle /** * Set integer value by a JSON Pointer * - * @note The parent nodes will be created if they do not exist. If the target value already exists, it will be replaced by the new value + * @note The parent nodes will be created if they do not exist. + * If the target value already exists, it will be replaced by the new value * * @param path The JSON pointer string * @param value The integer value to be set @@ -515,10 +670,12 @@ methodmap YYJSON < Handle /** * Set integer64 value by a JSON Pointer * - * @note The parent nodes will be created if they do not exist. If the target value already exists, it will be replaced by the new value + * @note The parent nodes will be created if they do not exist. + * If the target value already exists, it will be replaced by the new value + * @note This function auto-detects whether the value is signed or unsigned * * @param path The JSON pointer string - * @param value The integer64 value to be set + * @param value The integer64 value to be set (can be signed or unsigned) * * @return true if JSON pointer is valid and new value is set, false otherwise */ @@ -527,7 +684,8 @@ methodmap YYJSON < Handle /** * Set string value by a JSON Pointer * - * @note The parent nodes will be created if they do not exist. If the target value already exists, it will be replaced by the new value + * @note The parent nodes will be created if they do not exist. + * If the target value already exists, it will be replaced by the new value * * @param path The JSON pointer string * @param value The string value to be set @@ -539,7 +697,8 @@ methodmap YYJSON < Handle /** * Set null value by a JSON Pointer * - * @note The parent nodes will be created if they do not exist. If the target value already exists, it will be replaced by the new value + * @note The parent nodes will be created if they do not exist. + * If the target value already exists, it will be replaced by the new value * * @param path The JSON pointer string * @@ -555,7 +714,7 @@ methodmap YYJSON < Handle * * @return true if JSON pointer is valid and new value is set, false otherwise */ - public native bool PtrAdd(const char[] path, YYJSON value); + public native bool PtrAdd(const char[] path, JSON value); /** * Add (insert) boolean value by a JSON pointer @@ -633,7 +792,7 @@ methodmap YYJSON < Handle * * @return true on success, false otherwise */ - public native bool PtrTryGetVal(const char[] path, YYJSON &value); + public native bool PtrTryGetVal(const char[] path, JSON &value); /** * Try to get boolean value by a JSON Pointer @@ -666,14 +825,14 @@ methodmap YYJSON < Handle public native bool PtrTryGetInt(const char[] path, int &value); /** - * Try to get integer64 value by a JSON Pointer - * - * @param path The JSON pointer string - * @param buffer Buffer to store the integer64 value - * @param maxlength Maximum length of the buffer - * - * @return true on success, false otherwise - */ + * Try to get integer64 value by a JSON Pointer (automatically detects signed/unsigned) + * + * @param path The JSON pointer string + * @param buffer Buffer to store the integer64 value + * @param maxlength Maximum length of the buffer + * + * @return true on success, false otherwise + */ public native bool PtrTryGetInt64(const char[] path, char[] buffer, int maxlength); /** @@ -690,14 +849,14 @@ methodmap YYJSON < Handle /** * Retrieves json type */ - property YYJSON_TYPE Type { + property JSON_TYPE Type { public native get(); } /** * Retrieves json subtype */ - property YYJSON_SUBTYPE SubType { + property JSON_SUBTYPE SubType { public native get(); } @@ -812,7 +971,7 @@ methodmap YYJSON < Handle * @return Size in bytes (including null terminator) or 0 if not from parsing * * @note This value only applies to documents created from parsing (Parse, FromString, FromFile) - * @note Manually created documents (new YYJSONObject(), YYJSON.CreateBool(), etc.) will return 0 + * @note Manually created documents (new JSONObject(), JSON.CreateBool(), etc.) will return 0 * @note This value does not auto-update if the document is modified * @note For modified documents, use GetSerializedSize() to obtain the current size */ @@ -821,7 +980,7 @@ methodmap YYJSON < Handle } }; -methodmap YYJSONObject < YYJSON +methodmap JSONObject < JSON { /** * Creates a JSON object A JSON object maps strings (called "keys") to values Keys in a @@ -829,12 +988,12 @@ methodmap YYJSONObject < YYJSON * * @note Needs to be freed using delete or CloseHandle() */ - public native YYJSONObject(); + public native JSONObject(); /** * Creates a new JSON object from an array of strings representing key-value pairs. - * The array must contain an even number of strings, where even indices are keys - * and odd indices are values. Keys cannot be empty strings. + * The array must contain an even number of strings, where even indices are keys + * and odd indices are values. Keys cannot be empty strings * * @param pairs Array of strings containing alternating keys and values * @param size Total size of the array (must be even) @@ -843,7 +1002,7 @@ methodmap YYJSONObject < YYJSON * @error If array size is invalid, any key is empty, or if creation fails * */ - public static native YYJSONObject FromStrings(const char[][] pairs, int size); + public static native JSONObject FromStrings(const char[][] pairs, int size); /** * Loads a JSON object from a file @@ -853,7 +1012,7 @@ methodmap YYJSONObject < YYJSON * * @return Object handle, or null on failure */ - public static native YYJSONObject FromFile(const char[] file, YYJSON_READ_FLAG flag = YYJSON_READ_NOFLAG); + public static native JSONObject FromFile(const char[] file, JSON_READ_FLAG flag = JSON_READ_NOFLAG); /** * Loads a JSON object from a string @@ -863,7 +1022,7 @@ methodmap YYJSONObject < YYJSON * * @return Object handle, or null on failure */ - public static native YYJSONObject FromString(const char[] buffer, YYJSON_READ_FLAG flag = YYJSON_READ_NOFLAG); + public static native JSONObject FromString(const char[] buffer, JSON_READ_FLAG flag = JSON_READ_NOFLAG); /** * Gets a value from the object @@ -884,7 +1043,7 @@ methodmap YYJSONObject < YYJSON * * @return True if succeed, false otherwise */ - public native bool Set(const char[] key, const YYJSON value); + public native bool Set(const char[] key, const JSON value); /** * Gets a boolean value from the object @@ -914,14 +1073,14 @@ methodmap YYJSONObject < YYJSON public native int GetInt(const char[] key); /** - * Retrieves a 64-bit integer value from the object - * - * @param key Key string - * @param buffer String buffer to store value - * @param maxlength Maximum length of the string buffer - * - * @return True on success, false if the key was not found - */ + * Retrieves a 64-bit integer value from the object (automatically detects signed/unsigned) + * + * @param key Key string + * @param buffer String buffer to store value + * @param maxlength Maximum length of the string buffer + * + * @return True on success, false if the key was not found + */ public native bool GetInt64(const char[] key, char[] buffer, int maxlength); /** @@ -1024,8 +1183,10 @@ methodmap YYJSONObject < YYJSON /** * Sets a 64-bit integer value in the object, either inserting a new entry or replacing an old one * + * @note This function automatically detects whether the value is signed or unsigned + * * @param key Key string - * @param value Value to store at this key + * @param value Value to store at this key (can be signed or unsigned) * * @return True on success, false on failure */ @@ -1074,12 +1235,12 @@ methodmap YYJSONObject < YYJSON * @note This function performs a lexicographical sort on the object's keys * - The values maintain their association with their respective keys * - * @param order Sort order, see YYJSON_SORT_ORDER enums + * @param order Sort order, see JSON_SORT_ORDER enums * * @return True on success, false on failure * @error Invalid handle or handle is not an object */ - public native bool Sort(YYJSON_SORT_ORDER order = YYJSON_SORT_ASC); + public native bool Sort(JSON_SORT_ORDER order = JSON_SORT_ASC); /** * Retrieves the size of the object @@ -1089,23 +1250,60 @@ methodmap YYJSONObject < YYJSON } }; -methodmap YYJSONArray < YYJSON +methodmap JSONArray < JSON { /** * Creates a JSON array * * @note Needs to be freed using delete or CloseHandle() */ - public native YYJSONArray(); + public native JSONArray(); + + /** + * Creates a new JSON array from an array of strings + * + * @param strings Array of strings to create array from + * @param size Size of the array + * @return New JSON array handle, or null if creation failed + */ + public static native JSONArray FromStrings(const char[][] strings, int size); /** - * Creates a new JSON array from an array of strings. + * Creates a new JSON array from an array of integer values * - * @param strings Array of strings to create array from. - * @param size Size of the array. - * @return New JSON array handle, or null if creation failed. + * @param values Array of int values to create array from + * @param size Size of the array + * @return New JSON array handle, or null if creation failed */ - public static native YYJSONArray FromStrings(const char[][] strings, int size); + public static native JSONArray FromInt(const int[] values, int size); + + /** + * Creates a new JSON array from an array of 64-bit integer strings (auto-detects signed/unsigned) + * + * @param values Array of int64 string values (can be signed or unsigned) + * @param size Size of the array + * @return New JSON array handle, or null if creation failed + * @error If any value cannot be parsed as int64 + */ + public static native JSONArray FromInt64(const char[][] values, int size); + + /** + * Creates a new JSON array from an array of boolean values + * + * @param values Array of bool values to create array from + * @param size Size of the array + * @return New JSON array handle, or null if creation failed + */ + public static native JSONArray FromBool(const bool[] values, int size); + + /** + * Creates a new JSON array from an array of float values + * + * @param values Array of float values to create array from + * @param size Size of the array + * @return New JSON array handle, or null if creation failed + */ + public static native JSONArray FromFloat(const float[] values, int size); /** * Loads a JSON array from a file @@ -1114,7 +1312,7 @@ methodmap YYJSONArray < YYJSON * @param flag Read flag * @return Array handle, or null on failure */ - public static native YYJSONArray FromFile(const char[] file, YYJSON_READ_FLAG flag = YYJSON_READ_NOFLAG); + public static native JSONArray FromFile(const char[] file, JSON_READ_FLAG flag = JSON_READ_NOFLAG); /** * Loads a JSON array from a string @@ -1123,7 +1321,7 @@ methodmap YYJSONArray < YYJSON * @param flag Read flag * @return Array handle, or null on failure */ - public static native YYJSONArray FromString(const char[] buffer, YYJSON_READ_FLAG flag = YYJSON_READ_NOFLAG); + public static native JSONArray FromString(const char[] buffer, JSON_READ_FLAG flag = JSON_READ_NOFLAG); /** * Gets a value from the array @@ -1164,14 +1362,14 @@ methodmap YYJSONArray < YYJSON public native int GetInt(int index); /** - * Gets a 64-bit integer from the array - * - * @param index Position in the array (starting from 0) - * @param buffer Buffer to copy to - * @param maxlength Maximum size of the buffer - * - * @return 64-bit integer - */ + * Gets a 64-bit integer from the array (automatically detects signed/unsigned) + * + * @param index Position in the array (starting from 0) + * @param buffer Buffer to copy to + * @param maxlength Maximum size of the buffer + * + * @return 64-bit integer + */ public native void GetInt64(int index, char[] buffer, int maxlength); /** @@ -1204,7 +1402,7 @@ methodmap YYJSONArray < YYJSON * * @return True if succeed, false otherwise */ - public native bool Set(int index, const YYJSON value); + public native bool Set(int index, const JSON value); /** * Replaces a boolean value at index @@ -1239,8 +1437,10 @@ methodmap YYJSONArray < YYJSON /** * Replaces an integer64 value at index * + * @note This function automatically detects whether the value is signed or unsigned + * * @param index The index to which to replace the value - * @param value The new integer64 value to replace + * @param value The new integer64 value to replace (can be signed or unsigned) * * @return True if succeed, false otherwise */ @@ -1270,16 +1470,16 @@ methodmap YYJSONArray < YYJSON * * @param value JSON handle to set * - * @return The value to be inserted. Returns false if it is NULL + * @return The value to be inserted. Returns false if it is null */ - public native bool Push(const YYJSON value); + public native bool Push(const JSON value); /** * Inserts a boolean value at the end of the array * * @param value Boolean value to set * - * @return The value to be inserted. Returns false if it is NULL + * @return The value to be inserted. Returns false if it is null */ public native bool PushBool(bool value); @@ -1288,7 +1488,7 @@ methodmap YYJSONArray < YYJSON * * @param value float to set * - * @return The value to be inserted. Returns false if it is NULL + * @return The value to be inserted. Returns false if it is null */ public native bool PushFloat(float value); @@ -1297,7 +1497,7 @@ methodmap YYJSONArray < YYJSON * * @param value integer to set * - * @return The value to be inserted. Returns false if it is NULL + * @return The value to be inserted. Returns false if it is null */ public native bool PushInt(int value); @@ -1306,7 +1506,7 @@ methodmap YYJSONArray < YYJSON * * @param value integer64 value * - * @return The value to be inserted. Returns false if it is NULL + * @return The value to be inserted. Returns false if it is null */ public native bool PushInt64(const char[] value); @@ -1315,17 +1515,147 @@ methodmap YYJSONArray < YYJSON * * @param value String to copy * - * @return The value to be inserted. Returns false if it is NULL + * @return The value to be inserted. Returns false if it is null */ public native bool PushString(const char[] value); /** * Inserts a null value at the end of the array * - * @return The value to be inserted. Returns false if it is NULL + * @return The value to be inserted. Returns false if it is null */ public native bool PushNull(); + /** + * Inserts a JSON value at specific index + * + * @param index Position to insert (0 to size, size means append) + * @param value JSON handle to insert + * + * @return True if succeed, false otherwise + */ + public native bool Insert(int index, const JSON value); + + /** + * Inserts a boolean value at specific index + * + * @param index Position to insert + * @param value Boolean value + * + * @return True if succeed, false otherwise + */ + public native bool InsertBool(int index, bool value); + + /** + * Inserts an integer value at specific index + * + * @param index Position to insert + * @param value Integer value + * + * @return True if succeed, false otherwise + */ + public native bool InsertInt(int index, int value); + + /** + * Inserts an integer64 value at specific index + * + * @param index Position to insert + * @param value Integer64 value (as string) + * + * @return True if succeed, false otherwise + */ + public native bool InsertInt64(int index, const char[] value); + + /** + * Inserts a float value at specific index + * + * @param index Position to insert + * @param value Float value + * + * @return True if succeed, false otherwise + */ + public native bool InsertFloat(int index, float value); + + /** + * Inserts a string value at specific index + * + * @param index Position to insert + * @param value String value + * + * @return True if succeed, false otherwise + */ + public native bool InsertString(int index, const char[] value); + + /** + * Inserts a null value at specific index + * + * @param index Position to insert + * + * @return True if succeed, false otherwise + */ + public native bool InsertNull(int index); + + /** + * Prepends a JSON value to the beginning of the array + * + * @param value JSON handle to prepend + * + * @return True if succeed, false otherwise + */ + public native bool Prepend(const JSON value); + + /** + * Prepends a boolean value to the beginning of the array + * + * @param value Boolean value + * + * @return True if succeed, false otherwise + */ + public native bool PrependBool(bool value); + + /** + * Prepends an integer value to the beginning of the array + * + * @param value Integer value + * + * @return True if succeed, false otherwise + */ + public native bool PrependInt(int value); + + /** + * Prepends an integer64 value to the beginning of the array + * + * @param value Integer64 value (as string) + * + * @return True if succeed, false otherwise + */ + public native bool PrependInt64(const char[] value); + + /** + * Prepends a float value to the beginning of the array + * + * @param value Float value + * + * @return True if succeed, false otherwise + */ + public native bool PrependFloat(float value); + + /** + * Prepends a string value to the beginning of the array + * + * @param value String value + * + * @return True if succeed, false otherwise + */ + public native bool PrependString(const char[] value); + + /** + * Prepends a null value to the beginning of the array + * + * @return True if succeed, false otherwise + */ + public native bool PrependNull(); + /** * Removes an element from the array * @@ -1431,12 +1761,12 @@ methodmap YYJSONArray < YYJSON * - Booleans are sorted with false before true * - Other types (null, object, array) are sorted by type only * - * @param order Sort order, see YYJSON_SORT_ORDER enums + * @param order Sort order, see JSON_SORT_ORDER enums * * @return True on success, false on failure * @error Invalid handle or handle is not an array */ - public native bool Sort(YYJSON_SORT_ORDER order = YYJSON_SORT_ASC); + public native bool Sort(JSON_SORT_ORDER order = JSON_SORT_ASC); /** * Retrieves the size of the array @@ -1448,27 +1778,171 @@ methodmap YYJSONArray < YYJSON /** * @note This function takes a linear search time * Returns the first element of this array - * Returns NULL if arr is NULL/empty or type is not array + * Returns null if arr is null/empty or type is not array * Needs to be freed using delete or CloseHandle() */ - property YYJSON First { + property JSON First { public native get(); } /** * @note This function takes a linear search time * Returns the last element of this array - * Returns NULL if arr is NULL/empty or type is not array + * Returns null if arr is null/empty or type is not array * Needs to be freed using delete or CloseHandle() */ - property YYJSON Last { + property JSON Last { + public native get(); + } +}; + +methodmap JSONArrIter < Handle +{ + /** + * Creates an array iterator from a JSON array value + * + * @note Needs to be freed using delete or CloseHandle() + * + * @param array JSON array value to iterate + * + * @return Array iterator handle, or null on failure + * @error Invalid handle or handle is not an array + */ + public native JSONArrIter(JSON array); + + /** + * Moves the iterator to the next element + * + * @return JSON value handle for the next element, or null if iteration is complete + * @error Invalid iterator handle + */ + property JSON Next { + public native get(); + } + + /** + * Checks if there are more elements to iterate + * + * @return True if there are more elements, false otherwise + * @error Invalid iterator handle + */ + property bool HasNext { + public native get(); + } + + /** + * Gets the current index in the array iteration + * + * @return Current index (0-based), or -1 if iterator is not positioned + * @error Invalid iterator handle + */ + property int Index { public native get(); } + + /** + * Removes the current element from the array (mutable iterators only) + * + * @note This function can only be called on mutable iterators + * @note After calling Remove(), the iterator should not be used to continue iteration + * + * @return True on success, false on failure + * @error Invalid iterator handle or iterator is not mutable + */ + public native bool Remove(); +}; + +methodmap JSONObjIter < Handle +{ + /** + * Creates an object iterator from a JSON object value + * + * @note Needs to be freed using delete or CloseHandle() + * + * @param obj JSON object value to iterate + * + * @return Object iterator handle, or null on failure + * @error Invalid handle or handle is not an object + */ + public native JSONObjIter(JSON obj); + + /** + * Moves the iterator to the next key + * + * @param buffer Buffer to copy key name to + * @param maxlength Maximum length of the string buffer + * + * @return True if there are more elements, false when iteration is complete + * @error Invalid iterator handle + */ + public native bool Next(char[] buffer, int maxlength); + + /** + * Checks if there are more elements to iterate + * + * @return True if there are more elements, false otherwise + * @error Invalid iterator handle + */ + property bool HasNext { + public native get(); + } + + /** + * Gets the value associated with the current key + * + * @note Needs to be freed using delete or CloseHandle() + * @note This should be called after Next() to get the value for the current key + * + * @return JSON value handle for the current key's value, or null on failure + * @error Invalid iterator handle + */ + property JSON Value { + public native get(); + } + + /** + * Iterates to a specified key and returns the value + * + * @note Needs to be freed using delete or CloseHandle() + * @note This function does the same thing as JSONObject.Get(), but is much faster + * if the ordering of the keys is known at compile-time and you are using the same + * order to look up the values + * @note If the key exists in this object, then the iterator will stop at the next key, + * otherwise the iterator will not change and null is returned + * @note This function takes a linear search time if the key is not nearby + * + * @param key Key name to search for (should be a UTF-8 string with null-terminator) + * + * @return JSON value handle for the key's value, or null if key not found or input is invalid + * @error Invalid iterator handle + */ + public native JSON Get(const char[] key); + + /** + * Gets the current index in the object iteration + * + * @return Current index (0-based), or -1 if iterator is not positioned + * @error Invalid iterator handle + */ + property int Index { + public native get(); + } + + /** + * Removes the current key-value pair from the object (mutable iterators only) + * + * @note This function can only be called on mutable iterators + * @note After calling Remove(), the iterator should not be used to continue iteration + * + * @return True on success, false on failure + * @error Invalid iterator handle or iterator is not mutable + */ + public native bool Remove(); }; -public Extension __ext_yyjson = { - name = "yyjson", - file = "yyjson.ext", +public Extension __ext_json = { + name = "json", + file = "json.ext", #if defined AUTOLOAD_EXTENSIONS autoload = 1, #else @@ -1482,153 +1956,198 @@ public Extension __ext_yyjson = { }; #if !defined REQUIRE_EXTENSIONS -public void __pl_yyjson_SetNTVOptional() +public void __pl_json_SetNTVOptional() { // JSONObject - MarkNativeAsOptional("YYJSONObject.YYJSONObject"); - MarkNativeAsOptional("YYJSONObject.FromStrings"); - MarkNativeAsOptional("YYJSONObject.Size.get"); - MarkNativeAsOptional("YYJSONObject.Get"); - MarkNativeAsOptional("YYJSONObject.GetBool"); - MarkNativeAsOptional("YYJSONObject.GetFloat"); - MarkNativeAsOptional("YYJSONObject.GetInt"); - MarkNativeAsOptional("YYJSONObject.GetInt64"); - MarkNativeAsOptional("YYJSONObject.GetString"); - MarkNativeAsOptional("YYJSONObject.IsNull"); - MarkNativeAsOptional("YYJSONObject.GetKey"); - MarkNativeAsOptional("YYJSONObject.GetValueAt"); - MarkNativeAsOptional("YYJSONObject.HasKey"); - MarkNativeAsOptional("YYJSONObject.RenameKey"); - MarkNativeAsOptional("YYJSONObject.Set"); - MarkNativeAsOptional("YYJSONObject.SetBool"); - MarkNativeAsOptional("YYJSONObject.SetFloat"); - MarkNativeAsOptional("YYJSONObject.SetInt"); - MarkNativeAsOptional("YYJSONObject.SetInt64"); - MarkNativeAsOptional("YYJSONObject.SetNull"); - MarkNativeAsOptional("YYJSONObject.SetString"); - MarkNativeAsOptional("YYJSONObject.Remove"); - MarkNativeAsOptional("YYJSONObject.Clear"); - MarkNativeAsOptional("YYJSONObject.FromString"); - MarkNativeAsOptional("YYJSONObject.FromFile"); - MarkNativeAsOptional("YYJSONObject.Sort"); + MarkNativeAsOptional("JSONObject.JSONObject"); + MarkNativeAsOptional("JSONObject.FromStrings"); + MarkNativeAsOptional("JSONObject.Size.get"); + MarkNativeAsOptional("JSONObject.Get"); + MarkNativeAsOptional("JSONObject.GetBool"); + MarkNativeAsOptional("JSONObject.GetFloat"); + MarkNativeAsOptional("JSONObject.GetInt"); + MarkNativeAsOptional("JSONObject.GetInt64"); + MarkNativeAsOptional("JSONObject.GetString"); + MarkNativeAsOptional("JSONObject.IsNull"); + MarkNativeAsOptional("JSONObject.GetKey"); + MarkNativeAsOptional("JSONObject.GetValueAt"); + MarkNativeAsOptional("JSONObject.HasKey"); + MarkNativeAsOptional("JSONObject.RenameKey"); + MarkNativeAsOptional("JSONObject.Set"); + MarkNativeAsOptional("JSONObject.SetBool"); + MarkNativeAsOptional("JSONObject.SetFloat"); + MarkNativeAsOptional("JSONObject.SetInt"); + MarkNativeAsOptional("JSONObject.SetInt64"); + MarkNativeAsOptional("JSONObject.SetNull"); + MarkNativeAsOptional("JSONObject.SetString"); + MarkNativeAsOptional("JSONObject.Remove"); + MarkNativeAsOptional("JSONObject.Clear"); + MarkNativeAsOptional("JSONObject.FromString"); + MarkNativeAsOptional("JSONObject.FromFile"); + MarkNativeAsOptional("JSONObject.Sort"); // JSONArray - MarkNativeAsOptional("YYJSONArray.YYJSONArray"); - MarkNativeAsOptional("YYJSONArray.FromStrings"); - MarkNativeAsOptional("YYJSONArray.Length.get"); - MarkNativeAsOptional("YYJSONArray.Get"); - MarkNativeAsOptional("YYJSONArray.First.get"); - MarkNativeAsOptional("YYJSONArray.Last.get"); - MarkNativeAsOptional("YYJSONArray.GetBool"); - MarkNativeAsOptional("YYJSONArray.GetFloat"); - MarkNativeAsOptional("YYJSONArray.GetInt"); - MarkNativeAsOptional("YYJSONArray.GetInt64"); - MarkNativeAsOptional("YYJSONArray.GetString"); - MarkNativeAsOptional("YYJSONArray.IsNull"); - MarkNativeAsOptional("YYJSONArray.Set"); - MarkNativeAsOptional("YYJSONArray.SetBool"); - MarkNativeAsOptional("YYJSONArray.SetFloat"); - MarkNativeAsOptional("YYJSONArray.SetInt"); - MarkNativeAsOptional("YYJSONArray.SetInt64"); - MarkNativeAsOptional("YYJSONArray.SetNull"); - MarkNativeAsOptional("YYJSONArray.SetString"); - MarkNativeAsOptional("YYJSONArray.Push"); - MarkNativeAsOptional("YYJSONArray.PushBool"); - MarkNativeAsOptional("YYJSONArray.PushFloat"); - MarkNativeAsOptional("YYJSONArray.PushInt"); - MarkNativeAsOptional("YYJSONArray.PushInt64"); - MarkNativeAsOptional("YYJSONArray.PushNull"); - MarkNativeAsOptional("YYJSONArray.PushString"); - MarkNativeAsOptional("YYJSONArray.Remove"); - MarkNativeAsOptional("YYJSONArray.RemoveFirst"); - MarkNativeAsOptional("YYJSONArray.RemoveLast"); - MarkNativeAsOptional("YYJSONArray.RemoveRange"); - MarkNativeAsOptional("YYJSONArray.Clear"); - MarkNativeAsOptional("YYJSONArray.FromString"); - MarkNativeAsOptional("YYJSONArray.FromFile"); - MarkNativeAsOptional("YYJSONArray.IndexOfBool"); - MarkNativeAsOptional("YYJSONArray.IndexOfString"); - MarkNativeAsOptional("YYJSONArray.IndexOfInt"); - MarkNativeAsOptional("YYJSONArray.IndexOfInt64"); - MarkNativeAsOptional("YYJSONArray.IndexOfFloat"); - MarkNativeAsOptional("YYJSONArray.Sort"); + MarkNativeAsOptional("JSONArray.JSONArray"); + MarkNativeAsOptional("JSONArray.FromStrings"); + MarkNativeAsOptional("JSONArray.FromInt"); + MarkNativeAsOptional("JSONArray.FromInt64"); + MarkNativeAsOptional("JSONArray.FromBool"); + MarkNativeAsOptional("JSONArray.FromFloat"); + MarkNativeAsOptional("JSONArray.Length.get"); + MarkNativeAsOptional("JSONArray.Get"); + MarkNativeAsOptional("JSONArray.First.get"); + MarkNativeAsOptional("JSONArray.Last.get"); + MarkNativeAsOptional("JSONArray.GetBool"); + MarkNativeAsOptional("JSONArray.GetFloat"); + MarkNativeAsOptional("JSONArray.GetInt"); + MarkNativeAsOptional("JSONArray.GetInt64"); + MarkNativeAsOptional("JSONArray.GetString"); + MarkNativeAsOptional("JSONArray.IsNull"); + MarkNativeAsOptional("JSONArray.Set"); + MarkNativeAsOptional("JSONArray.SetBool"); + MarkNativeAsOptional("JSONArray.SetFloat"); + MarkNativeAsOptional("JSONArray.SetInt"); + MarkNativeAsOptional("JSONArray.SetInt64"); + MarkNativeAsOptional("JSONArray.SetNull"); + MarkNativeAsOptional("JSONArray.SetString"); + MarkNativeAsOptional("JSONArray.Push"); + MarkNativeAsOptional("JSONArray.PushBool"); + MarkNativeAsOptional("JSONArray.PushFloat"); + MarkNativeAsOptional("JSONArray.PushInt"); + MarkNativeAsOptional("JSONArray.PushInt64"); + MarkNativeAsOptional("JSONArray.PushNull"); + MarkNativeAsOptional("JSONArray.PushString"); + MarkNativeAsOptional("JSONArray.Insert"); + MarkNativeAsOptional("JSONArray.InsertBool"); + MarkNativeAsOptional("JSONArray.InsertInt"); + MarkNativeAsOptional("JSONArray.InsertInt64"); + MarkNativeAsOptional("JSONArray.InsertFloat"); + MarkNativeAsOptional("JSONArray.InsertString"); + MarkNativeAsOptional("JSONArray.InsertNull"); + MarkNativeAsOptional("JSONArray.Prepend"); + MarkNativeAsOptional("JSONArray.PrependBool"); + MarkNativeAsOptional("JSONArray.PrependInt"); + MarkNativeAsOptional("JSONArray.PrependInt64"); + MarkNativeAsOptional("JSONArray.PrependFloat"); + MarkNativeAsOptional("JSONArray.PrependString"); + MarkNativeAsOptional("JSONArray.PrependNull"); + MarkNativeAsOptional("JSONArray.Remove"); + MarkNativeAsOptional("JSONArray.RemoveFirst"); + MarkNativeAsOptional("JSONArray.RemoveLast"); + MarkNativeAsOptional("JSONArray.RemoveRange"); + MarkNativeAsOptional("JSONArray.Clear"); + MarkNativeAsOptional("JSONArray.FromString"); + MarkNativeAsOptional("JSONArray.FromFile"); + MarkNativeAsOptional("JSONArray.IndexOfBool"); + MarkNativeAsOptional("JSONArray.IndexOfString"); + MarkNativeAsOptional("JSONArray.IndexOfInt"); + MarkNativeAsOptional("JSONArray.IndexOfInt64"); + MarkNativeAsOptional("JSONArray.IndexOfFloat"); + MarkNativeAsOptional("JSONArray.Sort"); // JSON - MarkNativeAsOptional("YYJSON.ToString"); - MarkNativeAsOptional("YYJSON.ToFile"); - MarkNativeAsOptional("YYJSON.Parse"); - MarkNativeAsOptional("YYJSON.Equals"); - MarkNativeAsOptional("YYJSON.DeepCopy"); - MarkNativeAsOptional("YYJSON.GetTypeDesc"); - MarkNativeAsOptional("YYJSON.GetSerializedSize"); - MarkNativeAsOptional("YYJSON.ReadSize.get"); - MarkNativeAsOptional("YYJSON.Type.get"); - MarkNativeAsOptional("YYJSON.SubType.get"); - MarkNativeAsOptional("YYJSON.IsArray.get"); - MarkNativeAsOptional("YYJSON.IsObject.get"); - MarkNativeAsOptional("YYJSON.IsInt.get"); - MarkNativeAsOptional("YYJSON.IsUint.get"); - MarkNativeAsOptional("YYJSON.IsSint.get"); - MarkNativeAsOptional("YYJSON.IsNum.get"); - MarkNativeAsOptional("YYJSON.IsBool.get"); - MarkNativeAsOptional("YYJSON.IsTrue.get"); - MarkNativeAsOptional("YYJSON.IsFalse.get"); - MarkNativeAsOptional("YYJSON.IsFloat.get"); - MarkNativeAsOptional("YYJSON.IsStr.get"); - MarkNativeAsOptional("YYJSON.IsNull.get"); - MarkNativeAsOptional("YYJSON.IsCtn.get"); - MarkNativeAsOptional("YYJSON.IsMutable.get"); - MarkNativeAsOptional("YYJSON.IsImmutable.get"); - MarkNativeAsOptional("YYJSON.ForeachObject"); - MarkNativeAsOptional("YYJSON.ForeachArray"); - MarkNativeAsOptional("YYJSON.ForeachKey"); - MarkNativeAsOptional("YYJSON.ForeachIndex"); - MarkNativeAsOptional("YYJSON.ToMutable"); - MarkNativeAsOptional("YYJSON.ToImmutable"); + MarkNativeAsOptional("JSON.ToString"); + MarkNativeAsOptional("JSON.ToFile"); + MarkNativeAsOptional("JSON.Parse"); + MarkNativeAsOptional("JSON.Equals"); + MarkNativeAsOptional("JSON.EqualsStr"); + MarkNativeAsOptional("JSON.DeepCopy"); + MarkNativeAsOptional("JSON.GetTypeDesc"); + MarkNativeAsOptional("JSON.GetSerializedSize"); + MarkNativeAsOptional("JSON.ReadSize.get"); + MarkNativeAsOptional("JSON.Type.get"); + MarkNativeAsOptional("JSON.SubType.get"); + MarkNativeAsOptional("JSON.IsArray.get"); + MarkNativeAsOptional("JSON.IsObject.get"); + MarkNativeAsOptional("JSON.IsInt.get"); + MarkNativeAsOptional("JSON.IsUint.get"); + MarkNativeAsOptional("JSON.IsSint.get"); + MarkNativeAsOptional("JSON.IsNum.get"); + MarkNativeAsOptional("JSON.IsBool.get"); + MarkNativeAsOptional("JSON.IsTrue.get"); + MarkNativeAsOptional("JSON.IsFalse.get"); + MarkNativeAsOptional("JSON.IsFloat.get"); + MarkNativeAsOptional("JSON.IsStr.get"); + MarkNativeAsOptional("JSON.IsNull.get"); + MarkNativeAsOptional("JSON.IsCtn.get"); + MarkNativeAsOptional("JSON.IsMutable.get"); + MarkNativeAsOptional("JSON.IsImmutable.get"); + MarkNativeAsOptional("JSON.ForeachObject"); + MarkNativeAsOptional("JSON.ForeachArray"); + MarkNativeAsOptional("JSON.ForeachKey"); + MarkNativeAsOptional("JSON.ForeachIndex"); + MarkNativeAsOptional("JSON.ToMutable"); + MarkNativeAsOptional("JSON.ToImmutable"); // JSON CREATE & GET - MarkNativeAsOptional("YYJSON.Pack"); - MarkNativeAsOptional("YYJSON.CreateBool"); - MarkNativeAsOptional("YYJSON.CreateFloat"); - MarkNativeAsOptional("YYJSON.CreateInt"); - MarkNativeAsOptional("YYJSON.CreateInt64"); - MarkNativeAsOptional("YYJSON.CreateNull"); - MarkNativeAsOptional("YYJSON.CreateString"); - MarkNativeAsOptional("YYJSON.GetBool"); - MarkNativeAsOptional("YYJSON.GetFloat"); - MarkNativeAsOptional("YYJSON.GetInt"); - MarkNativeAsOptional("YYJSON.GetInt64"); - MarkNativeAsOptional("YYJSON.GetString"); + MarkNativeAsOptional("JSON.Pack"); + MarkNativeAsOptional("JSON.CreateBool"); + MarkNativeAsOptional("JSON.CreateFloat"); + MarkNativeAsOptional("JSON.CreateInt"); + MarkNativeAsOptional("JSON.CreateInt64"); + MarkNativeAsOptional("JSON.CreateNull"); + MarkNativeAsOptional("JSON.CreateString"); + MarkNativeAsOptional("JSON.GetBool"); + MarkNativeAsOptional("JSON.GetFloat"); + MarkNativeAsOptional("JSON.GetInt"); + MarkNativeAsOptional("JSON.GetInt64"); + MarkNativeAsOptional("JSON.GetString"); + MarkNativeAsOptional("JSON.ReadNumber"); + MarkNativeAsOptional("JSON.WriteNumber"); + MarkNativeAsOptional("JSON.SetFpToFloat"); + MarkNativeAsOptional("JSON.SetFpToFixed"); + MarkNativeAsOptional("JSON.SetBool"); + MarkNativeAsOptional("JSON.SetInt"); + MarkNativeAsOptional("JSON.SetInt64"); + MarkNativeAsOptional("JSON.SetFloat"); + MarkNativeAsOptional("JSON.SetString"); + MarkNativeAsOptional("JSON.SetNull"); // JSON POINTER - MarkNativeAsOptional("YYJSON.PtrGet"); - MarkNativeAsOptional("YYJSON.PtrGetBool"); - MarkNativeAsOptional("YYJSON.PtrGetFloat"); - MarkNativeAsOptional("YYJSON.PtrGetInt"); - MarkNativeAsOptional("YYJSON.PtrGetInt64"); - MarkNativeAsOptional("YYJSON.PtrGetString"); - MarkNativeAsOptional("YYJSON.PtrGetIsNull"); - MarkNativeAsOptional("YYJSON.PtrGetLength"); - MarkNativeAsOptional("YYJSON.PtrSet"); - MarkNativeAsOptional("YYJSON.PtrSetBool"); - MarkNativeAsOptional("YYJSON.PtrSetFloat"); - MarkNativeAsOptional("YYJSON.PtrSetInt"); - MarkNativeAsOptional("YYJSON.PtrSetInt64"); - MarkNativeAsOptional("YYJSON.PtrSetString"); - MarkNativeAsOptional("YYJSON.PtrSetNull"); - MarkNativeAsOptional("YYJSON.PtrAdd"); - MarkNativeAsOptional("YYJSON.PtrAddBool"); - MarkNativeAsOptional("YYJSON.PtrAddFloat"); - MarkNativeAsOptional("YYJSON.PtrAddInt"); - MarkNativeAsOptional("YYJSON.PtrAddInt64"); - MarkNativeAsOptional("YYJSON.PtrAddString"); - MarkNativeAsOptional("YYJSON.PtrAddNull"); - MarkNativeAsOptional("YYJSON.PtrRemove"); - MarkNativeAsOptional("YYJSON.PtrTryGetVal"); - MarkNativeAsOptional("YYJSON.PtrTryGetBool"); - MarkNativeAsOptional("YYJSON.PtrTryGetFloat"); - MarkNativeAsOptional("YYJSON.PtrTryGetInt"); - MarkNativeAsOptional("YYJSON.PtrTryGetInt64"); - MarkNativeAsOptional("YYJSON.PtrTryGetString"); + MarkNativeAsOptional("JSON.PtrGet"); + MarkNativeAsOptional("JSON.PtrGetBool"); + MarkNativeAsOptional("JSON.PtrGetFloat"); + MarkNativeAsOptional("JSON.PtrGetInt"); + MarkNativeAsOptional("JSON.PtrGetInt64"); + MarkNativeAsOptional("JSON.PtrGetString"); + MarkNativeAsOptional("JSON.PtrGetIsNull"); + MarkNativeAsOptional("JSON.PtrGetLength"); + MarkNativeAsOptional("JSON.PtrSet"); + MarkNativeAsOptional("JSON.PtrSetBool"); + MarkNativeAsOptional("JSON.PtrSetFloat"); + MarkNativeAsOptional("JSON.PtrSetInt"); + MarkNativeAsOptional("JSON.PtrSetInt64"); + MarkNativeAsOptional("JSON.PtrSetString"); + MarkNativeAsOptional("JSON.PtrSetNull"); + MarkNativeAsOptional("JSON.PtrAdd"); + MarkNativeAsOptional("JSON.PtrAddBool"); + MarkNativeAsOptional("JSON.PtrAddFloat"); + MarkNativeAsOptional("JSON.PtrAddInt"); + MarkNativeAsOptional("JSON.PtrAddInt64"); + MarkNativeAsOptional("JSON.PtrAddString"); + MarkNativeAsOptional("JSON.PtrAddNull"); + MarkNativeAsOptional("JSON.PtrRemove"); + MarkNativeAsOptional("JSON.PtrTryGetVal"); + MarkNativeAsOptional("JSON.PtrTryGetBool"); + MarkNativeAsOptional("JSON.PtrTryGetFloat"); + MarkNativeAsOptional("JSON.PtrTryGetInt"); + MarkNativeAsOptional("JSON.PtrTryGetInt64"); + MarkNativeAsOptional("JSON.PtrTryGetString"); + + // JSONArrIter + MarkNativeAsOptional("JSONArrIter.JSONArrIter"); + MarkNativeAsOptional("JSONArrIter.Next.get"); + MarkNativeAsOptional("JSONArrIter.HasNext.get"); + MarkNativeAsOptional("JSONArrIter.Index.get"); + MarkNativeAsOptional("JSONArrIter.Remove"); + + // JSONObjIter + MarkNativeAsOptional("JSONObjIter.JSONObjIter"); + MarkNativeAsOptional("JSONObjIter.Next"); + MarkNativeAsOptional("JSONObjIter.HasNext"); + MarkNativeAsOptional("JSONObjIter.Value.get"); + MarkNativeAsOptional("JSONObjIter.Get"); + MarkNativeAsOptional("JSONObjIter.Index.get"); + MarkNativeAsOptional("JSONObjIter.Remove"); } #endif \ No newline at end of file diff --git a/scripting/yyjson_perf_test.sp b/scripting/json_perf_test.sp old mode 100644 new mode 100755 similarity index 82% rename from scripting/yyjson_perf_test.sp rename to scripting/json_perf_test.sp index 9a10f39..ac701a3 --- a/scripting/yyjson_perf_test.sp +++ b/scripting/json_perf_test.sp @@ -1,6 +1,6 @@ #include #include -#include +#include #pragma dynamic 531072 #define TEST_ITERATIONS 100 @@ -8,23 +8,23 @@ Profiler g_hProfiler; public Plugin myinfo = { - name = "YYJSON Benchmark", + name = "JSON Benchmark", author = "ProjectSky", - description = "Performance benchmark for YYJSON", + description = "Performance benchmark for JSON", version = "1.0.0", url = "https://github.com/ProjectSky/sm-ext-yyjson" }; public void OnPluginStart() { - RegServerCmd("sm_yyjson_benchmark", Command_Benchmark, "Run YYJSON performance benchmark"); + RegServerCmd("sm_json_benchmark", Command_Benchmark, "Run JSON performance benchmark"); g_hProfiler = new Profiler(); } Action Command_Benchmark(int args) { - YYJSON json = YYJSON.Parse("twitter.json", true); + JSON json = JSON.Parse("twitter.json", true); int dataLength = json.ReadSize; @@ -36,7 +36,7 @@ Action Command_Benchmark(int args) g_hProfiler.Start(); for (int i = 0; i < TEST_ITERATIONS; i++) { - YYJSON testJson = YYJSON.Parse(jsonStr); + JSON testJson = JSON.Parse(jsonStr); delete testJson; } g_hProfiler.Stop(); @@ -53,7 +53,7 @@ Action Command_Benchmark(int args) float parseTimePerOp = parseTime * 1000.0 / TEST_ITERATIONS; float stringifyTimePerOp = stringifyTime * 1000.0 / TEST_ITERATIONS; - PrintToServer("=== YYJSON Performance Benchmark ==="); + PrintToServer("=== JSON Performance Benchmark ==="); PrintToServer("Test iterations: %d", TEST_ITERATIONS); PrintToServer("Data size: %.2f MB", float(dataLength) / (1024.0 * 1024.0)); PrintToServer("Parse time: %.3f seconds", parseTime); @@ -66,7 +66,7 @@ Action Command_Benchmark(int args) PrintToServer("Parse speed: %.2f MB/s (%.2f GB/s)", parseSpeed, parseSpeed / 1024.0); PrintToServer("Stringify speed: %.2f MB/s (%.2f GB/s)", stringifySpeed, stringifySpeed / 1024.0); PrintToServer("Stringify operations per second: %.2f ops/sec", 1000.0 / stringifyTimePerOp); - PrintToServer("=== YYJSON Performance Benchmark End ==="); + PrintToServer("=== JSON Performance Benchmark End ==="); delete json; return Plugin_Handled; diff --git a/scripting/test_json.sp b/scripting/test_json.sp new file mode 100755 index 0000000..d732b26 --- /dev/null +++ b/scripting/test_json.sp @@ -0,0 +1,2987 @@ +#include +#include + +#pragma newdecls required +#pragma semicolon 1 + +public Plugin myinfo = +{ + name = "JSON Test Suite", + author = "ProjectSky", + description = "test suite for JSON functions", + version = "1.0.0", + url = "https://github.com/ProjectSky/sm-ext-yyjson" +}; + +// Test statistics +int g_iTotalTests = 0; +int g_iPassedTests = 0; +int g_iFailedTests = 0; +char g_sCurrentTest[128]; +bool g_bCurrentTestFailed = false; + +public void OnPluginStart() +{ + RegServerCmd("test_json", Command_RunTests, "Run JSON test suite"); +} + +// ============================================================================ +// Test Framework - Assertion Functions +// ============================================================================ + +/** + * Start a new test case + */ +void TestStart(const char[] name) +{ + g_iTotalTests++; + strcopy(g_sCurrentTest, sizeof(g_sCurrentTest), name); + g_bCurrentTestFailed = false; +} + +/** + * End current test case + */ +void TestEnd() +{ + if (!g_bCurrentTestFailed) + { + g_iPassedTests++; + PrintToServer("[PASS] %s", g_sCurrentTest); + } + else + { + g_iFailedTests++; + } +} + +/** + * Assert a condition is true + */ +void Assert(bool condition, const char[] message = "") +{ + if (!condition) + { + g_bCurrentTestFailed = true; + PrintToServer("[FAIL] %s - %s", g_sCurrentTest, message); + } +} + +/** + * Assert two integers are equal + */ +void AssertEq(int a, int b, const char[] message = "") +{ + if (a != b) + { + g_bCurrentTestFailed = true; + char buffer[256]; + if (message[0] != '\0') + { + FormatEx(buffer, sizeof(buffer), "%s (expected %d, got %d)", message, b, a); + } + else + { + FormatEx(buffer, sizeof(buffer), "Expected %d, got %d", b, a); + } + PrintToServer("[FAIL] %s - %s", g_sCurrentTest, buffer); + } +} + +/** + * Assert two integers are not equal + */ +stock void AssertNeq(int a, int b, const char[] message = "") +{ + if (a == b) + { + g_bCurrentTestFailed = true; + char buffer[256]; + if (message[0] != '\0') + { + FormatEx(buffer, sizeof(buffer), "%s (values should not be equal: %d)", message, a); + } + else + { + FormatEx(buffer, sizeof(buffer), "Values should not be equal: %d", a); + } + PrintToServer("[FAIL] %s - %s", g_sCurrentTest, buffer); + } +} + +/** + * Assert condition is true + */ +void AssertTrue(bool condition, const char[] message = "") +{ + Assert(condition, message[0] != '\0' ? message : "Expected true, got false"); +} + +/** + * Assert condition is false + */ +void AssertFalse(bool condition, const char[] message = "") +{ + Assert(!condition, message[0] != '\0' ? message : "Expected false, got true"); +} + +/** + * Assert two strings are equal + */ +void AssertStrEq(const char[] a, const char[] b, const char[] message = "") +{ + if (strcmp(a, b) != 0) + { + g_bCurrentTestFailed = true; + char buffer[512]; + if (message[0] != '\0') + { + FormatEx(buffer, sizeof(buffer), "%s (expected \"%s\", got \"%s\")", message, b, a); + } + else + { + FormatEx(buffer, sizeof(buffer), "Expected \"%s\", got \"%s\"", b, a); + } + PrintToServer("[FAIL] %s - %s", g_sCurrentTest, buffer); + } +} + +/** + * Assert two floats are equal within epsilon + */ +void AssertFloatEq(float a, float b, const char[] message = "", float epsilon = 0.0001) +{ + if (FloatAbs(a - b) > epsilon) + { + g_bCurrentTestFailed = true; + char buffer[256]; + if (message[0] != '\0') + { + FormatEx(buffer, sizeof(buffer), "%s (expected %.4f, got %.4f)", message, b, a); + } + else + { + FormatEx(buffer, sizeof(buffer), "Expected %.4f, got %.4f", b, a); + } + PrintToServer("[FAIL] %s - %s", g_sCurrentTest, buffer); + } +} + +/** + * Assert handle is valid + */ +void AssertValidHandle(Handle handle, const char[] message = "") +{ + Assert(handle != null, message[0] != '\0' ? message : "Handle should not be null"); +} + +/** + * Assert handle is null + */ +void AssertNullHandle(Handle handle, const char[] message = "") +{ + Assert(handle == null, message[0] != '\0' ? message : "Handle should be null"); +} + +// ============================================================================ +// Test Command +// ============================================================================ + +public Action Command_RunTests(int args) +{ + PrintToServer("========================================"); + PrintToServer("JSON Test Suite"); + PrintToServer("========================================"); + + // Reset statistics + g_iTotalTests = 0; + g_iPassedTests = 0; + g_iFailedTests = 0; + + // Run all test categories + Test_BasicValues(); + Test_ObjectOperations(); + Test_ArrayOperations(); + Test_ParseAndSerialize(); + Test_Iterators(); + Test_JSONPointer(); + Test_AdvancedFeatures(); + Test_Int64Operations(); + Test_EdgeCases(); + + // Print results + PrintToServer("========================================"); + PrintToServer("JSON Test Suite Results"); + PrintToServer("========================================"); + PrintToServer("Total Tests: %d", g_iTotalTests); + PrintToServer("Passed: %d", g_iPassedTests); + PrintToServer("Failed: %d", g_iFailedTests); + + if (g_iTotalTests > 0) + { + float successRate = (float(g_iPassedTests) / float(g_iTotalTests)) * 100.0; + PrintToServer("Success Rate: %.2f%%", successRate); + } + + PrintToServer("========================================"); + + return Plugin_Handled; +} + +// ============================================================================ +// 2.1 Basic Value Tests +// ============================================================================ + +void Test_BasicValues() +{ + PrintToServer("\n[Category] Basic Value Tests"); + + // Test CreateBool & GetBool + TestStart("BasicValues_CreateBool_True"); + { + JSON val = JSON.CreateBool(true); + AssertValidHandle(val); + AssertTrue(val.GetBool()); + AssertEq(val.Type, JSON_TYPE_BOOL); + AssertTrue(val.IsBool); + AssertTrue(val.IsTrue); + AssertFalse(val.IsFalse); + delete val; + } + TestEnd(); + + TestStart("BasicValues_CreateBool_False"); + { + JSON val = JSON.CreateBool(false); + AssertValidHandle(val); + AssertFalse(val.GetBool()); + AssertTrue(val.IsBool); + AssertFalse(val.IsTrue); + AssertTrue(val.IsFalse); + delete val; + } + TestEnd(); + + // Test CreateInt & GetInt + TestStart("BasicValues_CreateInt_Positive"); + { + JSON val = JSON.CreateInt(42); + AssertValidHandle(val); + AssertEq(val.GetInt(), 42); + AssertEq(val.Type, JSON_TYPE_NUM); + AssertTrue(val.IsInt); + AssertTrue(val.IsNum); + delete val; + } + TestEnd(); + + TestStart("BasicValues_CreateInt_Negative"); + { + JSON val = JSON.CreateInt(-123); + AssertValidHandle(val); + AssertEq(val.GetInt(), -123); + AssertTrue(val.IsSint); + delete val; + } + TestEnd(); + + TestStart("BasicValues_CreateInt_Zero"); + { + JSON val = JSON.CreateInt(0); + AssertValidHandle(val); + AssertEq(val.GetInt(), 0); + delete val; + } + TestEnd(); + + // Test CreateFloat & GetFloat + TestStart("BasicValues_CreateFloat_Positive"); + { + JSON val = JSON.CreateFloat(3.14159); + AssertValidHandle(val); + AssertFloatEq(val.GetFloat(), 3.14159); + AssertTrue(val.IsFloat); + AssertTrue(val.IsNum); + delete val; + } + TestEnd(); + + TestStart("BasicValues_CreateFloat_Negative"); + { + JSON val = JSON.CreateFloat(-2.71828); + AssertValidHandle(val); + AssertFloatEq(val.GetFloat(), -2.71828); + delete val; + } + TestEnd(); + + TestStart("BasicValues_CreateFloat_Zero"); + { + JSON val = JSON.CreateFloat(0.0); + AssertValidHandle(val); + AssertFloatEq(val.GetFloat(), 0.0); + delete val; + } + TestEnd(); + + // Test CreateString & GetString + TestStart("BasicValues_CreateString_Regular"); + { + JSON val = JSON.CreateString("Hello, World!"); + AssertValidHandle(val); + char buffer[64]; + AssertTrue(val.GetString(buffer, sizeof(buffer))); + AssertStrEq(buffer, "Hello, World!"); + AssertEq(val.Type, JSON_TYPE_STR); + AssertTrue(val.IsStr); + delete val; + } + TestEnd(); + + TestStart("BasicValues_CreateString_Empty"); + { + JSON val = JSON.CreateString(""); + AssertValidHandle(val); + char buffer[64]; + AssertTrue(val.GetString(buffer, sizeof(buffer))); + AssertStrEq(buffer, ""); + delete val; + } + TestEnd(); + + TestStart("BasicValues_CreateString_Unicode"); + { + JSON val = JSON.CreateString("测试字符串"); + AssertValidHandle(val); + char buffer[64]; + AssertTrue(val.GetString(buffer, sizeof(buffer))); + AssertStrEq(buffer, "测试字符串"); + delete val; + } + TestEnd(); + + // Test CreateInt64 & GetInt64 + TestStart("BasicValues_CreateInt64_Large"); + { + JSON val = JSON.CreateInt64("9223372036854775807"); + AssertValidHandle(val); + char buffer[32]; + AssertTrue(val.GetInt64(buffer, sizeof(buffer))); + AssertStrEq(buffer, "9223372036854775807"); + delete val; + } + TestEnd(); + + TestStart("BasicValues_CreateInt64_Negative"); + { + JSON val = JSON.CreateInt64("-9223372036854775808"); + AssertValidHandle(val); + char buffer[32]; + AssertTrue(val.GetInt64(buffer, sizeof(buffer))); + AssertStrEq(buffer, "-9223372036854775808"); + delete val; + } + TestEnd(); + + // Test CreateNull + TestStart("BasicValues_CreateNull"); + { + JSON val = JSON.CreateNull(); + AssertValidHandle(val); + AssertEq(val.Type, JSON_TYPE_NULL); + AssertTrue(val.IsNull); + delete val; + } + TestEnd(); + + // Test GetTypeDesc + TestStart("BasicValues_GetTypeDesc"); + { + JSON boolVal = JSON.CreateBool(true); + JSON intVal = JSON.CreateInt(42); + JSON floatVal = JSON.CreateFloat(3.14); + JSON strVal = JSON.CreateString("test"); + JSON nullVal = JSON.CreateNull(); + + char buffer[32]; + + JSON.GetTypeDesc(boolVal, buffer, sizeof(buffer)); + AssertStrEq(buffer, "true"); + + JSON.GetTypeDesc(intVal, buffer, sizeof(buffer)); + AssertTrue(strcmp(buffer, "uint") == 0 || strcmp(buffer, "sint") == 0); + + JSON.GetTypeDesc(floatVal, buffer, sizeof(buffer)); + AssertStrEq(buffer, "real"); + + JSON.GetTypeDesc(strVal, buffer, sizeof(buffer)); + AssertStrEq(buffer, "string"); + + JSON.GetTypeDesc(nullVal, buffer, sizeof(buffer)); + AssertStrEq(buffer, "null"); + + delete boolVal; + delete intVal; + delete floatVal; + delete strVal; + delete nullVal; + } + TestEnd(); +} + +// ============================================================================ +// 2.2 Object Operations Tests +// ============================================================================ + +void Test_ObjectOperations() +{ + PrintToServer("\n[Category] Object Operations Tests"); + + // Test object creation + TestStart("Object_Constructor"); + { + JSONObject obj = new JSONObject(); + AssertValidHandle(obj); + AssertTrue(obj.IsObject); + AssertEq(obj.Size, 0); + delete obj; + } + TestEnd(); + + // Test Set/Get methods + TestStart("Object_SetGetInt"); + { + JSONObject obj = new JSONObject(); + AssertTrue(obj.SetInt("number", 42)); + AssertEq(obj.GetInt("number"), 42); + AssertEq(obj.Size, 1); + delete obj; + } + TestEnd(); + + TestStart("Object_SetGetFloat"); + { + JSONObject obj = new JSONObject(); + AssertTrue(obj.SetFloat("pi", 3.14159)); + AssertFloatEq(obj.GetFloat("pi"), 3.14159); + delete obj; + } + TestEnd(); + + TestStart("Object_SetGetBool"); + { + JSONObject obj = new JSONObject(); + AssertTrue(obj.SetBool("flag", true)); + AssertTrue(obj.GetBool("flag")); + delete obj; + } + TestEnd(); + + TestStart("Object_SetGetString"); + { + JSONObject obj = new JSONObject(); + AssertTrue(obj.SetString("name", "test")); + char buffer[64]; + AssertTrue(obj.GetString("name", buffer, sizeof(buffer))); + AssertStrEq(buffer, "test"); + delete obj; + } + TestEnd(); + + TestStart("Object_SetGetInt64"); + { + JSONObject obj = new JSONObject(); + AssertTrue(obj.SetInt64("bignum", "9223372036854775807")); + char buffer[32]; + AssertTrue(obj.GetInt64("bignum", buffer, sizeof(buffer))); + AssertStrEq(buffer, "9223372036854775807"); + delete obj; + } + TestEnd(); + + TestStart("Object_SetGetNull"); + { + JSONObject obj = new JSONObject(); + AssertTrue(obj.SetNull("nullable")); + AssertTrue(obj.IsNull("nullable")); + delete obj; + } + TestEnd(); + + // Test HasKey + TestStart("Object_HasKey"); + { + JSONObject obj = new JSONObject(); + obj.SetInt("exists", 1); + AssertTrue(obj.HasKey("exists")); + AssertFalse(obj.HasKey("notexists")); + delete obj; + } + TestEnd(); + + // Test GetKey and GetValueAt + TestStart("Object_GetKeyAndValueAt"); + { + JSONObject obj = new JSONObject(); + obj.SetInt("first", 1); + obj.SetInt("second", 2); + + char key[32]; + AssertTrue(obj.GetKey(0, key, sizeof(key))); + AssertStrEq(key, "first"); + + JSON val = obj.GetValueAt(0); + AssertValidHandle(val); + AssertEq(val.GetInt(), 1); + + delete val; + delete obj; + } + TestEnd(); + + // Test Remove + TestStart("Object_Remove"); + { + JSONObject obj = new JSONObject(); + obj.SetInt("remove_me", 1); + obj.SetInt("keep_me", 2); + AssertEq(obj.Size, 2); + + AssertTrue(obj.Remove("remove_me")); + AssertEq(obj.Size, 1); + AssertFalse(obj.HasKey("remove_me")); + AssertTrue(obj.HasKey("keep_me")); + + delete obj; + } + TestEnd(); + + // Test Clear + TestStart("Object_Clear"); + { + JSONObject obj = new JSONObject(); + obj.SetInt("a", 1); + obj.SetInt("b", 2); + obj.SetInt("c", 3); + AssertEq(obj.Size, 3); + + AssertTrue(obj.Clear()); + AssertEq(obj.Size, 0); + + delete obj; + } + TestEnd(); + + // Test RenameKey + TestStart("Object_RenameKey"); + { + JSONObject obj = new JSONObject(); + obj.SetInt("oldname", 42); + + AssertTrue(obj.RenameKey("oldname", "newname")); + AssertFalse(obj.HasKey("oldname")); + AssertTrue(obj.HasKey("newname")); + AssertEq(obj.GetInt("newname"), 42); + + delete obj; + } + TestEnd(); + + // Test FromString + TestStart("Object_FromString"); + { + JSONObject obj = JSONObject.FromString("{\"key\":\"value\",\"num\":123}"); + AssertValidHandle(obj); + + char buffer[64]; + AssertTrue(obj.GetString("key", buffer, sizeof(buffer))); + AssertStrEq(buffer, "value"); + AssertEq(obj.GetInt("num"), 123); + + delete obj; + } + TestEnd(); + + // Test FromStrings + TestStart("Object_FromStrings"); + { + char pairs[][] = {"name", "test", "type", "demo", "version", "1.0"}; + JSONObject obj = JSONObject.FromStrings(pairs, sizeof(pairs)); + AssertValidHandle(obj); + AssertEq(obj.Size, 3); + + char buffer[64]; + AssertTrue(obj.GetString("name", buffer, sizeof(buffer))); + AssertStrEq(buffer, "test"); + + delete obj; + } + TestEnd(); + + // Test Sort + TestStart("Object_Sort_Ascending"); + { + JSONObject obj = new JSONObject(); + obj.SetInt("zebra", 1); + obj.SetInt("alpha", 2); + obj.SetInt("beta", 3); + + AssertTrue(obj.Sort(JSON_SORT_ASC)); + + char key[32]; + obj.GetKey(0, key, sizeof(key)); + AssertStrEq(key, "alpha"); + + delete obj; + } + TestEnd(); + + TestStart("Object_Sort_Descending"); + { + JSONObject obj = new JSONObject(); + obj.SetInt("alpha", 1); + obj.SetInt("beta", 2); + obj.SetInt("gamma", 3); + + AssertTrue(obj.Sort(JSON_SORT_DESC)); + + char key[32]; + obj.GetKey(0, key, sizeof(key)); + AssertStrEq(key, "gamma"); + + delete obj; + } + TestEnd(); + + // Test Set with handle + TestStart("Object_SetWithHandle"); + { + JSONObject obj = new JSONObject(); + JSON val = JSON.CreateInt(999); + + AssertTrue(obj.Set("nested", val)); + AssertEq(obj.GetInt("nested"), 999); + + delete val; + delete obj; + } + TestEnd(); + + // Test Get with handle + TestStart("Object_GetHandle"); + { + JSONObject obj = new JSONObject(); + obj.SetInt("value", 42); + + JSON val = obj.Get("value"); + AssertValidHandle(val); + AssertEq(val.GetInt(), 42); + + delete val; + delete obj; + } + TestEnd(); +} + +// ============================================================================ +// 2.3 Array Operations Tests +// ============================================================================ + +void Test_ArrayOperations() +{ + PrintToServer("\n[Category] Array Operations Tests"); + + // Test array creation + TestStart("Array_Constructor"); + { + JSONArray arr = new JSONArray(); + AssertValidHandle(arr); + AssertTrue(arr.IsArray); + AssertEq(arr.Length, 0); + delete arr; + } + TestEnd(); + + // Test Push methods + TestStart("Array_PushInt"); + { + JSONArray arr = new JSONArray(); + AssertTrue(arr.PushInt(42)); + AssertEq(arr.Length, 1); + AssertEq(arr.GetInt(0), 42); + delete arr; + } + TestEnd(); + + TestStart("Array_PushFloat"); + { + JSONArray arr = new JSONArray(); + AssertTrue(arr.PushFloat(3.14)); + AssertFloatEq(arr.GetFloat(0), 3.14); + delete arr; + } + TestEnd(); + + TestStart("Array_PushBool"); + { + JSONArray arr = new JSONArray(); + AssertTrue(arr.PushBool(true)); + AssertTrue(arr.GetBool(0)); + delete arr; + } + TestEnd(); + + TestStart("Array_PushString"); + { + JSONArray arr = new JSONArray(); + AssertTrue(arr.PushString("test")); + char buffer[64]; + AssertTrue(arr.GetString(0, buffer, sizeof(buffer))); + AssertStrEq(buffer, "test"); + delete arr; + } + TestEnd(); + + TestStart("Array_PushInt64"); + { + JSONArray arr = new JSONArray(); + AssertTrue(arr.PushInt64("9223372036854775807")); + char buffer[32]; + arr.GetInt64(0, buffer, sizeof(buffer)); + AssertStrEq(buffer, "9223372036854775807"); + delete arr; + } + TestEnd(); + + TestStart("Array_PushNull"); + { + JSONArray arr = new JSONArray(); + AssertTrue(arr.PushNull()); + AssertTrue(arr.IsNull(0)); + delete arr; + } + TestEnd(); + + // Test Set/Get methods + TestStart("Array_SetGetInt"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(0); + AssertTrue(arr.SetInt(0, 100)); + AssertEq(arr.GetInt(0), 100); + delete arr; + } + TestEnd(); + + TestStart("Array_SetGetFloat"); + { + JSONArray arr = new JSONArray(); + arr.PushFloat(0.0); + AssertTrue(arr.SetFloat(0, 2.718)); + AssertFloatEq(arr.GetFloat(0), 2.718); + delete arr; + } + TestEnd(); + + // Test Remove methods + TestStart("Array_Remove"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(1); + arr.PushInt(2); + arr.PushInt(3); + AssertEq(arr.Length, 3); + + AssertTrue(arr.Remove(1)); + AssertEq(arr.Length, 2); + AssertEq(arr.GetInt(0), 1); + AssertEq(arr.GetInt(1), 3); + + delete arr; + } + TestEnd(); + + TestStart("Array_RemoveFirst"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(1); + arr.PushInt(2); + arr.PushInt(3); + + AssertTrue(arr.RemoveFirst()); + AssertEq(arr.Length, 2); + AssertEq(arr.GetInt(0), 2); + + delete arr; + } + TestEnd(); + + TestStart("Array_RemoveLast"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(1); + arr.PushInt(2); + arr.PushInt(3); + + AssertTrue(arr.RemoveLast()); + AssertEq(arr.Length, 2); + AssertEq(arr.GetInt(1), 2); + + delete arr; + } + TestEnd(); + + TestStart("Array_RemoveRange"); + { + JSONArray arr = new JSONArray(); + for (int i = 0; i < 10; i++) + { + arr.PushInt(i); + } + + AssertTrue(arr.RemoveRange(2, 5)); + AssertEq(arr.Length, 5); + + delete arr; + } + TestEnd(); + + TestStart("Array_Clear"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(1); + arr.PushInt(2); + arr.PushInt(3); + + AssertTrue(arr.Clear()); + AssertEq(arr.Length, 0); + + delete arr; + } + TestEnd(); + + // Test First/Last properties + TestStart("Array_FirstLast"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(10); + arr.PushInt(20); + arr.PushInt(30); + + JSON first = arr.First; + JSON last = arr.Last; + + AssertValidHandle(first); + AssertValidHandle(last); + AssertEq(first.GetInt(), 10); + AssertEq(last.GetInt(), 30); + + delete first; + delete last; + delete arr; + } + TestEnd(); + + // Test IndexOf methods + TestStart("Array_IndexOfInt"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(10); + arr.PushInt(20); + arr.PushInt(30); + + AssertEq(arr.IndexOfInt(20), 1); + AssertEq(arr.IndexOfInt(999), -1); + + delete arr; + } + TestEnd(); + + TestStart("Array_IndexOfBool"); + { + JSONArray arr = new JSONArray(); + arr.PushBool(false); + arr.PushBool(true); + arr.PushBool(false); + + AssertEq(arr.IndexOfBool(true), 1); + + delete arr; + } + TestEnd(); + + TestStart("Array_IndexOfString"); + { + JSONArray arr = new JSONArray(); + arr.PushString("apple"); + arr.PushString("banana"); + arr.PushString("cherry"); + + AssertEq(arr.IndexOfString("banana"), 1); + AssertEq(arr.IndexOfString("orange"), -1); + + delete arr; + } + TestEnd(); + + TestStart("Array_IndexOfFloat"); + { + JSONArray arr = new JSONArray(); + arr.PushFloat(1.1); + arr.PushFloat(2.2); + arr.PushFloat(3.3); + + AssertEq(arr.IndexOfFloat(2.2), 1); + + delete arr; + } + TestEnd(); + + TestStart("Array_IndexOfInt64"); + { + JSONArray arr = new JSONArray(); + arr.PushInt64("1000000000000"); + arr.PushInt64("2000000000000"); + + AssertEq(arr.IndexOfInt64("2000000000000"), 1); + AssertEq(arr.IndexOfInt64("3000000000000"), -1); + + delete arr; + } + TestEnd(); + + // Test FromString + TestStart("Array_FromString"); + { + JSONArray arr = JSONArray.FromString("[1,2,3,4,5]"); + AssertValidHandle(arr); + AssertEq(arr.Length, 5); + AssertEq(arr.GetInt(0), 1); + AssertEq(arr.GetInt(4), 5); + delete arr; + } + TestEnd(); + + // Test FromStrings + TestStart("Array_FromStrings"); + { + char strings[][] = {"apple", "banana", "cherry"}; + JSONArray arr = JSONArray.FromStrings(strings, sizeof(strings)); + AssertValidHandle(arr); + AssertEq(arr.Length, 3); + + char buffer[64]; + arr.GetString(1, buffer, sizeof(buffer)); + AssertStrEq(buffer, "banana"); + + delete arr; + } + TestEnd(); + + // Test Sort + TestStart("Array_Sort_Ascending"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(3); + arr.PushInt(1); + arr.PushInt(4); + arr.PushInt(1); + arr.PushInt(5); + + AssertTrue(arr.Sort(JSON_SORT_ASC)); + AssertEq(arr.GetInt(0), 1); + AssertEq(arr.GetInt(4), 5); + + delete arr; + } + TestEnd(); + + TestStart("Array_Sort_Descending"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(1); + arr.PushInt(2); + arr.PushInt(3); + arr.PushInt(4); + arr.PushInt(5); + + AssertTrue(arr.Sort(JSON_SORT_DESC)); + AssertEq(arr.GetInt(0), 5); + AssertEq(arr.GetInt(4), 1); + + delete arr; + } + TestEnd(); + + // Test Push with handle + TestStart("Array_PushHandle"); + { + JSONArray arr = new JSONArray(); + JSON val = JSON.CreateInt(999); + + AssertTrue(arr.Push(val)); + AssertEq(arr.GetInt(0), 999); + + delete val; + delete arr; + } + TestEnd(); + + // Test Get with handle + TestStart("Array_GetHandle"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(42); + + JSON val = arr.Get(0); + AssertValidHandle(val); + AssertEq(val.GetInt(), 42); + + delete val; + delete arr; + } + TestEnd(); + + // Test FromInt + TestStart("Array_FromInt"); + { + int values[] = {10, 20, 30, 40, 50}; + JSONArray arr = JSONArray.FromInt(values, sizeof(values)); + + AssertValidHandle(arr); + AssertEq(arr.Length, 5); + AssertEq(arr.GetInt(0), 10); + AssertEq(arr.GetInt(2), 30); + AssertEq(arr.GetInt(4), 50); + + delete arr; + } + TestEnd(); + + TestStart("Array_FromInt_Negative"); + { + int values[] = {-100, -50, 0, 50, 100}; + JSONArray arr = JSONArray.FromInt(values, sizeof(values)); + + AssertValidHandle(arr); + AssertEq(arr.Length, 5); + AssertEq(arr.GetInt(0), -100); + AssertEq(arr.GetInt(2), 0); + AssertEq(arr.GetInt(4), 100); + + delete arr; + } + TestEnd(); + + // Test FromInt64 + TestStart("Array_FromInt64_Mixed"); + { + char values[][] = {"9223372036854775807", "-9223372036854775808", "0", "18446744073709551615"}; + JSONArray arr = JSONArray.FromInt64(values, sizeof(values)); + + AssertValidHandle(arr); + AssertEq(arr.Length, 4); + + char buffer[32]; + arr.GetInt64(0, buffer, sizeof(buffer)); + AssertStrEq(buffer, "9223372036854775807"); + + arr.GetInt64(1, buffer, sizeof(buffer)); + AssertStrEq(buffer, "-9223372036854775808"); + + arr.GetInt64(2, buffer, sizeof(buffer)); + AssertStrEq(buffer, "0"); + + arr.GetInt64(3, buffer, sizeof(buffer)); + AssertStrEq(buffer, "18446744073709551615"); + + delete arr; + } + TestEnd(); + + TestStart("Array_FromInt64_LargeValues"); + { + char values[][] = {"1234567890123456", "9876543210987654", "5555555555555555"}; + JSONArray arr = JSONArray.FromInt64(values, sizeof(values)); + + AssertValidHandle(arr); + AssertEq(arr.Length, 3); + + char buffer[32]; + arr.GetInt64(1, buffer, sizeof(buffer)); + AssertStrEq(buffer, "9876543210987654"); + + delete arr; + } + TestEnd(); + + // Test FromBool + TestStart("Array_FromBool"); + { + bool values[] = {true, false, true, true, false}; + JSONArray arr = JSONArray.FromBool(values, sizeof(values)); + + AssertValidHandle(arr); + AssertEq(arr.Length, 5); + AssertTrue(arr.GetBool(0)); + AssertFalse(arr.GetBool(1)); + AssertTrue(arr.GetBool(2)); + AssertTrue(arr.GetBool(3)); + AssertFalse(arr.GetBool(4)); + + delete arr; + } + TestEnd(); + + TestStart("Array_FromBool_AllTrue"); + { + bool values[] = {true, true, true}; + JSONArray arr = JSONArray.FromBool(values, sizeof(values)); + + AssertValidHandle(arr); + AssertEq(arr.Length, 3); + AssertTrue(arr.GetBool(0)); + AssertTrue(arr.GetBool(1)); + AssertTrue(arr.GetBool(2)); + + delete arr; + } + TestEnd(); + + // Test FromFloat + TestStart("Array_FromFloat"); + { + float values[] = {1.1, 2.2, 3.3, 4.4, 5.5}; + JSONArray arr = JSONArray.FromFloat(values, sizeof(values)); + + AssertValidHandle(arr); + AssertEq(arr.Length, 5); + AssertFloatEq(arr.GetFloat(0), 1.1); + AssertFloatEq(arr.GetFloat(2), 3.3, "", 0.01); + AssertFloatEq(arr.GetFloat(4), 5.5); + + delete arr; + } + TestEnd(); + + TestStart("Array_FromFloat_Negative"); + { + float values[] = {-3.14, 0.0, 2.718, -1.414}; + JSONArray arr = JSONArray.FromFloat(values, sizeof(values)); + + AssertValidHandle(arr); + AssertEq(arr.Length, 4); + AssertFloatEq(arr.GetFloat(0), -3.14); + AssertFloatEq(arr.GetFloat(1), 0.0); + AssertFloatEq(arr.GetFloat(2), 2.718); + + delete arr; + } + TestEnd(); + + // Test Insert methods + TestStart("Array_InsertInt"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(1); + arr.PushInt(3); + arr.PushInt(4); + + AssertTrue(arr.InsertInt(1, 2)); + AssertEq(arr.Length, 4); + AssertEq(arr.GetInt(0), 1); + AssertEq(arr.GetInt(1), 2); + AssertEq(arr.GetInt(2), 3); + AssertEq(arr.GetInt(3), 4); + + delete arr; + } + TestEnd(); + + TestStart("Array_InsertBool"); + { + JSONArray arr = new JSONArray(); + arr.PushBool(true); + arr.PushBool(true); + + AssertTrue(arr.InsertBool(1, false)); + AssertEq(arr.Length, 3); + AssertTrue(arr.GetBool(0)); + AssertFalse(arr.GetBool(1)); + AssertTrue(arr.GetBool(2)); + + delete arr; + } + TestEnd(); + + TestStart("Array_InsertFloat"); + { + JSONArray arr = new JSONArray(); + arr.PushFloat(1.1); + arr.PushFloat(3.3); + + AssertTrue(arr.InsertFloat(1, 2.2)); + AssertEq(arr.Length, 3); + AssertFloatEq(arr.GetFloat(0), 1.1); + AssertFloatEq(arr.GetFloat(1), 2.2); + AssertFloatEq(arr.GetFloat(2), 3.3, "", 0.01); + + delete arr; + } + TestEnd(); + + TestStart("Array_InsertString"); + { + JSONArray arr = new JSONArray(); + arr.PushString("first"); + arr.PushString("third"); + + AssertTrue(arr.InsertString(1, "second")); + AssertEq(arr.Length, 3); + + char buffer[64]; + arr.GetString(0, buffer, sizeof(buffer)); + AssertStrEq(buffer, "first"); + + arr.GetString(1, buffer, sizeof(buffer)); + AssertStrEq(buffer, "second"); + + arr.GetString(2, buffer, sizeof(buffer)); + AssertStrEq(buffer, "third"); + + delete arr; + } + TestEnd(); + + TestStart("Array_InsertInt64"); + { + JSONArray arr = new JSONArray(); + arr.PushInt64("1111111111111111"); + arr.PushInt64("3333333333333333"); + + AssertTrue(arr.InsertInt64(1, "2222222222222222")); + AssertEq(arr.Length, 3); + + char buffer[32]; + arr.GetInt64(0, buffer, sizeof(buffer)); + AssertStrEq(buffer, "1111111111111111"); + + arr.GetInt64(1, buffer, sizeof(buffer)); + AssertStrEq(buffer, "2222222222222222"); + + arr.GetInt64(2, buffer, sizeof(buffer)); + AssertStrEq(buffer, "3333333333333333"); + + delete arr; + } + TestEnd(); + + TestStart("Array_InsertNull"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(1); + arr.PushInt(2); + + AssertTrue(arr.InsertNull(1)); + AssertEq(arr.Length, 3); + AssertEq(arr.GetInt(0), 1); + AssertTrue(arr.IsNull(1)); + AssertEq(arr.GetInt(2), 2); + + delete arr; + } + TestEnd(); + + TestStart("Array_InsertAtBeginning"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(2); + arr.PushInt(3); + + AssertTrue(arr.InsertInt(0, 1)); + AssertEq(arr.Length, 3); + AssertEq(arr.GetInt(0), 1); + AssertEq(arr.GetInt(1), 2); + AssertEq(arr.GetInt(2), 3); + + delete arr; + } + TestEnd(); + + TestStart("Array_InsertAtEnd"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(1); + arr.PushInt(2); + + AssertTrue(arr.InsertInt(2, 3)); + AssertEq(arr.Length, 3); + AssertEq(arr.GetInt(0), 1); + AssertEq(arr.GetInt(1), 2); + AssertEq(arr.GetInt(2), 3); + + delete arr; + } + TestEnd(); + + TestStart("Array_InsertHandle"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(1); + arr.PushInt(3); + + JSON val = JSON.CreateInt(2); + AssertTrue(arr.Insert(1, val)); + + AssertEq(arr.Length, 3); + AssertEq(arr.GetInt(0), 1); + AssertEq(arr.GetInt(1), 2); + AssertEq(arr.GetInt(2), 3); + + delete val; + delete arr; + } + TestEnd(); + + // Test Prepend methods + TestStart("Array_PrependInt"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(2); + arr.PushInt(3); + + AssertTrue(arr.PrependInt(1)); + AssertEq(arr.Length, 3); + AssertEq(arr.GetInt(0), 1); + AssertEq(arr.GetInt(1), 2); + AssertEq(arr.GetInt(2), 3); + + delete arr; + } + TestEnd(); + + TestStart("Array_PrependBool"); + { + JSONArray arr = new JSONArray(); + arr.PushBool(false); + arr.PushBool(false); + + AssertTrue(arr.PrependBool(true)); + AssertEq(arr.Length, 3); + AssertTrue(arr.GetBool(0)); + AssertFalse(arr.GetBool(1)); + AssertFalse(arr.GetBool(2)); + + delete arr; + } + TestEnd(); + + TestStart("Array_PrependFloat"); + { + JSONArray arr = new JSONArray(); + arr.PushFloat(2.2); + arr.PushFloat(3.3); + + AssertTrue(arr.PrependFloat(1.1)); + AssertEq(arr.Length, 3); + AssertFloatEq(arr.GetFloat(0), 1.1); + AssertFloatEq(arr.GetFloat(1), 2.2); + + delete arr; + } + TestEnd(); + + TestStart("Array_PrependString"); + { + JSONArray arr = new JSONArray(); + arr.PushString("second"); + arr.PushString("third"); + + AssertTrue(arr.PrependString("first")); + AssertEq(arr.Length, 3); + + char buffer[64]; + arr.GetString(0, buffer, sizeof(buffer)); + AssertStrEq(buffer, "first"); + + arr.GetString(1, buffer, sizeof(buffer)); + AssertStrEq(buffer, "second"); + + delete arr; + } + TestEnd(); + + TestStart("Array_PrependInt64"); + { + JSONArray arr = new JSONArray(); + arr.PushInt64("2222222222222222"); + arr.PushInt64("3333333333333333"); + + AssertTrue(arr.PrependInt64("1111111111111111")); + AssertEq(arr.Length, 3); + + char buffer[32]; + arr.GetInt64(0, buffer, sizeof(buffer)); + AssertStrEq(buffer, "1111111111111111"); + + arr.GetInt64(1, buffer, sizeof(buffer)); + AssertStrEq(buffer, "2222222222222222"); + + delete arr; + } + TestEnd(); + + TestStart("Array_PrependNull"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(1); + arr.PushInt(2); + + AssertTrue(arr.PrependNull()); + AssertEq(arr.Length, 3); + AssertTrue(arr.IsNull(0)); + AssertEq(arr.GetInt(1), 1); + AssertEq(arr.GetInt(2), 2); + + delete arr; + } + TestEnd(); + + TestStart("Array_PrependHandle"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(2); + arr.PushInt(3); + + JSON val = JSON.CreateInt(1); + AssertTrue(arr.Prepend(val)); + + AssertEq(arr.Length, 3); + AssertEq(arr.GetInt(0), 1); + AssertEq(arr.GetInt(1), 2); + + delete val; + delete arr; + } + TestEnd(); + + TestStart("Array_PrependToEmpty"); + { + JSONArray arr = new JSONArray(); + + AssertTrue(arr.PrependInt(1)); + AssertEq(arr.Length, 1); + AssertEq(arr.GetInt(0), 1); + + delete arr; + } + TestEnd(); + + // Test combined Insert and Prepend + TestStart("Array_CombinedInsertPrepend"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(5); + arr.PrependInt(1); + arr.InsertInt(1, 3); + arr.InsertInt(1, 2); + arr.InsertInt(3, 4); + + AssertEq(arr.Length, 5); + AssertEq(arr.GetInt(0), 1); + AssertEq(arr.GetInt(1), 2); + AssertEq(arr.GetInt(2), 3); + AssertEq(arr.GetInt(3), 4); + AssertEq(arr.GetInt(4), 5); + + delete arr; + } + TestEnd(); +} + +// ============================================================================ +// 2.4 Parse & Serialize Tests +// ============================================================================ + +void Test_ParseAndSerialize() +{ + PrintToServer("\n[Category] Parse & Serialize Tests"); + + // Test Parse string + TestStart("Parse_SimpleObject"); + { + JSON json = JSON.Parse("{\"key\":\"value\",\"num\":42}"); + AssertValidHandle(json); + AssertTrue(json.IsObject); + delete json; + } + TestEnd(); + + TestStart("Parse_SimpleArray"); + { + JSON json = JSON.Parse("[1,2,3,4,5]"); + AssertValidHandle(json); + AssertTrue(json.IsArray); + delete json; + } + TestEnd(); + + TestStart("Parse_NestedStructure"); + { + JSON json = JSON.Parse("{\"user\":{\"name\":\"test\",\"age\":25},\"items\":[1,2,3]}"); + AssertValidHandle(json); + delete json; + } + TestEnd(); + + // Test mutable/immutable + TestStart("Parse_ImmutableDocument"); + { + JSON json = JSON.Parse("{\"key\":\"value\"}"); + AssertValidHandle(json); + AssertTrue(json.IsImmutable); + AssertFalse(json.IsMutable); + delete json; + } + TestEnd(); + + TestStart("Parse_MutableDocument"); + { + JSON json = JSON.Parse("{\"key\":\"value\"}", false, true); + AssertValidHandle(json); + AssertTrue(json.IsMutable); + AssertFalse(json.IsImmutable); + delete json; + } + TestEnd(); + + // Test ToString + TestStart("Serialize_ToString"); + { + JSONObject obj = new JSONObject(); + obj.SetInt("num", 42); + obj.SetString("str", "test"); + + char buffer[256]; + int len = obj.ToString(buffer, sizeof(buffer)); + AssertTrue(len > 0); + Assert(StrContains(buffer, "num") != -1); + Assert(StrContains(buffer, "test") != -1); + + delete obj; + } + TestEnd(); + + TestStart("Serialize_ToString_Pretty"); + { + JSONObject obj = new JSONObject(); + obj.SetInt("a", 1); + obj.SetInt("b", 2); + + char buffer[256]; + int len = obj.ToString(buffer, sizeof(buffer), JSON_WRITE_PRETTY); + AssertTrue(len > 0); + Assert(StrContains(buffer, "\n") != -1, "Pretty output should contain newlines"); + + delete obj; + } + TestEnd(); + + // Test GetSerializedSize + TestStart("Serialize_GetSerializedSize"); + { + JSONObject obj = new JSONObject(); + obj.SetInt("test", 123); + + int size = obj.GetSerializedSize(); + AssertTrue(size > 0); + + char[] buffer = new char[size]; + int written = obj.ToString(buffer, size); + AssertEq(written, size); + + delete obj; + } + TestEnd(); + + // Test read flags + TestStart("Parse_WithTrailingCommas"); + { + JSON json = JSON.Parse("[1,2,3,]", false, false, JSON_READ_ALLOW_TRAILING_COMMAS); + AssertValidHandle(json); + delete json; + } + TestEnd(); + + TestStart("Parse_WithComments"); + { + JSON json = JSON.Parse("/* comment */ {\"key\":\"value\"}", false, false, JSON_READ_ALLOW_COMMENTS); + AssertValidHandle(json); + delete json; + } + TestEnd(); + + // Test file operations (create temporary test file) + TestStart("Parse_ToFile_FromFile"); + { + JSONObject obj = new JSONObject(); + obj.SetInt("filetest", 999); + obj.SetString("name", "testfile"); + + // Write to file + AssertTrue(obj.ToFile("json_test_temp.json")); + + // Read from file + JSONObject loaded = JSON.Parse("json_test_temp.json", true); + AssertValidHandle(loaded); + + // Verify content + JSONObject loadedObj = loaded; + AssertEq(loadedObj.GetInt("filetest"), 999); + + char buffer[64]; + loadedObj.GetString("name", buffer, sizeof(buffer)); + AssertStrEq(buffer, "testfile"); + + delete obj; + delete loaded; + + // Cleanup + DeleteFile("json_test_temp.json"); + } + TestEnd(); + + // Test round-trip serialization + TestStart("Parse_RoundTrip"); + { + char original[] = "{\"int\":42,\"float\":3.14,\"bool\":true,\"str\":\"test\",\"null\":null}"; + JSON json1 = JSON.Parse(original); + + char buffer[256]; + json1.ToString(buffer, sizeof(buffer)); + + JSON json2 = JSON.Parse(buffer); + AssertTrue(JSON.Equals(json1, json2)); + + delete json1; + delete json2; + } + TestEnd(); +} + +// ============================================================================ +// 2.5 Iterator Tests +// ============================================================================ + +void Test_Iterators() +{ + PrintToServer("\n[Category] Iterator Tests"); + + // Test ForeachObject + TestStart("Iterator_ForeachObject"); + { + JSONObject obj = new JSONObject(); + obj.SetInt("a", 1); + obj.SetInt("b", 2); + obj.SetInt("c", 3); + + int count = 0; + char key[32]; + JSON value; + + while (obj.ForeachObject(key, sizeof(key), value)) + { + count++; + AssertValidHandle(value); + delete value; + } + + AssertEq(count, 3); + delete obj; + } + TestEnd(); + + // Test ForeachArray + TestStart("Iterator_ForeachArray"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(10); + arr.PushInt(20); + arr.PushInt(30); + + int count = 0; + int index; + JSON value; + + while (arr.ForeachArray(index, value)) + { + AssertEq(index, count); + AssertValidHandle(value); + delete value; + count++; + } + + AssertEq(count, 3); + delete arr; + } + TestEnd(); + + // Test ForeachKey + TestStart("Iterator_ForeachKey"); + { + JSONObject obj = new JSONObject(); + obj.SetInt("key1", 1); + obj.SetInt("key2", 2); + obj.SetInt("key3", 3); + + int count = 0; + char key[32]; + + while (obj.ForeachKey(key, sizeof(key))) + { + AssertTrue(strlen(key) > 0); + count++; + } + + AssertEq(count, 3); + delete obj; + } + TestEnd(); + + // Test ForeachIndex + TestStart("Iterator_ForeachIndex"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(1); + arr.PushInt(2); + arr.PushInt(3); + + int count = 0; + int index; + + while (arr.ForeachIndex(index)) + { + AssertEq(index, count); + count++; + } + + AssertEq(count, 3); + delete arr; + } + TestEnd(); + + // Test empty iterations + TestStart("Iterator_EmptyObject"); + { + JSONObject obj = new JSONObject(); + + char key[32]; + JSON value; + AssertFalse(obj.ForeachObject(key, sizeof(key), value)); + + delete obj; + } + TestEnd(); + + TestStart("Iterator_EmptyArray"); + { + JSONArray arr = new JSONArray(); + + int index; + JSON value; + AssertFalse(arr.ForeachArray(index, value)); + + delete arr; + } + TestEnd(); +} + +// ============================================================================ +// 2.6 JSON Pointer Tests +// ============================================================================ + +void Test_JSONPointer() +{ + PrintToServer("\n[Category] JSON Pointer Tests"); + + // Test PtrSet methods + TestStart("Pointer_PtrSetInt"); + { + JSONObject obj = new JSONObject(); + AssertTrue(obj.PtrSetInt("/number", 42)); + AssertEq(obj.PtrGetInt("/number"), 42); + delete obj; + } + TestEnd(); + + TestStart("Pointer_PtrSetFloat"); + { + JSONObject obj = new JSONObject(); + AssertTrue(obj.PtrSetFloat("/pi", 3.14159)); + AssertFloatEq(obj.PtrGetFloat("/pi"), 3.14159); + delete obj; + } + TestEnd(); + + TestStart("Pointer_PtrSetBool"); + { + JSONObject obj = new JSONObject(); + AssertTrue(obj.PtrSetBool("/flag", true)); + AssertTrue(obj.PtrGetBool("/flag")); + delete obj; + } + TestEnd(); + + TestStart("Pointer_PtrSetString"); + { + JSONObject obj = new JSONObject(); + AssertTrue(obj.PtrSetString("/name", "test")); + + char buffer[64]; + AssertTrue(obj.PtrGetString("/name", buffer, sizeof(buffer))); + AssertStrEq(buffer, "test"); + + delete obj; + } + TestEnd(); + + TestStart("Pointer_PtrSetInt64"); + { + JSONObject obj = new JSONObject(); + AssertTrue(obj.PtrSetInt64("/bignum", "9223372036854775807")); + + char buffer[32]; + AssertTrue(obj.PtrGetInt64("/bignum", buffer, sizeof(buffer))); + AssertStrEq(buffer, "9223372036854775807"); + + delete obj; + } + TestEnd(); + + TestStart("Pointer_PtrSetNull"); + { + JSONObject obj = new JSONObject(); + AssertTrue(obj.PtrSetNull("/nullable")); + AssertTrue(obj.PtrGetIsNull("/nullable")); + delete obj; + } + TestEnd(); + + // Test nested path creation + TestStart("Pointer_NestedPathCreation"); + { + JSONObject obj = new JSONObject(); + AssertTrue(obj.PtrSetInt("/a/b/c/d", 123)); + AssertEq(obj.PtrGetInt("/a/b/c/d"), 123); + delete obj; + } + TestEnd(); + + // Test PtrGet with handle + TestStart("Pointer_PtrGet"); + { + JSONObject obj = new JSONObject(); + obj.PtrSetInt("/test", 999); + + JSON val = obj.PtrGet("/test"); + AssertValidHandle(val); + AssertEq(val.GetInt(), 999); + + delete val; + delete obj; + } + TestEnd(); + + // Test PtrAdd methods + TestStart("Pointer_PtrAddInt"); + { + JSONObject obj = new JSONObject(); + obj.PtrSetInt("/arr/0", 1); + AssertTrue(obj.PtrAddInt("/arr/1", 2)); + AssertEq(obj.PtrGetInt("/arr/1"), 2); + delete obj; + } + TestEnd(); + + TestStart("Pointer_PtrAddString"); + { + JSONObject obj = new JSONObject(); + obj.PtrSetString("/items/0", "first"); + AssertTrue(obj.PtrAddString("/items/1", "second")); + + char buffer[64]; + obj.PtrGetString("/items/1", buffer, sizeof(buffer)); + AssertStrEq(buffer, "second"); + + delete obj; + } + TestEnd(); + + // Test PtrRemove + TestStart("Pointer_PtrRemove"); + { + JSONObject obj = new JSONObject(); + obj.PtrSetInt("/remove_me", 1); + obj.PtrSetInt("/keep_me", 2); + + AssertTrue(obj.PtrRemove("/remove_me")); + + JSON val; + obj.PtrTryGetVal("/remove_me", val); + AssertNullHandle(val); + + delete obj; + } + TestEnd(); + + // Test PtrGetLength + TestStart("Pointer_PtrGetLength"); + { + JSONObject obj = new JSONObject(); + obj.PtrSetString("/text", "hello"); + + int len = obj.PtrGetLength("/text"); + AssertEq(len, 6); // Including null terminator + + delete obj; + } + TestEnd(); + + // Test PtrTryGet methods + TestStart("Pointer_PtrTryGetInt"); + { + JSONObject obj = new JSONObject(); + obj.PtrSetInt("/value", 42); + + int value; + AssertTrue(obj.PtrTryGetInt("/value", value)); + AssertEq(value, 42); + + AssertFalse(obj.PtrTryGetInt("/nonexistent", value)); + + delete obj; + } + TestEnd(); + + TestStart("Pointer_PtrTryGetBool"); + { + JSONObject obj = new JSONObject(); + obj.PtrSetBool("/flag", true); + + bool value; + AssertTrue(obj.PtrTryGetBool("/flag", value)); + AssertTrue(value); + + delete obj; + } + TestEnd(); + + TestStart("Pointer_PtrTryGetFloat"); + { + JSONObject obj = new JSONObject(); + obj.PtrSetFloat("/pi", 3.14); + + float value; + AssertTrue(obj.PtrTryGetFloat("/pi", value)); + AssertFloatEq(value, 3.14); + + delete obj; + } + TestEnd(); + + TestStart("Pointer_PtrTryGetString"); + { + JSONObject obj = new JSONObject(); + obj.PtrSetString("/name", "test"); + + char buffer[64]; + AssertTrue(obj.PtrTryGetString("/name", buffer, sizeof(buffer))); + AssertStrEq(buffer, "test"); + + delete obj; + } + TestEnd(); + + TestStart("Pointer_PtrTryGetInt64"); + { + JSONObject obj = new JSONObject(); + obj.PtrSetInt64("/bignum", "123456789012345"); + + char buffer[32]; + AssertTrue(obj.PtrTryGetInt64("/bignum", buffer, sizeof(buffer))); + AssertStrEq(buffer, "123456789012345"); + + delete obj; + } + TestEnd(); + + TestStart("Pointer_PtrTryGetVal"); + { + JSONObject obj = new JSONObject(); + obj.PtrSetInt("/test", 42); + + JSON value; + AssertTrue(obj.PtrTryGetVal("/test", value)); + AssertValidHandle(value); + AssertEq(value.GetInt(), 42); + + delete value; + delete obj; + } + TestEnd(); +} + +// ============================================================================ +// 2.7 Advanced Features Tests +// ============================================================================ + +void Test_AdvancedFeatures() +{ + PrintToServer("\n[Category] Advanced Features Tests"); + + // Test DeepCopy + TestStart("Advanced_DeepCopy_Object"); + { + JSONObject original = new JSONObject(); + original.SetInt("num", 42); + original.SetString("str", "test"); + + JSONObject target = new JSONObject(); + JSONObject copy = JSON.DeepCopy(target, original); + + AssertValidHandle(copy); + AssertEq(copy.GetInt("num"), 42); + + char buffer[64]; + copy.GetString("str", buffer, sizeof(buffer)); + AssertStrEq(buffer, "test"); + + delete original; + delete target; + delete copy; + } + TestEnd(); + + TestStart("Advanced_DeepCopy_Array"); + { + JSONArray original = new JSONArray(); + original.PushInt(1); + original.PushInt(2); + original.PushInt(3); + + JSONArray target = new JSONArray(); + JSONArray copy = JSON.DeepCopy(target, original); + + AssertValidHandle(copy); + AssertEq(copy.Length, 3); + AssertEq(copy.GetInt(0), 1); + AssertEq(copy.GetInt(2), 3); + + delete original; + delete target; + delete copy; + } + TestEnd(); + + // Test Equals + TestStart("Advanced_Equals_True"); + { + JSONObject obj1 = new JSONObject(); + obj1.SetInt("a", 1); + obj1.SetString("b", "test"); + + JSONObject obj2 = new JSONObject(); + obj2.SetInt("a", 1); + obj2.SetString("b", "test"); + + AssertTrue(JSON.Equals(obj1, obj2)); + + delete obj1; + delete obj2; + } + TestEnd(); + + TestStart("Advanced_Equals_False"); + { + JSONObject obj1 = new JSONObject(); + obj1.SetInt("a", 1); + + JSONObject obj2 = new JSONObject(); + obj2.SetInt("a", 2); + + AssertFalse(JSON.Equals(obj1, obj2)); + + delete obj1; + delete obj2; + } + TestEnd(); + + // Test ToMutable/ToImmutable + TestStart("Advanced_ToMutable"); + { + JSON immutable = JSON.Parse("{\"key\":\"value\"}"); + AssertTrue(immutable.IsImmutable); + + JSON mutable = immutable.ToMutable(); + AssertValidHandle(mutable); + AssertTrue(mutable.IsMutable); + + delete immutable; + delete mutable; + } + TestEnd(); + + TestStart("Advanced_ToImmutable"); + { + JSONObject mutable = new JSONObject(); + mutable.SetInt("key", 42); + AssertTrue(mutable.IsMutable); + + JSON immutable = mutable.ToImmutable(); + AssertValidHandle(immutable); + AssertTrue(immutable.IsImmutable); + + delete mutable; + delete immutable; + } + TestEnd(); + + // Test Pack + TestStart("Advanced_Pack_SimpleObject"); + { + JSONObject json = JSON.Pack("{s:i,s:s,s:b}", + "num", 42, + "str", "test", + "bool", true + ); + + AssertValidHandle(json); + AssertTrue(json.IsObject); + + JSONObject obj = json; + AssertEq(obj.GetInt("num"), 42); + + char buffer[64]; + obj.GetString("str", buffer, sizeof(buffer)); + AssertStrEq(buffer, "test"); + + AssertTrue(obj.GetBool("bool")); + + delete json; + } + TestEnd(); + + TestStart("Advanced_Pack_Array"); + { + JSONArray json = JSON.Pack("[i,i,i]", 1, 2, 3); + + AssertValidHandle(json); + AssertTrue(json.IsArray); + + JSONArray arr = json; + AssertEq(arr.Length, 3); + AssertEq(arr.GetInt(0), 1); + AssertEq(arr.GetInt(2), 3); + + delete json; + } + TestEnd(); + + TestStart("Advanced_Pack_Nested"); + { + JSONObject json = JSON.Pack("{s:{s:s,s:i}}", + "user", + "name", "test", + "age", 25 + ); + + AssertValidHandle(json); + + char buffer[64]; + JSONObject obj = json; + obj.PtrGetString("/user/name", buffer, sizeof(buffer)); + AssertStrEq(buffer, "test"); + AssertEq(obj.PtrGetInt("/user/age"), 25); + + delete json; + } + TestEnd(); + + // Test mixed type array sorting + TestStart("Advanced_MixedTypeSort"); + { + JSONArray json = JSON.Parse("[true, 42, \"hello\", 1.5, false]", false, true); + JSONArray arr = json; + + AssertTrue(arr.Sort(JSON_SORT_ASC)); + AssertEq(arr.Length, 5); + + delete json; + } + TestEnd(); +} + +// ============================================================================ +// 2.8 Int64 Operations Tests +// ============================================================================ + +void Test_Int64Operations() +{ + PrintToServer("\n[Category] Int64 Operations Tests"); + + // Test JSON.SetInt64 - Modify value in-place + TestStart("Int64_SetInt64_Positive"); + { + JSON val = JSON.CreateInt(100); + AssertTrue(val.SetInt64("123456789012345")); + + char buffer[32]; + AssertTrue(val.GetInt64(buffer, sizeof(buffer))); + AssertStrEq(buffer, "123456789012345"); + + delete val; + } + TestEnd(); + + TestStart("Int64_SetInt64_Negative"); + { + JSON val = JSON.CreateInt(100); + AssertTrue(val.SetInt64("-987654321098765")); + + char buffer[32]; + AssertTrue(val.GetInt64(buffer, sizeof(buffer))); + AssertStrEq(buffer, "-987654321098765"); + + delete val; + } + TestEnd(); + + TestStart("Int64_SetInt64_Zero"); + { + JSON val = JSON.CreateInt(999); + AssertTrue(val.SetInt64("0")); + + char buffer[32]; + AssertTrue(val.GetInt64(buffer, sizeof(buffer))); + AssertStrEq(buffer, "0"); + + delete val; + } + TestEnd(); + + // Test JSONArray.SetInt64 - Replace value in array + TestStart("Int64_Array_SetInt64"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(100); + arr.PushInt(200); + arr.PushInt(300); + + AssertTrue(arr.SetInt64(1, "5555555555555555")); + + char buffer[32]; + arr.GetInt64(1, buffer, sizeof(buffer)); + AssertStrEq(buffer, "5555555555555555"); + + delete arr; + } + TestEnd(); + + TestStart("Int64_Array_SetInt64_Negative"); + { + JSONArray arr = new JSONArray(); + arr.PushInt64("1111111111111111"); + + AssertTrue(arr.SetInt64(0, "-2222222222222222")); + + char buffer[32]; + arr.GetInt64(0, buffer, sizeof(buffer)); + AssertStrEq(buffer, "-2222222222222222"); + + delete arr; + } + TestEnd(); + + // Test large unsigned int64 values + TestStart("Int64_Array_PushLargeUnsigned"); + { + JSONArray arr = new JSONArray(); + AssertTrue(arr.PushInt64("18446744073709551615")); + + char buffer[32]; + arr.GetInt64(0, buffer, sizeof(buffer)); + AssertStrEq(buffer, "18446744073709551615"); + + delete arr; + } + TestEnd(); + + TestStart("Int64_Array_IndexOfLargeValues"); + { + JSONArray arr = new JSONArray(); + arr.PushInt64("11111111111111111"); + arr.PushInt64("22222222222222222"); + arr.PushInt64("33333333333333333"); + + AssertEq(arr.IndexOfInt64("22222222222222222"), 1); + AssertEq(arr.IndexOfInt64("99999999999999999"), -1); + + delete arr; + } + TestEnd(); + + // Test PtrAddInt64 + TestStart("Int64_Pointer_PtrAddInt64"); + { + JSONObject obj = new JSONObject(); + obj.PtrSetInt("/numbers/0", 1); + AssertTrue(obj.PtrAddInt64("/numbers/1", "7777777777777777")); + + char buffer[32]; + AssertTrue(obj.PtrGetInt64("/numbers/1", buffer, sizeof(buffer))); + AssertStrEq(buffer, "7777777777777777"); + + delete obj; + } + TestEnd(); + + TestStart("Int64_Pointer_PtrAddInt64_Negative"); + { + JSONObject obj = new JSONObject(); + obj.PtrSetInt("/data/0", 1); + AssertTrue(obj.PtrAddInt64("/data/1", "-8888888888888888")); + + char buffer[32]; + AssertTrue(obj.PtrGetInt64("/data/1", buffer, sizeof(buffer))); + AssertStrEq(buffer, "-8888888888888888"); + + delete obj; + } + TestEnd(); + + // Test PtrAddInt64 with large unsigned value + TestStart("Int64_Pointer_PtrAddInt64_LargeUnsigned"); + { + JSONObject obj = new JSONObject(); + obj.PtrSetInt("/bigvals/0", 1); + AssertTrue(obj.PtrAddInt64("/bigvals/1", "18446744073709551615")); + + char buffer[32]; + AssertTrue(obj.PtrGetInt64("/bigvals/1", buffer, sizeof(buffer))); + AssertStrEq(buffer, "18446744073709551615"); + + delete obj; + } + TestEnd(); + + // Test mixed signed/unsigned values + TestStart("Int64_Mixed_SignedUnsigned_Array"); + { + JSONArray arr = new JSONArray(); + arr.PushInt64("-9223372036854775808"); + arr.PushInt64("18446744073709551615"); + arr.PushInt64("0"); + arr.PushInt64("9223372036854775807"); + + AssertEq(arr.Length, 4); + + char buffer[32]; + arr.GetInt64(0, buffer, sizeof(buffer)); + AssertStrEq(buffer, "-9223372036854775808"); + + arr.GetInt64(1, buffer, sizeof(buffer)); + AssertStrEq(buffer, "18446744073709551615"); + + arr.GetInt64(2, buffer, sizeof(buffer)); + AssertStrEq(buffer, "0"); + + arr.GetInt64(3, buffer, sizeof(buffer)); + AssertStrEq(buffer, "9223372036854775807"); + + delete arr; + } + TestEnd(); + + TestStart("Int64_Mixed_SignedUnsigned_Object"); + { + JSONObject obj = new JSONObject(); + obj.SetInt64("min_int64", "-9223372036854775808"); + obj.SetInt64("max_int64", "9223372036854775807"); + obj.SetInt64("max_uint64", "18446744073709551615"); + + char buffer[32]; + + AssertTrue(obj.GetInt64("min_int64", buffer, sizeof(buffer))); + AssertStrEq(buffer, "-9223372036854775808"); + + AssertTrue(obj.GetInt64("max_int64", buffer, sizeof(buffer))); + AssertStrEq(buffer, "9223372036854775807"); + + AssertTrue(obj.GetInt64("max_uint64", buffer, sizeof(buffer))); + AssertStrEq(buffer, "18446744073709551615"); + + delete obj; + } + TestEnd(); + + // Test serialization with int64 + TestStart("Int64_Serialization_ToString"); + { + JSONObject obj = new JSONObject(); + obj.SetInt64("large_num", "9007199254740992"); + obj.SetInt64("negative_num", "-9007199254740992"); + + char json[256]; + int len = obj.ToString(json, sizeof(json)); + AssertTrue(len > 0); + + // Parse it back + JSONObject parsed = JSONObject.FromString(json); + AssertValidHandle(parsed); + + char buffer[32]; + AssertTrue(parsed.GetInt64("large_num", buffer, sizeof(buffer))); + AssertStrEq(buffer, "9007199254740992"); + + AssertTrue(parsed.GetInt64("negative_num", buffer, sizeof(buffer))); + AssertStrEq(buffer, "-9007199254740992"); + + delete obj; + delete parsed; + } + TestEnd(); + + TestStart("Int64_Serialization_Array"); + { + JSONArray arr = new JSONArray(); + arr.PushInt64("1234567890123456"); + arr.PushInt64("-9876543210987654"); + arr.PushInt64("18000000000000000000"); + + char json[256]; + int len = arr.ToString(json, sizeof(json)); + AssertTrue(len > 0); + + // Parse it back + JSONArray parsed = JSONArray.FromString(json); + AssertValidHandle(parsed); + AssertEq(parsed.Length, 3); + + char buffer[32]; + parsed.GetInt64(0, buffer, sizeof(buffer)); + AssertStrEq(buffer, "1234567890123456"); + + parsed.GetInt64(1, buffer, sizeof(buffer)); + AssertStrEq(buffer, "-9876543210987654"); + + parsed.GetInt64(2, buffer, sizeof(buffer)); + AssertStrEq(buffer, "18000000000000000000"); + + delete arr; + delete parsed; + } + TestEnd(); + + // Test boundary values + TestStart("Int64_Boundary_JustAboveInt32Max"); + { + JSON val = JSON.CreateInt64("2147483648"); + AssertValidHandle(val); + + char buffer[32]; + AssertTrue(val.GetInt64(buffer, sizeof(buffer))); + AssertStrEq(buffer, "2147483648"); + + delete val; + } + TestEnd(); + + TestStart("Int64_Boundary_JustBelowInt32Min"); + { + JSON val = JSON.CreateInt64("-2147483649"); + AssertValidHandle(val); + + char buffer[32]; + AssertTrue(val.GetInt64(buffer, sizeof(buffer))); + AssertStrEq(buffer, "-2147483649"); + + delete val; + } + TestEnd(); + + TestStart("Int64_Boundary_UInt32Max"); + { + JSON val = JSON.CreateInt64("4294967295"); + AssertValidHandle(val); + + char buffer[32]; + AssertTrue(val.GetInt64(buffer, sizeof(buffer))); + AssertStrEq(buffer, "4294967295"); + + delete val; + } + TestEnd(); + + TestStart("Int64_Boundary_JustAboveUInt32Max"); + { + JSON val = JSON.CreateInt64("4294967296"); + AssertValidHandle(val); + + char buffer[32]; + AssertTrue(val.GetInt64(buffer, sizeof(buffer))); + AssertStrEq(buffer, "4294967296"); + + delete val; + } + TestEnd(); + + // Test Int64 in nested structures + TestStart("Int64_Nested_ObjectInObject"); + { + JSONObject obj = new JSONObject(); + obj.PtrSetInt64("/outer/inner/deep", "5432109876543210"); + + char buffer[32]; + AssertTrue(obj.PtrGetInt64("/outer/inner/deep", buffer, sizeof(buffer))); + AssertStrEq(buffer, "5432109876543210"); + + delete obj; + } + TestEnd(); + + TestStart("Int64_Nested_ArrayInObject"); + { + JSONObject obj = new JSONObject(); + obj.PtrSetInt64("/data/values/0", "1111111111111111"); + obj.PtrAddInt64("/data/values/1", "2222222222222222"); + obj.PtrAddInt64("/data/values/2", "3333333333333333"); + + char buffer[32]; + AssertTrue(obj.PtrGetInt64("/data/values/0", buffer, sizeof(buffer))); + AssertStrEq(buffer, "1111111111111111"); + + AssertTrue(obj.PtrGetInt64("/data/values/1", buffer, sizeof(buffer))); + AssertStrEq(buffer, "2222222222222222"); + + AssertTrue(obj.PtrGetInt64("/data/values/2", buffer, sizeof(buffer))); + AssertStrEq(buffer, "3333333333333333"); + + delete obj; + } + TestEnd(); + + // Test DeepCopy with int64 + TestStart("Int64_DeepCopy_Object"); + { + JSONObject original = new JSONObject(); + original.SetInt64("bignum", "9999999999999999"); + original.SetInt64("negative", "-8888888888888888"); + + JSONObject target = new JSONObject(); + JSONObject copy = JSON.DeepCopy(target, original); + + AssertValidHandle(copy); + + char buffer[32]; + AssertTrue(copy.GetInt64("bignum", buffer, sizeof(buffer))); + AssertStrEq(buffer, "9999999999999999"); + + AssertTrue(copy.GetInt64("negative", buffer, sizeof(buffer))); + AssertStrEq(buffer, "-8888888888888888"); + + delete original; + delete target; + delete copy; + } + TestEnd(); + + TestStart("Int64_DeepCopy_Array"); + { + JSONArray original = new JSONArray(); + original.PushInt64("1111111111111111"); + original.PushInt64("-2222222222222222"); + original.PushInt64("18446744073709551615"); + + JSONArray target = new JSONArray(); + JSONArray copy = JSON.DeepCopy(target, original); + + AssertValidHandle(copy); + AssertEq(copy.Length, 3); + + char buffer[32]; + copy.GetInt64(0, buffer, sizeof(buffer)); + AssertStrEq(buffer, "1111111111111111"); + + copy.GetInt64(1, buffer, sizeof(buffer)); + AssertStrEq(buffer, "-2222222222222222"); + + copy.GetInt64(2, buffer, sizeof(buffer)); + AssertStrEq(buffer, "18446744073709551615"); + + delete original; + delete target; + delete copy; + } + TestEnd(); + + // Test array sorting with int64 values + TestStart("Int64_Array_Sort_Ascending"); + { + JSONArray arr = new JSONArray(); + arr.PushInt64("9999999999999999"); + arr.PushInt64("1111111111111111"); + arr.PushInt64("5555555555555555"); + arr.PushInt64("3333333333333333"); + + AssertTrue(arr.Sort(JSON_SORT_ASC)); + + char buffer[32]; + arr.GetInt64(0, buffer, sizeof(buffer)); + AssertStrEq(buffer, "1111111111111111"); + + arr.GetInt64(3, buffer, sizeof(buffer)); + AssertStrEq(buffer, "9999999999999999"); + + delete arr; + } + TestEnd(); + + TestStart("Int64_Array_Sort_Descending"); + { + JSONArray arr = new JSONArray(); + arr.PushInt64("1111111111111111"); + arr.PushInt64("5555555555555555"); + arr.PushInt64("3333333333333333"); + arr.PushInt64("9999999999999999"); + + AssertTrue(arr.Sort(JSON_SORT_DESC)); + + char buffer[32]; + arr.GetInt64(0, buffer, sizeof(buffer)); + AssertStrEq(buffer, "9999999999999999"); + + arr.GetInt64(3, buffer, sizeof(buffer)); + AssertStrEq(buffer, "1111111111111111"); + + delete arr; + } + TestEnd(); + + // Test int64 with type checks + TestStart("Int64_TypeChecks"); + { + JSON val = JSON.CreateInt64("9223372036854775807"); + + AssertTrue(val.IsNum); + AssertTrue(val.IsInt); + AssertFalse(val.IsFloat); + AssertFalse(val.IsStr); + AssertFalse(val.IsBool); + + delete val; + } + TestEnd(); + + TestStart("Int64_TypeChecks_Signed"); + { + JSON val = JSON.CreateInt64("-9223372036854775808"); + + AssertTrue(val.IsNum); + AssertTrue(val.IsInt); + AssertTrue(val.IsSint); + AssertFalse(val.IsUint); + + delete val; + } + TestEnd(); + + TestStart("Int64_TypeChecks_LargeUnsigned"); + { + JSON val = JSON.CreateInt64("18446744073709551615"); + + AssertTrue(val.IsNum); + AssertTrue(val.IsInt); + // Large unsigned value should be detected as unsigned integer + AssertTrue(val.IsUint); + AssertFalse(val.IsSint); + + delete val; + } + TestEnd(); +} + +// ============================================================================ +// 2.9 Edge Cases & Error Handling Tests +// ============================================================================ + +void Test_EdgeCases() +{ + PrintToServer("\n[Category] Edge Cases & Error Handling Tests"); + + // Test empty containers + TestStart("EdgeCase_EmptyObject"); + { + JSONObject obj = new JSONObject(); + AssertEq(obj.Size, 0); + AssertFalse(obj.HasKey("anything")); + + char buffer[64]; + int len = obj.ToString(buffer, sizeof(buffer)); + AssertTrue(len > 0); + + delete obj; + } + TestEnd(); + + TestStart("EdgeCase_EmptyArray"); + { + JSONArray arr = new JSONArray(); + AssertEq(arr.Length, 0); + delete arr; + } + TestEnd(); + + // Test nonexistent keys/indices + TestStart("EdgeCase_NonexistentKey"); + { + JSONObject obj = new JSONObject(); + obj.SetInt("exists", 1); + + AssertTrue(obj.HasKey("exists")); + AssertFalse(obj.HasKey("notexists")); + + delete obj; + } + TestEnd(); + + TestStart("EdgeCase_ArrayOutOfBounds"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(1); + + // Verify array bounds + AssertEq(arr.Length, 1); + AssertEq(arr.GetInt(0), 1); + + delete arr; + } + TestEnd(); + + // Test very long strings + TestStart("EdgeCase_LongString"); + { + char longStr[1024]; + for (int i = 0; i < sizeof(longStr) - 1; i++) + { + longStr[i] = 'A' + (i % 26); + } + longStr[sizeof(longStr) - 1] = '\0'; + + JSON val = JSON.CreateString(longStr); + AssertValidHandle(val); + + char retrieved[1024]; + AssertTrue(val.GetString(retrieved, sizeof(retrieved))); + AssertStrEq(retrieved, longStr); + + delete val; + } + TestEnd(); + + // Test deep nesting + TestStart("EdgeCase_DeepNesting"); + { + JSONObject obj = new JSONObject(); + + // Create deeply nested structure + obj.PtrSetInt("/a/b/c/d/e/f/g/h/i/j", 42); + AssertEq(obj.PtrGetInt("/a/b/c/d/e/f/g/h/i/j"), 42); + + delete obj; + } + TestEnd(); + + // Test large arrays + TestStart("EdgeCase_LargeArray"); + { + JSONArray arr = new JSONArray(); + + for (int i = 0; i < 1000; i++) + { + arr.PushInt(i); + } + + AssertEq(arr.Length, 1000); + AssertEq(arr.GetInt(0), 0); + AssertEq(arr.GetInt(999), 999); + + delete arr; + } + TestEnd(); + + // Test large objects + TestStart("EdgeCase_LargeObject"); + { + JSONObject obj = new JSONObject(); + + char key[32]; + for (int i = 0; i < 100; i++) + { + FormatEx(key, sizeof(key), "key_%d", i); + obj.SetInt(key, i); + } + + AssertEq(obj.Size, 100); + AssertEq(obj.GetInt("key_0"), 0); + AssertEq(obj.GetInt("key_99"), 99); + + delete obj; + } + TestEnd(); + + // Test int64 boundaries + TestStart("EdgeCase_Int64_MaxValue"); + { + JSON val = JSON.CreateInt64("9223372036854775807"); + AssertValidHandle(val); + + char buffer[32]; + val.GetInt64(buffer, sizeof(buffer)); + AssertStrEq(buffer, "9223372036854775807"); + + delete val; + } + TestEnd(); + + TestStart("EdgeCase_Int64_MinValue"); + { + JSON val = JSON.CreateInt64("-9223372036854775808"); + AssertValidHandle(val); + + char buffer[32]; + val.GetInt64(buffer, sizeof(buffer)); + AssertStrEq(buffer, "-9223372036854775808"); + + delete val; + } + TestEnd(); + + // Test special float values + TestStart("EdgeCase_Float_VerySmall"); + { + JSON val = JSON.CreateFloat(0.000001); + AssertValidHandle(val); + AssertFloatEq(val.GetFloat(), 0.000001); + delete val; + } + TestEnd(); + + TestStart("EdgeCase_Float_VeryLarge"); + { + JSON val = JSON.CreateFloat(999999.999999); + AssertValidHandle(val); + AssertFloatEq(val.GetFloat(), 999999.999999, "", 0.001); + delete val; + } + TestEnd(); + + // Test special characters in strings + TestStart("EdgeCase_SpecialCharacters"); + { + JSON val = JSON.CreateString("Line1\nLine2\tTabbed\"Quoted\""); + AssertValidHandle(val); + + char buffer[128]; + val.GetString(buffer, sizeof(buffer)); + Assert(StrContains(buffer, "Line1") != -1); + + delete val; + } + TestEnd(); + + // Test removing from empty array + TestStart("EdgeCase_RemoveFromEmptyArray"); + { + JSONArray arr = new JSONArray(); + AssertFalse(arr.RemoveFirst()); + AssertFalse(arr.RemoveLast()); + delete arr; + } + TestEnd(); + + // Test clearing already empty containers + TestStart("EdgeCase_ClearEmpty"); + { + JSONObject obj = new JSONObject(); + AssertTrue(obj.Clear()); + AssertEq(obj.Size, 0); + + JSONArray arr = new JSONArray(); + AssertTrue(arr.Clear()); + AssertEq(arr.Length, 0); + + delete obj; + delete arr; + } + TestEnd(); + + // Test IndexOf on empty array + TestStart("EdgeCase_IndexOfEmpty"); + { + JSONArray arr = new JSONArray(); + AssertEq(arr.IndexOfInt(42), -1); + AssertEq(arr.IndexOfString("test"), -1); + AssertEq(arr.IndexOfBool(true), -1); + delete arr; + } + TestEnd(); + + // Test pointer to nonexistent path + TestStart("EdgeCase_PointerNonexistentPath"); + { + JSONObject obj = new JSONObject(); + + // Note: PtrGet throws exception for nonexistent paths (expected behavior) + // Use PtrTryGet methods for safe access + int intVal; + AssertFalse(obj.PtrTryGetInt("/does/not/exist", intVal)); + + delete obj; + } + TestEnd(); + + // Test sorting empty containers + TestStart("EdgeCase_SortEmpty"); + { + JSONObject obj = new JSONObject(); + AssertTrue(obj.Sort()); + + JSONArray arr = new JSONArray(); + AssertTrue(arr.Sort()); + + delete obj; + delete arr; + } + TestEnd(); + + // Test single element operations + TestStart("EdgeCase_SingleElement"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(42); + + AssertTrue(arr.Sort()); + AssertEq(arr.GetInt(0), 42); + + AssertEq(arr.IndexOfInt(42), 0); + + JSON first = arr.First; + JSON last = arr.Last; + AssertEq(first.GetInt(), last.GetInt()); + + delete first; + delete last; + delete arr; + } + TestEnd(); +} \ No newline at end of file diff --git a/scripting/yyjson_test.sp b/scripting/yyjson_test.sp deleted file mode 100644 index 4b439d3..0000000 --- a/scripting/yyjson_test.sp +++ /dev/null @@ -1,566 +0,0 @@ -#include -#include - -public Plugin myinfo = -{ - name = "YYJSON Test Suite", - author = "ProjectSky", - description = "Test suite for YYJSON extension", - version = "1.0.2", - url = "https://github.com/ProjectSky/sm-ext-yyjson" -}; - -public void OnPluginStart() -{ - RegServerCmd("sm_yyjson_test", Command_RunTests, "Run YYJSON test suite"); -} - -Action Command_RunTests(int args) -{ - // Run all test cases - TestBasicOperations(); - TestArrayOperations(); - TestObjectOperations(); - TestSortOperations(); - TestSearchOperations(); - TestPointerOperations(); - TestIterationOperations(); - TestTypeOperations(); - TestFileOperations(); - TestImmutabilityOperations(); - TestPackOperations(); - TestFromStringsOperations(); - - PrintToServer("[YYJSON] All tests completed!"); - return Plugin_Handled; -} - -void TestBasicOperations() -{ - PrintToServer("[YYJSON] Testing basic operations..."); - - // Test creation and parsing - YYJSONObject obj = new YYJSONObject(); - obj.SetInt("int", 42); - obj.SetFloat("float", 3.14); - obj.SetBool("bool", true); - obj.SetString("string", "hello"); - obj.SetNull("null"); - - // Test serialization - char buffer[1024]; - obj.ToString(buffer, sizeof(buffer)); - PrintToServer("Serialized: %s", buffer); - - // Test type checking - PrintToServer("Type of 'int': %d", obj.Type); - PrintToServer("SubType of 'int': %d", obj.SubType); - PrintToServer("Is object: %d", obj.IsObject); - - // Test value existence - PrintToServer("Has 'int': %d", obj.HasKey("int")); - PrintToServer("Has 'nonexistent': %d", obj.HasKey("nonexistent")); - - // Test value retrieval - PrintToServer("Int value: %d", obj.GetInt("int")); - PrintToServer("Float value: %f", obj.GetFloat("float")); - PrintToServer("Bool value: %d", obj.GetBool("bool")); - - char strBuffer[64]; - obj.GetString("string", strBuffer, sizeof(strBuffer)); - PrintToServer("String value: %s", strBuffer); - - PrintToServer("Is 'null' null: %d", obj.IsNull("null")); - - // Test parsing - YYJSONObject parsed = YYJSON.Parse(buffer); - - // Test equality - PrintToServer("Objects are equal: %d", YYJSON.Equals(obj, parsed)); - - // Test deep copy - YYJSONObject copy = new YYJSONObject(); - YYJSONObject copyResult = YYJSON.DeepCopy(copy, obj); - PrintToServer("Copy equals original: %d", YYJSON.Equals(copyResult, obj)); - - // Test size and read size - PrintToServer("Object size: %d", obj.Size); - PrintToServer("Read size: %d", obj.ReadSize); - - // Test type description - char typeDesc[64]; - YYJSON.GetTypeDesc(obj, typeDesc, sizeof(typeDesc)); - PrintToServer("Type description: %s", typeDesc); - - delete obj; - delete parsed; - delete copy; - delete copyResult; -} - -void TestArrayOperations() -{ - PrintToServer("[YYJSON] Testing array operations..."); - - YYJSONArray arr = new YYJSONArray(); - - // Test push operations - arr.PushInt(1); - arr.PushFloat(2.5); - arr.PushBool(true); - arr.PushString("test"); - arr.PushNull(); - - PrintToServer("Array after push operations:"); - PrintJson(arr); - - // Test get operations - PrintToServer("First element: %d", arr.GetInt(0)); - PrintToServer("Second element: %f", arr.GetFloat(1)); - PrintToServer("Third element: %d", arr.GetBool(2)); - - char strBuffer[64]; - arr.GetString(3, strBuffer, sizeof(strBuffer)); - PrintToServer("Fourth element: %s", strBuffer); - - PrintToServer("Fifth element is null: %d", arr.IsNull(4)); - - YYJSON first = arr.First; - YYJSON last = arr.Last; - - // Test array properties - PrintToServer("Array length: %d", arr.Length); - PrintToServer("First value: %x", first); - PrintToServer("Last value: %x", last); - - // Test set operations - arr.SetInt(0, 100); - arr.SetFloat(1, 3.14); - arr.SetBool(2, false); - arr.SetString(3, "modified"); - arr.SetNull(4); - - PrintToServer("Array after set operations:"); - PrintJson(arr); - - // Test remove operations - arr.RemoveFirst(); - PrintToServer("After RemoveFirst:"); - PrintJson(arr); - - arr.RemoveLast(); - PrintToServer("After RemoveLast:"); - PrintJson(arr); - - arr.Remove(1); - PrintToServer("After Remove(1):"); - PrintJson(arr); - - arr.RemoveRange(0, 1); - PrintToServer("After RemoveRange(0, 1):"); - PrintJson(arr); - - arr.Clear(); - PrintToServer("Array length after Clear: %d", arr.Length); - - delete arr; - delete first; - delete last; -} - -void TestObjectOperations() -{ - PrintToServer("[YYJSON] Testing object operations..."); - - YYJSONObject obj = new YYJSONObject(); - - // Test set operations - obj.SetInt("int", 123); - obj.SetFloat("float", 3.14); - obj.SetBool("bool", true); - obj.SetString("string", "test"); - obj.SetNull("null"); - - PrintToServer("Object after set operations:"); - PrintJson(obj); - - // Test get operations - PrintToServer("Int value: %d", obj.GetInt("int")); - PrintToServer("Float value: %f", obj.GetFloat("float")); - PrintToServer("Bool value: %d", obj.GetBool("bool")); - - char strBuffer[64]; - obj.GetString("string", strBuffer, sizeof(strBuffer)); - PrintToServer("String value: %s", strBuffer); - - PrintToServer("Is null value null: %d", obj.IsNull("null")); - - // Test key operations - char key[64]; - for (int i = 0; i < obj.Size; i++) - { - obj.GetKey(i, key, sizeof(key)); - PrintToServer("Key at %d: %s", i, key); - - YYJSON value = obj.GetValueAt(i); - PrintToServer("Value type at %d: %d", i, value.Type); - delete value; - } - - // Test rename key - obj.RenameKey("int", "number"); - PrintToServer("After renaming 'int' to 'number':"); - PrintJson(obj); - - // Test remove operations - obj.Remove("number"); - PrintToServer("After removing 'number':"); - PrintJson(obj); - - obj.Clear(); - PrintToServer("Object size after Clear: %d", obj.Size); - - delete obj; -} - -void TestSortOperations() -{ - PrintToServer("[YYJSON] Testing sort operations..."); - - // Test array sorting - YYJSONArray arr = YYJSON.Parse("[3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]", .is_mutable_doc = true); - - PrintToServer("Original array:"); - PrintJson(arr); - - arr.Sort(); - PrintToServer("After ascending sort:"); - PrintJson(arr); - - arr.Sort(YYJSON_SORT_DESC); - PrintToServer("After descending sort:"); - PrintJson(arr); - - arr.Sort(YYJSON_SORT_RANDOM); - PrintToServer("After random sort:"); - PrintJson(arr); - - // Test mixed type array sorting - YYJSONArray mixed = YYJSON.Parse("[true, 42, \"hello\", 1.23, false, \"world\"]", .is_mutable_doc = true); - PrintToServer("Original mixed array:"); - PrintJson(mixed); - - mixed.Sort(); - PrintToServer("After sorting mixed array:"); - PrintJson(mixed); - - // Test object sorting - YYJSONObject obj = YYJSON.Parse("{\"zebra\": 1, \"alpha\": 2, \"beta\": 3}", .is_mutable_doc = true); - PrintToServer("Original object:"); - PrintJson(obj); - - obj.Sort(); - PrintToServer("After ascending sort:"); - PrintJson(obj); - - obj.Sort(YYJSON_SORT_DESC); - PrintToServer("After descending sort:"); - PrintJson(obj); - - obj.Sort(YYJSON_SORT_RANDOM); - PrintToServer("After random sort:"); - PrintJson(obj); - - delete arr; - delete mixed; - delete obj; -} - -void TestSearchOperations() -{ - PrintToServer("[YYJSON] Testing search operations..."); - - YYJSONArray arr = YYJSON.Parse("[42, true, \"hello\", 3.14, \"world\", false, 42]"); - PrintToServer("Test array:"); - PrintJson(arr); - - // Test all search methods - PrintToServer("Search results:"); - PrintToServer("IndexOfInt(42): %d", arr.IndexOfInt(42)); - PrintToServer("IndexOfInt(999): %d", arr.IndexOfInt(999)); - - PrintToServer("IndexOfBool(true): %d", arr.IndexOfBool(true)); - PrintToServer("IndexOfBool(false): %d", arr.IndexOfBool(false)); - - PrintToServer("IndexOfString(\"hello\"): %d", arr.IndexOfString("hello")); - PrintToServer("IndexOfString(\"missing\"): %d", arr.IndexOfString("missing")); - - PrintToServer("IndexOfFloat(3.14): %d", arr.IndexOfFloat(3.14)); - PrintToServer("IndexOfFloat(2.718): %d", arr.IndexOfFloat(2.718)); - - delete arr; -} - -void TestPointerOperations() -{ - PrintToServer("[YYJSON] Testing JSON pointer operations..."); - - YYJSONObject obj = new YYJSONObject(); - - // Test setting nested values - obj.PtrSetInt("/a/b/c", 1); - obj.PtrSetString("/a/b/name", "test"); - obj.PtrSetBool("/a/flag", true); - obj.PtrSetFloat("/a/b/pi", 3.14); - obj.PtrSetNull("/a/b/empty"); - - PrintToServer("After setting values:"); - PrintJson(obj); - - // Test getting values - PrintToServer("Pointer get operations:"); - PrintToServer("/a/b/c: %d", obj.PtrGetInt("/a/b/c")); - - char strBuffer[64]; - obj.PtrGetString("/a/b/name", strBuffer, sizeof(strBuffer)); - PrintToServer("/a/b/name: %s", strBuffer); - - PrintToServer("/a/flag: %d", obj.PtrGetBool("/a/flag")); - PrintToServer("/a/b/pi: %f", obj.PtrGetFloat("/a/b/pi")); - PrintToServer("/a/b/empty is null: %d", obj.PtrGetIsNull("/a/b/empty")); - - // Test adding values - obj.PtrAddInt("/a/b/numbers/0", 1); - obj.PtrAddInt("/a/b/numbers/1", 2); - obj.PtrAddString("/a/b/strings", "append"); - - PrintToServer("After adding values:"); - PrintJson(obj); - - // Test length - PrintToServer("Length of /a/b/numbers: %d", obj.PtrGetLength("/a/b/numbers")); - - // Test removing values - obj.PtrRemove("/a/b/c"); - PrintToServer("After removing /a/b/c:"); - PrintJson(obj); - - delete obj; -} - -void TestIterationOperations() -{ - PrintToServer("[YYJSON] Testing iteration operations..."); - - // Test object iteration - YYJSONObject obj = YYJSON.Parse("{\"a\": 1, \"b\": 2, \"c\": 3}"); - char key[64]; - YYJSON value; - - while (obj.ForeachObject(key, sizeof(key), value)) - { - PrintToServer("Key: %s", key); - delete value; - } - - // Test array iteration - YYJSONArray arr = YYJSON.Parse("[1, 2, 3, 4, 5]"); - int index; - - while (arr.ForeachArray(index, value)) - { - PrintToServer("Index: %d", index); - delete value; - } - - delete obj; - delete arr; -} - -void TestTypeOperations() -{ - PrintToServer("[YYJSON] Testing type operations..."); - - // Test value creation - YYJSON boolVal = YYJSON.CreateBool(true); - YYJSON intVal = YYJSON.CreateInt(42); - YYJSON floatVal = YYJSON.CreateFloat(3.14); - YYJSON strVal = YYJSON.CreateString("test"); - YYJSON nullVal = YYJSON.CreateNull(); - - // Test value types - PrintToServer("Value types:"); - PrintToServer("Bool type: %d", boolVal.Type); - PrintToServer("Int type: %d", intVal.Type); - PrintToServer("Float type: %d", floatVal.Type); - PrintToServer("String type: %d", strVal.Type); - PrintToServer("Null type: %d", nullVal.Type); - - // Test value retrieval - PrintToServer("Value contents:"); - PrintToServer("Bool value: %d", YYJSON.GetBool(boolVal)); - PrintToServer("Int value: %d", YYJSON.GetInt(intVal)); - PrintToServer("Float value: %f", YYJSON.GetFloat(floatVal)); - - char strBuffer[64]; - YYJSON.GetString(strVal, strBuffer, sizeof(strBuffer)); - PrintToServer("String value: %s", strBuffer); - - delete boolVal; - delete intVal; - delete floatVal; - delete strVal; - delete nullVal; -} - -void TestFileOperations() -{ - PrintToServer("[YYJSON] Testing file operations..."); - - // Create test data - YYJSONObject obj = new YYJSONObject(); - obj.SetInt("id", 1); - obj.SetString("name", "test"); - obj.SetBool("active", true); - - // Test file writing - PrintToServer("Writing to file..."); - obj.ToFile("test.json", YYJSON_WRITE_PRETTY_TWO_SPACES); - - // Test file reading - PrintToServer("Reading from file..."); - YYJSONObject loaded = YYJSON.Parse("test.json", true); - - PrintToServer("File content:"); - PrintJson(loaded); - delete loaded; - - delete obj; -} - -void TestImmutabilityOperations() -{ - PrintToServer("[YYJSON] Testing immutability operations..."); - - // Test immutable document creation - YYJSONObject immutable = YYJSON.Parse("{\"key\": 123, \"str\": \"test\"}"); - PrintToServer("Created immutable document:"); - PrintJson(immutable); - - // Test property checks - PrintToServer("Is mutable: %d", immutable.IsMutable); - PrintToServer("Is immutable: %d", immutable.IsImmutable); - - // Test read operations (should succeed) - PrintToServer("Read operations on immutable document:"); - PrintToServer("Int value: %d", immutable.GetInt("key")); - char buffer[64]; - immutable.GetString("str", buffer, sizeof(buffer)); - PrintToServer("String value: %s", buffer); - - // Test conversion to mutable - YYJSONObject mutable = immutable.ToMutable(); - PrintToServer("\nConverted to mutable document:"); - PrintToServer("Is mutable: %d", mutable.IsMutable); - PrintToServer("Is immutable: %d", mutable.IsImmutable); - - // Now modifications should work - mutable.SetInt("key", 456) - PrintToServer("Successfully modified mutable document:"); - PrintJson(mutable); - - // Test conversion back to immutable - YYJSONObject backToImmutable = mutable.ToImmutable(); - PrintToServer("\nConverted back to immutable:"); - PrintToServer("Is mutable: %d", backToImmutable.IsMutable); - PrintToServer("Is immutable: %d", backToImmutable.IsImmutable); - delete backToImmutable; - delete mutable; - delete immutable; - - // Test file operations with immutability - PrintToServer("\nTesting file operations with immutability..."); - - // Create and write a mutable document - YYJSONObject writeObj = new YYJSONObject(); - writeObj.SetInt("test", 123); - writeObj.ToFile("test_immutable.json"); - delete writeObj; - - // Read as immutable - YYJSONObject readImmutable = YYJSON.Parse("test_immutable.json", true); - PrintToServer("Read as immutable document:"); - PrintJson(readImmutable); - PrintToServer("Is mutable: %d", readImmutable.IsMutable); - PrintToServer("Is immutable: %d", readImmutable.IsImmutable); - delete readImmutable; -} - -void TestPackOperations() -{ - PrintToServer("[YYJSON] Testing pack operations..."); - - // Test basic pack operation with different types - YYJSON packed = YYJSON.Pack("{s:s,s:i,s:f,s:b,s:n}", - "name", "test", - "age", 25, - "height", 1.75, - "active", true, - "extra" - ); - - PrintToServer("Packed JSON:"); - PrintJson(packed); - - // Test nested object packing - YYJSON nested = YYJSON.Pack("{s:{s:s,s:[i,i,i]}}", - "user", - "name", "test", - "scores", 85, 90, 95 - ); - - PrintToServer("Nested packed JSON:"); - PrintJson(nested); - - // Test array packing with mixed types - YYJSON array = YYJSON.Pack("[s,i,f,b,n]", - "test", 42, 3.14, true - ); - - PrintToServer("Array packed JSON:"); - PrintJson(array); - - delete packed; - delete nested; - delete array; -} - -void TestFromStringsOperations() -{ - PrintToServer("[YYJSON] Testing FromStrings operations..."); - - // Test object creation from key-value string arrays - char pairs[][] = {"name", "test", "type", "demo", "version", "1.0.0"}; - - YYJSONObject obj = YYJSONObject.FromStrings(pairs, sizeof(pairs)); - PrintToServer("Object from strings:"); - PrintJson(obj); - - // Test array creation from string array - char items[][] = {"apple", "banana", "orange", "grape"}; - YYJSONArray arr = YYJSONArray.FromStrings(items, sizeof(items)); - PrintToServer("Array from strings:"); - PrintJson(arr); - - delete obj; - delete arr; -} - -// Helper function to print json contents -void PrintJson(YYJSON data) -{ - int len = data.GetSerializedSize(YYJSON_WRITE_PRETTY_TWO_SPACES); - char[] buffer = new char[len]; - data.ToString(buffer, len, YYJSON_WRITE_PRETTY_TWO_SPACES); - PrintToServer("%s", buffer); -} \ No newline at end of file diff --git a/src/YYJSONManager.cpp b/src/JsonManager.cpp old mode 100644 new mode 100755 similarity index 57% rename from src/YYJSONManager.cpp rename to src/JsonManager.cpp index f3b1fc9..2d03a9e --- a/src/YYJSONManager.cpp +++ b/src/JsonManager.cpp @@ -1,31 +1,47 @@ -#include "YYJSONManager.h" +#include "JsonManager.h" #include "extension.h" -std::unique_ptr YYJSONManager::CreateWrapper() { - return std::make_unique(); +static inline void ReadInt64FromVal(yyjson_val* val, std::variant* out_value) { + if (yyjson_is_uint(val)) { + *out_value = yyjson_get_uint(val); + } else { + *out_value = yyjson_get_sint(val); + } +} + +static inline void ReadInt64FromMutVal(yyjson_mut_val* val, std::variant* out_value) { + if (yyjson_mut_is_uint(val)) { + *out_value = yyjson_mut_get_uint(val); + } else { + *out_value = yyjson_mut_get_sint(val); + } } -std::shared_ptr YYJSONManager::WrapDocument(yyjson_mut_doc* doc) { +std::unique_ptr JsonManager::CreateWrapper() { + return std::make_unique(); +} + +std::shared_ptr JsonManager::WrapDocument(yyjson_mut_doc* doc) { return std::shared_ptr(doc, [](yyjson_mut_doc*){}); } -std::shared_ptr YYJSONManager::CopyDocument(yyjson_doc* doc) { +std::shared_ptr JsonManager::CopyDocument(yyjson_doc* doc) { return WrapDocument(yyjson_doc_mut_copy(doc, nullptr)); } -std::shared_ptr YYJSONManager::CreateDocument() { +std::shared_ptr JsonManager::CreateDocument() { return WrapDocument(yyjson_mut_doc_new(nullptr)); } -std::shared_ptr YYJSONManager::WrapImmutableDocument(yyjson_doc* doc) { +std::shared_ptr JsonManager::WrapImmutableDocument(yyjson_doc* doc) { return std::shared_ptr(doc, [](yyjson_doc*){}); } -YYJSONManager::YYJSONManager(): m_randomGenerator(m_randomDevice()) {} +JsonManager::JsonManager(): m_randomGenerator(m_randomDevice()) {} -YYJSONManager::~YYJSONManager() {} +JsonManager::~JsonManager() {} -YYJSONValue* YYJSONManager::ParseJSON(const char* json_str, bool is_file, bool is_mutable, +JsonValue* JsonManager::ParseJSON(const char* json_str, bool is_file, bool is_mutable, yyjson_read_flag read_flg, char* error, size_t error_size) { if (!json_str) { @@ -37,7 +53,7 @@ YYJSONValue* YYJSONManager::ParseJSON(const char* json_str, bool is_file, bool i yyjson_read_err readError; yyjson_doc* idoc; - auto pYYJSONValue = CreateWrapper(); + auto pJSONValue = CreateWrapper(); if (is_file) { char realpath[PLATFORM_MAX_PATH]; @@ -63,21 +79,21 @@ YYJSONValue* YYJSONManager::ParseJSON(const char* json_str, bool is_file, bool i return nullptr; } - pYYJSONValue->m_readSize = yyjson_doc_get_read_size(idoc); + pJSONValue->m_readSize = yyjson_doc_get_read_size(idoc); if (is_mutable) { - pYYJSONValue->m_pDocument_mut = CopyDocument(idoc); - pYYJSONValue->m_pVal_mut = yyjson_mut_doc_get_root(pYYJSONValue->m_pDocument_mut.get()); + pJSONValue->m_pDocument_mut = CopyDocument(idoc); + pJSONValue->m_pVal_mut = yyjson_mut_doc_get_root(pJSONValue->m_pDocument_mut.get()); yyjson_doc_free(idoc); } else { - pYYJSONValue->m_pDocument = WrapImmutableDocument(idoc); - pYYJSONValue->m_pVal = yyjson_doc_get_root(idoc); + pJSONValue->m_pDocument = WrapImmutableDocument(idoc); + pJSONValue->m_pVal = yyjson_doc_get_root(idoc); } - return pYYJSONValue.release(); + return pJSONValue.release(); } -bool YYJSONManager::WriteToString(YYJSONValue* handle, char* buffer, size_t buffer_size, +bool JsonManager::WriteToString(JsonValue* handle, char* buffer, size_t buffer_size, yyjson_write_flag write_flg, size_t* out_size) { if (!handle || !buffer || buffer_size == 0) { @@ -114,7 +130,7 @@ bool YYJSONManager::WriteToString(YYJSONValue* handle, char* buffer, size_t buff return true; } -bool YYJSONManager::WriteToFile(YYJSONValue* handle, const char* path, yyjson_write_flag write_flg, +bool JsonManager::WriteToFile(JsonValue* handle, const char* path, yyjson_write_flag write_flg, char* error, size_t error_size) { if (!handle || !path) { @@ -143,7 +159,7 @@ bool YYJSONManager::WriteToFile(YYJSONValue* handle, const char* path, yyjson_wr return is_success; } -bool YYJSONManager::Equals(YYJSONValue* handle1, YYJSONValue* handle2) +bool JsonManager::Equals(JsonValue* handle1, JsonValue* handle2) { if (!handle1 || !handle2) { return false; @@ -171,8 +187,8 @@ bool YYJSONManager::Equals(YYJSONValue* handle1, YYJSONValue* handle2) return yyjson_mut_equals(val1_mut, val2_mut); } - YYJSONValue* immutable = handle1->IsMutable() ? handle2 : handle1; - YYJSONValue* mutable_doc = handle1->IsMutable() ? handle1 : handle2; + JsonValue* immutable = handle1->IsMutable() ? handle2 : handle1; + JsonValue* mutable_doc = handle1->IsMutable() ? handle1 : handle2; auto doc_mut = CopyDocument(immutable->m_pDocument.get()); if (!doc_mut) { @@ -187,30 +203,43 @@ bool YYJSONManager::Equals(YYJSONValue* handle1, YYJSONValue* handle2) return yyjson_mut_equals(mutable_doc->m_pVal_mut, val_mut); } -YYJSONValue* YYJSONManager::DeepCopy(YYJSONValue* targetDoc, YYJSONValue* sourceValue) +bool JsonManager::EqualsStr(JsonValue* handle, const char* str) +{ + if (!handle || !str) { + return false; + } + + if (handle->IsMutable()) { + return yyjson_mut_equals_str(handle->m_pVal_mut, str); + } else { + return yyjson_equals_str(handle->m_pVal, str); + } +} + +JsonValue* JsonManager::DeepCopy(JsonValue* targetDoc, JsonValue* sourceValue) { if (!targetDoc || !sourceValue) { return nullptr; } - auto pYYJSONValue = CreateWrapper(); + auto pJSONValue = CreateWrapper(); if (targetDoc->IsMutable()) { - pYYJSONValue->m_pDocument_mut = CreateDocument(); + pJSONValue->m_pDocument_mut = CreateDocument(); yyjson_mut_val* val_copy = nullptr; if (sourceValue->IsMutable()) { - val_copy = yyjson_mut_val_mut_copy(pYYJSONValue->m_pDocument_mut.get(), sourceValue->m_pVal_mut); + val_copy = yyjson_mut_val_mut_copy(pJSONValue->m_pDocument_mut.get(), sourceValue->m_pVal_mut); } else { - val_copy = yyjson_val_mut_copy(pYYJSONValue->m_pDocument_mut.get(), sourceValue->m_pVal); + val_copy = yyjson_val_mut_copy(pJSONValue->m_pDocument_mut.get(), sourceValue->m_pVal); } if (!val_copy) { return nullptr; } - yyjson_mut_doc_set_root(pYYJSONValue->m_pDocument_mut.get(), val_copy); - pYYJSONValue->m_pVal_mut = val_copy; + yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut.get(), val_copy); + pJSONValue->m_pVal_mut = val_copy; } else { yyjson_mut_doc* temp_doc = yyjson_mut_doc_new(nullptr); if (!temp_doc) { @@ -238,14 +267,14 @@ YYJSONValue* YYJSONManager::DeepCopy(YYJSONValue* targetDoc, YYJSONValue* source return nullptr; } - pYYJSONValue->m_pDocument = WrapImmutableDocument(doc); - pYYJSONValue->m_pVal = yyjson_doc_get_root(doc); + pJSONValue->m_pDocument = WrapImmutableDocument(doc); + pJSONValue->m_pVal = yyjson_doc_get_root(doc); } - return pYYJSONValue.release(); + return pJSONValue.release(); } -const char* YYJSONManager::GetTypeDesc(YYJSONValue* handle) +const char* JsonManager::GetTypeDesc(JsonValue* handle) { if (!handle) { return "invalid"; @@ -258,7 +287,7 @@ const char* YYJSONManager::GetTypeDesc(YYJSONValue* handle) } } -size_t YYJSONManager::GetSerializedSize(YYJSONValue* handle, yyjson_write_flag write_flg) +size_t JsonManager::GetSerializedSize(JsonValue* handle, yyjson_write_flag write_flg) { if (!handle) { return 0; @@ -281,34 +310,34 @@ size_t YYJSONManager::GetSerializedSize(YYJSONValue* handle, yyjson_write_flag w return 0; } -YYJSONValue* YYJSONManager::ToMutable(YYJSONValue* handle) +JsonValue* JsonManager::ToMutable(JsonValue* handle) { if (!handle || handle->IsMutable()) { return nullptr; } - auto pYYJSONValue = CreateWrapper(); - pYYJSONValue->m_pDocument_mut = CopyDocument(handle->m_pDocument.get()); - pYYJSONValue->m_pVal_mut = yyjson_mut_doc_get_root(pYYJSONValue->m_pDocument_mut.get()); + auto pJSONValue = CreateWrapper(); + pJSONValue->m_pDocument_mut = CopyDocument(handle->m_pDocument.get()); + pJSONValue->m_pVal_mut = yyjson_mut_doc_get_root(pJSONValue->m_pDocument_mut.get()); - return pYYJSONValue.release(); + return pJSONValue.release(); } -YYJSONValue* YYJSONManager::ToImmutable(YYJSONValue* handle) +JsonValue* JsonManager::ToImmutable(JsonValue* handle) { if (!handle || !handle->IsMutable()) { return nullptr; } - auto pYYJSONValue = CreateWrapper(); + auto pJSONValue = CreateWrapper(); yyjson_doc* mdoc = yyjson_mut_doc_imut_copy(handle->m_pDocument_mut.get(), nullptr); - pYYJSONValue->m_pDocument = WrapImmutableDocument(mdoc); - pYYJSONValue->m_pVal = yyjson_doc_get_root(pYYJSONValue->m_pDocument.get()); + pJSONValue->m_pDocument = WrapImmutableDocument(mdoc); + pJSONValue->m_pVal = yyjson_doc_get_root(pJSONValue->m_pDocument.get()); - return pYYJSONValue.release(); + return pJSONValue.release(); } -yyjson_type YYJSONManager::GetType(YYJSONValue* handle) +yyjson_type JsonManager::GetType(JsonValue* handle) { if (!handle) { return YYJSON_TYPE_NONE; @@ -321,7 +350,7 @@ yyjson_type YYJSONManager::GetType(YYJSONValue* handle) } } -yyjson_subtype YYJSONManager::GetSubtype(YYJSONValue* handle) +yyjson_subtype JsonManager::GetSubtype(JsonValue* handle) { if (!handle) { return YYJSON_SUBTYPE_NONE; @@ -334,7 +363,7 @@ yyjson_subtype YYJSONManager::GetSubtype(YYJSONValue* handle) } } -bool YYJSONManager::IsArray(YYJSONValue* handle) +bool JsonManager::IsArray(JsonValue* handle) { if (!handle) { return false; @@ -347,7 +376,7 @@ bool YYJSONManager::IsArray(YYJSONValue* handle) } } -bool YYJSONManager::IsObject(YYJSONValue* handle) +bool JsonManager::IsObject(JsonValue* handle) { if (!handle) { return false; @@ -360,7 +389,7 @@ bool YYJSONManager::IsObject(YYJSONValue* handle) } } -bool YYJSONManager::IsInt(YYJSONValue* handle) +bool JsonManager::IsInt(JsonValue* handle) { if (!handle) { return false; @@ -373,7 +402,7 @@ bool YYJSONManager::IsInt(YYJSONValue* handle) } } -bool YYJSONManager::IsUint(YYJSONValue* handle) +bool JsonManager::IsUint(JsonValue* handle) { if (!handle) { return false; @@ -386,7 +415,7 @@ bool YYJSONManager::IsUint(YYJSONValue* handle) } } -bool YYJSONManager::IsSint(YYJSONValue* handle) +bool JsonManager::IsSint(JsonValue* handle) { if (!handle) { return false; @@ -399,7 +428,7 @@ bool YYJSONManager::IsSint(YYJSONValue* handle) } } -bool YYJSONManager::IsNum(YYJSONValue* handle) +bool JsonManager::IsNum(JsonValue* handle) { if (!handle) { return false; @@ -412,7 +441,7 @@ bool YYJSONManager::IsNum(YYJSONValue* handle) } } -bool YYJSONManager::IsBool(YYJSONValue* handle) +bool JsonManager::IsBool(JsonValue* handle) { if (!handle) { return false; @@ -425,7 +454,7 @@ bool YYJSONManager::IsBool(YYJSONValue* handle) } } -bool YYJSONManager::IsTrue(YYJSONValue* handle) +bool JsonManager::IsTrue(JsonValue* handle) { if (!handle) { return false; @@ -438,7 +467,7 @@ bool YYJSONManager::IsTrue(YYJSONValue* handle) } } -bool YYJSONManager::IsFalse(YYJSONValue* handle) +bool JsonManager::IsFalse(JsonValue* handle) { if (!handle) { return false; @@ -451,7 +480,7 @@ bool YYJSONManager::IsFalse(YYJSONValue* handle) } } -bool YYJSONManager::IsFloat(YYJSONValue* handle) +bool JsonManager::IsFloat(JsonValue* handle) { if (!handle) { return false; @@ -464,7 +493,7 @@ bool YYJSONManager::IsFloat(YYJSONValue* handle) } } -bool YYJSONManager::IsStr(YYJSONValue* handle) +bool JsonManager::IsStr(JsonValue* handle) { if (!handle) { return false; @@ -477,7 +506,7 @@ bool YYJSONManager::IsStr(YYJSONValue* handle) } } -bool YYJSONManager::IsNull(YYJSONValue* handle) +bool JsonManager::IsNull(JsonValue* handle) { if (!handle) { return false; @@ -490,7 +519,7 @@ bool YYJSONManager::IsNull(YYJSONValue* handle) } } -bool YYJSONManager::IsCtn(YYJSONValue* handle) +bool JsonManager::IsCtn(JsonValue* handle) { if (!handle) { return false; @@ -503,7 +532,7 @@ bool YYJSONManager::IsCtn(YYJSONValue* handle) } } -bool YYJSONManager::IsMutable(YYJSONValue* handle) +bool JsonManager::IsMutable(JsonValue* handle) { if (!handle) { return false; @@ -512,7 +541,7 @@ bool YYJSONManager::IsMutable(YYJSONValue* handle) return handle->IsMutable(); } -bool YYJSONManager::IsImmutable(YYJSONValue* handle) +bool JsonManager::IsImmutable(JsonValue* handle) { if (!handle) { return false; @@ -521,13 +550,12 @@ bool YYJSONManager::IsImmutable(YYJSONValue* handle) return handle->IsImmutable(); } -size_t YYJSONManager::GetReadSize(YYJSONValue* handle) +size_t JsonManager::GetReadSize(JsonValue* handle) { if (!handle) { return 0; } - // this not happen in normal case, but it's possible if the document is not from parsing. if (handle->m_readSize == 0) { return 0; } @@ -535,39 +563,39 @@ size_t YYJSONManager::GetReadSize(YYJSONValue* handle) return handle->m_readSize + 1; } -YYJSONValue* YYJSONManager::ObjectInit() +JsonValue* JsonManager::ObjectInit() { - auto pYYJSONValue = CreateWrapper(); - pYYJSONValue->m_pDocument_mut = CreateDocument(); - pYYJSONValue->m_pVal_mut = yyjson_mut_obj(pYYJSONValue->m_pDocument_mut.get()); - yyjson_mut_doc_set_root(pYYJSONValue->m_pDocument_mut.get(), pYYJSONValue->m_pVal_mut); + auto pJSONValue = CreateWrapper(); + pJSONValue->m_pDocument_mut = CreateDocument(); + pJSONValue->m_pVal_mut = yyjson_mut_obj(pJSONValue->m_pDocument_mut.get()); + yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut.get(), pJSONValue->m_pVal_mut); - return pYYJSONValue.release(); + return pJSONValue.release(); } -YYJSONValue* YYJSONManager::ObjectInitWithStrings(const char** pairs, size_t count) +JsonValue* JsonManager::ObjectInitWithStrings(const char** pairs, size_t count) { if (!pairs || count == 0) { return nullptr; } - auto pYYJSONValue = CreateWrapper(); - pYYJSONValue->m_pDocument_mut = CreateDocument(); + auto pJSONValue = CreateWrapper(); + pJSONValue->m_pDocument_mut = CreateDocument(); - pYYJSONValue->m_pVal_mut = yyjson_mut_obj_with_kv( - pYYJSONValue->m_pDocument_mut.get(), + pJSONValue->m_pVal_mut = yyjson_mut_obj_with_kv( + pJSONValue->m_pDocument_mut.get(), pairs, count ); - if (!pYYJSONValue->m_pVal_mut) { + if (!pJSONValue->m_pVal_mut) { return nullptr; } - return pYYJSONValue.release(); + return pJSONValue.release(); } -YYJSONValue* YYJSONManager::ObjectParseString(const char* str, yyjson_read_flag read_flg, +JsonValue* JsonManager::ObjectParseString(const char* str, yyjson_read_flag read_flg, char* error, size_t error_size) { if (!str) { @@ -577,7 +605,7 @@ YYJSONValue* YYJSONManager::ObjectParseString(const char* str, yyjson_read_flag return nullptr; } - auto pYYJSONValue = CreateWrapper(); + auto pJSONValue = CreateWrapper(); yyjson_read_err readError; yyjson_doc* idoc = yyjson_read_opts(const_cast(str), strlen(str), read_flg, nullptr, &readError); @@ -603,14 +631,14 @@ YYJSONValue* YYJSONManager::ObjectParseString(const char* str, yyjson_read_flag return nullptr; } - pYYJSONValue->m_readSize = yyjson_doc_get_read_size(idoc); - pYYJSONValue->m_pDocument = WrapImmutableDocument(idoc); - pYYJSONValue->m_pVal = root; + pJSONValue->m_readSize = yyjson_doc_get_read_size(idoc); + pJSONValue->m_pDocument = WrapImmutableDocument(idoc); + pJSONValue->m_pVal = root; - return pYYJSONValue.release(); + return pJSONValue.release(); } -YYJSONValue* YYJSONManager::ObjectParseFile(const char* path, yyjson_read_flag read_flg, +JsonValue* JsonManager::ObjectParseFile(const char* path, yyjson_read_flag read_flg, char* error, size_t error_size) { if (!path) { @@ -622,7 +650,7 @@ YYJSONValue* YYJSONManager::ObjectParseFile(const char* path, yyjson_read_flag r char realpath[PLATFORM_MAX_PATH]; smutils->BuildPath(Path_Game, realpath, sizeof(realpath), "%s", path); - auto pYYJSONValue = CreateWrapper(); + auto pJSONValue = CreateWrapper(); yyjson_read_err readError; yyjson_doc* idoc = yyjson_read_file(realpath, read_flg, nullptr, &readError); @@ -648,14 +676,14 @@ YYJSONValue* YYJSONManager::ObjectParseFile(const char* path, yyjson_read_flag r return nullptr; } - pYYJSONValue->m_readSize = yyjson_doc_get_read_size(idoc); - pYYJSONValue->m_pDocument = WrapImmutableDocument(idoc); - pYYJSONValue->m_pVal = root; + pJSONValue->m_readSize = yyjson_doc_get_read_size(idoc); + pJSONValue->m_pDocument = WrapImmutableDocument(idoc); + pJSONValue->m_pVal = root; - return pYYJSONValue.release(); + return pJSONValue.release(); } -size_t YYJSONManager::ObjectGetSize(YYJSONValue* handle) +size_t JsonManager::ObjectGetSize(JsonValue* handle) { if (!handle) { return 0; @@ -668,7 +696,7 @@ size_t YYJSONManager::ObjectGetSize(YYJSONValue* handle) } } -bool YYJSONManager::ObjectGetKey(YYJSONValue* handle, size_t index, const char** out_key) +bool JsonManager::ObjectGetKey(JsonValue* handle, size_t index, const char** out_key) { if (!handle || !out_key) { return false; @@ -717,20 +745,21 @@ bool YYJSONManager::ObjectGetKey(YYJSONValue* handle, size_t index, const char** } } -YYJSONValue* YYJSONManager::ObjectGetValueAt(YYJSONValue* handle, size_t index) +JsonValue* JsonManager::ObjectGetValueAt(JsonValue* handle, size_t index) { if (!handle) { return nullptr; } - auto pYYJSONValue = CreateWrapper(); + size_t obj_size = handle->IsMutable() ? yyjson_mut_obj_size(handle->m_pVal_mut) : yyjson_obj_size(handle->m_pVal); - if (handle->IsMutable()) { - size_t obj_size = yyjson_mut_obj_size(handle->m_pVal_mut); - if (index >= obj_size) { - return nullptr; - } + if (index >= obj_size) { + return nullptr; + } + auto pJSONValue = CreateWrapper(); + + if (handle->IsMutable()) { yyjson_mut_obj_iter iter; yyjson_mut_obj_iter_init(handle->m_pVal_mut, &iter); @@ -743,40 +772,34 @@ YYJSONValue* YYJSONManager::ObjectGetValueAt(YYJSONValue* handle, size_t index) return nullptr; } - pYYJSONValue->m_pDocument_mut = handle->m_pDocument_mut; - pYYJSONValue->m_pVal_mut = yyjson_mut_obj_iter_get_val(key); + pJSONValue->m_pDocument_mut = handle->m_pDocument_mut; + pJSONValue->m_pVal_mut = yyjson_mut_obj_iter_get_val(key); } else { - size_t obj_size = yyjson_obj_size(handle->m_pVal); - if (index >= obj_size) { - return nullptr; - } - yyjson_obj_iter iter; yyjson_obj_iter_init(handle->m_pVal, &iter); - for (size_t i = 0; i < index; i++) { - yyjson_obj_iter_next(&iter); - } - - yyjson_val* key = yyjson_obj_iter_next(&iter); - if (!key) { - return nullptr; + yyjson_val* key = nullptr; + for (size_t i = 0; i <= index; i++) { + key = yyjson_obj_iter_next(&iter); + if (!key) { + return nullptr; + } } - pYYJSONValue->m_pDocument = handle->m_pDocument; - pYYJSONValue->m_pVal = yyjson_obj_iter_get_val(key); + pJSONValue->m_pDocument = handle->m_pDocument; + pJSONValue->m_pVal = yyjson_obj_iter_get_val(key); } - return pYYJSONValue.release(); + return pJSONValue.release(); } -YYJSONValue* YYJSONManager::ObjectGet(YYJSONValue* handle, const char* key) +JsonValue* JsonManager::ObjectGet(JsonValue* handle, const char* key) { if (!handle || !key) { return nullptr; } - auto pYYJSONValue = CreateWrapper(); + auto pJSONValue = CreateWrapper(); if (handle->IsMutable()) { yyjson_mut_val* val = yyjson_mut_obj_get(handle->m_pVal_mut, key); @@ -784,22 +807,22 @@ YYJSONValue* YYJSONManager::ObjectGet(YYJSONValue* handle, const char* key) return nullptr; } - pYYJSONValue->m_pDocument_mut = handle->m_pDocument_mut; - pYYJSONValue->m_pVal_mut = val; + pJSONValue->m_pDocument_mut = handle->m_pDocument_mut; + pJSONValue->m_pVal_mut = val; } else { yyjson_val* val = yyjson_obj_get(handle->m_pVal, key); if (!val) { return nullptr; } - pYYJSONValue->m_pDocument = handle->m_pDocument; - pYYJSONValue->m_pVal = val; + pJSONValue->m_pDocument = handle->m_pDocument; + pJSONValue->m_pVal = val; } - return pYYJSONValue.release(); + return pJSONValue.release(); } -bool YYJSONManager::ObjectGetBool(YYJSONValue* handle, const char* key, bool* out_value) +bool JsonManager::ObjectGetBool(JsonValue* handle, const char* key, bool* out_value) { if (!handle || !key || !out_value) { return false; @@ -824,7 +847,7 @@ bool YYJSONManager::ObjectGetBool(YYJSONValue* handle, const char* key, bool* ou } } -bool YYJSONManager::ObjectGetFloat(YYJSONValue* handle, const char* key, double* out_value) +bool JsonManager::ObjectGetFloat(JsonValue* handle, const char* key, double* out_value) { if (!handle || !key || !out_value) { return false; @@ -849,7 +872,7 @@ bool YYJSONManager::ObjectGetFloat(YYJSONValue* handle, const char* key, double* } } -bool YYJSONManager::ObjectGetInt(YYJSONValue* handle, const char* key, int* out_value) +bool JsonManager::ObjectGetInt(JsonValue* handle, const char* key, int* out_value) { if (!handle || !key || !out_value) { return false; @@ -874,7 +897,7 @@ bool YYJSONManager::ObjectGetInt(YYJSONValue* handle, const char* key, int* out_ } } -bool YYJSONManager::ObjectGetInt64(YYJSONValue* handle, const char* key, int64_t* out_value) +bool JsonManager::ObjectGetInt64(JsonValue* handle, const char* key, std::variant* out_value) { if (!handle || !key || !out_value) { return false; @@ -886,7 +909,7 @@ bool YYJSONManager::ObjectGetInt64(YYJSONValue* handle, const char* key, int64_t return false; } - *out_value = yyjson_mut_get_sint(val); + ReadInt64FromMutVal(val, out_value); return true; } else { yyjson_val* val = yyjson_obj_get(handle->m_pVal, key); @@ -894,12 +917,12 @@ bool YYJSONManager::ObjectGetInt64(YYJSONValue* handle, const char* key, int64_t return false; } - *out_value = yyjson_get_sint(val); + ReadInt64FromVal(val, out_value); return true; } } -bool YYJSONManager::ObjectGetString(YYJSONValue* handle, const char* key, const char** out_str, size_t* out_len) +bool JsonManager::ObjectGetString(JsonValue* handle, const char* key, const char** out_str, size_t* out_len) { if (!handle || !key || !out_str) { return false; @@ -930,7 +953,7 @@ bool YYJSONManager::ObjectGetString(YYJSONValue* handle, const char* key, const } } -bool YYJSONManager::ObjectIsNull(YYJSONValue* handle, const char* key, bool* out_is_null) +bool JsonManager::ObjectIsNull(JsonValue* handle, const char* key, bool* out_is_null) { if (!handle || !key || !out_is_null) { return false; @@ -955,7 +978,7 @@ bool YYJSONManager::ObjectIsNull(YYJSONValue* handle, const char* key, bool* out } } -bool YYJSONManager::ObjectHasKey(YYJSONValue* handle, const char* key, bool use_pointer) +bool JsonManager::ObjectHasKey(JsonValue* handle, const char* key, bool use_pointer) { if (!handle || !key) { return false; @@ -978,7 +1001,7 @@ bool YYJSONManager::ObjectHasKey(YYJSONValue* handle, const char* key, bool use_ } } -bool YYJSONManager::ObjectRenameKey(YYJSONValue* handle, const char* old_key, const char* new_key, bool allow_duplicate) +bool JsonManager::ObjectRenameKey(JsonValue* handle, const char* old_key, const char* new_key, bool allow_duplicate) { if (!handle || !handle->IsMutable() || !old_key || !new_key) { return false; @@ -995,7 +1018,7 @@ bool YYJSONManager::ObjectRenameKey(YYJSONValue* handle, const char* old_key, co return yyjson_mut_obj_rename_key(handle->m_pDocument_mut.get(), handle->m_pVal_mut, old_key, new_key); } -bool YYJSONManager::ObjectSet(YYJSONValue* handle, const char* key, YYJSONValue* value) +bool JsonManager::ObjectSet(JsonValue* handle, const char* key, JsonValue* value) { if (!handle || !handle->IsMutable() || !key || !value) { return false; @@ -1015,7 +1038,7 @@ bool YYJSONManager::ObjectSet(YYJSONValue* handle, const char* key, YYJSONValue* return yyjson_mut_obj_put(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut.get(), key), val_copy); } -bool YYJSONManager::ObjectSetBool(YYJSONValue* handle, const char* key, bool value) +bool JsonManager::ObjectSetBool(JsonValue* handle, const char* key, bool value) { if (!handle || !handle->IsMutable() || !key) { return false; @@ -1024,7 +1047,7 @@ bool YYJSONManager::ObjectSetBool(YYJSONValue* handle, const char* key, bool val return yyjson_mut_obj_put(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut.get(), key), yyjson_mut_bool(handle->m_pDocument_mut.get(), value)); } -bool YYJSONManager::ObjectSetFloat(YYJSONValue* handle, const char* key, double value) +bool JsonManager::ObjectSetFloat(JsonValue* handle, const char* key, double value) { if (!handle || !handle->IsMutable() || !key) { return false; @@ -1033,7 +1056,7 @@ bool YYJSONManager::ObjectSetFloat(YYJSONValue* handle, const char* key, double return yyjson_mut_obj_put(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut.get(), key), yyjson_mut_real(handle->m_pDocument_mut.get(), value)); } -bool YYJSONManager::ObjectSetInt(YYJSONValue* handle, const char* key, int value) +bool JsonManager::ObjectSetInt(JsonValue* handle, const char* key, int value) { if (!handle || !handle->IsMutable() || !key) { return false; @@ -1042,16 +1065,24 @@ bool YYJSONManager::ObjectSetInt(YYJSONValue* handle, const char* key, int value return yyjson_mut_obj_put(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut.get(), key), yyjson_mut_int(handle->m_pDocument_mut.get(), value)); } -bool YYJSONManager::ObjectSetInt64(YYJSONValue* handle, const char* key, int64_t value) +bool JsonManager::ObjectSetInt64(JsonValue* handle, const char* key, std::variant value) { if (!handle || !handle->IsMutable() || !key) { return false; } - return yyjson_mut_obj_put(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut.get(), key), yyjson_mut_sint(handle->m_pDocument_mut.get(), value)); + return std::visit([&](auto&& val) -> bool { + using T = std::decay_t; + if constexpr (std::is_same_v) { + return yyjson_mut_obj_put(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut.get(), key), yyjson_mut_sint(handle->m_pDocument_mut.get(), val)); + } else if constexpr (std::is_same_v) { + return yyjson_mut_obj_put(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut.get(), key), yyjson_mut_uint(handle->m_pDocument_mut.get(), val)); + } + return false; + }, value); } -bool YYJSONManager::ObjectSetNull(YYJSONValue* handle, const char* key) +bool JsonManager::ObjectSetNull(JsonValue* handle, const char* key) { if (!handle || !handle->IsMutable() || !key) { return false; @@ -1060,7 +1091,7 @@ bool YYJSONManager::ObjectSetNull(YYJSONValue* handle, const char* key) return yyjson_mut_obj_put(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut.get(), key), yyjson_mut_null(handle->m_pDocument_mut.get())); } -bool YYJSONManager::ObjectSetString(YYJSONValue* handle, const char* key, const char* value) +bool JsonManager::ObjectSetString(JsonValue* handle, const char* key, const char* value) { if (!handle || !handle->IsMutable() || !key || !value) { return false; @@ -1069,7 +1100,7 @@ bool YYJSONManager::ObjectSetString(YYJSONValue* handle, const char* key, const return yyjson_mut_obj_put(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut.get(), key), yyjson_mut_strcpy(handle->m_pDocument_mut.get(), value)); } -bool YYJSONManager::ObjectRemove(YYJSONValue* handle, const char* key) +bool JsonManager::ObjectRemove(JsonValue* handle, const char* key) { if (!handle || !handle->IsMutable() || !key) { return false; @@ -1078,7 +1109,7 @@ bool YYJSONManager::ObjectRemove(YYJSONValue* handle, const char* key) return yyjson_mut_obj_remove_key(handle->m_pVal_mut, key) != nullptr; } -bool YYJSONManager::ObjectClear(YYJSONValue* handle) +bool JsonManager::ObjectClear(JsonValue* handle) { if (!handle || !handle->IsMutable()) { return false; @@ -1087,7 +1118,7 @@ bool YYJSONManager::ObjectClear(YYJSONValue* handle) return yyjson_mut_obj_clear(handle->m_pVal_mut); } -bool YYJSONManager::ObjectSort(YYJSONValue* handle, YYJSON_SORT_ORDER sort_mode) +bool JsonManager::ObjectSort(JsonValue* handle, JSON_SORT_ORDER sort_mode) { if (!handle || !handle->IsMutable()) { return false; @@ -1097,78 +1128,209 @@ bool YYJSONManager::ObjectSort(YYJSONValue* handle, YYJSON_SORT_ORDER sort_mode) return false; } - if (sort_mode < YYJSON_SORT_ASC || sort_mode > YYJSON_SORT_RANDOM) { + if (sort_mode < JSON_SORT_ASC || sort_mode > JSON_SORT_RANDOM) { return false; } size_t obj_size = yyjson_mut_obj_size(handle->m_pVal_mut); if (obj_size <= 1) return true; - static std::vector> pairs; - pairs.clear(); + struct KeyValuePair { + yyjson_mut_val* key; + const char* key_str; + size_t key_len; + yyjson_mut_val* val; + }; + std::vector pairs; pairs.reserve(obj_size); size_t idx, max; yyjson_mut_val *key, *val; yyjson_mut_obj_foreach(handle->m_pVal_mut, idx, max, key, val) { - pairs.emplace_back(key, val); + const char* key_str = yyjson_mut_get_str(key); + size_t key_len = yyjson_mut_get_len(key); + pairs.push_back({key, key_str, key_len, val}); } - if (sort_mode == YYJSON_SORT_RANDOM) { + if (sort_mode == JSON_SORT_RANDOM) { std::shuffle(pairs.begin(), pairs.end(), m_randomGenerator); } else { - auto compare = [sort_mode](const auto& a, const auto& b) { - const char* key_a = yyjson_mut_get_str(a.first); - const char* key_b = yyjson_mut_get_str(b.first); - int cmp = strcmp(key_a, key_b); - return sort_mode == YYJSON_SORT_ASC ? cmp < 0 : cmp > 0; + auto compare = [sort_mode](const KeyValuePair& a, const KeyValuePair& b) { + size_t min_len = a.key_len < b.key_len ? a.key_len : b.key_len; + int cmp = memcmp(a.key_str, b.key_str, min_len); + if (cmp == 0) { + cmp = (a.key_len < b.key_len) ? -1 : (a.key_len > b.key_len ? 1 : 0); + } + return sort_mode == JSON_SORT_ASC ? cmp < 0 : cmp > 0; }; std::sort(pairs.begin(), pairs.end(), compare); } yyjson_mut_obj_clear(handle->m_pVal_mut); + for (const auto& pair : pairs) { - yyjson_mut_obj_add(handle->m_pVal_mut, pair.first, pair.second); + yyjson_mut_obj_add(handle->m_pVal_mut, pair.key, pair.val); } return true; } -YYJSONValue* YYJSONManager::ArrayInit() +JsonValue* JsonManager::ArrayInit() { - auto pYYJSONValue = CreateWrapper(); - pYYJSONValue->m_pDocument_mut = CreateDocument(); - pYYJSONValue->m_pVal_mut = yyjson_mut_arr(pYYJSONValue->m_pDocument_mut.get()); - yyjson_mut_doc_set_root(pYYJSONValue->m_pDocument_mut.get(), pYYJSONValue->m_pVal_mut); + auto pJSONValue = CreateWrapper(); + pJSONValue->m_pDocument_mut = CreateDocument(); + pJSONValue->m_pVal_mut = yyjson_mut_arr(pJSONValue->m_pDocument_mut.get()); + yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut.get(), pJSONValue->m_pVal_mut); - return pYYJSONValue.release(); + return pJSONValue.release(); } -YYJSONValue* YYJSONManager::ArrayInitWithStrings(const char** strings, size_t count) +JsonValue* JsonManager::ArrayInitWithStrings(const char** strings, size_t count) { if (!strings) { return nullptr; } - auto pYYJSONValue = CreateWrapper(); - pYYJSONValue->m_pDocument_mut = CreateDocument(); + auto pJSONValue = CreateWrapper(); + pJSONValue->m_pDocument_mut = CreateDocument(); - pYYJSONValue->m_pVal_mut = yyjson_mut_arr_with_strcpy( - pYYJSONValue->m_pDocument_mut.get(), + pJSONValue->m_pVal_mut = yyjson_mut_arr_with_strcpy( + pJSONValue->m_pDocument_mut.get(), strings, count ); - if (!pYYJSONValue->m_pVal_mut) { + if (!pJSONValue->m_pVal_mut) { + return nullptr; + } + + return pJSONValue.release(); +} + +JsonValue* JsonManager::ArrayInitWithInt32(const int32_t* values, size_t count) +{ + if (!values) { + return nullptr; + } + + auto pJSONValue = CreateWrapper(); + pJSONValue->m_pDocument_mut = CreateDocument(); + + pJSONValue->m_pVal_mut = yyjson_mut_arr_with_sint32( + pJSONValue->m_pDocument_mut.get(), + values, + count + ); + + if (!pJSONValue->m_pVal_mut) { + return nullptr; + } + + return pJSONValue.release(); +} + +JsonValue* JsonManager::ArrayInitWithInt64(const char** values, size_t count, char* error, size_t error_size) +{ + if (!values) { + if (error && error_size > 0) { + snprintf(error, error_size, "Invalid values parameter"); + } + return nullptr; + } + + if (count == 0) { + auto pJSONValue = CreateWrapper(); + pJSONValue->m_pDocument_mut = CreateDocument(); + pJSONValue->m_pVal_mut = yyjson_mut_arr(pJSONValue->m_pDocument_mut.get()); + return pJSONValue.release(); + } + + auto pJSONValue = CreateWrapper(); + pJSONValue->m_pDocument_mut = CreateDocument(); + auto doc = pJSONValue->m_pDocument_mut.get(); + + pJSONValue->m_pVal_mut = yyjson_mut_arr(doc); + if (!pJSONValue->m_pVal_mut) { + if (error && error_size > 0) { + snprintf(error, error_size, "Failed to create array"); + } + return nullptr; + } + + for (size_t i = 0; i < count; i++) { + std::variant variant_value; + if (!ParseInt64Variant(values[i], &variant_value, error, error_size)) { + return nullptr; + } + + yyjson_mut_val* val = nullptr; + std::visit([&](auto&& arg) { + using T = std::decay_t; + if constexpr (std::is_same_v) { + val = yyjson_mut_sint(doc, arg); + } else if constexpr (std::is_same_v) { + val = yyjson_mut_uint(doc, arg); + } + }, variant_value); + + if (!val || !yyjson_mut_arr_append(pJSONValue->m_pVal_mut, val)) { + if (error && error_size > 0) { + snprintf(error, error_size, "Failed to append value at index %zu", i); + } + return nullptr; + } + } + + return pJSONValue.release(); +} + +JsonValue* JsonManager::ArrayInitWithBool(const bool* values, size_t count) +{ + if (!values) { + return nullptr; + } + + auto pJSONValue = CreateWrapper(); + pJSONValue->m_pDocument_mut = CreateDocument(); + + pJSONValue->m_pVal_mut = yyjson_mut_arr_with_bool( + pJSONValue->m_pDocument_mut.get(), + values, + count + ); + + if (!pJSONValue->m_pVal_mut) { + return nullptr; + } + + return pJSONValue.release(); +} + +JsonValue* JsonManager::ArrayInitWithFloat(const double* values, size_t count) +{ + if (!values) { + return nullptr; + } + + auto pJSONValue = CreateWrapper(); + pJSONValue->m_pDocument_mut = CreateDocument(); + + pJSONValue->m_pVal_mut = yyjson_mut_arr_with_real( + pJSONValue->m_pDocument_mut.get(), + values, + count + ); + + if (!pJSONValue->m_pVal_mut) { return nullptr; } - return pYYJSONValue.release(); + return pJSONValue.release(); } -YYJSONValue* YYJSONManager::ArrayParseString(const char* str, yyjson_read_flag read_flg, +JsonValue* JsonManager::ArrayParseString(const char* str, yyjson_read_flag read_flg, char* error, size_t error_size) { if (!str) { @@ -1178,7 +1340,7 @@ YYJSONValue* YYJSONManager::ArrayParseString(const char* str, yyjson_read_flag r return nullptr; } - auto pYYJSONValue = CreateWrapper(); + auto pJSONValue = CreateWrapper(); yyjson_read_err readError; yyjson_doc* idoc = yyjson_read_opts(const_cast(str), strlen(str), read_flg, nullptr, &readError); @@ -1204,14 +1366,14 @@ YYJSONValue* YYJSONManager::ArrayParseString(const char* str, yyjson_read_flag r return nullptr; } - pYYJSONValue->m_readSize = yyjson_doc_get_read_size(idoc); - pYYJSONValue->m_pDocument = WrapImmutableDocument(idoc); - pYYJSONValue->m_pVal = root; + pJSONValue->m_readSize = yyjson_doc_get_read_size(idoc); + pJSONValue->m_pDocument = WrapImmutableDocument(idoc); + pJSONValue->m_pVal = root; - return pYYJSONValue.release(); + return pJSONValue.release(); } -YYJSONValue* YYJSONManager::ArrayParseFile(const char* path, yyjson_read_flag read_flg, +JsonValue* JsonManager::ArrayParseFile(const char* path, yyjson_read_flag read_flg, char* error, size_t error_size) { if (!path) { @@ -1223,7 +1385,7 @@ YYJSONValue* YYJSONManager::ArrayParseFile(const char* path, yyjson_read_flag re char realpath[PLATFORM_MAX_PATH]; smutils->BuildPath(Path_Game, realpath, sizeof(realpath), "%s", path); - auto pYYJSONValue = CreateWrapper(); + auto pJSONValue = CreateWrapper(); yyjson_read_err readError; yyjson_doc* idoc = yyjson_read_file(realpath, read_flg, nullptr, &readError); @@ -1249,14 +1411,14 @@ YYJSONValue* YYJSONManager::ArrayParseFile(const char* path, yyjson_read_flag re return nullptr; } - pYYJSONValue->m_readSize = yyjson_doc_get_read_size(idoc); - pYYJSONValue->m_pDocument = WrapImmutableDocument(idoc); - pYYJSONValue->m_pVal = root; + pJSONValue->m_readSize = yyjson_doc_get_read_size(idoc); + pJSONValue->m_pDocument = WrapImmutableDocument(idoc); + pJSONValue->m_pVal = root; - return pYYJSONValue.release(); + return pJSONValue.release(); } -size_t YYJSONManager::ArrayGetSize(YYJSONValue* handle) +size_t JsonManager::ArrayGetSize(JsonValue* handle) { if (!handle) { return 0; @@ -1269,13 +1431,13 @@ size_t YYJSONManager::ArrayGetSize(YYJSONValue* handle) } } -YYJSONValue* YYJSONManager::ArrayGet(YYJSONValue* handle, size_t index) +JsonValue* JsonManager::ArrayGet(JsonValue* handle, size_t index) { if (!handle) { return nullptr; } - auto pYYJSONValue = CreateWrapper(); + auto pJSONValue = CreateWrapper(); if (handle->IsMutable()) { size_t arr_size = yyjson_mut_arr_size(handle->m_pVal_mut); @@ -1288,8 +1450,8 @@ YYJSONValue* YYJSONManager::ArrayGet(YYJSONValue* handle, size_t index) return nullptr; } - pYYJSONValue->m_pDocument_mut = handle->m_pDocument_mut; - pYYJSONValue->m_pVal_mut = val; + pJSONValue->m_pDocument_mut = handle->m_pDocument_mut; + pJSONValue->m_pVal_mut = val; } else { size_t arr_size = yyjson_arr_size(handle->m_pVal); if (index >= arr_size) { @@ -1301,20 +1463,20 @@ YYJSONValue* YYJSONManager::ArrayGet(YYJSONValue* handle, size_t index) return nullptr; } - pYYJSONValue->m_pDocument = handle->m_pDocument; - pYYJSONValue->m_pVal = val; + pJSONValue->m_pDocument = handle->m_pDocument; + pJSONValue->m_pVal = val; } - return pYYJSONValue.release(); + return pJSONValue.release(); } -YYJSONValue* YYJSONManager::ArrayGetFirst(YYJSONValue* handle) +JsonValue* JsonManager::ArrayGetFirst(JsonValue* handle) { if (!handle) { return nullptr; } - auto pYYJSONValue = CreateWrapper(); + auto pJSONValue = CreateWrapper(); if (handle->IsMutable()) { size_t arr_size = yyjson_mut_arr_size(handle->m_pVal_mut); @@ -1327,8 +1489,8 @@ YYJSONValue* YYJSONManager::ArrayGetFirst(YYJSONValue* handle) return nullptr; } - pYYJSONValue->m_pDocument_mut = handle->m_pDocument_mut; - pYYJSONValue->m_pVal_mut = val; + pJSONValue->m_pDocument_mut = handle->m_pDocument_mut; + pJSONValue->m_pVal_mut = val; } else { size_t arr_size = yyjson_arr_size(handle->m_pVal); if (arr_size == 0) { @@ -1340,20 +1502,20 @@ YYJSONValue* YYJSONManager::ArrayGetFirst(YYJSONValue* handle) return nullptr; } - pYYJSONValue->m_pDocument = handle->m_pDocument; - pYYJSONValue->m_pVal = val; + pJSONValue->m_pDocument = handle->m_pDocument; + pJSONValue->m_pVal = val; } - return pYYJSONValue.release(); + return pJSONValue.release(); } -YYJSONValue* YYJSONManager::ArrayGetLast(YYJSONValue* handle) +JsonValue* JsonManager::ArrayGetLast(JsonValue* handle) { if (!handle) { return nullptr; } - auto pYYJSONValue = CreateWrapper(); + auto pJSONValue = CreateWrapper(); if (handle->IsMutable()) { size_t arr_size = yyjson_mut_arr_size(handle->m_pVal_mut); @@ -1366,8 +1528,8 @@ YYJSONValue* YYJSONManager::ArrayGetLast(YYJSONValue* handle) return nullptr; } - pYYJSONValue->m_pDocument_mut = handle->m_pDocument_mut; - pYYJSONValue->m_pVal_mut = val; + pJSONValue->m_pDocument_mut = handle->m_pDocument_mut; + pJSONValue->m_pVal_mut = val; } else { size_t arr_size = yyjson_arr_size(handle->m_pVal); if (arr_size == 0) { @@ -1379,14 +1541,14 @@ YYJSONValue* YYJSONManager::ArrayGetLast(YYJSONValue* handle) return nullptr; } - pYYJSONValue->m_pDocument = handle->m_pDocument; - pYYJSONValue->m_pVal = val; + pJSONValue->m_pDocument = handle->m_pDocument; + pJSONValue->m_pVal = val; } - return pYYJSONValue.release(); + return pJSONValue.release(); } -bool YYJSONManager::ArrayGetBool(YYJSONValue* handle, size_t index, bool* out_value) +bool JsonManager::ArrayGetBool(JsonValue* handle, size_t index, bool* out_value) { if (!handle || !out_value) { return false; @@ -1421,7 +1583,7 @@ bool YYJSONManager::ArrayGetBool(YYJSONValue* handle, size_t index, bool* out_va } } -bool YYJSONManager::ArrayGetFloat(YYJSONValue* handle, size_t index, double* out_value) +bool JsonManager::ArrayGetFloat(JsonValue* handle, size_t index, double* out_value) { if (!handle || !out_value) { return false; @@ -1456,7 +1618,7 @@ bool YYJSONManager::ArrayGetFloat(YYJSONValue* handle, size_t index, double* out } } -bool YYJSONManager::ArrayGetInt(YYJSONValue* handle, size_t index, int* out_value) +bool JsonManager::ArrayGetInt(JsonValue* handle, size_t index, int* out_value) { if (!handle || !out_value) { return false; @@ -1491,7 +1653,7 @@ bool YYJSONManager::ArrayGetInt(YYJSONValue* handle, size_t index, int* out_valu } } -bool YYJSONManager::ArrayGetInt64(YYJSONValue* handle, size_t index, int64_t* out_value) +bool JsonManager::ArrayGetInt64(JsonValue* handle, size_t index, std::variant* out_value) { if (!handle || !out_value) { return false; @@ -1508,7 +1670,7 @@ bool YYJSONManager::ArrayGetInt64(YYJSONValue* handle, size_t index, int64_t* ou return false; } - *out_value = yyjson_mut_get_sint(val); + ReadInt64FromMutVal(val, out_value); return true; } else { size_t arr_size = yyjson_arr_size(handle->m_pVal); @@ -1521,12 +1683,12 @@ bool YYJSONManager::ArrayGetInt64(YYJSONValue* handle, size_t index, int64_t* ou return false; } - *out_value = yyjson_get_sint(val); + ReadInt64FromVal(val, out_value); return true; } } -bool YYJSONManager::ArrayGetString(YYJSONValue* handle, size_t index, const char** out_str, size_t* out_len) +bool JsonManager::ArrayGetString(JsonValue* handle, size_t index, const char** out_str, size_t* out_len) { if (!handle || !out_str) { return false; @@ -1567,7 +1729,7 @@ bool YYJSONManager::ArrayGetString(YYJSONValue* handle, size_t index, const char } } -bool YYJSONManager::ArrayIsNull(YYJSONValue* handle, size_t index) +bool JsonManager::ArrayIsNull(JsonValue* handle, size_t index) { if (!handle) { return false; @@ -1592,7 +1754,7 @@ bool YYJSONManager::ArrayIsNull(YYJSONValue* handle, size_t index) } } -bool YYJSONManager::ArrayReplace(YYJSONValue* handle, size_t index, YYJSONValue* value) +bool JsonManager::ArrayReplace(JsonValue* handle, size_t index, JsonValue* value) { if (!handle || !handle->IsMutable() || !value) { return false; @@ -1617,7 +1779,7 @@ bool YYJSONManager::ArrayReplace(YYJSONValue* handle, size_t index, YYJSONValue* return yyjson_mut_arr_replace(handle->m_pVal_mut, index, val_copy) != nullptr; } -bool YYJSONManager::ArrayReplaceBool(YYJSONValue* handle, size_t index, bool value) +bool JsonManager::ArrayReplaceBool(JsonValue* handle, size_t index, bool value) { if (!handle || !handle->IsMutable()) { return false; @@ -1631,7 +1793,7 @@ bool YYJSONManager::ArrayReplaceBool(YYJSONValue* handle, size_t index, bool val return yyjson_mut_arr_replace(handle->m_pVal_mut, index, yyjson_mut_bool(handle->m_pDocument_mut.get(), value)) != nullptr; } -bool YYJSONManager::ArrayReplaceFloat(YYJSONValue* handle, size_t index, double value) +bool JsonManager::ArrayReplaceFloat(JsonValue* handle, size_t index, double value) { if (!handle || !handle->IsMutable()) { return false; @@ -1645,7 +1807,7 @@ bool YYJSONManager::ArrayReplaceFloat(YYJSONValue* handle, size_t index, double return yyjson_mut_arr_replace(handle->m_pVal_mut, index, yyjson_mut_real(handle->m_pDocument_mut.get(), value)) != nullptr; } -bool YYJSONManager::ArrayReplaceInt(YYJSONValue* handle, size_t index, int value) +bool JsonManager::ArrayReplaceInt(JsonValue* handle, size_t index, int value) { if (!handle || !handle->IsMutable()) { return false; @@ -1659,7 +1821,7 @@ bool YYJSONManager::ArrayReplaceInt(YYJSONValue* handle, size_t index, int value return yyjson_mut_arr_replace(handle->m_pVal_mut, index, yyjson_mut_int(handle->m_pDocument_mut.get(), value)) != nullptr; } -bool YYJSONManager::ArrayReplaceInt64(YYJSONValue* handle, size_t index, int64_t value) +bool JsonManager::ArrayReplaceInt64(JsonValue* handle, size_t index, std::variant value) { if (!handle || !handle->IsMutable()) { return false; @@ -1670,10 +1832,18 @@ bool YYJSONManager::ArrayReplaceInt64(YYJSONValue* handle, size_t index, int64_t return false; } - return yyjson_mut_arr_replace(handle->m_pVal_mut, index, yyjson_mut_sint(handle->m_pDocument_mut.get(), value)) != nullptr; + return std::visit([&](auto&& val) -> bool { + using T = std::decay_t; + if constexpr (std::is_same_v) { + return yyjson_mut_arr_replace(handle->m_pVal_mut, index, yyjson_mut_sint(handle->m_pDocument_mut.get(), val)) != nullptr; + } else if constexpr (std::is_same_v) { + return yyjson_mut_arr_replace(handle->m_pVal_mut, index, yyjson_mut_uint(handle->m_pDocument_mut.get(), val)) != nullptr; + } + return false; + }, value); } -bool YYJSONManager::ArrayReplaceNull(YYJSONValue* handle, size_t index) +bool JsonManager::ArrayReplaceNull(JsonValue* handle, size_t index) { if (!handle || !handle->IsMutable()) { return false; @@ -1687,7 +1857,7 @@ bool YYJSONManager::ArrayReplaceNull(YYJSONValue* handle, size_t index) return yyjson_mut_arr_replace(handle->m_pVal_mut, index, yyjson_mut_null(handle->m_pDocument_mut.get())) != nullptr; } -bool YYJSONManager::ArrayReplaceString(YYJSONValue* handle, size_t index, const char* value) +bool JsonManager::ArrayReplaceString(JsonValue* handle, size_t index, const char* value) { if (!handle || !handle->IsMutable() || !value) { return false; @@ -1701,7 +1871,7 @@ bool YYJSONManager::ArrayReplaceString(YYJSONValue* handle, size_t index, const return yyjson_mut_arr_replace(handle->m_pVal_mut, index, yyjson_mut_strcpy(handle->m_pDocument_mut.get(), value)) != nullptr; } -bool YYJSONManager::ArrayAppend(YYJSONValue* handle, YYJSONValue* value) +bool JsonManager::ArrayAppend(JsonValue* handle, JsonValue* value) { if (!handle || !handle->IsMutable() || !value) { return false; @@ -1721,7 +1891,7 @@ bool YYJSONManager::ArrayAppend(YYJSONValue* handle, YYJSONValue* value) return yyjson_mut_arr_append(handle->m_pVal_mut, val_copy); } -bool YYJSONManager::ArrayAppendBool(YYJSONValue* handle, bool value) +bool JsonManager::ArrayAppendBool(JsonValue* handle, bool value) { if (!handle || !handle->IsMutable()) { return false; @@ -1730,7 +1900,7 @@ bool YYJSONManager::ArrayAppendBool(YYJSONValue* handle, bool value) return yyjson_mut_arr_append(handle->m_pVal_mut, yyjson_mut_bool(handle->m_pDocument_mut.get(), value)); } -bool YYJSONManager::ArrayAppendFloat(YYJSONValue* handle, double value) +bool JsonManager::ArrayAppendFloat(JsonValue* handle, double value) { if (!handle || !handle->IsMutable()) { return false; @@ -1739,7 +1909,7 @@ bool YYJSONManager::ArrayAppendFloat(YYJSONValue* handle, double value) return yyjson_mut_arr_append(handle->m_pVal_mut, yyjson_mut_real(handle->m_pDocument_mut.get(), value)); } -bool YYJSONManager::ArrayAppendInt(YYJSONValue* handle, int value) +bool JsonManager::ArrayAppendInt(JsonValue* handle, int value) { if (!handle || !handle->IsMutable()) { return false; @@ -1748,16 +1918,24 @@ bool YYJSONManager::ArrayAppendInt(YYJSONValue* handle, int value) return yyjson_mut_arr_append(handle->m_pVal_mut, yyjson_mut_int(handle->m_pDocument_mut.get(), value)); } -bool YYJSONManager::ArrayAppendInt64(YYJSONValue* handle, int64_t value) +bool JsonManager::ArrayAppendInt64(JsonValue* handle, std::variant value) { if (!handle || !handle->IsMutable()) { return false; } - return yyjson_mut_arr_append(handle->m_pVal_mut, yyjson_mut_sint(handle->m_pDocument_mut.get(), value)); + return std::visit([&](auto&& val) -> bool { + using T = std::decay_t; + if constexpr (std::is_same_v) { + return yyjson_mut_arr_append(handle->m_pVal_mut, yyjson_mut_sint(handle->m_pDocument_mut.get(), val)); + } else if constexpr (std::is_same_v) { + return yyjson_mut_arr_append(handle->m_pVal_mut, yyjson_mut_uint(handle->m_pDocument_mut.get(), val)); + } + return false; + }, value); } -bool YYJSONManager::ArrayAppendNull(YYJSONValue* handle) +bool JsonManager::ArrayAppendNull(JsonValue* handle) { if (!handle || !handle->IsMutable()) { return false; @@ -1766,7 +1944,7 @@ bool YYJSONManager::ArrayAppendNull(YYJSONValue* handle) return yyjson_mut_arr_append(handle->m_pVal_mut, yyjson_mut_null(handle->m_pDocument_mut.get())); } -bool YYJSONManager::ArrayAppendString(YYJSONValue* handle, const char* value) +bool JsonManager::ArrayAppendString(JsonValue* handle, const char* value) { if (!handle || !handle->IsMutable() || !value) { return false; @@ -1775,7 +1953,161 @@ bool YYJSONManager::ArrayAppendString(YYJSONValue* handle, const char* value) return yyjson_mut_arr_append(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut.get(), value)); } -bool YYJSONManager::ArrayRemove(YYJSONValue* handle, size_t index) +bool JsonManager::ArrayInsert(JsonValue* handle, size_t index, JsonValue* value) +{ + if (!handle || !handle->IsMutable() || !value) { + return false; + } + + return yyjson_mut_arr_insert(handle->m_pVal_mut, value->m_pVal_mut, index); +} + +bool JsonManager::ArrayInsertBool(JsonValue* handle, size_t index, bool value) +{ + if (!handle || !handle->IsMutable()) { + return false; + } + + return yyjson_mut_arr_insert(handle->m_pVal_mut, yyjson_mut_bool(handle->m_pDocument_mut.get(), value), index); +} + +bool JsonManager::ArrayInsertInt(JsonValue* handle, size_t index, int value) +{ + if (!handle || !handle->IsMutable()) { + return false; + } + + return yyjson_mut_arr_insert(handle->m_pVal_mut, yyjson_mut_sint(handle->m_pDocument_mut.get(), value), index); +} + +bool JsonManager::ArrayInsertInt64(JsonValue* handle, size_t index, std::variant value) +{ + if (!handle || !handle->IsMutable()) { + return false; + } + + yyjson_mut_val* val = nullptr; + std::visit([&](auto&& arg) { + using T = std::decay_t; + if constexpr (std::is_same_v) { + val = yyjson_mut_sint(handle->m_pDocument_mut.get(), arg); + } else if constexpr (std::is_same_v) { + val = yyjson_mut_uint(handle->m_pDocument_mut.get(), arg); + } + }, value); + + if (!val) { + return false; + } + + return yyjson_mut_arr_insert(handle->m_pVal_mut, val, index); +} + +bool JsonManager::ArrayInsertFloat(JsonValue* handle, size_t index, double value) +{ + if (!handle || !handle->IsMutable()) { + return false; + } + + return yyjson_mut_arr_insert(handle->m_pVal_mut, yyjson_mut_real(handle->m_pDocument_mut.get(), value), index); +} + +bool JsonManager::ArrayInsertString(JsonValue* handle, size_t index, const char* value) +{ + if (!handle || !handle->IsMutable() || !value) { + return false; + } + + return yyjson_mut_arr_insert(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut.get(), value), index); +} + +bool JsonManager::ArrayInsertNull(JsonValue* handle, size_t index) +{ + if (!handle || !handle->IsMutable()) { + return false; + } + + return yyjson_mut_arr_insert(handle->m_pVal_mut, yyjson_mut_null(handle->m_pDocument_mut.get()), index); +} + +bool JsonManager::ArrayPrepend(JsonValue* handle, JsonValue* value) +{ + if (!handle || !handle->IsMutable() || !value) { + return false; + } + + return yyjson_mut_arr_prepend(handle->m_pVal_mut, value->m_pVal_mut); +} + +bool JsonManager::ArrayPrependBool(JsonValue* handle, bool value) +{ + if (!handle || !handle->IsMutable()) { + return false; + } + + return yyjson_mut_arr_prepend(handle->m_pVal_mut, yyjson_mut_bool(handle->m_pDocument_mut.get(), value)); +} + +bool JsonManager::ArrayPrependInt(JsonValue* handle, int value) +{ + if (!handle || !handle->IsMutable()) { + return false; + } + + return yyjson_mut_arr_prepend(handle->m_pVal_mut, yyjson_mut_sint(handle->m_pDocument_mut.get(), value)); +} + +bool JsonManager::ArrayPrependInt64(JsonValue* handle, std::variant value) +{ + if (!handle || !handle->IsMutable()) { + return false; + } + + yyjson_mut_val* val = nullptr; + std::visit([&](auto&& arg) { + using T = std::decay_t; + if constexpr (std::is_same_v) { + val = yyjson_mut_sint(handle->m_pDocument_mut.get(), arg); + } else if constexpr (std::is_same_v) { + val = yyjson_mut_uint(handle->m_pDocument_mut.get(), arg); + } + }, value); + + if (!val) { + return false; + } + + return yyjson_mut_arr_prepend(handle->m_pVal_mut, val); +} + +bool JsonManager::ArrayPrependFloat(JsonValue* handle, double value) +{ + if (!handle || !handle->IsMutable()) { + return false; + } + + return yyjson_mut_arr_prepend(handle->m_pVal_mut, yyjson_mut_real(handle->m_pDocument_mut.get(), value)); +} + +bool JsonManager::ArrayPrependString(JsonValue* handle, const char* value) +{ + if (!handle || !handle->IsMutable() || !value) { + return false; + } + + return yyjson_mut_arr_prepend(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut.get(), value)); +} + +bool JsonManager::ArrayPrependNull(JsonValue* handle) +{ + if (!handle || !handle->IsMutable()) { + return false; + } + + return yyjson_mut_arr_prepend(handle->m_pVal_mut, yyjson_mut_null(handle->m_pDocument_mut.get())); +} + +bool JsonManager::ArrayRemove(JsonValue* handle, size_t index) { if (!handle || !handle->IsMutable()) { return false; @@ -1789,7 +2121,7 @@ bool YYJSONManager::ArrayRemove(YYJSONValue* handle, size_t index) return yyjson_mut_arr_remove(handle->m_pVal_mut, index) != nullptr; } -bool YYJSONManager::ArrayRemoveFirst(YYJSONValue* handle) +bool JsonManager::ArrayRemoveFirst(JsonValue* handle) { if (!handle || !handle->IsMutable()) { return false; @@ -1802,7 +2134,7 @@ bool YYJSONManager::ArrayRemoveFirst(YYJSONValue* handle) return yyjson_mut_arr_remove_first(handle->m_pVal_mut) != nullptr; } -bool YYJSONManager::ArrayRemoveLast(YYJSONValue* handle) +bool JsonManager::ArrayRemoveLast(JsonValue* handle) { if (!handle || !handle->IsMutable()) { return false; @@ -1815,7 +2147,7 @@ bool YYJSONManager::ArrayRemoveLast(YYJSONValue* handle) return yyjson_mut_arr_remove_last(handle->m_pVal_mut) != nullptr; } -bool YYJSONManager::ArrayRemoveRange(YYJSONValue* handle, size_t start_index, size_t end_index) +bool JsonManager::ArrayRemoveRange(JsonValue* handle, size_t start_index, size_t end_index) { if (!handle || !handle->IsMutable()) { return false; @@ -1830,7 +2162,7 @@ bool YYJSONManager::ArrayRemoveRange(YYJSONValue* handle, size_t start_index, si return yyjson_mut_arr_remove_range(handle->m_pVal_mut, start_index, end_index); } -bool YYJSONManager::ArrayClear(YYJSONValue* handle) +bool JsonManager::ArrayClear(JsonValue* handle) { if (!handle || !handle->IsMutable()) { return false; @@ -1839,7 +2171,7 @@ bool YYJSONManager::ArrayClear(YYJSONValue* handle) return yyjson_mut_arr_clear(handle->m_pVal_mut); } -int YYJSONManager::ArrayIndexOfBool(YYJSONValue* handle, bool search_value) +int JsonManager::ArrayIndexOfBool(JsonValue* handle, bool search_value) { if (!handle) { return -1; @@ -1866,7 +2198,7 @@ int YYJSONManager::ArrayIndexOfBool(YYJSONValue* handle, bool search_value) return -1; } -int YYJSONManager::ArrayIndexOfString(YYJSONValue* handle, const char* search_value) +int JsonManager::ArrayIndexOfString(JsonValue* handle, const char* search_value) { if (!handle || !search_value) { return -1; @@ -1893,7 +2225,7 @@ int YYJSONManager::ArrayIndexOfString(YYJSONValue* handle, const char* search_va return -1; } -int YYJSONManager::ArrayIndexOfInt(YYJSONValue* handle, int search_value) +int JsonManager::ArrayIndexOfInt(JsonValue* handle, int search_value) { if (!handle) { return -1; @@ -1920,7 +2252,7 @@ int YYJSONManager::ArrayIndexOfInt(YYJSONValue* handle, int search_value) return -1; } -int YYJSONManager::ArrayIndexOfInt64(YYJSONValue* handle, int64_t search_value) +int JsonManager::ArrayIndexOfInt64(JsonValue* handle, int64_t search_value) { if (!handle) { return -1; @@ -1947,7 +2279,34 @@ int YYJSONManager::ArrayIndexOfInt64(YYJSONValue* handle, int64_t search_value) return -1; } -int YYJSONManager::ArrayIndexOfFloat(YYJSONValue* handle, double search_value) +int JsonManager::ArrayIndexOfUint64(JsonValue* handle, uint64_t search_value) +{ + if (!handle) { + return -1; + } + + if (handle->IsMutable()) { + size_t idx, max; + yyjson_mut_val *val; + yyjson_mut_arr_foreach(handle->m_pVal_mut, idx, max, val) { + if (yyjson_mut_is_int(val) && yyjson_mut_get_uint(val) == search_value) { + return static_cast(idx); + } + } + } else { + size_t idx, max; + yyjson_val *val; + yyjson_arr_foreach(handle->m_pVal, idx, max, val) { + if (yyjson_is_int(val) && yyjson_get_uint(val) == search_value) { + return static_cast(idx); + } + } + } + + return -1; +} + +int JsonManager::ArrayIndexOfFloat(JsonValue* handle, double search_value) { if (!handle) { return -1; @@ -1980,7 +2339,7 @@ int YYJSONManager::ArrayIndexOfFloat(YYJSONValue* handle, double search_value) return -1; } -bool YYJSONManager::ArraySort(YYJSONValue* handle, YYJSON_SORT_ORDER sort_mode) +bool JsonManager::ArraySort(JsonValue* handle, JSON_SORT_ORDER sort_mode) { if (!handle || !handle->IsMutable()) { return false; @@ -1990,57 +2349,105 @@ bool YYJSONManager::ArraySort(YYJSONValue* handle, YYJSON_SORT_ORDER sort_mode) return false; } - if (sort_mode < YYJSON_SORT_ASC || sort_mode > YYJSON_SORT_RANDOM) { + if (sort_mode < JSON_SORT_ASC || sort_mode > JSON_SORT_RANDOM) { return false; } size_t arr_size = yyjson_mut_arr_size(handle->m_pVal_mut); if (arr_size <= 1) return true; - static std::vector values; - values.clear(); + struct ValueInfo { + yyjson_mut_val* val; + uint8_t type; + uint8_t subtype; // 0=float, 1=signed int, 2=unsigned int + }; + + std::vector values; values.reserve(arr_size); size_t idx, max; yyjson_mut_val *val; yyjson_mut_arr_foreach(handle->m_pVal_mut, idx, max, val) { - values.push_back(val); - } + uint8_t type = yyjson_mut_get_type(val); + uint8_t subtype = 0; - if (sort_mode == YYJSON_SORT_RANDOM) { + if (type == YYJSON_TYPE_NUM) { + if (yyjson_mut_is_int(val)) { + subtype = yyjson_mut_is_sint(val) ? 1 : 2; + } + } + + values.push_back({val, type, subtype}); + } + + if (sort_mode == JSON_SORT_RANDOM) { std::shuffle(values.begin(), values.end(), m_randomGenerator); } else { - auto compare = [sort_mode](yyjson_mut_val* a, yyjson_mut_val* b) { - if (a == b) return false; + auto compare = [sort_mode](const ValueInfo& a, const ValueInfo& b) { + if (a.val == b.val) return false; - uint8_t type_a = yyjson_mut_get_type(a); - uint8_t type_b = yyjson_mut_get_type(b); - if (type_a != type_b) { - return sort_mode == YYJSON_SORT_ASC ? type_a < type_b : type_a > type_b; + if (a.type != b.type) { + return sort_mode == JSON_SORT_ASC ? a.type < b.type : a.type > b.type; } - switch (type_a) { + switch (a.type) { case YYJSON_TYPE_STR: { - const char* str_a = yyjson_mut_get_str(a); - const char* str_b = yyjson_mut_get_str(b); + const char* str_a = yyjson_mut_get_str(a.val); + const char* str_b = yyjson_mut_get_str(b.val); int cmp = strcmp(str_a, str_b); - return sort_mode == YYJSON_SORT_ASC ? cmp < 0 : cmp > 0; + return sort_mode == JSON_SORT_ASC ? cmp < 0 : cmp > 0; } case YYJSON_TYPE_NUM: { - if (yyjson_mut_is_int(a) && yyjson_mut_is_int(b)) { - int64_t num_a = yyjson_mut_get_int(a); - int64_t num_b = yyjson_mut_get_int(b); - return sort_mode == YYJSON_SORT_ASC ? num_a < num_b : num_a > num_b; + if (a.subtype > 0 && b.subtype > 0) { + if (a.subtype == 1 && b.subtype == 1) { + int64_t num_a = yyjson_mut_get_sint(a.val); + int64_t num_b = yyjson_mut_get_sint(b.val); + return sort_mode == JSON_SORT_ASC ? num_a < num_b : num_a > num_b; + } + else if (a.subtype == 2 && b.subtype == 2) { + uint64_t num_a = yyjson_mut_get_uint(a.val); + uint64_t num_b = yyjson_mut_get_uint(b.val); + return sort_mode == JSON_SORT_ASC ? num_a < num_b : num_a > num_b; + } + else { + int64_t signed_val; + uint64_t unsigned_val; + bool a_is_signed = (a.subtype == 1); + + if (a_is_signed) { + signed_val = yyjson_mut_get_sint(a.val); + unsigned_val = yyjson_mut_get_uint(b.val); + + if (signed_val < 0) { + return sort_mode == JSON_SORT_ASC; + } + uint64_t a_as_unsigned = static_cast(signed_val); + return sort_mode == JSON_SORT_ASC ? + a_as_unsigned < unsigned_val : + a_as_unsigned > unsigned_val; + } else { + unsigned_val = yyjson_mut_get_uint(a.val); + signed_val = yyjson_mut_get_sint(b.val); + + if (signed_val < 0) { + return sort_mode == JSON_SORT_DESC; + } + uint64_t b_as_unsigned = static_cast(signed_val); + return sort_mode == JSON_SORT_ASC ? + unsigned_val < b_as_unsigned : + unsigned_val > b_as_unsigned; + } + } } - double num_a = yyjson_mut_get_num(a); - double num_b = yyjson_mut_get_num(b); - return sort_mode == YYJSON_SORT_ASC ? num_a < num_b : num_a > num_b; + double num_a = yyjson_mut_get_num(a.val); + double num_b = yyjson_mut_get_num(b.val); + return sort_mode == JSON_SORT_ASC ? num_a < num_b : num_a > num_b; } case YYJSON_TYPE_BOOL: { - bool val_a = yyjson_mut_get_bool(a); - bool val_b = yyjson_mut_get_bool(b); - return sort_mode == YYJSON_SORT_ASC ? val_a < val_b : val_a > val_b; + bool val_a = yyjson_mut_get_bool(a.val); + bool val_b = yyjson_mut_get_bool(b.val); + return sort_mode == JSON_SORT_ASC ? val_a < val_b : val_a > val_b; } default: return false; @@ -2051,14 +2458,14 @@ bool YYJSONManager::ArraySort(YYJSONValue* handle, YYJSON_SORT_ORDER sort_mode) } yyjson_mut_arr_clear(handle->m_pVal_mut); - for (auto val : values) { - yyjson_mut_arr_append(handle->m_pVal_mut, val); + for (const auto& info : values) { + yyjson_mut_arr_append(handle->m_pVal_mut, info.val); } return true; } -const char* YYJSONManager::SkipSeparators(const char* ptr) +const char* JsonManager::SkipSeparators(const char* ptr) { while (*ptr && (isspace(*ptr) || *ptr == ':' || *ptr == ',')) { ptr++; @@ -2066,7 +2473,7 @@ const char* YYJSONManager::SkipSeparators(const char* ptr) return ptr; } -void YYJSONManager::SetPackError(char* error, size_t error_size, const char* fmt, ...) +void JsonManager::SetPackError(char* error, size_t error_size, const char* fmt, ...) { if (error && error_size > 0) { va_list args; @@ -2077,8 +2484,8 @@ void YYJSONManager::SetPackError(char* error, size_t error_size, const char* fmt } } -yyjson_mut_val* YYJSONManager::PackImpl(yyjson_mut_doc* doc, const char* format, - IPackParamProvider* provider, +yyjson_mut_val* JsonManager::PackImpl(yyjson_mut_doc* doc, const char* format, + IPackParamProvider* provider, char* error, size_t error_size, const char** out_end_ptr) { @@ -2133,7 +2540,7 @@ yyjson_mut_val* YYJSONManager::PackImpl(yyjson_mut_doc* doc, const char* format, } ptr = SkipSeparators(ptr + 1); - if (*ptr != 's' && *ptr != 'i' && *ptr != 'f' && *ptr != 'b' && + if (*ptr != 's' && *ptr != 'i' && *ptr != 'f' && *ptr != 'b' && *ptr != 'n' && *ptr != '{' && *ptr != '[') { SetPackError(error, error_size, "Invalid value type after key"); return nullptr; @@ -2296,117 +2703,125 @@ yyjson_mut_val* YYJSONManager::PackImpl(yyjson_mut_doc* doc, const char* format, return root; } -YYJSONValue* YYJSONManager::Pack(const char* format, IPackParamProvider* param_provider, char* error, size_t error_size) +JsonValue* JsonManager::Pack(const char* format, IPackParamProvider* param_provider, char* error, size_t error_size) { if (!format || !param_provider) { SetPackError(error, error_size, "Invalid arguments"); return nullptr; } - auto pYYJSONValue = CreateWrapper(); - pYYJSONValue->m_pDocument_mut = CreateDocument(); + auto pJSONValue = CreateWrapper(); + pJSONValue->m_pDocument_mut = CreateDocument(); - if (!pYYJSONValue->m_pDocument_mut) { + if (!pJSONValue->m_pDocument_mut) { SetPackError(error, error_size, "Failed to create document"); return nullptr; } const char* end_ptr = nullptr; - pYYJSONValue->m_pVal_mut = PackImpl(pYYJSONValue->m_pDocument_mut.get(), format, + pJSONValue->m_pVal_mut = PackImpl(pJSONValue->m_pDocument_mut.get(), format, param_provider, error, error_size, &end_ptr); - if (!pYYJSONValue->m_pVal_mut) { + if (!pJSONValue->m_pVal_mut) { return nullptr; } - yyjson_mut_doc_set_root(pYYJSONValue->m_pDocument_mut.get(), pYYJSONValue->m_pVal_mut); + yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut.get(), pJSONValue->m_pVal_mut); - return pYYJSONValue.release(); + return pJSONValue.release(); } -YYJSONValue* YYJSONManager::CreateBool(bool value) +JsonValue* JsonManager::CreateBool(bool value) { - auto pYYJSONValue = CreateWrapper(); - pYYJSONValue->m_pDocument_mut = CreateDocument(); - pYYJSONValue->m_pVal_mut = yyjson_mut_bool(pYYJSONValue->m_pDocument_mut.get(), value); + auto pJSONValue = CreateWrapper(); + pJSONValue->m_pDocument_mut = CreateDocument(); + pJSONValue->m_pVal_mut = yyjson_mut_bool(pJSONValue->m_pDocument_mut.get(), value); - if (!pYYJSONValue->m_pVal_mut) { + if (!pJSONValue->m_pVal_mut) { return nullptr; } - return pYYJSONValue.release(); + return pJSONValue.release(); } -YYJSONValue* YYJSONManager::CreateFloat(double value) +JsonValue* JsonManager::CreateFloat(double value) { - auto pYYJSONValue = CreateWrapper(); - pYYJSONValue->m_pDocument_mut = CreateDocument(); - pYYJSONValue->m_pVal_mut = yyjson_mut_real(pYYJSONValue->m_pDocument_mut.get(), value); + auto pJSONValue = CreateWrapper(); + pJSONValue->m_pDocument_mut = CreateDocument(); + pJSONValue->m_pVal_mut = yyjson_mut_real(pJSONValue->m_pDocument_mut.get(), value); - if (!pYYJSONValue->m_pVal_mut) { + if (!pJSONValue->m_pVal_mut) { return nullptr; } - return pYYJSONValue.release(); + return pJSONValue.release(); } -YYJSONValue* YYJSONManager::CreateInt(int value) +JsonValue* JsonManager::CreateInt(int value) { - auto pYYJSONValue = CreateWrapper(); - pYYJSONValue->m_pDocument_mut = CreateDocument(); - pYYJSONValue->m_pVal_mut = yyjson_mut_int(pYYJSONValue->m_pDocument_mut.get(), value); + auto pJSONValue = CreateWrapper(); + pJSONValue->m_pDocument_mut = CreateDocument(); + pJSONValue->m_pVal_mut = yyjson_mut_int(pJSONValue->m_pDocument_mut.get(), value); - if (!pYYJSONValue->m_pVal_mut) { + if (!pJSONValue->m_pVal_mut) { return nullptr; } - return pYYJSONValue.release(); + return pJSONValue.release(); } -YYJSONValue* YYJSONManager::CreateInt64(int64_t value) +JsonValue* JsonManager::CreateInt64(std::variant value) { - auto pYYJSONValue = CreateWrapper(); - pYYJSONValue->m_pDocument_mut = CreateDocument(); - pYYJSONValue->m_pVal_mut = yyjson_mut_sint(pYYJSONValue->m_pDocument_mut.get(), value); + auto pJSONValue = CreateWrapper(); + pJSONValue->m_pDocument_mut = CreateDocument(); + + std::visit([&](auto&& val) { + using T = std::decay_t; + if constexpr (std::is_same_v) { + pJSONValue->m_pVal_mut = yyjson_mut_sint(pJSONValue->m_pDocument_mut.get(), val); + } else if constexpr (std::is_same_v) { + pJSONValue->m_pVal_mut = yyjson_mut_uint(pJSONValue->m_pDocument_mut.get(), val); + } + }, value); - if (!pYYJSONValue->m_pVal_mut) { + if (!pJSONValue->m_pVal_mut) { return nullptr; } - return pYYJSONValue.release(); + return pJSONValue.release(); } -YYJSONValue* YYJSONManager::CreateNull() +JsonValue* JsonManager::CreateNull() { - auto pYYJSONValue = CreateWrapper(); - pYYJSONValue->m_pDocument_mut = CreateDocument(); - pYYJSONValue->m_pVal_mut = yyjson_mut_null(pYYJSONValue->m_pDocument_mut.get()); + auto pJSONValue = CreateWrapper(); + pJSONValue->m_pDocument_mut = CreateDocument(); + pJSONValue->m_pVal_mut = yyjson_mut_null(pJSONValue->m_pDocument_mut.get()); - if (!pYYJSONValue->m_pVal_mut) { + if (!pJSONValue->m_pVal_mut) { return nullptr; } - return pYYJSONValue.release(); + return pJSONValue.release(); } -YYJSONValue* YYJSONManager::CreateString(const char* value) +JsonValue* JsonManager::CreateString(const char* value) { if (!value) { return nullptr; } - auto pYYJSONValue = CreateWrapper(); - pYYJSONValue->m_pDocument_mut = CreateDocument(); - pYYJSONValue->m_pVal_mut = yyjson_mut_strcpy(pYYJSONValue->m_pDocument_mut.get(), value); + auto pJSONValue = CreateWrapper(); + pJSONValue->m_pDocument_mut = CreateDocument(); + pJSONValue->m_pVal_mut = yyjson_mut_strcpy(pJSONValue->m_pDocument_mut.get(), value); - if (!pYYJSONValue->m_pVal_mut) { + if (!pJSONValue->m_pVal_mut) { return nullptr; } - return pYYJSONValue.release(); + return pJSONValue.release(); } -bool YYJSONManager::GetBool(YYJSONValue* handle, bool* out_value) +bool JsonManager::GetBool(JsonValue* handle, bool* out_value) { if (!handle || !out_value) { return false; @@ -2427,7 +2842,7 @@ bool YYJSONManager::GetBool(YYJSONValue* handle, bool* out_value) } } -bool YYJSONManager::GetFloat(YYJSONValue* handle, double* out_value) +bool JsonManager::GetFloat(JsonValue* handle, double* out_value) { if (!handle || !out_value) { return false; @@ -2448,7 +2863,7 @@ bool YYJSONManager::GetFloat(YYJSONValue* handle, double* out_value) } } -bool YYJSONManager::GetInt(YYJSONValue* handle, int* out_value) +bool JsonManager::GetInt(JsonValue* handle, int* out_value) { if (!handle || !out_value) { return false; @@ -2469,7 +2884,7 @@ bool YYJSONManager::GetInt(YYJSONValue* handle, int* out_value) } } -bool YYJSONManager::GetInt64(YYJSONValue* handle, int64_t* out_value) +bool JsonManager::GetInt64(JsonValue* handle, std::variant* out_value) { if (!handle || !out_value) { return false; @@ -2479,18 +2894,18 @@ bool YYJSONManager::GetInt64(YYJSONValue* handle, int64_t* out_value) if (!yyjson_mut_is_int(handle->m_pVal_mut)) { return false; } - *out_value = yyjson_mut_get_sint(handle->m_pVal_mut); + ReadInt64FromMutVal(handle->m_pVal_mut, out_value); return true; } else { if (!yyjson_is_int(handle->m_pVal)) { return false; } - *out_value = yyjson_get_sint(handle->m_pVal); + ReadInt64FromVal(handle->m_pVal, out_value); return true; } } -bool YYJSONManager::GetString(YYJSONValue* handle, const char** out_str, size_t* out_len) +bool JsonManager::GetString(JsonValue* handle, const char** out_str, size_t* out_len) { if (!handle || !out_str) { return false; @@ -2517,7 +2932,7 @@ bool YYJSONManager::GetString(YYJSONValue* handle, const char** out_str, size_t* } } -YYJSONValue* YYJSONManager::PtrGet(YYJSONValue* handle, const char* path, char* error, size_t error_size) +JsonValue* JsonManager::PtrGet(JsonValue* handle, const char* path, char* error, size_t error_size) { if (!handle || !path) { if (error && error_size > 0) { @@ -2526,7 +2941,7 @@ YYJSONValue* YYJSONManager::PtrGet(YYJSONValue* handle, const char* path, char* return nullptr; } - auto pYYJSONValue = CreateWrapper(); + auto pJSONValue = CreateWrapper(); yyjson_ptr_err ptrGetError; if (handle->IsMutable()) { @@ -2534,33 +2949,33 @@ YYJSONValue* YYJSONManager::PtrGet(YYJSONValue* handle, const char* path, char* if (!val || ptrGetError.code) { if (error && error_size > 0) { - snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); } return nullptr; } - pYYJSONValue->m_pDocument_mut = handle->m_pDocument_mut; - pYYJSONValue->m_pVal_mut = val; + pJSONValue->m_pDocument_mut = handle->m_pDocument_mut; + pJSONValue->m_pVal_mut = val; } else { yyjson_val* val = yyjson_doc_ptr_getx(handle->m_pDocument.get(), path, strlen(path), &ptrGetError); if (!val || ptrGetError.code) { if (error && error_size > 0) { - snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); } return nullptr; } - pYYJSONValue->m_pDocument = handle->m_pDocument; - pYYJSONValue->m_pVal = val; + pJSONValue->m_pDocument = handle->m_pDocument; + pJSONValue->m_pVal = val; } - return pYYJSONValue.release(); + return pJSONValue.release(); } -bool YYJSONManager::PtrGetBool(YYJSONValue* handle, const char* path, bool* out_value, char* error, size_t error_size) +bool JsonManager::PtrGetBool(JsonValue* handle, const char* path, bool* out_value, char* error, size_t error_size) { if (!handle || !path || !out_value) { if (error && error_size > 0) { @@ -2576,7 +2991,7 @@ bool YYJSONManager::PtrGetBool(YYJSONValue* handle, const char* path, bool* out_ if (ptrGetError.code) { if (error && error_size > 0) { - snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); } return false; @@ -2596,7 +3011,7 @@ bool YYJSONManager::PtrGetBool(YYJSONValue* handle, const char* path, bool* out_ if (ptrGetError.code) { if (error && error_size > 0) { - snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); } return false; @@ -2614,7 +3029,7 @@ bool YYJSONManager::PtrGetBool(YYJSONValue* handle, const char* path, bool* out_ } } -bool YYJSONManager::PtrGetFloat(YYJSONValue* handle, const char* path, double* out_value, char* error, size_t error_size) +bool JsonManager::PtrGetFloat(JsonValue* handle, const char* path, double* out_value, char* error, size_t error_size) { if (!handle || !path || !out_value) { if (error && error_size > 0) { @@ -2630,7 +3045,7 @@ bool YYJSONManager::PtrGetFloat(YYJSONValue* handle, const char* path, double* o if (ptrGetError.code) { if (error && error_size > 0) { - snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); } return false; @@ -2650,7 +3065,7 @@ bool YYJSONManager::PtrGetFloat(YYJSONValue* handle, const char* path, double* o if (ptrGetError.code) { if (error && error_size > 0) { - snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); } return false; @@ -2668,7 +3083,7 @@ bool YYJSONManager::PtrGetFloat(YYJSONValue* handle, const char* path, double* o } } -bool YYJSONManager::PtrGetInt(YYJSONValue* handle, const char* path, int* out_value, char* error, size_t error_size) +bool JsonManager::PtrGetInt(JsonValue* handle, const char* path, int* out_value, char* error, size_t error_size) { if (!handle || !path || !out_value) { if (error && error_size > 0) { @@ -2684,7 +3099,7 @@ bool YYJSONManager::PtrGetInt(YYJSONValue* handle, const char* path, int* out_va if (ptrGetError.code) { if (error && error_size > 0) { - snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); } return false; @@ -2704,7 +3119,7 @@ bool YYJSONManager::PtrGetInt(YYJSONValue* handle, const char* path, int* out_va if (ptrGetError.code) { if (error && error_size > 0) { - snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); } return false; @@ -2722,7 +3137,7 @@ bool YYJSONManager::PtrGetInt(YYJSONValue* handle, const char* path, int* out_va } } -bool YYJSONManager::PtrGetInt64(YYJSONValue* handle, const char* path, int64_t* out_value, char* error, size_t error_size) +bool JsonManager::PtrGetInt64(JsonValue* handle, const char* path, std::variant* out_value, char* error, size_t error_size) { if (!handle || !path || !out_value) { if (error && error_size > 0) { @@ -2736,9 +3151,9 @@ bool YYJSONManager::PtrGetInt64(YYJSONValue* handle, const char* path, int64_t* if (handle->IsMutable()) { yyjson_mut_val* val = yyjson_mut_doc_ptr_getx(handle->m_pDocument_mut.get(), path, strlen(path), nullptr, &ptrGetError); - if (ptrGetError.code) { + if (!val || ptrGetError.code) { if (error && error_size > 0) { - snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); } return false; @@ -2751,14 +3166,14 @@ bool YYJSONManager::PtrGetInt64(YYJSONValue* handle, const char* path, int64_t* return false; } - *out_value = yyjson_mut_get_sint(val); + ReadInt64FromMutVal(val, out_value); return true; } else { yyjson_val* val = yyjson_doc_ptr_getx(handle->m_pDocument.get(), path, strlen(path), &ptrGetError); - if (ptrGetError.code) { + if (!val || ptrGetError.code) { if (error && error_size > 0) { - snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); } return false; @@ -2771,12 +3186,12 @@ bool YYJSONManager::PtrGetInt64(YYJSONValue* handle, const char* path, int64_t* return false; } - *out_value = yyjson_get_sint(val); + ReadInt64FromVal(val, out_value); return true; } } -bool YYJSONManager::PtrGetString(YYJSONValue* handle, const char* path, const char** out_str, size_t* out_len, char* error, size_t error_size) +bool JsonManager::PtrGetString(JsonValue* handle, const char* path, const char** out_str, size_t* out_len, char* error, size_t error_size) { if (!handle || !path || !out_str) { if (error && error_size > 0) { @@ -2792,7 +3207,7 @@ bool YYJSONManager::PtrGetString(YYJSONValue* handle, const char* path, const ch if (ptrGetError.code) { if (error && error_size > 0) { - snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); } return false; @@ -2815,7 +3230,7 @@ bool YYJSONManager::PtrGetString(YYJSONValue* handle, const char* path, const ch if (ptrGetError.code) { if (error && error_size > 0) { - snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); } return false; @@ -2836,7 +3251,7 @@ bool YYJSONManager::PtrGetString(YYJSONValue* handle, const char* path, const ch } } -bool YYJSONManager::PtrGetIsNull(YYJSONValue* handle, const char* path, bool* out_is_null, char* error, size_t error_size) +bool JsonManager::PtrGetIsNull(JsonValue* handle, const char* path, bool* out_is_null, char* error, size_t error_size) { if (!handle || !path || !out_is_null) { if (error && error_size > 0) { @@ -2852,7 +3267,7 @@ bool YYJSONManager::PtrGetIsNull(YYJSONValue* handle, const char* path, bool* ou if (ptrGetError.code) { if (error && error_size > 0) { - snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); } return false; @@ -2865,7 +3280,7 @@ bool YYJSONManager::PtrGetIsNull(YYJSONValue* handle, const char* path, bool* ou if (ptrGetError.code) { if (error && error_size > 0) { - snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); } return false; @@ -2876,7 +3291,7 @@ bool YYJSONManager::PtrGetIsNull(YYJSONValue* handle, const char* path, bool* ou } } -bool YYJSONManager::PtrGetLength(YYJSONValue* handle, const char* path, size_t* out_len, char* error, size_t error_size) +bool JsonManager::PtrGetLength(JsonValue* handle, const char* path, size_t* out_len, char* error, size_t error_size) { if (!handle || !path || !out_len) { if (error && error_size > 0) { @@ -2892,7 +3307,7 @@ bool YYJSONManager::PtrGetLength(YYJSONValue* handle, const char* path, size_t* if (ptrGetError.code) { if (error && error_size > 0) { - snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); } return false; @@ -2909,7 +3324,7 @@ bool YYJSONManager::PtrGetLength(YYJSONValue* handle, const char* path, size_t* if (ptrGetError.code) { if (error && error_size > 0) { - snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); } return false; @@ -2924,7 +3339,7 @@ bool YYJSONManager::PtrGetLength(YYJSONValue* handle, const char* path, size_t* } } -bool YYJSONManager::PtrSet(YYJSONValue* handle, const char* path, YYJSONValue* value, char* error, size_t error_size) +bool JsonManager::PtrSet(JsonValue* handle, const char* path, JsonValue* value, char* error, size_t error_size) { if (!handle || !handle->IsMutable() || !path || !value) { if (error && error_size > 0) { @@ -2951,14 +3366,14 @@ bool YYJSONManager::PtrSet(YYJSONValue* handle, const char* path, YYJSONValue* v bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut.get(), path, strlen(path), val_copy, true, nullptr, &ptrSetError); if (ptrSetError.code && error && error_size > 0) { - snprintf(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", + snprintf(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrSetError.msg, ptrSetError.code, ptrSetError.pos, path); } return success; } -bool YYJSONManager::PtrSetBool(YYJSONValue* handle, const char* path, bool value, char* error, size_t error_size) +bool JsonManager::PtrSetBool(JsonValue* handle, const char* path, bool value, char* error, size_t error_size) { if (!handle || !handle->IsMutable() || !path) { if (error && error_size > 0) { @@ -2971,14 +3386,14 @@ bool YYJSONManager::PtrSetBool(YYJSONValue* handle, const char* path, bool value bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_bool(handle->m_pDocument_mut.get(), value), true, nullptr, &ptrSetError); if (ptrSetError.code && error && error_size > 0) { - snprintf(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", + snprintf(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrSetError.msg, ptrSetError.code, ptrSetError.pos, path); } return success; } -bool YYJSONManager::PtrSetFloat(YYJSONValue* handle, const char* path, double value, char* error, size_t error_size) +bool JsonManager::PtrSetFloat(JsonValue* handle, const char* path, double value, char* error, size_t error_size) { if (!handle || !handle->IsMutable() || !path) { if (error && error_size > 0) { @@ -2991,14 +3406,14 @@ bool YYJSONManager::PtrSetFloat(YYJSONValue* handle, const char* path, double va bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_real(handle->m_pDocument_mut.get(), value), true, nullptr, &ptrSetError); if (ptrSetError.code && error && error_size > 0) { - snprintf(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", + snprintf(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrSetError.msg, ptrSetError.code, ptrSetError.pos, path); } return success; } -bool YYJSONManager::PtrSetInt(YYJSONValue* handle, const char* path, int value, char* error, size_t error_size) +bool JsonManager::PtrSetInt(JsonValue* handle, const char* path, int value, char* error, size_t error_size) { if (!handle || !handle->IsMutable() || !path) { if (error && error_size > 0) { @@ -3011,14 +3426,14 @@ bool YYJSONManager::PtrSetInt(YYJSONValue* handle, const char* path, int value, bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_int(handle->m_pDocument_mut.get(), value), true, nullptr, &ptrSetError); if (ptrSetError.code && error && error_size > 0) { - snprintf(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", + snprintf(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrSetError.msg, ptrSetError.code, ptrSetError.pos, path); } return success; } -bool YYJSONManager::PtrSetInt64(YYJSONValue* handle, const char* path, int64_t value, char* error, size_t error_size) +bool JsonManager::PtrSetInt64(JsonValue* handle, const char* path, std::variant value, char* error, size_t error_size) { if (!handle || !handle->IsMutable() || !path) { if (error && error_size > 0) { @@ -3028,17 +3443,25 @@ bool YYJSONManager::PtrSetInt64(YYJSONValue* handle, const char* path, int64_t v } yyjson_ptr_err ptrSetError; - bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_sint(handle->m_pDocument_mut.get(), value), true, nullptr, &ptrSetError); + bool success = std::visit([&](auto&& val) -> bool { + using T = std::decay_t; + if constexpr (std::is_same_v) { + return yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_sint(handle->m_pDocument_mut.get(), val), true, nullptr, &ptrSetError); + } else if constexpr (std::is_same_v) { + return yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_uint(handle->m_pDocument_mut.get(), val), true, nullptr, &ptrSetError); + } + return false; + }, value); - if (ptrSetError.code && error && error_size > 0) { - snprintf(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", + if (!success && ptrSetError.code && error && error_size > 0) { + snprintf(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrSetError.msg, ptrSetError.code, ptrSetError.pos, path); } return success; } -bool YYJSONManager::PtrSetString(YYJSONValue* handle, const char* path, const char* value, char* error, size_t error_size) +bool JsonManager::PtrSetString(JsonValue* handle, const char* path, const char* value, char* error, size_t error_size) { if (!handle || !handle->IsMutable() || !path || !value) { if (error && error_size > 0) { @@ -3051,14 +3474,14 @@ bool YYJSONManager::PtrSetString(YYJSONValue* handle, const char* path, const ch bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_strcpy(handle->m_pDocument_mut.get(), value), true, nullptr, &ptrSetError); if (ptrSetError.code && error && error_size > 0) { - snprintf(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", + snprintf(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrSetError.msg, ptrSetError.code, ptrSetError.pos, path); } return success; } -bool YYJSONManager::PtrSetNull(YYJSONValue* handle, const char* path, char* error, size_t error_size) +bool JsonManager::PtrSetNull(JsonValue* handle, const char* path, char* error, size_t error_size) { if (!handle || !handle->IsMutable() || !path) { if (error && error_size > 0) { @@ -3071,14 +3494,14 @@ bool YYJSONManager::PtrSetNull(YYJSONValue* handle, const char* path, char* erro bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_null(handle->m_pDocument_mut.get()), true, nullptr, &ptrSetError); if (ptrSetError.code && error && error_size > 0) { - snprintf(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", + snprintf(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrSetError.msg, ptrSetError.code, ptrSetError.pos, path); } return success; } -bool YYJSONManager::PtrAdd(YYJSONValue* handle, const char* path, YYJSONValue* value, char* error, size_t error_size) +bool JsonManager::PtrAdd(JsonValue* handle, const char* path, JsonValue* value, char* error, size_t error_size) { if (!handle || !handle->IsMutable() || !path || !value) { if (error && error_size > 0) { @@ -3105,14 +3528,14 @@ bool YYJSONManager::PtrAdd(YYJSONValue* handle, const char* path, YYJSONValue* v bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut.get(), path, strlen(path), val_copy, true, nullptr, &ptrAddError); if (ptrAddError.code && error && error_size > 0) { - snprintf(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", + snprintf(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrAddError.msg, ptrAddError.code, ptrAddError.pos, path); } return success; } -bool YYJSONManager::PtrAddBool(YYJSONValue* handle, const char* path, bool value, char* error, size_t error_size) +bool JsonManager::PtrAddBool(JsonValue* handle, const char* path, bool value, char* error, size_t error_size) { if (!handle || !handle->IsMutable() || !path) { if (error && error_size > 0) { @@ -3125,14 +3548,14 @@ bool YYJSONManager::PtrAddBool(YYJSONValue* handle, const char* path, bool value bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_bool(handle->m_pDocument_mut.get(), value), true, nullptr, &ptrAddError); if (ptrAddError.code && error && error_size > 0) { - snprintf(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", + snprintf(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrAddError.msg, ptrAddError.code, ptrAddError.pos, path); } return success; } -bool YYJSONManager::PtrAddFloat(YYJSONValue* handle, const char* path, double value, char* error, size_t error_size) +bool JsonManager::PtrAddFloat(JsonValue* handle, const char* path, double value, char* error, size_t error_size) { if (!handle || !handle->IsMutable() || !path) { if (error && error_size > 0) { @@ -3145,14 +3568,14 @@ bool YYJSONManager::PtrAddFloat(YYJSONValue* handle, const char* path, double va bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_real(handle->m_pDocument_mut.get(), value), true, nullptr, &ptrAddError); if (ptrAddError.code && error && error_size > 0) { - snprintf(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", + snprintf(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrAddError.msg, ptrAddError.code, ptrAddError.pos, path); } return success; } -bool YYJSONManager::PtrAddInt(YYJSONValue* handle, const char* path, int value, char* error, size_t error_size) +bool JsonManager::PtrAddInt(JsonValue* handle, const char* path, int value, char* error, size_t error_size) { if (!handle || !handle->IsMutable() || !path) { if (error && error_size > 0) { @@ -3165,14 +3588,14 @@ bool YYJSONManager::PtrAddInt(YYJSONValue* handle, const char* path, int value, bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_int(handle->m_pDocument_mut.get(), value), true, nullptr, &ptrAddError); if (ptrAddError.code && error && error_size > 0) { - snprintf(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", + snprintf(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrAddError.msg, ptrAddError.code, ptrAddError.pos, path); } return success; } -bool YYJSONManager::PtrAddInt64(YYJSONValue* handle, const char* path, int64_t value, char* error, size_t error_size) +bool JsonManager::PtrAddInt64(JsonValue* handle, const char* path, std::variant value, char* error, size_t error_size) { if (!handle || !handle->IsMutable() || !path) { if (error && error_size > 0) { @@ -3182,17 +3605,25 @@ bool YYJSONManager::PtrAddInt64(YYJSONValue* handle, const char* path, int64_t v } yyjson_ptr_err ptrAddError; - bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_sint(handle->m_pDocument_mut.get(), value), true, nullptr, &ptrAddError); + bool success = std::visit([&](auto&& val) -> bool { + using T = std::decay_t; + if constexpr (std::is_same_v) { + return yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_sint(handle->m_pDocument_mut.get(), val), true, nullptr, &ptrAddError); + } else if constexpr (std::is_same_v) { + return yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_uint(handle->m_pDocument_mut.get(), val), true, nullptr, &ptrAddError); + } + return false; + }, value); if (ptrAddError.code && error && error_size > 0) { - snprintf(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", + snprintf(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrAddError.msg, ptrAddError.code, ptrAddError.pos, path); } return success; } -bool YYJSONManager::PtrAddString(YYJSONValue* handle, const char* path, const char* value, char* error, size_t error_size) +bool JsonManager::PtrAddString(JsonValue* handle, const char* path, const char* value, char* error, size_t error_size) { if (!handle || !handle->IsMutable() || !path || !value) { if (error && error_size > 0) { @@ -3205,14 +3636,14 @@ bool YYJSONManager::PtrAddString(YYJSONValue* handle, const char* path, const ch bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_strcpy(handle->m_pDocument_mut.get(), value), true, nullptr, &ptrAddError); if (ptrAddError.code && error && error_size > 0) { - snprintf(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", + snprintf(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrAddError.msg, ptrAddError.code, ptrAddError.pos, path); } return success; } -bool YYJSONManager::PtrAddNull(YYJSONValue* handle, const char* path, char* error, size_t error_size) +bool JsonManager::PtrAddNull(JsonValue* handle, const char* path, char* error, size_t error_size) { if (!handle || !handle->IsMutable() || !path) { if (error && error_size > 0) { @@ -3225,14 +3656,14 @@ bool YYJSONManager::PtrAddNull(YYJSONValue* handle, const char* path, char* erro bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_null(handle->m_pDocument_mut.get()), true, nullptr, &ptrAddError); if (ptrAddError.code && error && error_size > 0) { - snprintf(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", + snprintf(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrAddError.msg, ptrAddError.code, ptrAddError.pos, path); } return success; } -bool YYJSONManager::PtrRemove(YYJSONValue* handle, const char* path, char* error, size_t error_size) +bool JsonManager::PtrRemove(JsonValue* handle, const char* path, char* error, size_t error_size) { if (!handle || !handle->IsMutable() || !path) { if (error && error_size > 0) { @@ -3245,192 +3676,200 @@ bool YYJSONManager::PtrRemove(YYJSONValue* handle, const char* path, char* error bool success = yyjson_mut_doc_ptr_removex(handle->m_pDocument_mut.get(), path, strlen(path), nullptr, &ptrRemoveError) != nullptr; if (ptrRemoveError.code && error && error_size > 0) { - snprintf(error, error_size, "Failed to remove JSON pointer: %s (error code: %u, position: %zu, path: %s)", + snprintf(error, error_size, "Failed to remove JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrRemoveError.msg, ptrRemoveError.code, ptrRemoveError.pos, path); } return success; } -YYJSONValue* YYJSONManager::PtrTryGet(YYJSONValue* handle, const char* path) +JsonManager::PtrGetValueResult JsonManager::PtrGetValueInternal(JsonValue* handle, const char* path) { + PtrGetValueResult result; + result.success = false; + if (!handle || !path) { - return nullptr; + return result; } - auto pYYJSONValue = CreateWrapper(); yyjson_ptr_err ptrGetError; if (handle->IsMutable()) { - yyjson_mut_val* val = yyjson_mut_doc_ptr_getx(handle->m_pDocument_mut.get(), path, strlen(path), nullptr, &ptrGetError); + result.mut_val = yyjson_mut_doc_ptr_getx(handle->m_pDocument_mut.get(), path, strlen(path), nullptr, &ptrGetError); + if (result.mut_val && !ptrGetError.code) { + result.success = true; + } + } else { + result.imm_val = yyjson_doc_ptr_getx(handle->m_pDocument.get(), path, strlen(path), &ptrGetError); + if (result.imm_val && !ptrGetError.code) { + result.success = true; + } + } - if (!val || ptrGetError.code) { + return result; +} + +JsonValue* JsonManager::PtrTryGet(JsonValue* handle, const char* path) +{ + if (!handle || !path) { return nullptr; } - pYYJSONValue->m_pDocument_mut = handle->m_pDocument_mut; - pYYJSONValue->m_pVal_mut = val; - } else { - yyjson_val* val = yyjson_doc_ptr_getx(handle->m_pDocument.get(), path, strlen(path), &ptrGetError); - - if (!val || ptrGetError.code) { + auto result = PtrGetValueInternal(handle, path); + if (!result.success) { return nullptr; } - pYYJSONValue->m_pDocument = handle->m_pDocument; - pYYJSONValue->m_pVal = val; + auto pJSONValue = CreateWrapper(); + if (handle->IsMutable()) { + pJSONValue->m_pDocument_mut = handle->m_pDocument_mut; + pJSONValue->m_pVal_mut = result.mut_val; + } else { + pJSONValue->m_pDocument = handle->m_pDocument; + pJSONValue->m_pVal = result.imm_val; } - return pYYJSONValue.release(); + return pJSONValue.release(); } -bool YYJSONManager::PtrTryGetBool(YYJSONValue* handle, const char* path, bool* out_value) +bool JsonManager::PtrTryGetBool(JsonValue* handle, const char* path, bool* out_value) { if (!handle || !path || !out_value) { return false; } - yyjson_ptr_err ptrGetError; + auto result = PtrGetValueInternal(handle, path); + if (!result.success) { + return false; + } if (handle->IsMutable()) { - yyjson_mut_val* val = yyjson_mut_doc_ptr_getx(handle->m_pDocument_mut.get(), path, strlen(path), nullptr, &ptrGetError); - - if (!val || ptrGetError.code || !yyjson_mut_is_bool(val)) { + if (!yyjson_mut_is_bool(result.mut_val)) { return false; } - - *out_value = yyjson_mut_get_bool(val); + *out_value = yyjson_mut_get_bool(result.mut_val); return true; } else { - yyjson_val* val = yyjson_doc_ptr_getx(handle->m_pDocument.get(), path, strlen(path), &ptrGetError); - - if (!val || ptrGetError.code || !yyjson_is_bool(val)) { + if (!yyjson_is_bool(result.imm_val)) { return false; } - - *out_value = yyjson_get_bool(val); + *out_value = yyjson_get_bool(result.imm_val); return true; } } -bool YYJSONManager::PtrTryGetFloat(YYJSONValue* handle, const char* path, double* out_value) +bool JsonManager::PtrTryGetFloat(JsonValue* handle, const char* path, double* out_value) { if (!handle || !path || !out_value) { return false; } - yyjson_ptr_err ptrGetError; + auto result = PtrGetValueInternal(handle, path); + if (!result.success) { + return false; + } if (handle->IsMutable()) { - yyjson_mut_val* val = yyjson_mut_doc_ptr_getx(handle->m_pDocument_mut.get(), path, strlen(path), nullptr, &ptrGetError); - - if (!val || ptrGetError.code || !yyjson_mut_is_num(val)) { + if (!yyjson_mut_is_num(result.mut_val)) { return false; } - - *out_value = yyjson_mut_get_num(val); + *out_value = yyjson_mut_get_num(result.mut_val); return true; } else { - yyjson_val* val = yyjson_doc_ptr_getx(handle->m_pDocument.get(), path, strlen(path), &ptrGetError); - - if (!val || ptrGetError.code || !yyjson_is_num(val)) { + if (!yyjson_is_num(result.imm_val)) { return false; } - - *out_value = yyjson_get_num(val); + *out_value = yyjson_get_num(result.imm_val); return true; } } -bool YYJSONManager::PtrTryGetInt(YYJSONValue* handle, const char* path, int* out_value) +bool JsonManager::PtrTryGetInt(JsonValue* handle, const char* path, int* out_value) { if (!handle || !path || !out_value) { return false; } - yyjson_ptr_err ptrGetError; + auto result = PtrGetValueInternal(handle, path); + if (!result.success) { + return false; + } if (handle->IsMutable()) { - yyjson_mut_val* val = yyjson_mut_doc_ptr_getx(handle->m_pDocument_mut.get(), path, strlen(path), nullptr, &ptrGetError); - - if (!val || ptrGetError.code || !yyjson_mut_is_int(val)) { + if (!yyjson_mut_is_int(result.mut_val)) { return false; } - *out_value = yyjson_mut_get_int(val); + *out_value = yyjson_mut_get_int(result.mut_val); return true; } else { - yyjson_val* val = yyjson_doc_ptr_getx(handle->m_pDocument.get(), path, strlen(path), &ptrGetError); - - if (!val || ptrGetError.code || !yyjson_is_int(val)) { + if (!yyjson_is_int(result.imm_val)) { return false; } - *out_value = yyjson_get_int(val); + *out_value = yyjson_get_int(result.imm_val); return true; } } -bool YYJSONManager::PtrTryGetInt64(YYJSONValue* handle, const char* path, int64_t* out_value) +bool JsonManager::PtrTryGetInt64(JsonValue* handle, const char* path, std::variant* out_value) { if (!handle || !path || !out_value) { return false; } - yyjson_ptr_err ptrGetError; + auto result = PtrGetValueInternal(handle, path); + if (!result.success) { + return false; + } if (handle->IsMutable()) { - yyjson_mut_val* val = yyjson_mut_doc_ptr_getx(handle->m_pDocument_mut.get(), path, strlen(path), nullptr, &ptrGetError); - if (!val || ptrGetError.code || !yyjson_mut_is_int(val)) { + if (!yyjson_mut_is_int(result.mut_val)) { return false; } - *out_value = yyjson_mut_get_sint(val); + ReadInt64FromMutVal(result.mut_val, out_value); return true; } else { - yyjson_val* val = yyjson_doc_ptr_getx(handle->m_pDocument.get(), path, strlen(path), &ptrGetError); - if (!val || ptrGetError.code || !yyjson_is_int(val)) { + if (!yyjson_is_int(result.imm_val)) { return false; } - *out_value = yyjson_get_sint(val); + ReadInt64FromVal(result.imm_val, out_value); return true; } } -bool YYJSONManager::PtrTryGetString(YYJSONValue* handle, const char* path, const char** out_str, size_t* out_len) +bool JsonManager::PtrTryGetString(JsonValue* handle, const char* path, const char** out_str, size_t* out_len) { if (!handle || !path || !out_str) { + return false; + } + + auto result = PtrGetValueInternal(handle, path); + if (!result.success) { return false; } - yyjson_ptr_err ptrGetError; - if (handle->IsMutable()) { - yyjson_mut_val* val = yyjson_mut_doc_ptr_getx(handle->m_pDocument_mut.get(), path, strlen(path), nullptr, &ptrGetError); - - if (!val || ptrGetError.code || !yyjson_mut_is_str(val)) { + if (!yyjson_mut_is_str(result.mut_val)) { return false; } - - *out_str = yyjson_mut_get_str(val); + *out_str = yyjson_mut_get_str(result.mut_val); if (out_len) { - *out_len = yyjson_mut_get_len(val); + *out_len = yyjson_mut_get_len(result.mut_val); } return true; } else { - yyjson_val* val = yyjson_doc_ptr_getx(handle->m_pDocument.get(), path, strlen(path), &ptrGetError); - - if (!val || ptrGetError.code || !yyjson_is_str(val)) { + if (!yyjson_is_str(result.imm_val)) { return false; } - - *out_str = yyjson_get_str(val); + *out_str = yyjson_get_str(result.imm_val); if (out_len) { - *out_len = yyjson_get_len(val); + *out_len = yyjson_get_len(result.imm_val); } return true; } } -bool YYJSONManager::ObjectForeachNext(YYJSONValue* handle, const char** out_key, - size_t* out_key_len, YYJSONValue** out_value) +bool JsonManager::ObjectForeachNext(JsonValue* handle, const char** out_key, + size_t* out_key_len, JsonValue** out_value) { if (!handle || !IsObject(handle)) { return false; @@ -3490,8 +3929,8 @@ bool YYJSONManager::ObjectForeachNext(YYJSONValue* handle, const char** out_key, return false; } -bool YYJSONManager::ArrayForeachNext(YYJSONValue* handle, size_t* out_index, - YYJSONValue** out_value) +bool JsonManager::ArrayForeachNext(JsonValue* handle, size_t* out_index, + JsonValue** out_value) { if (!handle || !IsArray(handle)) { return false; @@ -3543,7 +3982,7 @@ bool YYJSONManager::ArrayForeachNext(YYJSONValue* handle, size_t* out_index, return false; } -bool YYJSONManager::ObjectForeachKeyNext(YYJSONValue* handle, const char** out_key, +bool JsonManager::ObjectForeachKeyNext(JsonValue* handle, const char** out_key, size_t* out_key_len) { if (!handle || !IsObject(handle)) { @@ -3588,7 +4027,7 @@ bool YYJSONManager::ObjectForeachKeyNext(YYJSONValue* handle, const char** out_k return false; } -bool YYJSONManager::ArrayForeachIndexNext(YYJSONValue* handle, size_t* out_index) +bool JsonManager::ArrayForeachIndexNext(JsonValue* handle, size_t* out_index) { if (!handle || !IsArray(handle)) { return false; @@ -3628,29 +4067,607 @@ bool YYJSONManager::ArrayForeachIndexNext(YYJSONValue* handle, size_t* out_index return false; } -void YYJSONManager::Release(YYJSONValue* value) +void JsonManager::Release(JsonValue* value) { if (value) { delete value; } } -HandleType_t YYJSONManager::GetHandleType() +HandleType_t JsonManager::GetHandleType() { - return g_htJSON; + return g_JsonType; } -YYJSONValue* YYJSONManager::GetFromHandle(IPluginContext* pContext, Handle_t handle) +JsonValue* JsonManager::GetFromHandle(IPluginContext* pContext, Handle_t handle) { HandleError err; HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity()); - YYJSONValue* pYYJSONValue; - if ((err = handlesys->ReadHandle(handle, g_htJSON, &sec, (void**)&pYYJSONValue)) != HandleError_None) + JsonValue* pJSONValue; + if ((err = handlesys->ReadHandle(handle, g_JsonType, &sec, (void**)&pJSONValue)) != HandleError_None) { - pContext->ReportError("Invalid YYJSON handle %x (error %d)", handle, err); + pContext->ReportError("Invalid JSON handle %x (error %d)", handle, err); + return nullptr; + } + + return pJSONValue; +} + +JsonArrIter* JsonManager::ArrIterInit(JsonValue* handle) +{ + return ArrIterWith(handle); +} + +JsonArrIter* JsonManager::ArrIterWith(JsonValue* handle) +{ + if (!handle || !IsArray(handle)) { + return nullptr; + } + + auto iter = new JsonArrIter(); + iter->m_isMutable = handle->IsMutable(); + iter->m_pDocument_mut = handle->m_pDocument_mut; + iter->m_pDocument = handle->m_pDocument; + + if (handle->IsMutable()) { + if (!yyjson_mut_arr_iter_init(handle->m_pVal_mut, &iter->m_iterMut)) { + delete iter; + return nullptr; + } + } else { + if (!yyjson_arr_iter_init(handle->m_pVal, &iter->m_iterImm)) { + delete iter; + return nullptr; + } + } + + iter->m_initialized = true; + return iter; +} + +JsonValue* JsonManager::ArrIterNext(JsonArrIter* iter) +{ + if (!iter || !iter->m_initialized) { + return nullptr; + } + + JsonValue* val = nullptr; + + if (iter->m_isMutable) { + yyjson_mut_val* raw_val = yyjson_mut_arr_iter_next(&iter->m_iterMut); + if (!raw_val) { + return nullptr; + } + + auto pWrapper = CreateWrapper(); + pWrapper->m_pDocument_mut = iter->m_pDocument_mut; + pWrapper->m_pVal_mut = raw_val; + val = pWrapper.release(); + } else { + yyjson_val* raw_val = yyjson_arr_iter_next(&iter->m_iterImm); + if (!raw_val) { + return nullptr; + } + + auto pWrapper = CreateWrapper(); + pWrapper->m_pDocument = iter->m_pDocument; + pWrapper->m_pVal = raw_val; + val = pWrapper.release(); + } + + return val; +} + +bool JsonManager::ArrIterHasNext(JsonArrIter* iter) +{ + if (!iter || !iter->m_initialized) { + return false; + } + + if (iter->m_isMutable) { + return yyjson_mut_arr_iter_has_next(&iter->m_iterMut); + } else { + return yyjson_arr_iter_has_next(&iter->m_iterImm); + } +} + +size_t JsonManager::ArrIterGetIndex(JsonArrIter* iter) +{ + if (!iter || !iter->m_initialized) { + return SIZE_MAX; + } + + if (iter->m_isMutable) { + if (iter->m_iterMut.idx == 0) { + return SIZE_MAX; + } + return iter->m_iterMut.idx - 1; + } else { + if (iter->m_iterImm.idx == 0) { + return SIZE_MAX; + } + return iter->m_iterImm.idx - 1; + } +} + +void* JsonManager::ArrIterRemove(JsonArrIter* iter) +{ + if (!iter || !iter->m_isMutable) { + return nullptr; + } + + return yyjson_mut_arr_iter_remove(&iter->m_iterMut); +} + +JsonObjIter* JsonManager::ObjIterInit(JsonValue* handle) +{ + return ObjIterWith(handle); +} + +JsonObjIter* JsonManager::ObjIterWith(JsonValue* handle) +{ + if (!handle || !IsObject(handle)) { + return nullptr; + } + + auto iter = new JsonObjIter(); + iter->m_isMutable = handle->IsMutable(); + iter->m_pDocument_mut = handle->m_pDocument_mut; + iter->m_pDocument = handle->m_pDocument; + + if (handle->IsMutable()) { + if (!yyjson_mut_obj_iter_init(handle->m_pVal_mut, &iter->m_iterMut)) { + delete iter; + return nullptr; + } + } else { + if (!yyjson_obj_iter_init(handle->m_pVal, &iter->m_iterImm)) { + delete iter; + return nullptr; + } + } + + iter->m_initialized = true; + return iter; +} + +void* JsonManager::ObjIterNext(JsonObjIter* iter) +{ + if (!iter || !iter->m_initialized) { + return nullptr; + } + + if (iter->m_isMutable) { + yyjson_mut_val* current_key = yyjson_mut_obj_iter_next(&iter->m_iterMut); + if (!current_key) { + return nullptr; + } + iter->m_currentKey = current_key; + return current_key; + } else { + void* key = yyjson_obj_iter_next(&iter->m_iterImm); + iter->m_currentKey = key; + return key; + } +} + +bool JsonManager::ObjIterHasNext(JsonObjIter* iter) +{ + if (!iter || !iter->m_initialized) { + return false; + } + + if (iter->m_isMutable) { + return yyjson_mut_obj_iter_has_next(&iter->m_iterMut); + } else { + return yyjson_obj_iter_has_next(&iter->m_iterImm); + } +} + +JsonValue* JsonManager::ObjIterGetVal(JsonObjIter* iter, void* key) +{ + if (!iter || !iter->m_initialized || !key) { return nullptr; } - return pYYJSONValue; + auto pWrapper = CreateWrapper(); + + if (iter->m_isMutable) { + yyjson_mut_val* val = yyjson_mut_obj_iter_get_val(reinterpret_cast(key)); + if (!val) { + return nullptr; + } + pWrapper->m_pDocument_mut = iter->m_pDocument_mut; + pWrapper->m_pVal_mut = val; + } else { + yyjson_val* val = yyjson_obj_iter_get_val(reinterpret_cast(key)); + if (!val) { + return nullptr; + } + pWrapper->m_pDocument = iter->m_pDocument; + pWrapper->m_pVal = val; + } + + return pWrapper.release(); +} + +JsonValue* JsonManager::ObjIterGet(JsonObjIter* iter, const char* key) +{ + if (!iter || !iter->m_initialized || !key) { + return nullptr; + } + + auto pWrapper = CreateWrapper(); + + if (iter->m_isMutable) { + yyjson_mut_val* val = yyjson_mut_obj_iter_get(&iter->m_iterMut, key); + if (!val) { + return nullptr; + } + pWrapper->m_pDocument_mut = iter->m_pDocument_mut; + pWrapper->m_pVal_mut = val; + } else { + yyjson_val* val = yyjson_obj_iter_get(&iter->m_iterImm, key); + if (!val) { + return nullptr; + } + pWrapper->m_pDocument = iter->m_pDocument; + pWrapper->m_pVal = val; + } + + return pWrapper.release(); +} + +size_t JsonManager::ObjIterGetIndex(JsonObjIter* iter) +{ + if (!iter || !iter->m_initialized) { + return SIZE_MAX; + } + if (iter->m_isMutable) { + if (iter->m_currentKey == nullptr) { + return SIZE_MAX; + } + if (iter->m_iterMut.idx >= iter->m_iterMut.max) { + return iter->m_iterMut.max - 1; + } + return iter->m_iterMut.idx - 1; + } else { + if (iter->m_iterImm.idx == 0) { + return SIZE_MAX; + } + if (iter->m_iterImm.idx >= iter->m_iterImm.max) { + return iter->m_iterImm.max - 1; + } + return iter->m_iterImm.idx - 1; + } +} + +void* JsonManager::ObjIterRemove(JsonObjIter* iter) +{ + if (!iter || !iter->m_isMutable) { + return nullptr; + } + + return yyjson_mut_obj_iter_remove(&iter->m_iterMut); +} + +void JsonManager::ReleaseArrIter(JsonArrIter* iter) +{ + if (iter) { + delete iter; + } +} + +void JsonManager::ReleaseObjIter(JsonObjIter* iter) +{ + if (iter) { + delete iter; + } +} + +HandleType_t JsonManager::GetArrIterHandleType() +{ + return g_ArrIterType; +} + +HandleType_t JsonManager::GetObjIterHandleType() +{ + return g_ObjIterType; +} + +JsonArrIter* JsonManager::GetArrIterFromHandle(IPluginContext* pContext, Handle_t handle) +{ + HandleError err; + HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity()); + + JsonArrIter* pIter; + if ((err = handlesys->ReadHandle(handle, g_ArrIterType, &sec, (void**)&pIter)) != HandleError_None) + { + pContext->ReportError("Invalid JSONArrIter handle %x (error %d)", handle, err); + return nullptr; + } + + return pIter; +} + +JsonObjIter* JsonManager::GetObjIterFromHandle(IPluginContext* pContext, Handle_t handle) +{ + HandleError err; + HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity()); + + JsonObjIter* pIter; + if ((err = handlesys->ReadHandle(handle, g_ObjIterType, &sec, (void**)&pIter)) != HandleError_None) + { + pContext->ReportError("Invalid JSONObjIter handle %x (error %d)", handle, err); + return nullptr; + } + + return pIter; +} + +JsonValue* JsonManager::ReadNumber(const char* dat, uint32_t read_flg, char* error, size_t error_size, size_t* out_consumed) +{ + if (!dat) { + if (error && error_size > 0) { + snprintf(error, error_size, "Invalid input data"); + } + return nullptr; + } + + auto pJSONValue = CreateWrapper(); + pJSONValue->m_pDocument_mut = CreateDocument(); + + yyjson_mut_val* val = yyjson_mut_int(pJSONValue->m_pDocument_mut.get(), 0); + if (!val) { + if (error && error_size > 0) { + snprintf(error, error_size, "Failed to create number value"); + } + return nullptr; + } + + yyjson_read_err readError; + const char* end_ptr = yyjson_mut_read_number(dat, val, + static_cast(read_flg), nullptr, &readError); + + if (!end_ptr || readError.code) { + if (error && error_size > 0) { + snprintf(error, error_size, "Failed to read number: %s (error code: %u, position: %zu)", + readError.msg, readError.code, readError.pos); + } + return nullptr; + } + + if (out_consumed) { + *out_consumed = end_ptr - dat; + } + + pJSONValue->m_pVal_mut = val; + yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut.get(), val); + + return pJSONValue.release(); +} + +bool JsonManager::WriteNumber(JsonValue* handle, char* buffer, size_t buffer_size, size_t* out_written) +{ + if (!handle || !buffer || buffer_size == 0) { + return false; + } + + if (!IsNum(handle)) { + return false; + } + + char* result = nullptr; + if (handle->IsMutable()) { + result = yyjson_mut_write_number(handle->m_pVal_mut, buffer); + } else { + result = yyjson_write_number(handle->m_pVal, buffer); + } + + if (!result) { + return false; + } + + size_t written = result - buffer; + if (written >= buffer_size) { + return false; + } + + if (out_written) { + *out_written = written; + } + + return true; +} + +bool JsonManager::SetFpToFloat(JsonValue* handle, bool flt) +{ + if (!handle) { + return false; + } + + if (handle->IsMutable()) { + return yyjson_mut_set_fp_to_float(handle->m_pVal_mut, flt); + } else { + return yyjson_set_fp_to_float(handle->m_pVal, flt); + } +} + +bool JsonManager::SetFpToFixed(JsonValue* handle, int prec) +{ + if (!handle) { + return false; + } + + if (prec < 1 || prec > 15) { + return false; + } + + if (handle->IsMutable()) { + return yyjson_mut_set_fp_to_fixed(handle->m_pVal_mut, prec); + } else { + return yyjson_set_fp_to_fixed(handle->m_pVal, prec); + } +} + +bool JsonManager::SetBool(JsonValue* handle, bool value) +{ + if (!handle) { + return false; + } + + if (handle->IsMutable()) { + return yyjson_mut_set_bool(handle->m_pVal_mut, value); + } else { + return yyjson_set_bool(handle->m_pVal, value); + } +} + +bool JsonManager::SetInt(JsonValue* handle, int value) +{ + if (!handle) { + return false; + } + + if (handle->IsMutable()) { + return yyjson_mut_set_int(handle->m_pVal_mut, value); + } else { + return yyjson_set_int(handle->m_pVal, value); + } +} + +bool JsonManager::SetInt64(JsonValue* handle, std::variant value) +{ + if (!handle) { + return false; + } + + return std::visit([&](auto&& val) -> bool { + using T = std::decay_t; + if (handle->IsMutable()) { + if constexpr (std::is_same_v) { + return yyjson_mut_set_sint(handle->m_pVal_mut, val); + } else if constexpr (std::is_same_v) { + return yyjson_mut_set_uint(handle->m_pVal_mut, val); + } + } else { + if constexpr (std::is_same_v) { + return yyjson_set_sint(handle->m_pVal, val); + } else if constexpr (std::is_same_v) { + return yyjson_set_uint(handle->m_pVal, val); + } + } + return false; + }, value); +} + +bool JsonManager::SetFloat(JsonValue* handle, double value) +{ + if (!handle) { + return false; + } + + if (handle->IsMutable()) { + return yyjson_mut_set_float(handle->m_pVal_mut, value); + } else { + return yyjson_set_float(handle->m_pVal, value); + } +} + +bool JsonManager::SetString(JsonValue* handle, const char* value) +{ + if (!handle || !value) { + return false; + } + + if (handle->IsMutable()) { + return yyjson_mut_set_str(handle->m_pVal_mut, value); + } else { + return yyjson_set_str(handle->m_pVal, value); + } +} + +bool JsonManager::SetNull(JsonValue* handle) +{ + if (!handle) { + return false; + } + + if (handle->IsMutable()) { + return yyjson_mut_set_null(handle->m_pVal_mut); + } else { + return yyjson_set_null(handle->m_pVal); + } +} + +bool JsonManager::ParseInt64Variant(const char* value, std::variant* out_value, char* error, size_t error_size) +{ + if (!value || !*value) { + if (error && error_size > 0) { + snprintf(error, error_size, "Empty integer64 value"); + } + return false; + } + + if (!out_value) { + if (error && error_size > 0) { + snprintf(error, error_size, "Invalid output parameter"); + } + return false; + } + + std::string_view sv(value); + bool is_negative = (sv[0] == '-'); + + if (is_negative) { + int64_t signed_val; + auto result = std::from_chars(sv.data(), sv.data() + sv.size(), signed_val); + + if (result.ec == std::errc{} && result.ptr == sv.data() + sv.size()) { + *out_value = signed_val; + return true; + } + + if (error && error_size > 0) { + if (result.ec == std::errc::result_out_of_range) { + snprintf(error, error_size, "Integer64 value out of range: %s", value); + } else { + snprintf(error, error_size, "Invalid integer64 value: %s", value); + } + } + return false; + } + + int64_t signed_val; + auto result = std::from_chars(sv.data(), sv.data() + sv.size(), signed_val); + + if (result.ec == std::errc{} && result.ptr == sv.data() + sv.size()) { + *out_value = signed_val; + return true; + } + + if (result.ec == std::errc::result_out_of_range) { + uint64_t unsigned_val; + auto unsigned_result = std::from_chars(sv.data(), sv.data() + sv.size(), unsigned_val); + + if (unsigned_result.ec == std::errc{} && unsigned_result.ptr == sv.data() + sv.size()) { + *out_value = unsigned_val; + return true; + } + + if (error && error_size > 0) { + if (unsigned_result.ec == std::errc::result_out_of_range) { + snprintf(error, error_size, "Integer64 value out of range: %s", value); + } else { + snprintf(error, error_size, "Invalid integer64 value: %s", value); + } + } + return false; + } + + if (error && error_size > 0) { + snprintf(error, error_size, "Invalid integer64 value: %s", value); + } + return false; } \ No newline at end of file diff --git a/src/JsonManager.h b/src/JsonManager.h new file mode 100755 index 0000000..76c356e --- /dev/null +++ b/src/JsonManager.h @@ -0,0 +1,402 @@ +#ifndef _INCLUDE_SM_JSON_JSONMANAGER_H_ +#define _INCLUDE_SM_JSON_JSONMANAGER_H_ + +#include +#include +#include +#include +#include + +/** + * @brief JSON value wrapper + * + * Wraps json mutable/immutable documents and values. + * Used as the primary data type for JSON operations. + */ +class JsonValue { +public: + JsonValue() = default; + ~JsonValue() { + if (m_pDocument_mut.unique()) { + yyjson_mut_doc_free(m_pDocument_mut.get()); + } + if (m_pDocument.unique()) { + yyjson_doc_free(m_pDocument.get()); + } + } + + JsonValue(const JsonValue&) = delete; + JsonValue& operator=(const JsonValue&) = delete; + + void ResetObjectIterator() { + m_iterInitialized = false; + } + + void ResetArrayIterator() { + m_iterInitialized = false; + m_arrayIndex = 0; + } + + bool IsMutable() const { + return m_pDocument_mut != nullptr; + } + + bool IsImmutable() const { + return m_pDocument != nullptr; + } + + // Mutable document + std::shared_ptr m_pDocument_mut; + yyjson_mut_val* m_pVal_mut{ nullptr }; + + // Immutable document + std::shared_ptr m_pDocument; + yyjson_val* m_pVal{ nullptr }; + + // Mutable document iterators + yyjson_mut_obj_iter m_iterObj; + yyjson_mut_arr_iter m_iterArr; + + // Immutable document iterators + yyjson_obj_iter m_iterObjImm; + yyjson_arr_iter m_iterArrImm; + + Handle_t m_handle{ BAD_HANDLE }; + size_t m_arrayIndex{ 0 }; + size_t m_readSize{ 0 }; + bool m_iterInitialized{ false }; +}; + +/** + * @brief Array iterator wrapper + * + * Wraps yyjson_arr_iter and yyjson_mut_arr_iter for array iteration. + */ +class JsonArrIter { +public: + JsonArrIter() = default; + ~JsonArrIter() = default; + + JsonArrIter(const JsonArrIter&) = delete; + JsonArrIter& operator=(const JsonArrIter&) = delete; + + bool IsMutable() const { + return m_isMutable; + } + + std::shared_ptr m_pDocument_mut; + std::shared_ptr m_pDocument; + + yyjson_mut_arr_iter m_iterMut; + + yyjson_arr_iter m_iterImm; + + Handle_t m_handle{ BAD_HANDLE }; + bool m_isMutable{ false }; + bool m_initialized{ false }; +}; + +/** + * @brief Object iterator wrapper + * + * Wraps yyjson_obj_iter and yyjson_mut_obj_iter for object iteration. + */ +class JsonObjIter { +public: + JsonObjIter() = default; + ~JsonObjIter() = default; + + JsonObjIter(const JsonObjIter&) = delete; + JsonObjIter& operator=(const JsonObjIter&) = delete; + + bool IsMutable() const { + return m_isMutable; + } + + std::shared_ptr m_pDocument_mut; + std::shared_ptr m_pDocument; + + yyjson_mut_obj_iter m_iterMut; + + yyjson_obj_iter m_iterImm; + + void* m_currentKey{ nullptr }; + + Handle_t m_handle{ BAD_HANDLE }; + bool m_isMutable{ false }; + bool m_initialized{ false }; +}; + +class JsonManager : public IJsonManager +{ +public: + JsonManager(); + ~JsonManager(); + +public: + // ========== Document Operations ========== + virtual JsonValue* ParseJSON(const char* json_str, bool is_file, bool is_mutable, + yyjson_read_flag read_flg, char* error, size_t error_size) override; + virtual bool WriteToString(JsonValue* handle, char* buffer, size_t buffer_size, + yyjson_write_flag write_flg, size_t* out_size) override; + virtual bool WriteToFile(JsonValue* handle, const char* path, yyjson_write_flag write_flg, + char* error, size_t error_size) override; + virtual bool Equals(JsonValue* handle1, JsonValue* handle2) override; + virtual bool EqualsStr(JsonValue* handle, const char* str) override; + virtual JsonValue* DeepCopy(JsonValue* targetDoc, JsonValue* sourceValue) override; + virtual const char* GetTypeDesc(JsonValue* handle) override; + virtual size_t GetSerializedSize(JsonValue* handle, yyjson_write_flag write_flg) override; + virtual JsonValue* ToMutable(JsonValue* handle) override; + virtual JsonValue* ToImmutable(JsonValue* handle) override; + virtual yyjson_type GetType(JsonValue* handle) override; + virtual yyjson_subtype GetSubtype(JsonValue* handle) override; + virtual bool IsArray(JsonValue* handle) override; + virtual bool IsObject(JsonValue* handle) override; + virtual bool IsInt(JsonValue* handle) override; + virtual bool IsUint(JsonValue* handle) override; + virtual bool IsSint(JsonValue* handle) override; + virtual bool IsNum(JsonValue* handle) override; + virtual bool IsBool(JsonValue* handle) override; + virtual bool IsTrue(JsonValue* handle) override; + virtual bool IsFalse(JsonValue* handle) override; + virtual bool IsFloat(JsonValue* handle) override; + virtual bool IsStr(JsonValue* handle) override; + virtual bool IsNull(JsonValue* handle) override; + virtual bool IsCtn(JsonValue* handle) override; + virtual bool IsMutable(JsonValue* handle) override; + virtual bool IsImmutable(JsonValue* handle) override; + virtual size_t GetReadSize(JsonValue* handle) override; + + // ========== Object Operations ========== + virtual JsonValue* ObjectInit() override; + virtual JsonValue* ObjectInitWithStrings(const char** pairs, size_t count) override; + virtual JsonValue* ObjectParseString(const char* str, yyjson_read_flag read_flg, + char* error, size_t error_size) override; + virtual JsonValue* ObjectParseFile(const char* path, yyjson_read_flag read_flg, + char* error, size_t error_size) override; + virtual size_t ObjectGetSize(JsonValue* handle) override; + virtual bool ObjectGetKey(JsonValue* handle, size_t index, const char** out_key) override; + virtual JsonValue* ObjectGetValueAt(JsonValue* handle, size_t index) override; + virtual JsonValue* ObjectGet(JsonValue* handle, const char* key) override; + virtual bool ObjectGetBool(JsonValue* handle, const char* key, bool* out_value) override; + virtual bool ObjectGetFloat(JsonValue* handle, const char* key, double* out_value) override; + virtual bool ObjectGetInt(JsonValue* handle, const char* key, int* out_value) override; + virtual bool ObjectGetInt64(JsonValue* handle, const char* key, std::variant* out_value) override; + virtual bool ObjectGetString(JsonValue* handle, const char* key, const char** out_str, size_t* out_len) override; + virtual bool ObjectIsNull(JsonValue* handle, const char* key, bool* out_is_null) override; + virtual bool ObjectHasKey(JsonValue* handle, const char* key, bool use_pointer) override; + virtual bool ObjectRenameKey(JsonValue* handle, const char* old_key, const char* new_key, bool allow_duplicate) override; + virtual bool ObjectSet(JsonValue* handle, const char* key, JsonValue* value) override; + virtual bool ObjectSetBool(JsonValue* handle, const char* key, bool value) override; + virtual bool ObjectSetFloat(JsonValue* handle, const char* key, double value) override; + virtual bool ObjectSetInt(JsonValue* handle, const char* key, int value) override; + virtual bool ObjectSetInt64(JsonValue* handle, const char* key, std::variant value) override; + virtual bool ObjectSetNull(JsonValue* handle, const char* key) override; + virtual bool ObjectSetString(JsonValue* handle, const char* key, const char* value) override; + virtual bool ObjectRemove(JsonValue* handle, const char* key) override; + virtual bool ObjectClear(JsonValue* handle) override; + virtual bool ObjectSort(JsonValue* handle, JSON_SORT_ORDER sort_mode) override; + + // ========== Array Operations ========== + virtual JsonValue* ArrayInit() override; + virtual JsonValue* ArrayInitWithStrings(const char** strings, size_t count) override; + virtual JsonValue* ArrayInitWithInt32(const int32_t* values, size_t count) override; + virtual JsonValue* ArrayInitWithInt64(const char** values, size_t count, + char* error, size_t error_size) override; + virtual JsonValue* ArrayInitWithBool(const bool* values, size_t count) override; + virtual JsonValue* ArrayInitWithFloat(const double* values, size_t count) override; + virtual JsonValue* ArrayParseString(const char* str, yyjson_read_flag read_flg, + char* error, size_t error_size) override; + virtual JsonValue* ArrayParseFile(const char* path, yyjson_read_flag read_flg, + char* error, size_t error_size) override; + virtual size_t ArrayGetSize(JsonValue* handle) override; + virtual JsonValue* ArrayGet(JsonValue* handle, size_t index) override; + virtual JsonValue* ArrayGetFirst(JsonValue* handle) override; + virtual JsonValue* ArrayGetLast(JsonValue* handle) override; + virtual bool ArrayGetBool(JsonValue* handle, size_t index, bool* out_value) override; + virtual bool ArrayGetFloat(JsonValue* handle, size_t index, double* out_value) override; + virtual bool ArrayGetInt(JsonValue* handle, size_t index, int* out_value) override; + virtual bool ArrayGetInt64(JsonValue* handle, size_t index, std::variant* out_value) override; + virtual bool ArrayGetString(JsonValue* handle, size_t index, const char** out_str, size_t* out_len) override; + virtual bool ArrayIsNull(JsonValue* handle, size_t index) override; + virtual bool ArrayReplace(JsonValue* handle, size_t index, JsonValue* value) override; + virtual bool ArrayReplaceBool(JsonValue* handle, size_t index, bool value) override; + virtual bool ArrayReplaceFloat(JsonValue* handle, size_t index, double value) override; + virtual bool ArrayReplaceInt(JsonValue* handle, size_t index, int value) override; + virtual bool ArrayReplaceInt64(JsonValue* handle, size_t index, std::variant value) override; + virtual bool ArrayReplaceNull(JsonValue* handle, size_t index) override; + virtual bool ArrayReplaceString(JsonValue* handle, size_t index, const char* value) override; + virtual bool ArrayAppend(JsonValue* handle, JsonValue* value) override; + virtual bool ArrayAppendBool(JsonValue* handle, bool value) override; + virtual bool ArrayAppendFloat(JsonValue* handle, double value) override; + virtual bool ArrayAppendInt(JsonValue* handle, int value) override; + virtual bool ArrayAppendInt64(JsonValue* handle, std::variant value) override; + virtual bool ArrayAppendNull(JsonValue* handle) override; + virtual bool ArrayAppendString(JsonValue* handle, const char* value) override; + virtual bool ArrayInsert(JsonValue* handle, size_t index, JsonValue* value) override; + virtual bool ArrayInsertBool(JsonValue* handle, size_t index, bool value) override; + virtual bool ArrayInsertInt(JsonValue* handle, size_t index, int value) override; + virtual bool ArrayInsertInt64(JsonValue* handle, size_t index, std::variant value) override; + virtual bool ArrayInsertFloat(JsonValue* handle, size_t index, double value) override; + virtual bool ArrayInsertString(JsonValue* handle, size_t index, const char* value) override; + virtual bool ArrayInsertNull(JsonValue* handle, size_t index) override; + virtual bool ArrayPrepend(JsonValue* handle, JsonValue* value) override; + virtual bool ArrayPrependBool(JsonValue* handle, bool value) override; + virtual bool ArrayPrependInt(JsonValue* handle, int value) override; + virtual bool ArrayPrependInt64(JsonValue* handle, std::variant value) override; + virtual bool ArrayPrependFloat(JsonValue* handle, double value) override; + virtual bool ArrayPrependString(JsonValue* handle, const char* value) override; + virtual bool ArrayPrependNull(JsonValue* handle) override; + virtual bool ArrayRemove(JsonValue* handle, size_t index) override; + virtual bool ArrayRemoveFirst(JsonValue* handle) override; + virtual bool ArrayRemoveLast(JsonValue* handle) override; + virtual bool ArrayRemoveRange(JsonValue* handle, size_t start_index, size_t end_index) override; + virtual bool ArrayClear(JsonValue* handle) override; + virtual int ArrayIndexOfBool(JsonValue* handle, bool search_value) override; + virtual int ArrayIndexOfString(JsonValue* handle, const char* search_value) override; + virtual int ArrayIndexOfInt(JsonValue* handle, int search_value) override; + virtual int ArrayIndexOfInt64(JsonValue* handle, int64_t search_value) override; + virtual int ArrayIndexOfUint64(JsonValue* handle, uint64_t search_value) override; + virtual int ArrayIndexOfFloat(JsonValue* handle, double search_value) override; + virtual bool ArraySort(JsonValue* handle, JSON_SORT_ORDER sort_mode) override; + + // ========== Value Operations ========== + virtual JsonValue* Pack(const char* format, IPackParamProvider* param_provider, char* error, size_t error_size) override; + virtual JsonValue* CreateBool(bool value) override; + virtual JsonValue* CreateFloat(double value) override; + virtual JsonValue* CreateInt(int value) override; + virtual JsonValue* CreateInt64(std::variant value) override; + virtual JsonValue* CreateNull() override; + virtual JsonValue* CreateString(const char* value) override; + virtual bool GetBool(JsonValue* handle, bool* out_value) override; + virtual bool GetFloat(JsonValue* handle, double* out_value) override; + virtual bool GetInt(JsonValue* handle, int* out_value) override; + virtual bool GetInt64(JsonValue* handle, std::variant* out_value) override; + virtual bool GetString(JsonValue* handle, const char** out_str, size_t* out_len) override; + + // ========== Pointer Operations ========== + virtual JsonValue* PtrGet(JsonValue* handle, const char* path, char* error, size_t error_size) override; + virtual bool PtrGetBool(JsonValue* handle, const char* path, bool* out_value, char* error, size_t error_size) override; + virtual bool PtrGetFloat(JsonValue* handle, const char* path, double* out_value, char* error, size_t error_size) override; + virtual bool PtrGetInt(JsonValue* handle, const char* path, int* out_value, char* error, size_t error_size) override; + virtual bool PtrGetInt64(JsonValue* handle, const char* path, std::variant* out_value, char* error, size_t error_size) override; + virtual bool PtrGetString(JsonValue* handle, const char* path, const char** out_str, size_t* out_len, char* error, size_t error_size) override; + virtual bool PtrGetIsNull(JsonValue* handle, const char* path, bool* out_is_null, char* error, size_t error_size) override; + virtual bool PtrGetLength(JsonValue* handle, const char* path, size_t* out_len, char* error, size_t error_size) override; + virtual bool PtrSet(JsonValue* handle, const char* path, JsonValue* value, char* error, size_t error_size) override; + virtual bool PtrSetBool(JsonValue* handle, const char* path, bool value, char* error, size_t error_size) override; + virtual bool PtrSetFloat(JsonValue* handle, const char* path, double value, char* error, size_t error_size) override; + virtual bool PtrSetInt(JsonValue* handle, const char* path, int value, char* error, size_t error_size) override; + virtual bool PtrSetInt64(JsonValue* handle, const char* path, std::variant value, char* error, size_t error_size) override; + virtual bool PtrSetString(JsonValue* handle, const char* path, const char* value, char* error, size_t error_size) override; + virtual bool PtrSetNull(JsonValue* handle, const char* path, char* error, size_t error_size) override; + virtual bool PtrAdd(JsonValue* handle, const char* path, JsonValue* value, char* error, size_t error_size) override; + virtual bool PtrAddBool(JsonValue* handle, const char* path, bool value, char* error, size_t error_size) override; + virtual bool PtrAddFloat(JsonValue* handle, const char* path, double value, char* error, size_t error_size) override; + virtual bool PtrAddInt(JsonValue* handle, const char* path, int value, char* error, size_t error_size) override; + virtual bool PtrAddInt64(JsonValue* handle, const char* path, std::variant value, char* error, size_t error_size) override; + virtual bool PtrAddString(JsonValue* handle, const char* path, const char* value, char* error, size_t error_size) override; + virtual bool PtrAddNull(JsonValue* handle, const char* path, char* error, size_t error_size) override; + virtual bool PtrRemove(JsonValue* handle, const char* path, char* error, size_t error_size) override; + virtual JsonValue* PtrTryGet(JsonValue* handle, const char* path) override; + virtual bool PtrTryGetBool(JsonValue* handle, const char* path, bool* out_value) override; + virtual bool PtrTryGetFloat(JsonValue* handle, const char* path, double* out_value) override; + virtual bool PtrTryGetInt(JsonValue* handle, const char* path, int* out_value) override; + virtual bool PtrTryGetInt64(JsonValue* handle, const char* path, std::variant* out_value) override; + virtual bool PtrTryGetString(JsonValue* handle, const char* path, const char** out_str, size_t* out_len) override; + + // ========== Iterator Operations ========== + virtual bool ObjectForeachNext(JsonValue* handle, const char** out_key, + size_t* out_key_len, JsonValue** out_value) override; + virtual bool ArrayForeachNext(JsonValue* handle, size_t* out_index, + JsonValue** out_value) override; + virtual bool ObjectForeachKeyNext(JsonValue* handle, const char** out_key, + size_t* out_key_len) override; + virtual bool ArrayForeachIndexNext(JsonValue* handle, size_t* out_index) override; + + // ========== Array Iterator Operations ========== + virtual JsonArrIter* ArrIterInit(JsonValue* handle) override; + virtual JsonArrIter* ArrIterWith(JsonValue* handle) override; + virtual JsonValue* ArrIterNext(JsonArrIter* iter) override; + virtual bool ArrIterHasNext(JsonArrIter* iter) override; + virtual size_t ArrIterGetIndex(JsonArrIter* iter) override; + virtual void* ArrIterRemove(JsonArrIter* iter) override; + + // ========== Object Iterator Operations ========== + virtual JsonObjIter* ObjIterInit(JsonValue* handle) override; + virtual JsonObjIter* ObjIterWith(JsonValue* handle) override; + virtual void* ObjIterNext(JsonObjIter* iter) override; + virtual bool ObjIterHasNext(JsonObjIter* iter) override; + virtual JsonValue* ObjIterGetVal(JsonObjIter* iter, void* key) override; + virtual JsonValue* ObjIterGet(JsonObjIter* iter, const char* key) override; + virtual size_t ObjIterGetIndex(JsonObjIter* iter) override; + virtual void* ObjIterRemove(JsonObjIter* iter) override; + + // ========== Iterator Release Operations ========== + virtual void ReleaseArrIter(JsonArrIter* iter) override; + virtual void ReleaseObjIter(JsonObjIter* iter) override; + + // ========== Iterator Handle Type Operations ========== + virtual HandleType_t GetArrIterHandleType() override; + virtual HandleType_t GetObjIterHandleType() override; + virtual JsonArrIter* GetArrIterFromHandle(IPluginContext* pContext, Handle_t handle) override; + virtual JsonObjIter* GetObjIterFromHandle(IPluginContext* pContext, Handle_t handle) override; + + // ========== Release Operations ========== + virtual void Release(JsonValue* value) override; + + // ========== Handle Type Operations ========== + virtual HandleType_t GetHandleType() override; + + // ========== Handle Operations ========== + virtual JsonValue* GetFromHandle(IPluginContext* pContext, Handle_t handle) override; + + // ========== Number Read/Write Operations ========== + virtual JsonValue* ReadNumber(const char* dat, uint32_t read_flg = 0, + char* error = nullptr, size_t error_size = 0, size_t* out_consumed = nullptr) override; + virtual bool WriteNumber(JsonValue* handle, char* buffer, size_t buffer_size, + size_t* out_written = nullptr) override; + + // ========== Floating-Point Format Operations ========== + virtual bool SetFpToFloat(JsonValue* handle, bool flt) override; + virtual bool SetFpToFixed(JsonValue* handle, int prec) override; + + // ========== Direct Value Modification Operations ========== + virtual bool SetBool(JsonValue* handle, bool value) override; + virtual bool SetInt(JsonValue* handle, int value) override; + virtual bool SetInt64(JsonValue* handle, std::variant value) override; + virtual bool SetFloat(JsonValue* handle, double value) override; + virtual bool SetString(JsonValue* handle, const char* value) override; + virtual bool SetNull(JsonValue* handle) override; + + virtual bool ParseInt64Variant(const char* value, std::variant* out_value, + char* error = nullptr, size_t error_size = 0) override; + +private: + std::random_device m_randomDevice; + std::mt19937 m_randomGenerator; + + // Helper methods + static std::unique_ptr CreateWrapper(); + static std::shared_ptr WrapDocument(yyjson_mut_doc* doc); + static std::shared_ptr CopyDocument(yyjson_doc* doc); + static std::shared_ptr CreateDocument(); + static std::shared_ptr WrapImmutableDocument(yyjson_doc* doc); + + // Pack helper methods + static const char* SkipSeparators(const char* ptr); + static void SetPackError(char* error, size_t error_size, const char* fmt, ...); + static yyjson_mut_val* PackImpl(yyjson_mut_doc* doc, const char* format, + IPackParamProvider* provider, char* error, + size_t error_size, const char** out_end_ptr); + + // PtrTryGet helper methods + struct PtrGetValueResult { + yyjson_mut_val* mut_val{ nullptr }; + yyjson_val* imm_val{ nullptr }; + bool success{ false }; + }; + static PtrGetValueResult PtrGetValueInternal(JsonValue* handle, const char* path); +}; + +#endif // _INCLUDE_SM_JSON_JSONMANAGER_H_ \ No newline at end of file diff --git a/src/JsonNatives.cpp b/src/JsonNatives.cpp new file mode 100755 index 0000000..e5c86d9 --- /dev/null +++ b/src/JsonNatives.cpp @@ -0,0 +1,3244 @@ +#include "extension.h" +#include "JsonManager.h" + +class SourceModPackParamProvider : public IPackParamProvider +{ +private: + IPluginContext* m_pContext; + const cell_t* m_pParams; + unsigned int m_currentIndex; + +public: + SourceModPackParamProvider(IPluginContext* pContext, const cell_t* params, unsigned int startIndex) + : m_pContext(pContext), m_pParams(params), m_currentIndex(startIndex) {} + + bool GetNextString(const char** out_str) override { + char* str; + if (m_pContext->LocalToString(m_pParams[m_currentIndex++], &str) != SP_ERROR_NONE) { + return false; + } + *out_str = str; + return str != nullptr; + } + + bool GetNextInt(int* out_value) override { + cell_t* val; + if (m_pContext->LocalToPhysAddr(m_pParams[m_currentIndex++], &val) != SP_ERROR_NONE) { + return false; + } + *out_value = *val; + return true; + } + + bool GetNextFloat(float* out_value) override { + cell_t* val; + if (m_pContext->LocalToPhysAddr(m_pParams[m_currentIndex++], &val) != SP_ERROR_NONE) { + return false; + } + *out_value = sp_ctof(*val); + return true; + } + + bool GetNextBool(bool* out_value) override { + cell_t* val; + if (m_pContext->LocalToPhysAddr(m_pParams[m_currentIndex++], &val) != SP_ERROR_NONE) { + return false; + } + *out_value = (*val != 0); + return true; + } +}; + +/** + * Helper function: Create a SourceMod handle for JsonValue and return it directly + * Used by functions that return Handle_t + * + * @param pContext Plugin context + * @param pJSONValue JSON value to wrap (will be released on failure) + * @param error_context Descriptive context for error messages + * @return Handle on success, throws native error on failure + */ +static cell_t CreateAndReturnHandle(IPluginContext* pContext, JsonValue* pJSONValue, const char* error_context) +{ + if (!pJSONValue) { + return pContext->ThrowNativeError("Failed to create %s", error_context); + } + + HandleError err; + HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity()); + pJSONValue->m_handle = handlesys->CreateHandleEx(g_JsonType, pJSONValue, &sec, nullptr, &err); + + if (!pJSONValue->m_handle) { + g_pJsonManager->Release(pJSONValue); + return pContext->ThrowNativeError("Failed to create handle for %s (error code: %d)", error_context, err); + } + + return pJSONValue->m_handle; +} + +/** + * Helper function: Create a SourceMod handle for JsonValue and assign to output parameter + * Used by iterator functions (foreach) that assign handle via reference + * + * @param pContext Plugin context + * @param pJSONValue JSON value to wrap (will be released on failure) + * @param param_index Parameter index for output handle + * @param error_context Descriptive context for error messages + * @return true on success, false on failure (throws native error) + */ +static bool CreateAndAssignHandle(IPluginContext* pContext, JsonValue* pJSONValue, + cell_t param_index, const char* error_context) +{ + HandleError err; + HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity()); + pJSONValue->m_handle = handlesys->CreateHandleEx(g_JsonType, pJSONValue, &sec, nullptr, &err); + + if (!pJSONValue->m_handle) { + g_pJsonManager->Release(pJSONValue); + pContext->ThrowNativeError("Failed to create handle for %s (error code: %d)", error_context, err); + return false; + } + + cell_t* valHandle; + pContext->LocalToPhysAddr(param_index, &valHandle); + *valHandle = pJSONValue->m_handle; + return true; +} + +/** + * Helper function: Create a SourceMod handle for JsonArrIter and return it directly + */ +static cell_t CreateAndReturnArrIterHandle(IPluginContext* pContext, JsonArrIter* iter, const char* error_context) +{ + if (!iter) { + return pContext->ThrowNativeError("Failed to create %s", error_context); + } + + HandleError err; + HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity()); + Handle_t handle = handlesys->CreateHandleEx(g_ArrIterType, iter, &sec, nullptr, &err); + + if (!handle) { + g_pJsonManager->ReleaseArrIter(iter); + return pContext->ThrowNativeError("Failed to create handle for %s (error code: %d)", error_context, err); + } + + return handle; +} + +/** + * Helper function: Create a SourceMod handle for JsonObjIter and return it directly + */ +static cell_t CreateAndReturnObjIterHandle(IPluginContext* pContext, JsonObjIter* iter, const char* error_context) +{ + if (!iter) { + return pContext->ThrowNativeError("Failed to create %s", error_context); + } + + HandleError err; + HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity()); + Handle_t handle = handlesys->CreateHandleEx(g_ObjIterType, iter, &sec, nullptr, &err); + + if (!handle) { + g_pJsonManager->ReleaseObjIter(iter); + return pContext->ThrowNativeError("Failed to create handle for %s (error code: %d)", error_context, err); + } + + return handle; +} + +static cell_t json_pack(IPluginContext* pContext, const cell_t* params) { + char* fmt; + pContext->LocalToString(params[1], &fmt); + + SourceModPackParamProvider provider(pContext, params, 2); + + char error[JSON_PACK_ERROR_SIZE]; + JsonValue* pJSONValue = g_pJsonManager->Pack(fmt, &provider, error, sizeof(error)); + + if (!pJSONValue) { + return pContext->ThrowNativeError("Failed to pack JSON: %s", error); + } + + return CreateAndReturnHandle(pContext, pJSONValue, "packed JSON"); +} + +static cell_t json_doc_parse(IPluginContext* pContext, const cell_t* params) +{ + char* str; + pContext->LocalToString(params[1], &str); + + bool is_file = params[2]; + bool is_mutable_doc = params[3]; + yyjson_read_flag read_flg = static_cast(params[4]); + + char error[JSON_ERROR_BUFFER_SIZE]; + JsonValue* pJSONValue = g_pJsonManager->ParseJSON(str, is_file, is_mutable_doc, read_flg, error, sizeof(error)); + + if (!pJSONValue) { + return pContext->ThrowNativeError(error); + } + + return CreateAndReturnHandle(pContext, pJSONValue, "parsed JSON document"); +} + +static cell_t json_doc_equals(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle1 = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle2 = g_pJsonManager->GetFromHandle(pContext, params[2]); + + if (!handle1 || !handle2) return 0; + + return g_pJsonManager->Equals(handle1, handle2); +} + +static cell_t json_doc_copy_deep(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* targetDoc = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* sourceValue = g_pJsonManager->GetFromHandle(pContext, params[2]); + + if (!targetDoc || !sourceValue) return 0; + + JsonValue* pJSONValue = g_pJsonManager->DeepCopy(targetDoc, sourceValue); + + if (!pJSONValue) { + return pContext->ThrowNativeError("Failed to copy JSON value"); + } + + return CreateAndReturnHandle(pContext, pJSONValue, "copied JSON value"); +} + +static cell_t json_get_type_desc(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + const char* type_desc = g_pJsonManager->GetTypeDesc(handle); + pContext->StringToLocalUTF8(params[2], params[3], type_desc, nullptr); + + return 1; +} + +static cell_t json_obj_parse_str(IPluginContext* pContext, const cell_t* params) +{ + char* str; + pContext->LocalToString(params[1], &str); + yyjson_read_flag read_flg = static_cast(params[2]); + + char error[JSON_PACK_ERROR_SIZE]; + JsonValue* pJSONValue = g_pJsonManager->ObjectParseString(str, read_flg, error, sizeof(error)); + + if (!pJSONValue) { + return pContext->ThrowNativeError(error); + } + + return CreateAndReturnHandle(pContext, pJSONValue, "JSON object from string"); +} + +static cell_t json_obj_parse_file(IPluginContext* pContext, const cell_t* params) +{ + char* path; + pContext->LocalToString(params[1], &path); + yyjson_read_flag read_flg = static_cast(params[2]); + + char error[JSON_PACK_ERROR_SIZE]; + JsonValue* pJSONValue = g_pJsonManager->ObjectParseFile(path, read_flg, error, sizeof(error)); + + if (!pJSONValue) { + return pContext->ThrowNativeError(error); + } + + return CreateAndReturnHandle(pContext, pJSONValue, "JSON object from file"); +} + +static cell_t json_arr_parse_str(IPluginContext* pContext, const cell_t* params) +{ + char* str; + pContext->LocalToString(params[1], &str); + yyjson_read_flag read_flg = static_cast(params[2]); + + char error[JSON_PACK_ERROR_SIZE]; + JsonValue* pJSONValue = g_pJsonManager->ArrayParseString(str, read_flg, error, sizeof(error)); + + if (!pJSONValue) { + return pContext->ThrowNativeError(error); + } + + return CreateAndReturnHandle(pContext, pJSONValue, "JSON array from string"); +} + +static cell_t json_arr_parse_file(IPluginContext* pContext, const cell_t* params) +{ + char* path; + pContext->LocalToString(params[1], &path); + yyjson_read_flag read_flg = static_cast(params[2]); + + char error[JSON_PACK_ERROR_SIZE]; + JsonValue* pJSONValue = g_pJsonManager->ArrayParseFile(path, read_flg, error, sizeof(error)); + + if (!pJSONValue) { + return pContext->ThrowNativeError(error); + } + + return CreateAndReturnHandle(pContext, pJSONValue, "JSON array from file"); +} + +static cell_t json_arr_index_of_bool(IPluginContext *pContext, const cell_t *params) +{ + JsonValue *handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + bool searchValue = params[2]; + return g_pJsonManager->ArrayIndexOfBool(handle, searchValue); +} + +static cell_t json_arr_index_of_str(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + char* searchStr; + pContext->LocalToString(params[2], &searchStr); + + return g_pJsonManager->ArrayIndexOfString(handle, searchStr); +} + +static cell_t json_arr_index_of_int(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + int searchValue = params[2]; + return g_pJsonManager->ArrayIndexOfInt(handle, searchValue); +} + +static cell_t json_arr_index_of_integer64(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + char* searchStr; + pContext->LocalToString(params[2], &searchStr); + + char* endptr; + errno = 0; + long long searchValue = strtoll(searchStr, &endptr, 10); + + if (errno == ERANGE || *endptr != '\0') { + return pContext->ThrowNativeError("Invalid integer64 value: %s", searchStr); + } + + return g_pJsonManager->ArrayIndexOfInt64(handle, searchValue); +} + +static cell_t json_arr_index_of_uint64(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + char* searchStr; + pContext->LocalToString(params[2], &searchStr); + + char* endptr; + errno = 0; + unsigned long long searchValue = strtoull(searchStr, &endptr, 10); + + if (errno == ERANGE || *endptr != '\0') { + return pContext->ThrowNativeError("Invalid unsigned integer64 value: %s", searchStr); + } + + return g_pJsonManager->ArrayIndexOfUint64(handle, searchValue); +} + +static cell_t json_arr_index_of_float(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + double searchValue = static_cast(sp_ctof(params[2])); + return g_pJsonManager->ArrayIndexOfFloat(handle, searchValue); +} + +static cell_t json_get_type(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + return g_pJsonManager->GetType(handle); +} + +static cell_t json_get_subtype(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + return g_pJsonManager->GetSubtype(handle); +} + +static cell_t json_is_array(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + return g_pJsonManager->IsArray(handle); +} + +static cell_t json_is_object(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + return g_pJsonManager->IsObject(handle); +} + +static cell_t json_is_int(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + return g_pJsonManager->IsInt(handle); +} + +static cell_t json_is_uint(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + return g_pJsonManager->IsUint(handle); +} + +static cell_t json_is_sint(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + return g_pJsonManager->IsSint(handle); +} + +static cell_t json_is_num(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + return g_pJsonManager->IsNum(handle); +} + +static cell_t json_is_bool(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + return g_pJsonManager->IsBool(handle); +} + +static cell_t json_is_true(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + return g_pJsonManager->IsTrue(handle); +} + +static cell_t json_is_false(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + return g_pJsonManager->IsFalse(handle); +} + +static cell_t json_is_float(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + return g_pJsonManager->IsFloat(handle); +} + +static cell_t json_is_str(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + return g_pJsonManager->IsStr(handle); +} + +static cell_t json_is_null(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + return g_pJsonManager->IsNull(handle); +} + +static cell_t json_is_ctn(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + return g_pJsonManager->IsCtn(handle); +} + +static cell_t json_is_mutable(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + return g_pJsonManager->IsMutable(handle); +} + +static cell_t json_is_immutable(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + return g_pJsonManager->IsImmutable(handle); +} + +static cell_t json_obj_init(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* pJSONValue = g_pJsonManager->ObjectInit(); + return CreateAndReturnHandle(pContext, pJSONValue, "JSON object"); +} + +static cell_t json_obj_init_with_str(IPluginContext* pContext, const cell_t* params) +{ + cell_t* addr; + pContext->LocalToPhysAddr(params[1], &addr); + cell_t array_size = params[2]; + + if (array_size < 2) { + return pContext->ThrowNativeError("Array must contain at least one key-value pair"); + } + if (array_size % 2 != 0) { + return pContext->ThrowNativeError("Array must contain an even number of strings (got %d)", array_size); + } + + std::vector kv_pairs; + kv_pairs.reserve(array_size); + + for (cell_t i = 0; i < array_size; i += 2) { + char* key; + char* value; + + if (pContext->LocalToString(addr[i], &key) != SP_ERROR_NONE) { + return pContext->ThrowNativeError("Failed to read key at index %d", i); + } + if (!key || !key[0]) { + return pContext->ThrowNativeError("Empty key at index %d", i); + } + + if (pContext->LocalToString(addr[i + 1], &value) != SP_ERROR_NONE) { + return pContext->ThrowNativeError("Failed to read value at index %d", i + 1); + } + if (!value) { + return pContext->ThrowNativeError("Invalid value at index %d", i + 1); + } + + kv_pairs.push_back(key); + kv_pairs.push_back(value); + } + + JsonValue* pJSONValue = g_pJsonManager->ObjectInitWithStrings(kv_pairs.data(), array_size / 2); + + if (!pJSONValue) { + return pContext->ThrowNativeError("Failed to create JSON object from key-value pairs"); + } + + return CreateAndReturnHandle(pContext, pJSONValue, "JSON object from key-value pairs"); +} + +static cell_t json_create_bool(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* pJSONValue = g_pJsonManager->CreateBool(params[1]); + return CreateAndReturnHandle(pContext, pJSONValue, "JSON boolean value"); +} + +static cell_t json_create_float(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* pJSONValue = g_pJsonManager->CreateFloat(sp_ctof(params[1])); + return CreateAndReturnHandle(pContext, pJSONValue, "JSON float value"); +} + +static cell_t json_create_int(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* pJSONValue = g_pJsonManager->CreateInt(params[1]); + return CreateAndReturnHandle(pContext, pJSONValue, "JSON integer value"); +} + +static cell_t json_create_integer64(IPluginContext* pContext, const cell_t* params) +{ + char* value; + pContext->LocalToString(params[1], &value); + + std::variant variant_value; + char error[JSON_ERROR_BUFFER_SIZE]; + if (!g_pJsonManager->ParseInt64Variant(value, &variant_value, error, sizeof(error))) { + return pContext->ThrowNativeError("%s", error); + } + + JsonValue* pJSONValue = g_pJsonManager->CreateInt64(variant_value); + + if (!pJSONValue) { + return pContext->ThrowNativeError("Failed to create JSON integer64 value"); + } + + return CreateAndReturnHandle(pContext, pJSONValue, "JSON integer64 value"); +} + +static cell_t json_create_str(IPluginContext* pContext, const cell_t* params) +{ + char* str; + pContext->LocalToString(params[1], &str); + + JsonValue* pJSONValue = g_pJsonManager->CreateString(str); + + if (!pJSONValue) { + return pContext->ThrowNativeError("Failed to create JSON string value"); + } + + return CreateAndReturnHandle(pContext, pJSONValue, "JSON string value"); +} + +static cell_t json_get_bool(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + bool value; + if (!g_pJsonManager->GetBool(handle, &value)) { + return pContext->ThrowNativeError("Type mismatch: expected boolean value"); + } + + return value; +} + +static cell_t json_get_float(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + double value; + if (!g_pJsonManager->GetFloat(handle, &value)) { + return pContext->ThrowNativeError("Type mismatch: expected float value"); + } + + return sp_ftoc(static_cast(value)); +} + +static cell_t json_get_int(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + int value; + if (!g_pJsonManager->GetInt(handle, &value)) { + return pContext->ThrowNativeError("Type mismatch: expected integer value"); + } + + return value; +} + +static cell_t json_get_integer64(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + std::variant value; + if (!g_pJsonManager->GetInt64(handle, &value)) { + return pContext->ThrowNativeError("Type mismatch: expected integer64 value"); + } + + char result[JSON_INT64_BUFFER_SIZE]; + if (std::holds_alternative(value)) { + snprintf(result, sizeof(result), "%" PRIu64, std::get(value)); + } else { + snprintf(result, sizeof(result), "%" PRId64, std::get(value)); + } + pContext->StringToLocalUTF8(params[2], params[3], result, nullptr); + + return 1; +} + +static cell_t json_get_str(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + const char* str = nullptr; + size_t len = 0; + + if (!g_pJsonManager->GetString(handle, &str, &len)) { + return pContext->ThrowNativeError("Type mismatch: expected string value"); + } + + size_t maxlen = static_cast(params[3]); + + if (len + 1 > maxlen) { + return pContext->ThrowNativeError("Buffer is too small (need %d, have %d)", len + 1, maxlen); + } + + pContext->StringToLocalUTF8(params[2], maxlen, str, nullptr); + + return 1; +} + +static cell_t json_equals_str(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + if (!handle) return 0; + + char* str; + pContext->LocalToString(params[2], &str); + + return g_pJsonManager->EqualsStr(handle, str); +} + +static cell_t json_get_serialized_size(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + yyjson_write_flag write_flg = static_cast(params[2]); + size_t size = g_pJsonManager->GetSerializedSize(handle, write_flg); + + return static_cast(size); +} + +static cell_t json_get_read_size(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + size_t size = g_pJsonManager->GetReadSize(handle); + if (size == 0) return 0; + + return static_cast(size); +} + +static cell_t json_create_null(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* pJSONValue = g_pJsonManager->CreateNull(); + return CreateAndReturnHandle(pContext, pJSONValue, "JSON null value"); +} + +static cell_t json_arr_init(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* pJSONValue = g_pJsonManager->ArrayInit(); + return CreateAndReturnHandle(pContext, pJSONValue, "JSON array"); +} + +static cell_t json_arr_init_with_str(IPluginContext* pContext, const cell_t* params) +{ + cell_t* addr; + pContext->LocalToPhysAddr(params[1], &addr); + cell_t array_size = params[2]; + + std::vector strs; + strs.reserve(array_size); + + for (cell_t i = 0; i < array_size; i++) { + char* str; + pContext->LocalToString(addr[i], &str); + strs.push_back(str); + } + + JsonValue* pJSONValue = g_pJsonManager->ArrayInitWithStrings(strs.data(), strs.size()); + + if (!pJSONValue) { + return pContext->ThrowNativeError("Failed to create JSON array from strings"); + } + + return CreateAndReturnHandle(pContext, pJSONValue, "JSON array from strings"); +} + +static cell_t json_arr_init_with_int32(IPluginContext* pContext, const cell_t* params) +{ + cell_t* addr; + pContext->LocalToPhysAddr(params[1], &addr); + cell_t array_size = params[2]; + + std::vector values; + values.reserve(array_size); + + for (cell_t i = 0; i < array_size; i++) { + values.push_back(static_cast(addr[i])); + } + + JsonValue* pJSONValue = g_pJsonManager->ArrayInitWithInt32(values.data(), values.size()); + + if (!pJSONValue) { + return pContext->ThrowNativeError("Failed to create JSON array from int32 values"); + } + + return CreateAndReturnHandle(pContext, pJSONValue, "JSON array from int32 values"); +} + +static cell_t json_arr_init_with_int64(IPluginContext* pContext, const cell_t* params) +{ + cell_t* addr; + pContext->LocalToPhysAddr(params[1], &addr); + cell_t array_size = params[2]; + + std::vector strs; + strs.reserve(array_size); + + for (cell_t i = 0; i < array_size; i++) { + char* str; + pContext->LocalToString(addr[i], &str); + strs.push_back(str); + } + + char error[JSON_ERROR_BUFFER_SIZE]; + JsonValue* pJSONValue = g_pJsonManager->ArrayInitWithInt64(strs.data(), strs.size(), error, sizeof(error)); + + if (!pJSONValue) { + return pContext->ThrowNativeError("Failed to create JSON array from int64 values: %s", error); + } + + return CreateAndReturnHandle(pContext, pJSONValue, "JSON array from int64 values"); +} + +static cell_t json_arr_init_with_bool(IPluginContext* pContext, const cell_t* params) +{ + cell_t* addr; + pContext->LocalToPhysAddr(params[1], &addr); + cell_t array_size = params[2]; + + // Use unique_ptr for automatic memory management + // Note: std::vector is specialized and doesn't work with .data() + std::unique_ptr values(new bool[array_size]); + + for (cell_t i = 0; i < array_size; i++) { + values[i] = (addr[i] != 0); + } + + JsonValue* pJSONValue = g_pJsonManager->ArrayInitWithBool(values.get(), array_size); + + if (!pJSONValue) { + return pContext->ThrowNativeError("Failed to create JSON array from bool values"); + } + + return CreateAndReturnHandle(pContext, pJSONValue, "JSON array from bool values"); +} + +static cell_t json_arr_init_with_float(IPluginContext* pContext, const cell_t* params) +{ + cell_t* addr; + pContext->LocalToPhysAddr(params[1], &addr); + cell_t array_size = params[2]; + + std::vector values; + values.reserve(array_size); + + for (cell_t i = 0; i < array_size; i++) { + values.push_back(sp_ctof(addr[i])); + } + + JsonValue* pJSONValue = g_pJsonManager->ArrayInitWithFloat(values.data(), values.size()); + + if (!pJSONValue) { + return pContext->ThrowNativeError("Failed to create JSON array from float values"); + } + + return CreateAndReturnHandle(pContext, pJSONValue, "JSON array from float values"); +} + +static cell_t json_arr_get_size(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + size_t size = g_pJsonManager->ArrayGetSize(handle); + return static_cast(size); +} + +static cell_t json_arr_get_val(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + size_t index = static_cast(params[2]); + + JsonValue* pJSONValue = g_pJsonManager->ArrayGet(handle, index); + + if (!pJSONValue) { + return pContext->ThrowNativeError("Index %d is out of bounds", index); + } + + return CreateAndReturnHandle(pContext, pJSONValue, "JSON array value"); +} + +static cell_t json_arr_get_first(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + JsonValue* pJSONValue = g_pJsonManager->ArrayGetFirst(handle); + + if (!pJSONValue) { + return pContext->ThrowNativeError("Array is empty"); + } + + return CreateAndReturnHandle(pContext, pJSONValue, "first JSON array value"); +} + +static cell_t json_arr_get_last(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + JsonValue* pJSONValue = g_pJsonManager->ArrayGetLast(handle); + + if (!pJSONValue) { + return pContext->ThrowNativeError("Array is empty"); + } + + return CreateAndReturnHandle(pContext, pJSONValue, "last JSON array value"); +} + +static cell_t json_arr_get_bool(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + size_t index = static_cast(params[2]); + + bool value; + if (!g_pJsonManager->ArrayGetBool(handle, index, &value)) { + return pContext->ThrowNativeError("Failed to get boolean at index %d", index); + } + + return value; +} + +static cell_t json_arr_get_float(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + size_t index = static_cast(params[2]); + + double value; + if (!g_pJsonManager->ArrayGetFloat(handle, index, &value)) { + return pContext->ThrowNativeError("Failed to get float at index %d", index); + } + + return sp_ftoc(static_cast(value)); +} + +static cell_t json_arr_get_integer(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + size_t index = static_cast(params[2]); + + int value; + if (!g_pJsonManager->ArrayGetInt(handle, index, &value)) { + return pContext->ThrowNativeError("Failed to get integer at index %d", index); + } + + return value; +} + +static cell_t json_arr_get_integer64(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + size_t index = static_cast(params[2]); + std::variant value; + + if (!g_pJsonManager->ArrayGetInt64(handle, index, &value)) { + return pContext->ThrowNativeError("Failed to get integer64 at index %d", index); + } + + char result[JSON_INT64_BUFFER_SIZE]; + if (std::holds_alternative(value)) { + snprintf(result, sizeof(result), "%" PRIu64, std::get(value)); + } else { + snprintf(result, sizeof(result), "%" PRId64, std::get(value)); + } + pContext->StringToLocalUTF8(params[3], params[4], result, nullptr); + + return 1; +} + +static cell_t json_arr_get_str(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + size_t index = static_cast(params[2]); + const char* str = nullptr; + size_t len = 0; + + if (!g_pJsonManager->ArrayGetString(handle, index, &str, &len)) { + return pContext->ThrowNativeError("Failed to get string at index %d", index); + } + + size_t maxlen = static_cast(params[4]); + if (len + 1 > maxlen) { + return pContext->ThrowNativeError("Buffer is too small (need %d, have %d)", len + 1, maxlen); + } + + pContext->StringToLocalUTF8(params[3], maxlen, str, nullptr); + + return 1; +} + +static cell_t json_arr_is_null(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + size_t index = static_cast(params[2]); + + return g_pJsonManager->ArrayIsNull(handle, index); +} + +static cell_t json_arr_replace_val(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle1 = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle2 = g_pJsonManager->GetFromHandle(pContext, params[3]); + + if (!handle1 || !handle2) return 0; + + if (!handle1->IsMutable()) { + return pContext->ThrowNativeError("Cannot replace value in an immutable JSON array"); + } + + size_t index = static_cast(params[2]); + return g_pJsonManager->ArrayReplace(handle1, index, handle2); +} + +static cell_t json_arr_replace_bool(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot replace value in an immutable JSON array"); + } + + size_t index = static_cast(params[2]); + return g_pJsonManager->ArrayReplaceBool(handle, index, params[3]); +} + +static cell_t json_arr_replace_float(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot replace value in an immutable JSON array"); + } + + size_t index = static_cast(params[2]); + return g_pJsonManager->ArrayReplaceFloat(handle, index, sp_ctof(params[3])); +} + +static cell_t json_arr_replace_integer(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot replace value in an immutable JSON array"); + } + + size_t index = static_cast(params[2]); + return g_pJsonManager->ArrayReplaceInt(handle, index, params[3]); +} + +static cell_t json_arr_replace_integer64(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot replace value in an immutable JSON array"); + } + + char* value; + pContext->LocalToString(params[3], &value); + + std::variant variant_value; + char error[JSON_ERROR_BUFFER_SIZE]; + if (!g_pJsonManager->ParseInt64Variant(value, &variant_value, error, sizeof(error))) { + return pContext->ThrowNativeError("%s", error); + } + + size_t index = static_cast(params[2]); + return g_pJsonManager->ArrayReplaceInt64(handle, index, variant_value); +} + +static cell_t json_arr_replace_null(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot replace value in an immutable JSON array"); + } + + size_t index = static_cast(params[2]); + return g_pJsonManager->ArrayReplaceNull(handle, index); +} + +static cell_t json_arr_replace_str(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot replace value in an immutable JSON array"); + } + + char* val; + pContext->LocalToString(params[3], &val); + + size_t index = static_cast(params[2]); + return g_pJsonManager->ArrayReplaceString(handle, index, val); +} + +static cell_t json_arr_append_val(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle1 = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle2 = g_pJsonManager->GetFromHandle(pContext, params[2]); + + if (!handle1 || !handle2) return 0; + + if (!handle1->IsMutable()) { + return pContext->ThrowNativeError("Cannot append value to an immutable JSON array"); + } + + return g_pJsonManager->ArrayAppend(handle1, handle2); +} + +static cell_t json_arr_append_bool(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot append value to an immutable JSON array"); + } + + return g_pJsonManager->ArrayAppendBool(handle, params[2]); +} + +static cell_t json_arr_append_float(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot append value to an immutable JSON array"); + } + + return g_pJsonManager->ArrayAppendFloat(handle, sp_ctof(params[2])); +} + +static cell_t json_arr_append_int(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot append value to an immutable JSON array"); + } + + return g_pJsonManager->ArrayAppendInt(handle, params[2]); +} + +static cell_t json_arr_append_integer64(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot append value to an immutable JSON array"); + } + + char* value; + pContext->LocalToString(params[2], &value); + + std::variant variant_value; + char error[JSON_ERROR_BUFFER_SIZE]; + if (!g_pJsonManager->ParseInt64Variant(value, &variant_value, error, sizeof(error))) { + return pContext->ThrowNativeError("%s", error); + } + + return g_pJsonManager->ArrayAppendInt64(handle, variant_value); +} + +static cell_t json_arr_append_null(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot append value to an immutable JSON array"); + } + + return g_pJsonManager->ArrayAppendNull(handle); +} + +static cell_t json_arr_append_str(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot append value to an immutable JSON array"); + } + + char* str; + pContext->LocalToString(params[2], &str); + + return g_pJsonManager->ArrayAppendString(handle, str); +} + +// ========== Array Insert Operations ========== + +static cell_t json_arr_insert(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot insert value into an immutable JSON array"); + } + + size_t index = params[2]; + JsonValue* value = g_pJsonManager->GetFromHandle(pContext, params[3]); + if (!value) return 0; + + return g_pJsonManager->ArrayInsert(handle, index, value); +} + +static cell_t json_arr_insert_bool(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot insert value into an immutable JSON array"); + } + + size_t index = params[2]; + bool value = params[3] != 0; + + return g_pJsonManager->ArrayInsertBool(handle, index, value); +} + +static cell_t json_arr_insert_int(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot insert value into an immutable JSON array"); + } + + size_t index = params[2]; + int value = params[3]; + + return g_pJsonManager->ArrayInsertInt(handle, index, value); +} + +static cell_t json_arr_insert_int64(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot insert value into an immutable JSON array"); + } + + size_t index = params[2]; + char* value; + pContext->LocalToString(params[3], &value); + + std::variant variant_value; + char error[JSON_ERROR_BUFFER_SIZE]; + if (!g_pJsonManager->ParseInt64Variant(value, &variant_value, error, sizeof(error))) { + return pContext->ThrowNativeError("%s", error); + } + + return g_pJsonManager->ArrayInsertInt64(handle, index, variant_value); +} + +static cell_t json_arr_insert_float(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot insert value into an immutable JSON array"); + } + + size_t index = params[2]; + double value = sp_ctof(params[3]); + + return g_pJsonManager->ArrayInsertFloat(handle, index, value); +} + +static cell_t json_arr_insert_str(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot insert value into an immutable JSON array"); + } + + size_t index = params[2]; + char* str; + pContext->LocalToString(params[3], &str); + + return g_pJsonManager->ArrayInsertString(handle, index, str); +} + +static cell_t json_arr_insert_null(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot insert value into an immutable JSON array"); + } + + size_t index = params[2]; + + return g_pJsonManager->ArrayInsertNull(handle, index); +} + +// ========== Array Prepend Operations ========== + +static cell_t json_arr_prepend(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot prepend value to an immutable JSON array"); + } + + JsonValue* value = g_pJsonManager->GetFromHandle(pContext, params[2]); + if (!value) return 0; + + return g_pJsonManager->ArrayPrepend(handle, value); +} + +static cell_t json_arr_prepend_bool(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot prepend value to an immutable JSON array"); + } + + bool value = params[2] != 0; + + return g_pJsonManager->ArrayPrependBool(handle, value); +} + +static cell_t json_arr_prepend_int(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot prepend value to an immutable JSON array"); + } + + int value = params[2]; + + return g_pJsonManager->ArrayPrependInt(handle, value); +} + +static cell_t json_arr_prepend_int64(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot prepend value to an immutable JSON array"); + } + + char* value; + pContext->LocalToString(params[2], &value); + + std::variant variant_value; + char error[JSON_ERROR_BUFFER_SIZE]; + if (!g_pJsonManager->ParseInt64Variant(value, &variant_value, error, sizeof(error))) { + return pContext->ThrowNativeError("%s", error); + } + + return g_pJsonManager->ArrayPrependInt64(handle, variant_value); +} + +static cell_t json_arr_prepend_float(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot prepend value to an immutable JSON array"); + } + + double value = sp_ctof(params[2]); + + return g_pJsonManager->ArrayPrependFloat(handle, value); +} + +static cell_t json_arr_prepend_str(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot prepend value to an immutable JSON array"); + } + + char* str; + pContext->LocalToString(params[2], &str); + + return g_pJsonManager->ArrayPrependString(handle, str); +} + +static cell_t json_arr_prepend_null(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot prepend value to an immutable JSON array"); + } + + return g_pJsonManager->ArrayPrependNull(handle); +} + +static cell_t json_arr_remove(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot remove value from an immutable JSON array"); + } + + size_t index = static_cast(params[2]); + return g_pJsonManager->ArrayRemove(handle, index); +} + +static cell_t json_arr_remove_first(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot remove value from an immutable JSON array"); + } + + return g_pJsonManager->ArrayRemoveFirst(handle); +} + +static cell_t json_arr_remove_last(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot remove value from an immutable JSON array"); + } + + return g_pJsonManager->ArrayRemoveLast(handle); +} + +static cell_t json_arr_remove_range(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot remove value from an immutable JSON array"); + } + + size_t start_index = static_cast(params[2]); + size_t end_index = static_cast(params[3]); + + return g_pJsonManager->ArrayRemoveRange(handle, start_index, end_index); +} + +static cell_t json_arr_clear(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot clear an immutable JSON array"); + } + + return g_pJsonManager->ArrayClear(handle); +} + +static cell_t json_doc_write_to_str(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + size_t buffer_size = static_cast(params[3]); + yyjson_write_flag write_flg = static_cast(params[4]); + + char* temp_buffer = (char*)malloc(buffer_size); + if (!temp_buffer) { + return pContext->ThrowNativeError("Failed to allocate buffer"); + } + + size_t output_len = 0; + if (!g_pJsonManager->WriteToString(handle, temp_buffer, buffer_size, write_flg, &output_len)) { + free(temp_buffer); + return pContext->ThrowNativeError("Buffer too small or write failed"); + } + + pContext->StringToLocalUTF8(params[2], buffer_size, temp_buffer, nullptr); + free(temp_buffer); + return static_cast(output_len); +} + +static cell_t json_doc_write_to_file(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + char* path; + pContext->LocalToString(params[2], &path); + yyjson_write_flag write_flg = static_cast(params[3]); + + char error[JSON_PACK_ERROR_SIZE]; + if (!g_pJsonManager->WriteToFile(handle, path, write_flg, error, sizeof(error))) { + return pContext->ThrowNativeError(error); + } + + return true; +} + +static cell_t json_obj_get_size(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + size_t size = g_pJsonManager->ObjectGetSize(handle); + return static_cast(size); +} + +static cell_t json_obj_get_key(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + size_t index = static_cast(params[2]); + const char* key = nullptr; + + if (!g_pJsonManager->ObjectGetKey(handle, index, &key)) { + return pContext->ThrowNativeError("Index %d is out of bounds", index); + } + + pContext->StringToLocalUTF8(params[3], params[4], key, nullptr); + return 1; +} + +static cell_t json_obj_get_val_at(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + size_t index = static_cast(params[2]); + + JsonValue* pJSONValue = g_pJsonManager->ObjectGetValueAt(handle, index); + + if (!pJSONValue) { + return pContext->ThrowNativeError("Index %d is out of bounds", index); + } + + return CreateAndReturnHandle(pContext, pJSONValue, "JSON object value"); +} + +static cell_t json_obj_get_val(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + char* key; + pContext->LocalToString(params[2], &key); + + JsonValue* pJSONValue = g_pJsonManager->ObjectGet(handle, key); + + if (!pJSONValue) { + return pContext->ThrowNativeError("Key not found: %s", key); + } + + return CreateAndReturnHandle(pContext, pJSONValue, "JSON object value"); +} + +static cell_t json_obj_get_bool(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + char* key; + pContext->LocalToString(params[2], &key); + + bool value; + if (!g_pJsonManager->ObjectGetBool(handle, key, &value)) { + return pContext->ThrowNativeError("Failed to get boolean for key '%s'", key); + } + + return value; +} + +static cell_t json_obj_get_float(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + char* key; + pContext->LocalToString(params[2], &key); + + double value; + if (!g_pJsonManager->ObjectGetFloat(handle, key, &value)) { + return pContext->ThrowNativeError("Failed to get float for key '%s'", key); + } + + return sp_ftoc(static_cast(value)); +} + +static cell_t json_obj_get_int(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + char* key; + pContext->LocalToString(params[2], &key); + + int value; + if (!g_pJsonManager->ObjectGetInt(handle, key, &value)) { + return pContext->ThrowNativeError("Failed to get integer for key '%s'", key); + } + + return value; +} + +static cell_t json_obj_get_integer64(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + char* key; + pContext->LocalToString(params[2], &key); + + std::variant value; + if (!g_pJsonManager->ObjectGetInt64(handle, key, &value)) { + return pContext->ThrowNativeError("Failed to get integer64 for key '%s'", key); + } + + char result[JSON_INT64_BUFFER_SIZE]; + if (std::holds_alternative(value)) { + snprintf(result, sizeof(result), "%" PRIu64, std::get(value)); + } else { + snprintf(result, sizeof(result), "%" PRId64, std::get(value)); + } + pContext->StringToLocalUTF8(params[3], params[4], result, nullptr); + + return 1; +} + +static cell_t json_obj_get_str(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + char* key; + pContext->LocalToString(params[2], &key); + + const char* str = nullptr; + size_t len = 0; + if (!g_pJsonManager->ObjectGetString(handle, key, &str, &len)) { + return pContext->ThrowNativeError("Failed to get string for key '%s'", key); + } + + size_t maxlen = static_cast(params[4]); + if (len + 1 > maxlen) { + return pContext->ThrowNativeError("Buffer is too small (need %d, have %d)", len + 1, maxlen); + } + + pContext->StringToLocalUTF8(params[3], maxlen, str, nullptr); + + return 1; +} + +static cell_t json_obj_clear(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot clear an immutable JSON object"); + } + + return g_pJsonManager->ObjectClear(handle); +} + +static cell_t json_obj_is_null(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + char* key; + pContext->LocalToString(params[2], &key); + + bool is_null = false; + if (!g_pJsonManager->ObjectIsNull(handle, key, &is_null)) { + return pContext->ThrowNativeError("Key not found: %s", key); + } + + return is_null; +} + +static cell_t json_obj_has_key(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + char* key; + pContext->LocalToString(params[2], &key); + + bool ptr_use = params[3]; + + return g_pJsonManager->ObjectHasKey(handle, key, ptr_use); +} + +static cell_t json_obj_rename_key(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot rename key in an immutable JSON object"); + } + + char* old_key; + pContext->LocalToString(params[2], &old_key); + + char* new_key; + pContext->LocalToString(params[3], &new_key); + + bool allow_duplicate = params[4]; + + if (!g_pJsonManager->ObjectRenameKey(handle, old_key, new_key, allow_duplicate)) { + return pContext->ThrowNativeError("Failed to rename key from '%s' to '%s'", old_key, new_key); + } + + return true; +} + +static cell_t json_obj_set_val(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle1 = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle2 = g_pJsonManager->GetFromHandle(pContext, params[3]); + + if (!handle1 || !handle2) return 0; + + if (!handle1->IsMutable()) { + return pContext->ThrowNativeError("Cannot set value in an immutable JSON object"); + } + + char* key; + pContext->LocalToString(params[2], &key); + + return g_pJsonManager->ObjectSet(handle1, key, handle2); +} + +static cell_t json_obj_set_bool(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot set value in an immutable JSON object"); + } + + char* key; + pContext->LocalToString(params[2], &key); + + return g_pJsonManager->ObjectSetBool(handle, key, params[3]); +} + +static cell_t json_obj_set_float(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot set value in an immutable JSON object"); + } + + char* key; + pContext->LocalToString(params[2], &key); + + return g_pJsonManager->ObjectSetFloat(handle, key, sp_ctof(params[3])); +} + +static cell_t json_obj_set_int(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot set value in an immutable JSON object"); + } + + char* key; + pContext->LocalToString(params[2], &key); + + return g_pJsonManager->ObjectSetInt(handle, key, params[3]); +} + +static cell_t json_obj_set_integer64(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot set value in an immutable JSON object"); + } + + char* key, * value; + pContext->LocalToString(params[2], &key); + pContext->LocalToString(params[3], &value); + + std::variant variant_value; + char error[JSON_ERROR_BUFFER_SIZE]; + if (!g_pJsonManager->ParseInt64Variant(value, &variant_value, error, sizeof(error))) { + return pContext->ThrowNativeError("%s", error); + } + + return g_pJsonManager->ObjectSetInt64(handle, key, variant_value); +} + +static cell_t json_obj_set_null(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot set value in an immutable JSON object"); + } + + char* key; + pContext->LocalToString(params[2], &key); + + return g_pJsonManager->ObjectSetNull(handle, key); +} + +static cell_t json_obj_set_str(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot set value in an immutable JSON object"); + } + + char* key, * value; + pContext->LocalToString(params[2], &key); + pContext->LocalToString(params[3], &value); + + return g_pJsonManager->ObjectSetString(handle, key, value); +} + +static cell_t json_obj_remove(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot remove value from an immutable JSON object"); + } + + char* key; + pContext->LocalToString(params[2], &key); + + return g_pJsonManager->ObjectRemove(handle, key); +} + +static cell_t json_ptr_get_val(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + char* path; + pContext->LocalToString(params[2], &path); + + char error[JSON_PACK_ERROR_SIZE]; + JsonValue* pJSONValue = g_pJsonManager->PtrGet(handle, path, error, sizeof(error)); + + if (!pJSONValue) { + return pContext->ThrowNativeError("%s", error); + } + + return CreateAndReturnHandle(pContext, pJSONValue, "JSON pointer value"); +} + +static cell_t json_ptr_get_bool(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + char* path; + pContext->LocalToString(params[2], &path); + + bool value; + char error[JSON_PACK_ERROR_SIZE]; + if (!g_pJsonManager->PtrGetBool(handle, path, &value, error, sizeof(error))) { + return pContext->ThrowNativeError("%s", error); + } + + return value; +} + +static cell_t json_ptr_get_float(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + char* path; + pContext->LocalToString(params[2], &path); + + double value; + char error[JSON_PACK_ERROR_SIZE]; + if (!g_pJsonManager->PtrGetFloat(handle, path, &value, error, sizeof(error))) { + return pContext->ThrowNativeError("%s", error); + } + + return sp_ftoc(static_cast(value)); +} + +static cell_t json_ptr_get_int(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + char* path; + pContext->LocalToString(params[2], &path); + + int value; + char error[JSON_PACK_ERROR_SIZE]; + if (!g_pJsonManager->PtrGetInt(handle, path, &value, error, sizeof(error))) { + return pContext->ThrowNativeError("%s", error); + } + + return value; +} + +static cell_t json_ptr_get_integer64(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + char* path; + pContext->LocalToString(params[2], &path); + + std::variant value; + char error[JSON_PACK_ERROR_SIZE]; + if (!g_pJsonManager->PtrGetInt64(handle, path, &value, error, sizeof(error))) { + return pContext->ThrowNativeError("%s", error); + } + + char result[JSON_INT64_BUFFER_SIZE]; + if (std::holds_alternative(value)) { + snprintf(result, sizeof(result), "%" PRIu64, std::get(value)); + } else { + snprintf(result, sizeof(result), "%" PRId64, std::get(value)); + } + pContext->StringToLocalUTF8(params[3], params[4], result, nullptr); + + return 1; +} + +static cell_t json_ptr_get_str(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + char* path; + pContext->LocalToString(params[2], &path); + + const char* str = nullptr; + size_t len = 0; + char error[JSON_PACK_ERROR_SIZE]; + if (!g_pJsonManager->PtrGetString(handle, path, &str, &len, error, sizeof(error))) { + return pContext->ThrowNativeError("%s", error); + } + + size_t maxlen = static_cast(params[4]); + if (len + 1 > maxlen) { + return pContext->ThrowNativeError("Buffer is too small (need %d, have %d)", len + 1, maxlen); + } + + pContext->StringToLocalUTF8(params[3], maxlen, str, nullptr); + + return 1; +} + +static cell_t json_ptr_get_is_null(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + char* path; + pContext->LocalToString(params[2], &path); + + bool is_null; + char error[JSON_PACK_ERROR_SIZE]; + if (!g_pJsonManager->PtrGetIsNull(handle, path, &is_null, error, sizeof(error))) { + return pContext->ThrowNativeError("%s", error); + } + + return is_null; +} + +static cell_t json_ptr_get_length(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + char* path; + pContext->LocalToString(params[2], &path); + + size_t len; + char error[JSON_PACK_ERROR_SIZE]; + if (!g_pJsonManager->PtrGetLength(handle, path, &len, error, sizeof(error))) { + return pContext->ThrowNativeError("%s", error); + } + + return static_cast(len); +} + +static cell_t json_ptr_set_val(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle1 = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle2 = g_pJsonManager->GetFromHandle(pContext, params[3]); + + if (!handle1 || !handle2) return 0; + + if (!handle1->IsMutable()) { + return pContext->ThrowNativeError("Cannot set value in an immutable JSON document using pointer"); + } + + char* path; + pContext->LocalToString(params[2], &path); + + char error[JSON_PACK_ERROR_SIZE]; + if (!g_pJsonManager->PtrSet(handle1, path, handle2, error, sizeof(error))) { + return pContext->ThrowNativeError("%s", error); + } + + return true; +} + +static cell_t json_ptr_set_bool(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot set value in an immutable JSON document using pointer"); + } + + char* path; + pContext->LocalToString(params[2], &path); + + char error[JSON_PACK_ERROR_SIZE]; + if (!g_pJsonManager->PtrSetBool(handle, path, params[3], error, sizeof(error))) { + return pContext->ThrowNativeError("%s", error); + } + + return true; +} + +static cell_t json_ptr_set_float(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot set value in an immutable JSON document using pointer"); + } + + char* path; + pContext->LocalToString(params[2], &path); + + char error[JSON_PACK_ERROR_SIZE]; + if (!g_pJsonManager->PtrSetFloat(handle, path, sp_ctof(params[3]), error, sizeof(error))) { + return pContext->ThrowNativeError("%s", error); + } + + return true; +} + +static cell_t json_ptr_set_int(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot set value in an immutable JSON document using pointer"); + } + + char* path; + pContext->LocalToString(params[2], &path); + + char error[JSON_PACK_ERROR_SIZE]; + if (!g_pJsonManager->PtrSetInt(handle, path, params[3], error, sizeof(error))) { + return pContext->ThrowNativeError("%s", error); + } + + return true; +} + +static cell_t json_ptr_set_integer64(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot set value in an immutable JSON document using pointer"); + } + + char* path, * value; + pContext->LocalToString(params[2], &path); + pContext->LocalToString(params[3], &value); + + std::variant variant_value; + char error[JSON_PACK_ERROR_SIZE]; + if (!g_pJsonManager->ParseInt64Variant(value, &variant_value, error, sizeof(error))) { + return pContext->ThrowNativeError("%s", error); + } + + if (!g_pJsonManager->PtrSetInt64(handle, path, variant_value, error, sizeof(error))) { + return pContext->ThrowNativeError("%s", error); + } + + return true; +} + +static cell_t json_ptr_set_str(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot set value in an immutable JSON document using pointer"); + } + + char* path, * str; + pContext->LocalToString(params[2], &path); + pContext->LocalToString(params[3], &str); + + char error[JSON_PACK_ERROR_SIZE]; + if (!g_pJsonManager->PtrSetString(handle, path, str, error, sizeof(error))) { + return pContext->ThrowNativeError("%s", error); + } + + return true; +} + +static cell_t json_ptr_set_null(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot set value in an immutable JSON document using pointer"); + } + + char* path; + pContext->LocalToString(params[2], &path); + + char error[JSON_PACK_ERROR_SIZE]; + if (!g_pJsonManager->PtrSetNull(handle, path, error, sizeof(error))) { + return pContext->ThrowNativeError("%s", error); + } + + return true; +} + +static cell_t json_ptr_add_val(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle1 = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle2 = g_pJsonManager->GetFromHandle(pContext, params[3]); + + if (!handle1 || !handle2) return 0; + + if (!handle1->IsMutable()) { + return pContext->ThrowNativeError("Cannot add value to an immutable JSON document using pointer"); + } + + char* path; + pContext->LocalToString(params[2], &path); + + char error[JSON_PACK_ERROR_SIZE]; + if (!g_pJsonManager->PtrAdd(handle1, path, handle2, error, sizeof(error))) { + return pContext->ThrowNativeError("%s", error); + } + + return true; +} + +static cell_t json_ptr_add_bool(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot add value to an immutable JSON document using pointer"); + } + + char* path; + pContext->LocalToString(params[2], &path); + + char error[JSON_PACK_ERROR_SIZE]; + if (!g_pJsonManager->PtrAddBool(handle, path, params[3], error, sizeof(error))) { + return pContext->ThrowNativeError("%s", error); + } + + return true; +} + +static cell_t json_ptr_add_float(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot add value to an immutable JSON document using pointer"); + } + + char* path; + pContext->LocalToString(params[2], &path); + + char error[JSON_PACK_ERROR_SIZE]; + if (!g_pJsonManager->PtrAddFloat(handle, path, sp_ctof(params[3]), error, sizeof(error))) { + return pContext->ThrowNativeError("%s", error); + } + + return true; +} + +static cell_t json_ptr_add_int(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot add value to an immutable JSON document using pointer"); + } + + char* path; + pContext->LocalToString(params[2], &path); + + char error[JSON_PACK_ERROR_SIZE]; + if (!g_pJsonManager->PtrAddInt(handle, path, params[3], error, sizeof(error))) { + return pContext->ThrowNativeError("%s", error); + } + + return true; +} + +static cell_t json_ptr_add_integer64(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot add value to an immutable JSON document using pointer"); + } + + char* path, * value; + pContext->LocalToString(params[2], &path); + pContext->LocalToString(params[3], &value); + + std::variant variant_value; + char error[JSON_PACK_ERROR_SIZE]; + if (!g_pJsonManager->ParseInt64Variant(value, &variant_value, error, sizeof(error))) { + return pContext->ThrowNativeError("%s", error); + } + + if (!g_pJsonManager->PtrAddInt64(handle, path, variant_value, error, sizeof(error))) { + return pContext->ThrowNativeError("%s", error); + } + + return true; +} + +static cell_t json_ptr_add_str(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot add value to an immutable JSON document using pointer"); + } + + char* path, * str; + pContext->LocalToString(params[2], &path); + pContext->LocalToString(params[3], &str); + + char error[JSON_PACK_ERROR_SIZE]; + if (!g_pJsonManager->PtrAddString(handle, path, str, error, sizeof(error))) { + return pContext->ThrowNativeError("%s", error); + } + + return true; +} + +static cell_t json_ptr_add_null(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot add value to an immutable JSON document using pointer"); + } + + char* path; + pContext->LocalToString(params[2], &path); + + char error[JSON_PACK_ERROR_SIZE]; + if (!g_pJsonManager->PtrAddNull(handle, path, error, sizeof(error))) { + return pContext->ThrowNativeError("%s", error); + } + + return true; +} + +static cell_t json_ptr_remove_val(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot remove value from an immutable JSON document using pointer"); + } + + char* path; + pContext->LocalToString(params[2], &path); + + char error[JSON_PACK_ERROR_SIZE]; + if (!g_pJsonManager->PtrRemove(handle, path, error, sizeof(error))) { + return pContext->ThrowNativeError("%s", error); + } + + return true; +} + +static cell_t json_ptr_try_get_val(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + char* path; + pContext->LocalToString(params[2], &path); + + JsonValue* pJSONValue = g_pJsonManager->PtrTryGet(handle, path); + + if (!pJSONValue) { + return 0; + } + + return CreateAndAssignHandle(pContext, pJSONValue, params[3], "JSON pointer value"); +} + +static cell_t json_ptr_try_get_bool(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + if (!handle) return 0; + + char* path; + pContext->LocalToString(params[2], &path); + + bool value; + if (!g_pJsonManager->PtrTryGetBool(handle, path, &value)) { + return 0; + } + + cell_t* addr; + pContext->LocalToPhysAddr(params[3], &addr); + *addr = value; + + return 1; +} + +static cell_t json_ptr_try_get_float(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + if (!handle) return 0; + + char* path; + pContext->LocalToString(params[2], &path); + + double value; + if (!g_pJsonManager->PtrTryGetFloat(handle, path, &value)) { + return 0; + } + + cell_t* addr; + pContext->LocalToPhysAddr(params[3], &addr); + *addr = sp_ftoc(static_cast(value)); + + return 1; +} + +static cell_t json_ptr_try_get_int(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + if (!handle) return 0; + + char* path; + pContext->LocalToString(params[2], &path); + + int value; + if (!g_pJsonManager->PtrTryGetInt(handle, path, &value)) { + return 0; + } + + cell_t* addr; + pContext->LocalToPhysAddr(params[3], &addr); + *addr = value; + + return 1; +} + +static cell_t json_ptr_try_get_integer64(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + if (!handle) return 0; + + char* path; + pContext->LocalToString(params[2], &path); + + std::variant value; + if (!g_pJsonManager->PtrTryGetInt64(handle, path, &value)) { + return 0; + } + + size_t maxlen = static_cast(params[4]); + char result[JSON_INT64_BUFFER_SIZE]; + if (std::holds_alternative(value)) { + snprintf(result, sizeof(result), "%" PRIu64, std::get(value)); + } else { + snprintf(result, sizeof(result), "%" PRId64, std::get(value)); + } + pContext->StringToLocalUTF8(params[3], maxlen, result, nullptr); + return 1; +} + +static cell_t json_ptr_try_get_str(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + if (!handle) return 0; + + char* path; + pContext->LocalToString(params[2], &path); + + const char* str = nullptr; + size_t len = 0; + + if (!g_pJsonManager->PtrTryGetString(handle, path, &str, &len)) { + return 0; + } + + size_t maxlen = static_cast(params[4]); + if (len + 1 > maxlen) { + return 0; + } + + pContext->StringToLocalUTF8(params[3], maxlen, str, nullptr); + + return 1; +} + +static cell_t json_obj_foreach(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + if (!handle) return 0; + + const char* key = nullptr; + JsonValue* pJSONValue = nullptr; + + if (!g_pJsonManager->ObjectForeachNext(handle, &key, nullptr, &pJSONValue)) { + return false; + } + + pContext->StringToLocalUTF8(params[2], params[3], key, nullptr); + + return CreateAndAssignHandle(pContext, pJSONValue, params[4], "JSON object value"); +} + +static cell_t json_arr_foreach(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + if (!handle) return 0; + + size_t index = 0; + JsonValue* pJSONValue = nullptr; + + if (!g_pJsonManager->ArrayForeachNext(handle, &index, &pJSONValue)) { + return false; + } + + cell_t* indexPtr; + pContext->LocalToPhysAddr(params[2], &indexPtr); + *indexPtr = static_cast(index); + + return CreateAndAssignHandle(pContext, pJSONValue, params[3], "JSON array value"); +} + +static cell_t json_obj_foreach_key(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + if (!handle) return 0; + + const char* key = nullptr; + + if (!g_pJsonManager->ObjectForeachKeyNext(handle, &key, nullptr)) { + return false; + } + + pContext->StringToLocalUTF8(params[2], params[3], key, nullptr); + + return true; +} + +static cell_t json_arr_foreach_index(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + if (!handle) return 0; + + size_t index = 0; + + if (!g_pJsonManager->ArrayForeachIndexNext(handle, &index)) { + return false; + } + + cell_t* indexPtr; + pContext->LocalToPhysAddr(params[2], &indexPtr); + *indexPtr = static_cast(index); + + return true; +} + +static cell_t json_arr_sort(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot sort an immutable JSON array"); + } + + JSON_SORT_ORDER sort_mode = static_cast(params[2]); + if (sort_mode < JSON_SORT_ASC || sort_mode > JSON_SORT_RANDOM) { + return pContext->ThrowNativeError("Invalid sort mode: %d (expected 0=ascending, 1=descending, 2=random)", sort_mode); + } + + return g_pJsonManager->ArraySort(handle, sort_mode); +} + +static cell_t json_obj_sort(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot sort an immutable JSON object"); + } + + JSON_SORT_ORDER sort_mode = static_cast(params[2]); + if (sort_mode < JSON_SORT_ASC || sort_mode > JSON_SORT_RANDOM) { + return pContext->ThrowNativeError("Invalid sort mode: %d (expected 0=ascending, 1=descending, 2=random)", sort_mode); + } + + return g_pJsonManager->ObjectSort(handle, sort_mode); +} + +static cell_t json_doc_to_mutable(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (handle->IsMutable()) { + return pContext->ThrowNativeError("Document is already mutable"); + } + + JsonValue* pJSONValue = g_pJsonManager->ToMutable(handle); + + if (!pJSONValue) { + return pContext->ThrowNativeError("Failed to convert to mutable document"); + } + + return CreateAndReturnHandle(pContext, pJSONValue, "mutable JSON document"); +} + +static cell_t json_doc_to_immutable(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Document is already immutable"); + } + + JsonValue* pJSONValue = g_pJsonManager->ToImmutable(handle); + + if (!pJSONValue) { + return pContext->ThrowNativeError("Failed to convert to immutable document"); + } + + return CreateAndReturnHandle(pContext, pJSONValue, "immutable JSON document"); +} + +static cell_t json_arr_iter_init(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + if (!handle) return 0; + + JsonArrIter* iter = g_pJsonManager->ArrIterWith(handle); + return CreateAndReturnArrIterHandle(pContext, iter, "array iterator"); +} + +static cell_t json_arr_iter_next(IPluginContext* pContext, const cell_t* params) +{ + JsonArrIter* iter = g_pJsonManager->GetArrIterFromHandle(pContext, params[1]); + if (!iter) return 0; + + JsonValue* val = g_pJsonManager->ArrIterNext(iter); + if (!val) return 0; + + return CreateAndReturnHandle(pContext, val, "array iterator value"); +} + +static cell_t json_arr_iter_has_next(IPluginContext* pContext, const cell_t* params) +{ + JsonArrIter* iter = g_pJsonManager->GetArrIterFromHandle(pContext, params[1]); + if (!iter) return 0; + + return g_pJsonManager->ArrIterHasNext(iter); +} + +static cell_t json_arr_iter_get_index(IPluginContext* pContext, const cell_t* params) +{ + JsonArrIter* iter = g_pJsonManager->GetArrIterFromHandle(pContext, params[1]); + if (!iter) return 0; + + size_t index = g_pJsonManager->ArrIterGetIndex(iter); + if (index == SIZE_MAX) { + return -1; + } + + return static_cast(index); +} + +static cell_t json_arr_iter_remove(IPluginContext* pContext, const cell_t* params) +{ + JsonArrIter* iter = g_pJsonManager->GetArrIterFromHandle(pContext, params[1]); + if (!iter) return 0; + + if (!iter->IsMutable()) { + return pContext->ThrowNativeError("Cannot remove from immutable array iterator"); + } + + void* removed = g_pJsonManager->ArrIterRemove(iter); + return removed != nullptr; +} + +static cell_t json_obj_iter_init(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + if (!handle) return 0; + + JsonObjIter* iter = g_pJsonManager->ObjIterWith(handle); + return CreateAndReturnObjIterHandle(pContext, iter, "object iterator"); +} + +static cell_t json_obj_iter_next(IPluginContext* pContext, const cell_t* params) +{ + JsonObjIter* iter = g_pJsonManager->GetObjIterFromHandle(pContext, params[1]); + if (!iter) return 0; + + void* key = g_pJsonManager->ObjIterNext(iter); + if (!key) return 0; + + const char* key_str; + if (iter->IsMutable()) { + key_str = yyjson_mut_get_str(reinterpret_cast(key)); + } else { + key_str = yyjson_get_str(reinterpret_cast(key)); + } + + pContext->StringToLocalUTF8(params[2], params[3], key_str, nullptr); + return 1; +} + +static cell_t json_obj_iter_has_next(IPluginContext* pContext, const cell_t* params) +{ + JsonObjIter* iter = g_pJsonManager->GetObjIterFromHandle(pContext, params[1]); + if (!iter) return 0; + + return g_pJsonManager->ObjIterHasNext(iter); +} + +static cell_t json_obj_iter_get_val(IPluginContext* pContext, const cell_t* params) +{ + JsonObjIter* iter = g_pJsonManager->GetObjIterFromHandle(pContext, params[1]); + if (!iter) return 0; + + void* key = nullptr; + if (iter->IsMutable()) { + key = iter->m_currentKey; + } else { + key = iter->m_currentKey; + } + + if (!key) { + return pContext->ThrowNativeError("Iterator not positioned at a valid key (call Next() first)"); + } + + JsonValue* val = g_pJsonManager->ObjIterGetVal(iter, key); + if (!val) { + return pContext->ThrowNativeError("Failed to get value from iterator"); + } + + return CreateAndReturnHandle(pContext, val, "object iterator value"); +} + +static cell_t json_obj_iter_get(IPluginContext* pContext, const cell_t* params) +{ + JsonObjIter* iter = g_pJsonManager->GetObjIterFromHandle(pContext, params[1]); + if (!iter) return 0; + + char* key; + pContext->LocalToString(params[2], &key); + + JsonValue* val = g_pJsonManager->ObjIterGet(iter, key); + if (!val) { + return 0; + } + + return CreateAndReturnHandle(pContext, val, "object iterator value"); +} + +static cell_t json_obj_iter_get_index(IPluginContext* pContext, const cell_t* params) +{ + JsonObjIter* iter = g_pJsonManager->GetObjIterFromHandle(pContext, params[1]); + if (!iter) return 0; + + size_t index = g_pJsonManager->ObjIterGetIndex(iter); + if (index == SIZE_MAX) { + return -1; + } + + return static_cast(index); +} + +static cell_t json_obj_iter_remove(IPluginContext* pContext, const cell_t* params) +{ + JsonObjIter* iter = g_pJsonManager->GetObjIterFromHandle(pContext, params[1]); + if (!iter) return 0; + + if (!iter->IsMutable()) { + return pContext->ThrowNativeError("Cannot remove from immutable object iterator"); + } + + void* removed = g_pJsonManager->ObjIterRemove(iter); + return removed != nullptr; +} + +static cell_t json_read_number(IPluginContext* pContext, const cell_t* params) +{ + char* dat; + pContext->LocalToString(params[1], &dat); + yyjson_read_flag read_flg = static_cast(params[2]); + + char error[JSON_ERROR_BUFFER_SIZE]; + size_t consumed = 0; + JsonValue* pJSONValue = g_pJsonManager->ReadNumber(dat, read_flg, error, sizeof(error), &consumed); + + if (!pJSONValue) { + return pContext->ThrowNativeError("%s", error); + } + + cell_t* consumedPtr = nullptr; + if (params[4] != 0) { + pContext->LocalToPhysAddr(params[4], &consumedPtr); + if (consumedPtr) { + *consumedPtr = static_cast(consumed); + } + } + + return CreateAndReturnHandle(pContext, pJSONValue, "read number"); +} + +static cell_t json_write_number(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + if (!handle) return 0; + + size_t buffer_size = static_cast(params[3]); + char* temp_buffer = (char*)malloc(buffer_size); + if (!temp_buffer) { + return pContext->ThrowNativeError("Failed to allocate buffer"); + } + + size_t written = 0; + if (!g_pJsonManager->WriteNumber(handle, temp_buffer, buffer_size, &written)) { + free(temp_buffer); + return pContext->ThrowNativeError("Failed to write number or buffer too small"); + } + + pContext->StringToLocalUTF8(params[2], buffer_size, temp_buffer, nullptr); + free(temp_buffer); + + cell_t* writtenPtr = nullptr; + if (params[4] != 0) { + pContext->LocalToPhysAddr(params[4], &writtenPtr); + if (writtenPtr) { + *writtenPtr = static_cast(written); + } + } + + return 1; +} + +static cell_t json_set_fp_to_float(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + if (!handle) return 0; + + bool flt = params[2] != 0; + if (!g_pJsonManager->SetFpToFloat(handle, flt)) { + return pContext->ThrowNativeError("Failed to set floating-point format to float (value is not a floating-point number)"); + } + + return 1; +} + +static cell_t json_set_fp_to_fixed(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + if (!handle) return 0; + + int prec = params[2]; + if (!g_pJsonManager->SetFpToFixed(handle, prec)) { + return pContext->ThrowNativeError("Failed to set floating-point format to fixed (value is not a floating-point number or precision out of range 1-15)"); + } + + return 1; +} + +static cell_t json_set_bool(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + if (!handle) return 0; + + bool value = params[2] != 0; + if (!g_pJsonManager->SetBool(handle, value)) { + return pContext->ThrowNativeError("Failed to set value to boolean (value is object or array)"); + } + + return 1; +} + +static cell_t json_set_int(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + if (!handle) return 0; + + int value = params[2]; + if (!g_pJsonManager->SetInt(handle, value)) { + return pContext->ThrowNativeError("Failed to set value to integer (value is object or array)"); + } + + return 1; +} + +static cell_t json_set_int64(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + if (!handle) return 0; + + char* str; + pContext->LocalToString(params[2], &str); + + std::variant variant_value; + char error[JSON_ERROR_BUFFER_SIZE]; + if (!g_pJsonManager->ParseInt64Variant(str, &variant_value, error, sizeof(error))) { + return pContext->ThrowNativeError("%s", error); + } + + if (!g_pJsonManager->SetInt64(handle, variant_value)) { + return pContext->ThrowNativeError("Failed to set value to int64 (value is object or array)"); + } + + return 1; +} + +static cell_t json_set_float(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + if (!handle) return 0; + + double value = sp_ctof(params[2]); + if (!g_pJsonManager->SetFloat(handle, value)) { + return pContext->ThrowNativeError("Failed to set value to float (value is object or array)"); + } + + return 1; +} + +static cell_t json_set_string(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + if (!handle) return 0; + + char* value; + pContext->LocalToString(params[2], &value); + + if (!g_pJsonManager->SetString(handle, value)) { + return pContext->ThrowNativeError("Failed to set value to string (value is object or array)"); + } + + return 1; +} + +static cell_t json_set_null(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + if (!handle) return 0; + + if (!g_pJsonManager->SetNull(handle)) { + return pContext->ThrowNativeError("Failed to set value to null (value is object or array)"); + } + + return 1; +} + +const sp_nativeinfo_t g_JsonNatives[] = +{ + // JSONObject + {"JSONObject.JSONObject", json_obj_init}, + {"JSONObject.FromStrings", json_obj_init_with_str}, + {"JSONObject.Size.get", json_obj_get_size}, + {"JSONObject.Get", json_obj_get_val}, + {"JSONObject.GetBool", json_obj_get_bool}, + {"JSONObject.GetFloat", json_obj_get_float}, + {"JSONObject.GetInt", json_obj_get_int}, + {"JSONObject.GetInt64", json_obj_get_integer64}, + {"JSONObject.GetString", json_obj_get_str}, + {"JSONObject.IsNull", json_obj_is_null}, + {"JSONObject.GetKey", json_obj_get_key}, + {"JSONObject.GetValueAt", json_obj_get_val_at}, + {"JSONObject.HasKey", json_obj_has_key}, + {"JSONObject.RenameKey", json_obj_rename_key}, + {"JSONObject.Set", json_obj_set_val}, + {"JSONObject.SetBool", json_obj_set_bool}, + {"JSONObject.SetFloat", json_obj_set_float}, + {"JSONObject.SetInt", json_obj_set_int}, + {"JSONObject.SetInt64", json_obj_set_integer64}, + {"JSONObject.SetNull", json_obj_set_null}, + {"JSONObject.SetString", json_obj_set_str}, + {"JSONObject.Remove", json_obj_remove}, + {"JSONObject.Clear", json_obj_clear}, + {"JSONObject.FromString", json_obj_parse_str}, + {"JSONObject.FromFile", json_obj_parse_file}, + {"JSONObject.Sort", json_obj_sort}, + + // JSONArray + {"JSONArray.JSONArray", json_arr_init}, + {"JSONArray.FromStrings", json_arr_init_with_str}, + {"JSONArray.FromInt", json_arr_init_with_int32}, + {"JSONArray.FromInt64", json_arr_init_with_int64}, + {"JSONArray.FromBool", json_arr_init_with_bool}, + {"JSONArray.FromFloat", json_arr_init_with_float}, + {"JSONArray.Length.get", json_arr_get_size}, + {"JSONArray.Get", json_arr_get_val}, + {"JSONArray.First.get", json_arr_get_first}, + {"JSONArray.Last.get", json_arr_get_last}, + {"JSONArray.GetBool", json_arr_get_bool}, + {"JSONArray.GetFloat", json_arr_get_float}, + {"JSONArray.GetInt", json_arr_get_integer}, + {"JSONArray.GetInt64", json_arr_get_integer64}, + {"JSONArray.GetString", json_arr_get_str}, + {"JSONArray.IsNull", json_arr_is_null}, + {"JSONArray.Set", json_arr_replace_val}, + {"JSONArray.SetBool", json_arr_replace_bool}, + {"JSONArray.SetFloat", json_arr_replace_float}, + {"JSONArray.SetInt", json_arr_replace_integer}, + {"JSONArray.SetInt64", json_arr_replace_integer64}, + {"JSONArray.SetNull", json_arr_replace_null}, + {"JSONArray.SetString", json_arr_replace_str}, + {"JSONArray.Push", json_arr_append_val}, + {"JSONArray.PushBool", json_arr_append_bool}, + {"JSONArray.PushFloat", json_arr_append_float}, + {"JSONArray.PushInt", json_arr_append_int}, + {"JSONArray.PushInt64", json_arr_append_integer64}, + {"JSONArray.PushNull", json_arr_append_null}, + {"JSONArray.PushString", json_arr_append_str}, + {"JSONArray.Insert", json_arr_insert}, + {"JSONArray.InsertBool", json_arr_insert_bool}, + {"JSONArray.InsertInt", json_arr_insert_int}, + {"JSONArray.InsertInt64", json_arr_insert_int64}, + {"JSONArray.InsertFloat", json_arr_insert_float}, + {"JSONArray.InsertString", json_arr_insert_str}, + {"JSONArray.InsertNull", json_arr_insert_null}, + {"JSONArray.Prepend", json_arr_prepend}, + {"JSONArray.PrependBool", json_arr_prepend_bool}, + {"JSONArray.PrependInt", json_arr_prepend_int}, + {"JSONArray.PrependInt64", json_arr_prepend_int64}, + {"JSONArray.PrependFloat", json_arr_prepend_float}, + {"JSONArray.PrependString", json_arr_prepend_str}, + {"JSONArray.PrependNull", json_arr_prepend_null}, + {"JSONArray.Remove", json_arr_remove}, + {"JSONArray.RemoveFirst", json_arr_remove_first}, + {"JSONArray.RemoveLast", json_arr_remove_last}, + {"JSONArray.RemoveRange", json_arr_remove_range}, + {"JSONArray.Clear", json_arr_clear}, + {"JSONArray.FromString", json_arr_parse_str}, + {"JSONArray.FromFile", json_arr_parse_file}, + {"JSONArray.IndexOfBool", json_arr_index_of_bool}, + {"JSONArray.IndexOfString", json_arr_index_of_str}, + {"JSONArray.IndexOfInt", json_arr_index_of_int}, + {"JSONArray.IndexOfInt64", json_arr_index_of_integer64}, + {"JSONArray.IndexOfUint64", json_arr_index_of_uint64}, + {"JSONArray.IndexOfFloat", json_arr_index_of_float}, + {"JSONArray.Sort", json_arr_sort}, + + // JSON UTILITY + {"JSON.ToString", json_doc_write_to_str}, + {"JSON.ToFile", json_doc_write_to_file}, + {"JSON.Parse", json_doc_parse}, + {"JSON.Equals", json_doc_equals}, + {"JSON.EqualsStr", json_equals_str}, + {"JSON.DeepCopy", json_doc_copy_deep}, + {"JSON.GetTypeDesc", json_get_type_desc}, + {"JSON.GetSerializedSize", json_get_serialized_size}, + {"JSON.ReadSize.get", json_get_read_size}, + {"JSON.Type.get", json_get_type}, + {"JSON.SubType.get", json_get_subtype}, + {"JSON.IsArray.get", json_is_array}, + {"JSON.IsObject.get", json_is_object}, + {"JSON.IsInt.get", json_is_int}, + {"JSON.IsUint.get", json_is_uint}, + {"JSON.IsSint.get", json_is_sint}, + {"JSON.IsNum.get", json_is_num}, + {"JSON.IsBool.get", json_is_bool}, + {"JSON.IsTrue.get", json_is_true}, + {"JSON.IsFalse.get", json_is_false}, + {"JSON.IsFloat.get", json_is_float}, + {"JSON.IsStr.get", json_is_str}, + {"JSON.IsNull.get", json_is_null}, + {"JSON.IsCtn.get", json_is_ctn}, + {"JSON.IsMutable.get", json_is_mutable}, + {"JSON.IsImmutable.get", json_is_immutable}, + {"JSON.ForeachObject", json_obj_foreach}, + {"JSON.ForeachArray", json_arr_foreach}, + {"JSON.ForeachKey", json_obj_foreach_key}, + {"JSON.ForeachIndex", json_arr_foreach_index}, + {"JSON.ToMutable", json_doc_to_mutable}, + {"JSON.ToImmutable", json_doc_to_immutable}, + {"JSON.ReadNumber", json_read_number}, + {"JSON.WriteNumber", json_write_number}, + {"JSON.SetFpToFloat", json_set_fp_to_float}, + {"JSON.SetFpToFixed", json_set_fp_to_fixed}, + + // JSON CREATE/GET/SET + {"JSON.Pack", json_pack}, + {"JSON.CreateBool", json_create_bool}, + {"JSON.CreateFloat", json_create_float}, + {"JSON.CreateInt", json_create_int}, + {"JSON.CreateInt64", json_create_integer64}, + {"JSON.CreateNull", json_create_null}, + {"JSON.CreateString", json_create_str}, + {"JSON.GetBool", json_get_bool}, + {"JSON.GetFloat", json_get_float}, + {"JSON.GetInt", json_get_int}, + {"JSON.GetInt64", json_get_integer64}, + {"JSON.GetString", json_get_str}, + {"JSON.SetBool", json_set_bool}, + {"JSON.SetInt", json_set_int}, + {"JSON.SetInt64", json_set_int64}, + {"JSON.SetFloat", json_set_float}, + {"JSON.SetString", json_set_string}, + {"JSON.SetNull", json_set_null}, + + // JSON POINTER + {"JSON.PtrGet", json_ptr_get_val}, + {"JSON.PtrGetBool", json_ptr_get_bool}, + {"JSON.PtrGetFloat", json_ptr_get_float}, + {"JSON.PtrGetInt", json_ptr_get_int}, + {"JSON.PtrGetInt64", json_ptr_get_integer64}, + {"JSON.PtrGetString", json_ptr_get_str}, + {"JSON.PtrGetIsNull", json_ptr_get_is_null}, + {"JSON.PtrGetLength", json_ptr_get_length}, + {"JSON.PtrSet", json_ptr_set_val}, + {"JSON.PtrSetBool", json_ptr_set_bool}, + {"JSON.PtrSetFloat", json_ptr_set_float}, + {"JSON.PtrSetInt", json_ptr_set_int}, + {"JSON.PtrSetInt64", json_ptr_set_integer64}, + {"JSON.PtrSetString", json_ptr_set_str}, + {"JSON.PtrSetNull", json_ptr_set_null}, + {"JSON.PtrAdd", json_ptr_add_val}, + {"JSON.PtrAddBool", json_ptr_add_bool}, + {"JSON.PtrAddFloat", json_ptr_add_float}, + {"JSON.PtrAddInt", json_ptr_add_int}, + {"JSON.PtrAddInt64", json_ptr_add_integer64}, + {"JSON.PtrAddString", json_ptr_add_str}, + {"JSON.PtrAddNull", json_ptr_add_null}, + {"JSON.PtrRemove", json_ptr_remove_val}, + {"JSON.PtrTryGetVal", json_ptr_try_get_val}, + {"JSON.PtrTryGetBool", json_ptr_try_get_bool}, + {"JSON.PtrTryGetFloat", json_ptr_try_get_float}, + {"JSON.PtrTryGetInt", json_ptr_try_get_int}, + {"JSON.PtrTryGetInt64", json_ptr_try_get_integer64}, + {"JSON.PtrTryGetString", json_ptr_try_get_str}, + + // JSONArrIter + {"JSONArrIter.JSONArrIter", json_arr_iter_init}, + {"JSONArrIter.Next.get", json_arr_iter_next}, + {"JSONArrIter.HasNext.get", json_arr_iter_has_next}, + {"JSONArrIter.Index.get", json_arr_iter_get_index}, + {"JSONArrIter.Remove", json_arr_iter_remove}, + + // JSONObjIter + {"JSONObjIter.JSONObjIter", json_obj_iter_init}, + {"JSONObjIter.Next", json_obj_iter_next}, + {"JSONObjIter.HasNext.get", json_obj_iter_has_next}, + {"JSONObjIter.Value.get", json_obj_iter_get_val}, + {"JSONObjIter.Get", json_obj_iter_get}, + {"JSONObjIter.Index.get", json_obj_iter_get_index}, + {"JSONObjIter.Remove", json_obj_iter_remove}, + + {nullptr, nullptr} +}; \ No newline at end of file diff --git a/src/YYJSONManager.h b/src/YYJSONManager.h deleted file mode 100644 index 802e5c5..0000000 --- a/src/YYJSONManager.h +++ /dev/null @@ -1,264 +0,0 @@ -#ifndef _INCLUDE_SM_YYJSON_YYJSONMANAGER_H_ -#define _INCLUDE_SM_YYJSON_YYJSONMANAGER_H_ - -#include -#include -#include -#include -#include - -/** - * @brief JSON value wrapper - * - * Wraps yyjson mutable/immutable documents and values. - * Used as the primary data type for JSON operations. - */ -class YYJSONValue { -public: - YYJSONValue() = default; - ~YYJSONValue() { - if (m_pDocument_mut.unique()) { - yyjson_mut_doc_free(m_pDocument_mut.get()); - } - if (m_pDocument.unique()) { - yyjson_doc_free(m_pDocument.get()); - } - } - - YYJSONValue(const YYJSONValue&) = delete; - YYJSONValue& operator=(const YYJSONValue&) = delete; - - void ResetObjectIterator() { - m_iterInitialized = false; - } - - void ResetArrayIterator() { - m_iterInitialized = false; - m_arrayIndex = 0; - } - - bool IsMutable() const { - return m_pDocument_mut != nullptr; - } - - bool IsImmutable() const { - return m_pDocument != nullptr; - } - - // Mutable document - std::shared_ptr m_pDocument_mut; - yyjson_mut_val* m_pVal_mut{ nullptr }; - - // Immutable document - std::shared_ptr m_pDocument; - yyjson_val* m_pVal{ nullptr }; - - // Mutable document iterators - yyjson_mut_obj_iter m_iterObj; - yyjson_mut_arr_iter m_iterArr; - - // Immutable document iterators - yyjson_obj_iter m_iterObjImm; - yyjson_arr_iter m_iterArrImm; - - Handle_t m_handle{ BAD_HANDLE }; - size_t m_arrayIndex{ 0 }; - size_t m_readSize{ 0 }; - bool m_iterInitialized{ false }; -}; - -class YYJSONManager : public IYYJSONManager -{ -public: - YYJSONManager(); - ~YYJSONManager(); - -public: - // ========== Document Operations ========== - virtual YYJSONValue* ParseJSON(const char* json_str, bool is_file, bool is_mutable, - yyjson_read_flag read_flg, char* error, size_t error_size) override; - virtual bool WriteToString(YYJSONValue* handle, char* buffer, size_t buffer_size, - yyjson_write_flag write_flg, size_t* out_size) override; - virtual bool WriteToFile(YYJSONValue* handle, const char* path, yyjson_write_flag write_flg, - char* error, size_t error_size) override; - virtual bool Equals(YYJSONValue* handle1, YYJSONValue* handle2) override; - virtual YYJSONValue* DeepCopy(YYJSONValue* targetDoc, YYJSONValue* sourceValue) override; - virtual const char* GetTypeDesc(YYJSONValue* handle) override; - virtual size_t GetSerializedSize(YYJSONValue* handle, yyjson_write_flag write_flg) override; - virtual YYJSONValue* ToMutable(YYJSONValue* handle) override; - virtual YYJSONValue* ToImmutable(YYJSONValue* handle) override; - virtual yyjson_type GetType(YYJSONValue* handle) override; - virtual yyjson_subtype GetSubtype(YYJSONValue* handle) override; - virtual bool IsArray(YYJSONValue* handle) override; - virtual bool IsObject(YYJSONValue* handle) override; - virtual bool IsInt(YYJSONValue* handle) override; - virtual bool IsUint(YYJSONValue* handle) override; - virtual bool IsSint(YYJSONValue* handle) override; - virtual bool IsNum(YYJSONValue* handle) override; - virtual bool IsBool(YYJSONValue* handle) override; - virtual bool IsTrue(YYJSONValue* handle) override; - virtual bool IsFalse(YYJSONValue* handle) override; - virtual bool IsFloat(YYJSONValue* handle) override; - virtual bool IsStr(YYJSONValue* handle) override; - virtual bool IsNull(YYJSONValue* handle) override; - virtual bool IsCtn(YYJSONValue* handle) override; - virtual bool IsMutable(YYJSONValue* handle) override; - virtual bool IsImmutable(YYJSONValue* handle) override; - virtual size_t GetReadSize(YYJSONValue* handle) override; - - // ========== Object Operations ========== - virtual YYJSONValue* ObjectInit() override; - virtual YYJSONValue* ObjectInitWithStrings(const char** pairs, size_t count) override; - virtual YYJSONValue* ObjectParseString(const char* str, yyjson_read_flag read_flg, - char* error, size_t error_size) override; - virtual YYJSONValue* ObjectParseFile(const char* path, yyjson_read_flag read_flg, - char* error, size_t error_size) override; - virtual size_t ObjectGetSize(YYJSONValue* handle) override; - virtual bool ObjectGetKey(YYJSONValue* handle, size_t index, const char** out_key) override; - virtual YYJSONValue* ObjectGetValueAt(YYJSONValue* handle, size_t index) override; - virtual YYJSONValue* ObjectGet(YYJSONValue* handle, const char* key) override; - virtual bool ObjectGetBool(YYJSONValue* handle, const char* key, bool* out_value) override; - virtual bool ObjectGetFloat(YYJSONValue* handle, const char* key, double* out_value) override; - virtual bool ObjectGetInt(YYJSONValue* handle, const char* key, int* out_value) override; - virtual bool ObjectGetInt64(YYJSONValue* handle, const char* key, int64_t* out_value) override; - virtual bool ObjectGetString(YYJSONValue* handle, const char* key, const char** out_str, size_t* out_len) override; - virtual bool ObjectIsNull(YYJSONValue* handle, const char* key, bool* out_is_null) override; - virtual bool ObjectHasKey(YYJSONValue* handle, const char* key, bool use_pointer) override; - virtual bool ObjectRenameKey(YYJSONValue* handle, const char* old_key, const char* new_key, bool allow_duplicate) override; - virtual bool ObjectSet(YYJSONValue* handle, const char* key, YYJSONValue* value) override; - virtual bool ObjectSetBool(YYJSONValue* handle, const char* key, bool value) override; - virtual bool ObjectSetFloat(YYJSONValue* handle, const char* key, double value) override; - virtual bool ObjectSetInt(YYJSONValue* handle, const char* key, int value) override; - virtual bool ObjectSetInt64(YYJSONValue* handle, const char* key, int64_t value) override; - virtual bool ObjectSetNull(YYJSONValue* handle, const char* key) override; - virtual bool ObjectSetString(YYJSONValue* handle, const char* key, const char* value) override; - virtual bool ObjectRemove(YYJSONValue* handle, const char* key) override; - virtual bool ObjectClear(YYJSONValue* handle) override; - virtual bool ObjectSort(YYJSONValue* handle, YYJSON_SORT_ORDER sort_mode) override; - - // ========== Array Operations ========== - virtual YYJSONValue* ArrayInit() override; - virtual YYJSONValue* ArrayInitWithStrings(const char** strings, size_t count) override; - virtual YYJSONValue* ArrayParseString(const char* str, yyjson_read_flag read_flg, - char* error, size_t error_size) override; - virtual YYJSONValue* ArrayParseFile(const char* path, yyjson_read_flag read_flg, - char* error, size_t error_size) override; - virtual size_t ArrayGetSize(YYJSONValue* handle) override; - virtual YYJSONValue* ArrayGet(YYJSONValue* handle, size_t index) override; - virtual YYJSONValue* ArrayGetFirst(YYJSONValue* handle) override; - virtual YYJSONValue* ArrayGetLast(YYJSONValue* handle) override; - virtual bool ArrayGetBool(YYJSONValue* handle, size_t index, bool* out_value) override; - virtual bool ArrayGetFloat(YYJSONValue* handle, size_t index, double* out_value) override; - virtual bool ArrayGetInt(YYJSONValue* handle, size_t index, int* out_value) override; - virtual bool ArrayGetInt64(YYJSONValue* handle, size_t index, int64_t* out_value) override; - virtual bool ArrayGetString(YYJSONValue* handle, size_t index, const char** out_str, size_t* out_len) override; - virtual bool ArrayIsNull(YYJSONValue* handle, size_t index) override; - virtual bool ArrayReplace(YYJSONValue* handle, size_t index, YYJSONValue* value) override; - virtual bool ArrayReplaceBool(YYJSONValue* handle, size_t index, bool value) override; - virtual bool ArrayReplaceFloat(YYJSONValue* handle, size_t index, double value) override; - virtual bool ArrayReplaceInt(YYJSONValue* handle, size_t index, int value) override; - virtual bool ArrayReplaceInt64(YYJSONValue* handle, size_t index, int64_t value) override; - virtual bool ArrayReplaceNull(YYJSONValue* handle, size_t index) override; - virtual bool ArrayReplaceString(YYJSONValue* handle, size_t index, const char* value) override; - virtual bool ArrayAppend(YYJSONValue* handle, YYJSONValue* value) override; - virtual bool ArrayAppendBool(YYJSONValue* handle, bool value) override; - virtual bool ArrayAppendFloat(YYJSONValue* handle, double value) override; - virtual bool ArrayAppendInt(YYJSONValue* handle, int value) override; - virtual bool ArrayAppendInt64(YYJSONValue* handle, int64_t value) override; - virtual bool ArrayAppendNull(YYJSONValue* handle) override; - virtual bool ArrayAppendString(YYJSONValue* handle, const char* value) override; - virtual bool ArrayRemove(YYJSONValue* handle, size_t index) override; - virtual bool ArrayRemoveFirst(YYJSONValue* handle) override; - virtual bool ArrayRemoveLast(YYJSONValue* handle) override; - virtual bool ArrayRemoveRange(YYJSONValue* handle, size_t start_index, size_t end_index) override; - virtual bool ArrayClear(YYJSONValue* handle) override; - virtual int ArrayIndexOfBool(YYJSONValue* handle, bool search_value) override; - virtual int ArrayIndexOfString(YYJSONValue* handle, const char* search_value) override; - virtual int ArrayIndexOfInt(YYJSONValue* handle, int search_value) override; - virtual int ArrayIndexOfInt64(YYJSONValue* handle, int64_t search_value) override; - virtual int ArrayIndexOfFloat(YYJSONValue* handle, double search_value) override; - virtual bool ArraySort(YYJSONValue* handle, YYJSON_SORT_ORDER sort_mode) override; - - // ========== Value Operations ========== - virtual YYJSONValue* Pack(const char* format, IPackParamProvider* param_provider, char* error, size_t error_size) override; - virtual YYJSONValue* CreateBool(bool value) override; - virtual YYJSONValue* CreateFloat(double value) override; - virtual YYJSONValue* CreateInt(int value) override; - virtual YYJSONValue* CreateInt64(int64_t value) override; - virtual YYJSONValue* CreateNull() override; - virtual YYJSONValue* CreateString(const char* value) override; - virtual bool GetBool(YYJSONValue* handle, bool* out_value) override; - virtual bool GetFloat(YYJSONValue* handle, double* out_value) override; - virtual bool GetInt(YYJSONValue* handle, int* out_value) override; - virtual bool GetInt64(YYJSONValue* handle, int64_t* out_value) override; - virtual bool GetString(YYJSONValue* handle, const char** out_str, size_t* out_len) override; - - // ========== Pointer Operations ========== - virtual YYJSONValue* PtrGet(YYJSONValue* handle, const char* path, char* error, size_t error_size) override; - virtual bool PtrGetBool(YYJSONValue* handle, const char* path, bool* out_value, char* error, size_t error_size) override; - virtual bool PtrGetFloat(YYJSONValue* handle, const char* path, double* out_value, char* error, size_t error_size) override; - virtual bool PtrGetInt(YYJSONValue* handle, const char* path, int* out_value, char* error, size_t error_size) override; - virtual bool PtrGetInt64(YYJSONValue* handle, const char* path, int64_t* out_value, char* error, size_t error_size) override; - virtual bool PtrGetString(YYJSONValue* handle, const char* path, const char** out_str, size_t* out_len, char* error, size_t error_size) override; - virtual bool PtrGetIsNull(YYJSONValue* handle, const char* path, bool* out_is_null, char* error, size_t error_size) override; - virtual bool PtrGetLength(YYJSONValue* handle, const char* path, size_t* out_len, char* error, size_t error_size) override; - virtual bool PtrSet(YYJSONValue* handle, const char* path, YYJSONValue* value, char* error, size_t error_size) override; - virtual bool PtrSetBool(YYJSONValue* handle, const char* path, bool value, char* error, size_t error_size) override; - virtual bool PtrSetFloat(YYJSONValue* handle, const char* path, double value, char* error, size_t error_size) override; - virtual bool PtrSetInt(YYJSONValue* handle, const char* path, int value, char* error, size_t error_size) override; - virtual bool PtrSetInt64(YYJSONValue* handle, const char* path, int64_t value, char* error, size_t error_size) override; - virtual bool PtrSetString(YYJSONValue* handle, const char* path, const char* value, char* error, size_t error_size) override; - virtual bool PtrSetNull(YYJSONValue* handle, const char* path, char* error, size_t error_size) override; - virtual bool PtrAdd(YYJSONValue* handle, const char* path, YYJSONValue* value, char* error, size_t error_size) override; - virtual bool PtrAddBool(YYJSONValue* handle, const char* path, bool value, char* error, size_t error_size) override; - virtual bool PtrAddFloat(YYJSONValue* handle, const char* path, double value, char* error, size_t error_size) override; - virtual bool PtrAddInt(YYJSONValue* handle, const char* path, int value, char* error, size_t error_size) override; - virtual bool PtrAddInt64(YYJSONValue* handle, const char* path, int64_t value, char* error, size_t error_size) override; - virtual bool PtrAddString(YYJSONValue* handle, const char* path, const char* value, char* error, size_t error_size) override; - virtual bool PtrAddNull(YYJSONValue* handle, const char* path, char* error, size_t error_size) override; - virtual bool PtrRemove(YYJSONValue* handle, const char* path, char* error, size_t error_size) override; - virtual YYJSONValue* PtrTryGet(YYJSONValue* handle, const char* path) override; - virtual bool PtrTryGetBool(YYJSONValue* handle, const char* path, bool* out_value) override; - virtual bool PtrTryGetFloat(YYJSONValue* handle, const char* path, double* out_value) override; - virtual bool PtrTryGetInt(YYJSONValue* handle, const char* path, int* out_value) override; - virtual bool PtrTryGetInt64(YYJSONValue* handle, const char* path, int64_t* out_value) override; - virtual bool PtrTryGetString(YYJSONValue* handle, const char* path, const char** out_str, size_t* out_len) override; - - // ========== Iterator Operations ========== - virtual bool ObjectForeachNext(YYJSONValue* handle, const char** out_key, - size_t* out_key_len, YYJSONValue** out_value) override; - virtual bool ArrayForeachNext(YYJSONValue* handle, size_t* out_index, - YYJSONValue** out_value) override; - virtual bool ObjectForeachKeyNext(YYJSONValue* handle, const char** out_key, - size_t* out_key_len) override; - virtual bool ArrayForeachIndexNext(YYJSONValue* handle, size_t* out_index) override; - - // ========== Release Operations ========== - virtual void Release(YYJSONValue* value) override; - - // ========== Handle Type Operations ========== - virtual HandleType_t GetHandleType() override; - - // ========== Handle Operations ========== - virtual YYJSONValue* GetFromHandle(IPluginContext* pContext, Handle_t handle) override; - -private: - std::random_device m_randomDevice; - std::mt19937 m_randomGenerator; - - // Helper methods - static std::unique_ptr CreateWrapper(); - static std::shared_ptr WrapDocument(yyjson_mut_doc* doc); - static std::shared_ptr CopyDocument(yyjson_doc* doc); - static std::shared_ptr CreateDocument(); - static std::shared_ptr WrapImmutableDocument(yyjson_doc* doc); - - // Pack helper methods - static const char* SkipSeparators(const char* ptr); - static void SetPackError(char* error, size_t error_size, const char* fmt, ...); - static yyjson_mut_val* PackImpl(yyjson_mut_doc* doc, const char* format, - IPackParamProvider* provider, char* error, - size_t error_size, const char** out_end_ptr); -}; - -#endif // _INCLUDE_SM_YYJSON_YYJSONMANAGER_H_ \ No newline at end of file diff --git a/src/extension.cpp b/src/extension.cpp index f927f23..3a3ed0c 100755 --- a/src/extension.cpp +++ b/src/extension.cpp @@ -1,17 +1,21 @@ #include "extension.h" -#include "YYJSONManager.h" +#include "JsonManager.h" -JsonExtension g_JsonExtension; -SMEXT_LINK(&g_JsonExtension); +JsonExtension g_JsonExt; +SMEXT_LINK(&g_JsonExt); -HandleType_t g_htJSON; -JSONHandler g_JSONHandler; -IYYJSONManager* g_pYYJSONManager; +HandleType_t g_JsonType; +HandleType_t g_ArrIterType; +HandleType_t g_ObjIterType; +JsonHandler g_JsonHandler; +ArrIterHandler g_ArrIterHandler; +ObjIterHandler g_ObjIterHandler; +IJsonManager* g_pJsonManager; bool JsonExtension::SDK_OnLoad(char* error, size_t maxlen, bool late) { - sharesys->AddNatives(myself, json_natives); - sharesys->RegisterLibrary(myself, "yyjson"); + sharesys->AddNatives(myself, g_JsonNatives); + sharesys->RegisterLibrary(myself, "json"); HandleAccess haJSON; handlesys->InitAccessDefaults(nullptr, &haJSON); @@ -19,39 +23,63 @@ bool JsonExtension::SDK_OnLoad(char* error, size_t maxlen, bool late) haJSON.access[HandleAccess_Delete] = 0; HandleError err; - g_htJSON = handlesys->CreateType("YYJSON", &g_JSONHandler, 0, nullptr, &haJSON, myself->GetIdentity(), &err); + g_JsonType = handlesys->CreateType("JSON", &g_JsonHandler, 0, nullptr, &haJSON, myself->GetIdentity(), &err); - if (!g_htJSON) { - snprintf(error, maxlen, "Failed to create YYJSON handle type (err: %d)", err); + if (!g_JsonType) { + snprintf(error, maxlen, "Failed to create JSON handle type (err: %d)", err); + return false; + } + + g_ArrIterType = handlesys->CreateType("JSONArrIter", &g_ArrIterHandler, 0, nullptr, &haJSON, myself->GetIdentity(), &err); + if (!g_ArrIterType) { + snprintf(error, maxlen, "Failed to create JSONArrIter handle type (err: %d)", err); + return false; + } + + g_ObjIterType = handlesys->CreateType("JSONObjIter", &g_ObjIterHandler, 0, nullptr, &haJSON, myself->GetIdentity(), &err); + if (!g_ObjIterType) { + snprintf(error, maxlen, "Failed to create JSONObjIter handle type (err: %d)", err); return false; } // Delete the existing instance if it exists - if (g_pYYJSONManager) { - delete g_pYYJSONManager; - g_pYYJSONManager = nullptr; + if (g_pJsonManager) { + delete g_pJsonManager; + g_pJsonManager = nullptr; } - g_pYYJSONManager = new YYJSONManager(); - if (!g_pYYJSONManager) { - snprintf(error, maxlen, "Failed to create YYJSONManager instance"); + g_pJsonManager = new JsonManager(); + if (!g_pJsonManager) { + snprintf(error, maxlen, "Failed to create JSON manager instance"); return false; } - return sharesys->AddInterface(myself, g_pYYJSONManager); + return sharesys->AddInterface(myself, g_pJsonManager); } void JsonExtension::SDK_OnUnload() { - handlesys->RemoveType(g_htJSON, myself->GetIdentity()); + handlesys->RemoveType(g_JsonType, myself->GetIdentity()); + handlesys->RemoveType(g_ArrIterType, myself->GetIdentity()); + handlesys->RemoveType(g_ObjIterType, myself->GetIdentity()); - if (g_pYYJSONManager) { - delete g_pYYJSONManager; - g_pYYJSONManager = nullptr; + if (g_pJsonManager) { + delete g_pJsonManager; + g_pJsonManager = nullptr; } } -void JSONHandler::OnHandleDestroy(HandleType_t type, void* object) +void JsonHandler::OnHandleDestroy(HandleType_t type, void* object) +{ + delete (JsonValue*)object; +} + +void ArrIterHandler::OnHandleDestroy(HandleType_t type, void* object) +{ + delete (JsonArrIter*)object; +} + +void ObjIterHandler::OnHandleDestroy(HandleType_t type, void* object) { - delete (YYJSONValue*)object; + delete (JsonObjIter*)object; } \ No newline at end of file diff --git a/src/extension.h b/src/extension.h index 0d1d503..3114905 100755 --- a/src/extension.h +++ b/src/extension.h @@ -2,9 +2,7 @@ #define _INCLUDE_SOURCEMOD_EXTENSION_PROPER_H_ #include "smsdk_ext.h" -#include -#include -#include "IYYJSONManager.h" +#include "IJsonManager.h" class JsonExtension : public SDKExtension { @@ -13,16 +11,32 @@ class JsonExtension : public SDKExtension virtual void SDK_OnUnload(); }; -class JSONHandler : public IHandleTypeDispatch +class JsonHandler : public IHandleTypeDispatch { public: void OnHandleDestroy(HandleType_t type, void *object); }; -extern JsonExtension g_JsonExtension; -extern HandleType_t g_htJSON; -extern JSONHandler g_JSONHandler; -extern const sp_nativeinfo_t json_natives[]; -extern IYYJSONManager* g_pYYJSONManager; +class ArrIterHandler : public IHandleTypeDispatch +{ +public: + void OnHandleDestroy(HandleType_t type, void *object); +}; + +class ObjIterHandler : public IHandleTypeDispatch +{ +public: + void OnHandleDestroy(HandleType_t type, void *object); +}; + +extern JsonExtension g_JsonExt; +extern HandleType_t g_JsonType; +extern HandleType_t g_ArrIterType; +extern HandleType_t g_ObjIterType; +extern JsonHandler g_JsonHandler; +extern ArrIterHandler g_ArrIterHandler; +extern ObjIterHandler g_ObjIterHandler; +extern const sp_nativeinfo_t g_JsonNatives[]; +extern IJsonManager* g_pJsonManager; #endif // _INCLUDE_SOURCEMOD_EXTENSION_PROPER_H_ \ No newline at end of file diff --git a/src/json_natives.cpp b/src/json_natives.cpp deleted file mode 100755 index b4774e6..0000000 --- a/src/json_natives.cpp +++ /dev/null @@ -1,2479 +0,0 @@ -#include "extension.h" -#include "YYJSONManager.h" - -class SourceModPackParamProvider : public IPackParamProvider -{ -private: - IPluginContext* m_pContext; - const cell_t* m_pParams; - unsigned int m_currentIndex; - -public: - SourceModPackParamProvider(IPluginContext* pContext, const cell_t* params, unsigned int startIndex) - : m_pContext(pContext), m_pParams(params), m_currentIndex(startIndex) {} - - bool GetNextString(const char** out_str) override { - char* str; - if (m_pContext->LocalToString(m_pParams[m_currentIndex++], &str) != SP_ERROR_NONE) { - return false; - } - *out_str = str; - return str != nullptr; - } - - bool GetNextInt(int* out_value) override { - cell_t* val; - if (m_pContext->LocalToPhysAddr(m_pParams[m_currentIndex++], &val) != SP_ERROR_NONE) { - return false; - } - *out_value = *val; - return true; - } - - bool GetNextFloat(float* out_value) override { - cell_t* val; - if (m_pContext->LocalToPhysAddr(m_pParams[m_currentIndex++], &val) != SP_ERROR_NONE) { - return false; - } - *out_value = sp_ctof(*val); - return true; - } - - bool GetNextBool(bool* out_value) override { - cell_t* val; - if (m_pContext->LocalToPhysAddr(m_pParams[m_currentIndex++], &val) != SP_ERROR_NONE) { - return false; - } - *out_value = (*val != 0); - return true; - } -}; - -/** - * Helper function: Create a SourceMod handle for YYJSONValue and return it directly - * Used by functions that return Handle_t - * - * @param pContext Plugin context - * @param pYYJSONValue JSON value to wrap (will be released on failure) - * @param error_context Descriptive context for error messages - * @return Handle on success, throws native error on failure - */ -static cell_t CreateAndReturnHandle(IPluginContext* pContext, YYJSONValue* pYYJSONValue, const char* error_context) -{ - if (!pYYJSONValue) { - return pContext->ThrowNativeError("Failed to create %s", error_context); - } - - HandleError err; - HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity()); - pYYJSONValue->m_handle = handlesys->CreateHandleEx(g_htJSON, pYYJSONValue, &sec, nullptr, &err); - - if (!pYYJSONValue->m_handle) { - g_pYYJSONManager->Release(pYYJSONValue); - return pContext->ThrowNativeError("Failed to create handle for %s (error code: %d)", error_context, err); - } - - return pYYJSONValue->m_handle; -} - -/** - * Helper function: Create a SourceMod handle for YYJSONValue and assign to output parameter - * Used by iterator functions (foreach) that assign handle via reference - * - * @param pContext Plugin context - * @param pYYJSONValue JSON value to wrap (will be released on failure) - * @param param_index Parameter index for output handle - * @param error_context Descriptive context for error messages - * @return true on success, false on failure (throws native error) - */ -static bool CreateAndAssignHandle(IPluginContext* pContext, YYJSONValue* pYYJSONValue, - cell_t param_index, const char* error_context) -{ - HandleError err; - HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity()); - pYYJSONValue->m_handle = handlesys->CreateHandleEx(g_htJSON, pYYJSONValue, &sec, nullptr, &err); - - if (!pYYJSONValue->m_handle) { - g_pYYJSONManager->Release(pYYJSONValue); - pContext->ThrowNativeError("Failed to create handle for %s (error code: %d)", error_context, err); - return false; - } - - cell_t* valHandle; - pContext->LocalToPhysAddr(param_index, &valHandle); - *valHandle = pYYJSONValue->m_handle; - return true; -} - -static cell_t json_val_pack(IPluginContext* pContext, const cell_t* params) { - char* fmt; - pContext->LocalToString(params[1], &fmt); - - SourceModPackParamProvider provider(pContext, params, 2); - - char error[YYJSON_PACK_ERROR_SIZE]; - YYJSONValue* pYYJSONValue = g_pYYJSONManager->Pack(fmt, &provider, error, sizeof(error)); - - if (!pYYJSONValue) { - return pContext->ThrowNativeError("Failed to pack JSON: %s", error); - } - - return CreateAndReturnHandle(pContext, pYYJSONValue, "packed JSON"); -} - -static cell_t json_doc_parse(IPluginContext* pContext, const cell_t* params) -{ - char* str; - pContext->LocalToString(params[1], &str); - - bool is_file = params[2]; - bool is_mutable_doc = params[3]; - yyjson_read_flag read_flg = static_cast(params[4]); - - char error[YYJSON_ERROR_BUFFER_SIZE]; - YYJSONValue* pYYJSONValue = g_pYYJSONManager->ParseJSON(str, is_file, is_mutable_doc, read_flg, error, sizeof(error)); - - if (!pYYJSONValue) { - return pContext->ThrowNativeError(error); - } - - return CreateAndReturnHandle(pContext, pYYJSONValue, "parsed JSON document"); -} - -static cell_t json_doc_equals(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle1 = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - YYJSONValue* handle2 = g_pYYJSONManager->GetFromHandle(pContext, params[2]); - - if (!handle1 || !handle2) return 0; - - return g_pYYJSONManager->Equals(handle1, handle2); -} - -static cell_t json_doc_copy_deep(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* targetDoc = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - YYJSONValue* sourceValue = g_pYYJSONManager->GetFromHandle(pContext, params[2]); - - if (!targetDoc || !sourceValue) return 0; - - YYJSONValue* pYYJSONValue = g_pYYJSONManager->DeepCopy(targetDoc, sourceValue); - - if (!pYYJSONValue) { - return pContext->ThrowNativeError("Failed to copy JSON value"); - } - - return CreateAndReturnHandle(pContext, pYYJSONValue, "copied JSON value"); -} - -static cell_t json_val_get_type_desc(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - const char* type_desc = g_pYYJSONManager->GetTypeDesc(handle); - pContext->StringToLocalUTF8(params[2], params[3], type_desc, nullptr); - - return 1; -} - -static cell_t json_obj_parse_str(IPluginContext* pContext, const cell_t* params) -{ - char* str; - pContext->LocalToString(params[1], &str); - yyjson_read_flag read_flg = static_cast(params[2]); - - char error[YYJSON_ERROR_BUFFER_SIZE]; - YYJSONValue* pYYJSONValue = g_pYYJSONManager->ObjectParseString(str, read_flg, error, sizeof(error)); - - if (!pYYJSONValue) { - return pContext->ThrowNativeError(error); - } - - return CreateAndReturnHandle(pContext, pYYJSONValue, "JSON object from string"); -} - -static cell_t json_obj_parse_file(IPluginContext* pContext, const cell_t* params) -{ - char* path; - pContext->LocalToString(params[1], &path); - yyjson_read_flag read_flg = static_cast(params[2]); - - char error[YYJSON_ERROR_BUFFER_SIZE]; - YYJSONValue* pYYJSONValue = g_pYYJSONManager->ObjectParseFile(path, read_flg, error, sizeof(error)); - - if (!pYYJSONValue) { - return pContext->ThrowNativeError(error); - } - - return CreateAndReturnHandle(pContext, pYYJSONValue, "JSON object from file"); -} - -static cell_t json_arr_parse_str(IPluginContext* pContext, const cell_t* params) -{ - char* str; - pContext->LocalToString(params[1], &str); - yyjson_read_flag read_flg = static_cast(params[2]); - - char error[YYJSON_ERROR_BUFFER_SIZE]; - YYJSONValue* pYYJSONValue = g_pYYJSONManager->ArrayParseString(str, read_flg, error, sizeof(error)); - - if (!pYYJSONValue) { - return pContext->ThrowNativeError(error); - } - - return CreateAndReturnHandle(pContext, pYYJSONValue, "JSON array from string"); -} - -static cell_t json_arr_parse_file(IPluginContext* pContext, const cell_t* params) -{ - char* path; - pContext->LocalToString(params[1], &path); - yyjson_read_flag read_flg = static_cast(params[2]); - - char error[YYJSON_ERROR_BUFFER_SIZE]; - YYJSONValue* pYYJSONValue = g_pYYJSONManager->ArrayParseFile(path, read_flg, error, sizeof(error)); - - if (!pYYJSONValue) { - return pContext->ThrowNativeError(error); - } - - return CreateAndReturnHandle(pContext, pYYJSONValue, "JSON array from file"); -} - -static cell_t json_arr_index_of_bool(IPluginContext *pContext, const cell_t *params) -{ - YYJSONValue *handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - bool searchValue = params[2]; - return g_pYYJSONManager->ArrayIndexOfBool(handle, searchValue); -} - -static cell_t json_arr_index_of_str(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - char* searchStr; - pContext->LocalToString(params[2], &searchStr); - - return g_pYYJSONManager->ArrayIndexOfString(handle, searchStr); -} - -static cell_t json_arr_index_of_int(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - int searchValue = params[2]; - return g_pYYJSONManager->ArrayIndexOfInt(handle, searchValue); -} - -static cell_t json_arr_index_of_integer64(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - char* searchStr; - pContext->LocalToString(params[2], &searchStr); - - char* endptr; - errno = 0; - long long searchValue = strtoll(searchStr, &endptr, 10); - - if (errno == ERANGE || *endptr != '\0') { - return pContext->ThrowNativeError("Invalid integer64 value: %s", searchStr); - } - - return g_pYYJSONManager->ArrayIndexOfInt64(handle, searchValue); -} - -static cell_t json_arr_index_of_float(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - double searchValue = static_cast(sp_ctof(params[2])); - return g_pYYJSONManager->ArrayIndexOfFloat(handle, searchValue); -} - -static cell_t json_val_get_type(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - return g_pYYJSONManager->GetType(handle); -} - -static cell_t json_val_get_subtype(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - return g_pYYJSONManager->GetSubtype(handle); -} - -static cell_t json_val_is_array(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - return g_pYYJSONManager->IsArray(handle); -} - -static cell_t json_val_is_object(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - return g_pYYJSONManager->IsObject(handle); -} - -static cell_t json_val_is_int(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - return g_pYYJSONManager->IsInt(handle); -} - -static cell_t json_val_is_uint(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - return g_pYYJSONManager->IsUint(handle); -} - -static cell_t json_val_is_sint(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - return g_pYYJSONManager->IsSint(handle); -} - -static cell_t json_val_is_num(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - return g_pYYJSONManager->IsNum(handle); -} - -static cell_t json_val_is_bool(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - return g_pYYJSONManager->IsBool(handle); -} - -static cell_t json_val_is_true(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - return g_pYYJSONManager->IsTrue(handle); -} - -static cell_t json_val_is_false(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - return g_pYYJSONManager->IsFalse(handle); -} - -static cell_t json_val_is_float(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - return g_pYYJSONManager->IsFloat(handle); -} - -static cell_t json_val_is_str(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - return g_pYYJSONManager->IsStr(handle); -} - -static cell_t json_val_is_null(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - return g_pYYJSONManager->IsNull(handle); -} - -static cell_t json_val_is_ctn(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - return g_pYYJSONManager->IsCtn(handle); -} - -static cell_t json_val_is_mutable(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - return g_pYYJSONManager->IsMutable(handle); -} - -static cell_t json_val_is_immutable(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - return g_pYYJSONManager->IsImmutable(handle); -} - -static cell_t json_obj_init(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* pYYJSONValue = g_pYYJSONManager->ObjectInit(); - return CreateAndReturnHandle(pContext, pYYJSONValue, "JSON object"); -} - -static cell_t json_obj_init_with_str(IPluginContext* pContext, const cell_t* params) -{ - cell_t* addr; - pContext->LocalToPhysAddr(params[1], &addr); - cell_t array_size = params[2]; - - if (array_size < 2) { - return pContext->ThrowNativeError("Array must contain at least one key-value pair"); - } - if (array_size % 2 != 0) { - return pContext->ThrowNativeError("Array must contain an even number of strings (got %d)", array_size); - } - - std::vector kv_pairs; - kv_pairs.reserve(array_size); - - for (cell_t i = 0; i < array_size; i += 2) { - char* key; - char* value; - - if (pContext->LocalToString(addr[i], &key) != SP_ERROR_NONE) { - return pContext->ThrowNativeError("Failed to read key at index %d", i); - } - if (!key || !key[0]) { - return pContext->ThrowNativeError("Empty key at index %d", i); - } - - if (pContext->LocalToString(addr[i + 1], &value) != SP_ERROR_NONE) { - return pContext->ThrowNativeError("Failed to read value at index %d", i + 1); - } - if (!value) { - return pContext->ThrowNativeError("Invalid value at index %d", i + 1); - } - - kv_pairs.push_back(key); - kv_pairs.push_back(value); - } - - YYJSONValue* pYYJSONValue = g_pYYJSONManager->ObjectInitWithStrings(kv_pairs.data(), array_size / 2); - - if (!pYYJSONValue) { - return pContext->ThrowNativeError("Failed to create JSON object from key-value pairs"); - } - - return CreateAndReturnHandle(pContext, pYYJSONValue, "JSON object from key-value pairs"); -} - -static cell_t json_val_create_bool(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* pYYJSONValue = g_pYYJSONManager->CreateBool(params[1]); - return CreateAndReturnHandle(pContext, pYYJSONValue, "JSON boolean value"); -} - -static cell_t json_val_create_float(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* pYYJSONValue = g_pYYJSONManager->CreateFloat(sp_ctof(params[1])); - return CreateAndReturnHandle(pContext, pYYJSONValue, "JSON float value"); -} - -static cell_t json_val_create_int(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* pYYJSONValue = g_pYYJSONManager->CreateInt(params[1]); - return CreateAndReturnHandle(pContext, pYYJSONValue, "JSON integer value"); -} - -static cell_t json_val_create_integer64(IPluginContext* pContext, const cell_t* params) -{ - char* value; - pContext->LocalToString(params[1], &value); - - char* endptr; - errno = 0; - long long num = strtoll(value, &endptr, 10); - - if (errno == ERANGE || *endptr != '\0') { - return pContext->ThrowNativeError("Invalid integer64 value: %s", value); - } - - YYJSONValue* pYYJSONValue = g_pYYJSONManager->CreateInt64(num); - - if (!pYYJSONValue) { - return pContext->ThrowNativeError("Failed to create JSON integer64 value"); - } - - return CreateAndReturnHandle(pContext, pYYJSONValue, "JSON integer64 value"); -} - -static cell_t json_val_create_str(IPluginContext* pContext, const cell_t* params) -{ - char* str; - pContext->LocalToString(params[1], &str); - - YYJSONValue* pYYJSONValue = g_pYYJSONManager->CreateString(str); - - if (!pYYJSONValue) { - return pContext->ThrowNativeError("Failed to create JSON string value"); - } - - return CreateAndReturnHandle(pContext, pYYJSONValue, "JSON string value"); -} - -static cell_t json_val_get_bool(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - bool value; - if (!g_pYYJSONManager->GetBool(handle, &value)) { - return pContext->ThrowNativeError("Type mismatch: expected boolean value"); - } - - return value; -} - -static cell_t json_val_get_float(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - double value; - if (!g_pYYJSONManager->GetFloat(handle, &value)) { - return pContext->ThrowNativeError("Type mismatch: expected float value"); - } - - return sp_ftoc(static_cast(value)); -} - -static cell_t json_val_get_int(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - int value; - if (!g_pYYJSONManager->GetInt(handle, &value)) { - return pContext->ThrowNativeError("Type mismatch: expected integer value"); - } - - return value; -} - -static cell_t json_val_get_integer64(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - int64_t value; - if (!g_pYYJSONManager->GetInt64(handle, &value)) { - return pContext->ThrowNativeError("Type mismatch: expected integer64 value"); - } - - char result[21]; - snprintf(result, sizeof(result), "%" PRId64, value); - pContext->StringToLocalUTF8(params[2], params[3], result, nullptr); - - return 1; -} - -static cell_t json_val_get_str(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - const char* str = nullptr; - size_t len = 0; - - if (!g_pYYJSONManager->GetString(handle, &str, &len)) { - return pContext->ThrowNativeError("Type mismatch: expected string value"); - } - - size_t maxlen = static_cast(params[3]); - - if (len + 1 > maxlen) { - return pContext->ThrowNativeError("Buffer is too small (need %d, have %d)", len + 1, maxlen); - } - - pContext->StringToLocalUTF8(params[2], maxlen, str, nullptr); - - return 1; -} - -static cell_t json_val_get_serialized_size(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - yyjson_write_flag write_flg = static_cast(params[2]); - size_t size = g_pYYJSONManager->GetSerializedSize(handle, write_flg); - - return static_cast(size); -} - -static cell_t json_val_get_read_size(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - size_t size = g_pYYJSONManager->GetReadSize(handle); - if (size == 0) return 0; - - return static_cast(size); -} - -static cell_t json_val_create_null(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* pYYJSONValue = g_pYYJSONManager->CreateNull(); - return CreateAndReturnHandle(pContext, pYYJSONValue, "JSON null value"); -} - -static cell_t json_arr_init(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* pYYJSONValue = g_pYYJSONManager->ArrayInit(); - return CreateAndReturnHandle(pContext, pYYJSONValue, "JSON array"); -} - -static cell_t json_arr_init_with_str(IPluginContext* pContext, const cell_t* params) -{ - cell_t* addr; - pContext->LocalToPhysAddr(params[1], &addr); - cell_t array_size = params[2]; - - std::vector strs; - strs.reserve(array_size); - - for (cell_t i = 0; i < array_size; i++) { - char* str; - pContext->LocalToString(addr[i], &str); - strs.push_back(str); - } - - YYJSONValue* pYYJSONValue = g_pYYJSONManager->ArrayInitWithStrings(strs.data(), strs.size()); - - if (!pYYJSONValue) { - return pContext->ThrowNativeError("Failed to create JSON array from strings"); - } - - return CreateAndReturnHandle(pContext, pYYJSONValue, "JSON array from strings"); -} - -static cell_t json_arr_get_size(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - size_t size = g_pYYJSONManager->ArrayGetSize(handle); - return static_cast(size); -} - -static cell_t json_arr_get_val(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - size_t index = static_cast(params[2]); - - YYJSONValue* pYYJSONValue = g_pYYJSONManager->ArrayGet(handle, index); - - if (!pYYJSONValue) { - return pContext->ThrowNativeError("Index %d is out of bounds", index); - } - - return CreateAndReturnHandle(pContext, pYYJSONValue, "JSON array value"); -} - -static cell_t json_arr_get_first(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - YYJSONValue* pYYJSONValue = g_pYYJSONManager->ArrayGetFirst(handle); - - if (!pYYJSONValue) { - return pContext->ThrowNativeError("Array is empty"); - } - - return CreateAndReturnHandle(pContext, pYYJSONValue, "first JSON array value"); -} - -static cell_t json_arr_get_last(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - YYJSONValue* pYYJSONValue = g_pYYJSONManager->ArrayGetLast(handle); - - if (!pYYJSONValue) { - return pContext->ThrowNativeError("Array is empty"); - } - - return CreateAndReturnHandle(pContext, pYYJSONValue, "last JSON array value"); -} - -static cell_t json_arr_get_bool(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - size_t index = static_cast(params[2]); - - bool value; - if (!g_pYYJSONManager->ArrayGetBool(handle, index, &value)) { - return pContext->ThrowNativeError("Failed to get boolean at index %d", index); - } - - return value; -} - -static cell_t json_arr_get_float(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - size_t index = static_cast(params[2]); - - double value; - if (!g_pYYJSONManager->ArrayGetFloat(handle, index, &value)) { - return pContext->ThrowNativeError("Failed to get float at index %d", index); - } - - return sp_ftoc(static_cast(value)); -} - -static cell_t json_arr_get_integer(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - size_t index = static_cast(params[2]); - - int value; - if (!g_pYYJSONManager->ArrayGetInt(handle, index, &value)) { - return pContext->ThrowNativeError("Failed to get integer at index %d", index); - } - - return value; -} - -static cell_t json_arr_get_integer64(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - size_t index = static_cast(params[2]); - int64_t value; - - if (!g_pYYJSONManager->ArrayGetInt64(handle, index, &value)) { - return pContext->ThrowNativeError("Failed to get integer64 at index %d", index); - } - - char result[21]; - snprintf(result, sizeof(result), "%" PRId64, value); - pContext->StringToLocalUTF8(params[3], params[4], result, nullptr); - - return 1; -} - -static cell_t json_arr_get_str(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - size_t index = static_cast(params[2]); - const char* str = nullptr; - size_t len = 0; - - if (!g_pYYJSONManager->ArrayGetString(handle, index, &str, &len)) { - return pContext->ThrowNativeError("Failed to get string at index %d", index); - } - - size_t maxlen = static_cast(params[4]); - if (len + 1 > maxlen) { - return pContext->ThrowNativeError("Buffer is too small (need %d, have %d)", len + 1, maxlen); - } - - pContext->StringToLocalUTF8(params[3], maxlen, str, nullptr); - - return 1; -} - -static cell_t json_arr_is_null(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - size_t index = static_cast(params[2]); - - return g_pYYJSONManager->ArrayIsNull(handle, index); -} - -static cell_t json_arr_replace_val(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle1 = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - YYJSONValue* handle2 = g_pYYJSONManager->GetFromHandle(pContext, params[3]); - - if (!handle1 || !handle2) return 0; - - if (!handle1->IsMutable()) { - return pContext->ThrowNativeError("Cannot replace value in an immutable JSON array"); - } - - size_t index = static_cast(params[2]); - return g_pYYJSONManager->ArrayReplace(handle1, index, handle2); -} - -static cell_t json_arr_replace_bool(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot replace value in an immutable JSON array"); - } - - size_t index = static_cast(params[2]); - return g_pYYJSONManager->ArrayReplaceBool(handle, index, params[3]); -} - -static cell_t json_arr_replace_float(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot replace value in an immutable JSON array"); - } - - size_t index = static_cast(params[2]); - return g_pYYJSONManager->ArrayReplaceFloat(handle, index, sp_ctof(params[3])); -} - -static cell_t json_arr_replace_integer(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot replace value in an immutable JSON array"); - } - - size_t index = static_cast(params[2]); - return g_pYYJSONManager->ArrayReplaceInt(handle, index, params[3]); -} - -static cell_t json_arr_replace_integer64(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot replace value in an immutable JSON array"); - } - - char* value; - pContext->LocalToString(params[3], &value); - - char* endptr; - errno = 0; - long long num = strtoll(value, &endptr, 10); - - if (errno == ERANGE || *endptr != '\0') { - return pContext->ThrowNativeError("Invalid integer64 value: %s", value); - } - - size_t index = static_cast(params[2]); - return g_pYYJSONManager->ArrayReplaceInt64(handle, index, num); -} - -static cell_t json_arr_replace_null(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot replace value in an immutable JSON array"); - } - - size_t index = static_cast(params[2]); - return g_pYYJSONManager->ArrayReplaceNull(handle, index); -} - -static cell_t json_arr_replace_str(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot replace value in an immutable JSON array"); - } - - char* val; - pContext->LocalToString(params[3], &val); - - size_t index = static_cast(params[2]); - return g_pYYJSONManager->ArrayReplaceString(handle, index, val); -} - -static cell_t json_arr_append_val(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle1 = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - YYJSONValue* handle2 = g_pYYJSONManager->GetFromHandle(pContext, params[2]); - - if (!handle1 || !handle2) return 0; - - if (!handle1->IsMutable()) { - return pContext->ThrowNativeError("Cannot append value to an immutable JSON array"); - } - - return g_pYYJSONManager->ArrayAppend(handle1, handle2); -} - -static cell_t json_arr_append_bool(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot append value to an immutable JSON array"); - } - - return g_pYYJSONManager->ArrayAppendBool(handle, params[2]); -} - -static cell_t json_arr_append_float(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot append value to an immutable JSON array"); - } - - return g_pYYJSONManager->ArrayAppendFloat(handle, sp_ctof(params[2])); -} - -static cell_t json_arr_append_int(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot append value to an immutable JSON array"); - } - - return g_pYYJSONManager->ArrayAppendInt(handle, params[2]); -} - -static cell_t json_arr_append_integer64(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot append value to an immutable JSON array"); - } - - char* value; - pContext->LocalToString(params[2], &value); - - char* endptr; - errno = 0; - long long num = strtoll(value, &endptr, 10); - - if (errno == ERANGE || *endptr != '\0') { - return pContext->ThrowNativeError("Invalid integer64 value: %s", value); - } - - return g_pYYJSONManager->ArrayAppendInt64(handle, num); -} - -static cell_t json_arr_append_null(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot append value to an immutable JSON array"); - } - - return g_pYYJSONManager->ArrayAppendNull(handle); -} - -static cell_t json_arr_append_str(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot append value to an immutable JSON array"); - } - - char* str; - pContext->LocalToString(params[2], &str); - - return g_pYYJSONManager->ArrayAppendString(handle, str); -} - -static cell_t json_arr_remove(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot remove value from an immutable JSON array"); - } - - size_t index = static_cast(params[2]); - return g_pYYJSONManager->ArrayRemove(handle, index); -} - -static cell_t json_arr_remove_first(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot remove value from an immutable JSON array"); - } - - return g_pYYJSONManager->ArrayRemoveFirst(handle); -} - -static cell_t json_arr_remove_last(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot remove value from an immutable JSON array"); - } - - return g_pYYJSONManager->ArrayRemoveLast(handle); -} - -static cell_t json_arr_remove_range(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot remove value from an immutable JSON array"); - } - - size_t start_index = static_cast(params[2]); - size_t end_index = static_cast(params[3]); - - return g_pYYJSONManager->ArrayRemoveRange(handle, start_index, end_index); -} - -static cell_t json_arr_clear(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot clear an immutable JSON array"); - } - - return g_pYYJSONManager->ArrayClear(handle); -} - -static cell_t json_doc_write_to_str(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - size_t buffer_size = static_cast(params[3]); - yyjson_write_flag write_flg = static_cast(params[4]); - - char* temp_buffer = (char*)malloc(buffer_size); - if (!temp_buffer) { - return pContext->ThrowNativeError("Failed to allocate buffer"); - } - - size_t output_len = 0; - if (!g_pYYJSONManager->WriteToString(handle, temp_buffer, buffer_size, write_flg, &output_len)) { - free(temp_buffer); - return pContext->ThrowNativeError("Buffer too small or write failed"); - } - - pContext->StringToLocalUTF8(params[2], buffer_size, temp_buffer, nullptr); - free(temp_buffer); - return static_cast(output_len); -} - -static cell_t json_doc_write_to_file(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - char* path; - pContext->LocalToString(params[2], &path); - yyjson_write_flag write_flg = static_cast(params[3]); - - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->WriteToFile(handle, path, write_flg, error, sizeof(error))) { - return pContext->ThrowNativeError(error); - } - - return true; -} - -static cell_t json_obj_get_size(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - size_t size = g_pYYJSONManager->ObjectGetSize(handle); - return static_cast(size); -} - -static cell_t json_obj_get_key(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - size_t index = static_cast(params[2]); - const char* key = nullptr; - - if (!g_pYYJSONManager->ObjectGetKey(handle, index, &key)) { - return pContext->ThrowNativeError("Index %d is out of bounds", index); - } - - pContext->StringToLocalUTF8(params[3], params[4], key, nullptr); - return 1; -} - -static cell_t json_obj_get_val_at(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - size_t index = static_cast(params[2]); - - YYJSONValue* pYYJSONValue = g_pYYJSONManager->ObjectGetValueAt(handle, index); - - if (!pYYJSONValue) { - return pContext->ThrowNativeError("Index %d is out of bounds", index); - } - - return CreateAndReturnHandle(pContext, pYYJSONValue, "JSON object value"); -} - -static cell_t json_obj_get_val(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - char* key; - pContext->LocalToString(params[2], &key); - - YYJSONValue* pYYJSONValue = g_pYYJSONManager->ObjectGet(handle, key); - - if (!pYYJSONValue) { - return pContext->ThrowNativeError("Key not found: %s", key); - } - - return CreateAndReturnHandle(pContext, pYYJSONValue, "JSON object value"); -} - -static cell_t json_obj_get_bool(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - char* key; - pContext->LocalToString(params[2], &key); - - bool value; - if (!g_pYYJSONManager->ObjectGetBool(handle, key, &value)) { - return pContext->ThrowNativeError("Failed to get boolean for key '%s'", key); - } - - return value; -} - -static cell_t json_obj_get_float(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - char* key; - pContext->LocalToString(params[2], &key); - - double value; - if (!g_pYYJSONManager->ObjectGetFloat(handle, key, &value)) { - return pContext->ThrowNativeError("Failed to get float for key '%s'", key); - } - - return sp_ftoc(static_cast(value)); -} - -static cell_t json_obj_get_int(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - char* key; - pContext->LocalToString(params[2], &key); - - int value; - if (!g_pYYJSONManager->ObjectGetInt(handle, key, &value)) { - return pContext->ThrowNativeError("Failed to get integer for key '%s'", key); - } - - return value; -} - -static cell_t json_obj_get_integer64(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - char* key; - pContext->LocalToString(params[2], &key); - - int64_t value; - if (!g_pYYJSONManager->ObjectGetInt64(handle, key, &value)) { - return pContext->ThrowNativeError("Failed to get integer64 for key '%s'", key); - } - - char result[21]; - snprintf(result, sizeof(result), "%" PRId64, value); - pContext->StringToLocalUTF8(params[3], params[4], result, nullptr); - - return 1; -} - -static cell_t json_obj_get_str(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - char* key; - pContext->LocalToString(params[2], &key); - - const char* str = nullptr; - size_t len = 0; - if (!g_pYYJSONManager->ObjectGetString(handle, key, &str, &len)) { - return pContext->ThrowNativeError("Failed to get string for key '%s'", key); - } - - size_t maxlen = static_cast(params[4]); - if (len + 1 > maxlen) { - return pContext->ThrowNativeError("Buffer is too small (need %d, have %d)", len + 1, maxlen); - } - - pContext->StringToLocalUTF8(params[3], maxlen, str, nullptr); - - return 1; -} - -static cell_t json_obj_clear(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot clear an immutable JSON object"); - } - - return g_pYYJSONManager->ObjectClear(handle); -} - -static cell_t json_obj_is_null(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - char* key; - pContext->LocalToString(params[2], &key); - - bool is_null = false; - if (!g_pYYJSONManager->ObjectIsNull(handle, key, &is_null)) { - return pContext->ThrowNativeError("Key not found: %s", key); - } - - return is_null; -} - -static cell_t json_obj_has_key(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - char* key; - pContext->LocalToString(params[2], &key); - - bool ptr_use = params[3]; - - return g_pYYJSONManager->ObjectHasKey(handle, key, ptr_use); -} - -static cell_t json_obj_rename_key(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot rename key in an immutable JSON object"); - } - - char* old_key; - pContext->LocalToString(params[2], &old_key); - - char* new_key; - pContext->LocalToString(params[3], &new_key); - - bool allow_duplicate = params[4]; - - if (!g_pYYJSONManager->ObjectRenameKey(handle, old_key, new_key, allow_duplicate)) { - return pContext->ThrowNativeError("Failed to rename key from '%s' to '%s'", old_key, new_key); - } - - return true; -} - -static cell_t json_obj_set_val(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle1 = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - YYJSONValue* handle2 = g_pYYJSONManager->GetFromHandle(pContext, params[3]); - - if (!handle1 || !handle2) return 0; - - if (!handle1->IsMutable()) { - return pContext->ThrowNativeError("Cannot set value in an immutable JSON object"); - } - - char* key; - pContext->LocalToString(params[2], &key); - - return g_pYYJSONManager->ObjectSet(handle1, key, handle2); -} - -static cell_t json_obj_set_bool(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot set value in an immutable JSON object"); - } - - char* key; - pContext->LocalToString(params[2], &key); - - return g_pYYJSONManager->ObjectSetBool(handle, key, params[3]); -} - -static cell_t json_obj_set_float(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot set value in an immutable JSON object"); - } - - char* key; - pContext->LocalToString(params[2], &key); - - return g_pYYJSONManager->ObjectSetFloat(handle, key, sp_ctof(params[3])); -} - -static cell_t json_obj_set_int(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot set value in an immutable JSON object"); - } - - char* key; - pContext->LocalToString(params[2], &key); - - return g_pYYJSONManager->ObjectSetInt(handle, key, params[3]); -} - -static cell_t json_obj_set_integer64(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot set value in an immutable JSON object"); - } - - char* key, * value; - pContext->LocalToString(params[2], &key); - pContext->LocalToString(params[3], &value); - - char* endptr; - errno = 0; - long long num = strtoll(value, &endptr, 10); - - if (errno == ERANGE || *endptr != '\0') { - return pContext->ThrowNativeError("Invalid integer64 value: %s", value); - } - - return g_pYYJSONManager->ObjectSetInt64(handle, key, num); -} - -static cell_t json_obj_set_null(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot set value in an immutable JSON object"); - } - - char* key; - pContext->LocalToString(params[2], &key); - - return g_pYYJSONManager->ObjectSetNull(handle, key); -} - -static cell_t json_obj_set_str(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot set value in an immutable JSON object"); - } - - char* key, * value; - pContext->LocalToString(params[2], &key); - pContext->LocalToString(params[3], &value); - - return g_pYYJSONManager->ObjectSetString(handle, key, value); -} - -static cell_t json_obj_remove(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot remove value from an immutable JSON object"); - } - - char* key; - pContext->LocalToString(params[2], &key); - - return g_pYYJSONManager->ObjectRemove(handle, key); -} - -static cell_t json_ptr_get_val(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - char* path; - pContext->LocalToString(params[2], &path); - - char error[YYJSON_ERROR_BUFFER_SIZE]; - YYJSONValue* pYYJSONValue = g_pYYJSONManager->PtrGet(handle, path, error, sizeof(error)); - - if (!pYYJSONValue) { - return pContext->ThrowNativeError("%s", error); - } - - return CreateAndReturnHandle(pContext, pYYJSONValue, "JSON pointer value"); -} - -static cell_t json_ptr_get_bool(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - char* path; - pContext->LocalToString(params[2], &path); - - bool value; - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->PtrGetBool(handle, path, &value, error, sizeof(error))) { - return pContext->ThrowNativeError("%s", error); - } - - return value; -} - -static cell_t json_ptr_get_float(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - char* path; - pContext->LocalToString(params[2], &path); - - double value; - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->PtrGetFloat(handle, path, &value, error, sizeof(error))) { - return pContext->ThrowNativeError("%s", error); - } - - return sp_ftoc(static_cast(value)); -} - -static cell_t json_ptr_get_int(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - char* path; - pContext->LocalToString(params[2], &path); - - int value; - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->PtrGetInt(handle, path, &value, error, sizeof(error))) { - return pContext->ThrowNativeError("%s", error); - } - - return value; -} - -static cell_t json_ptr_get_integer64(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - char* path; - pContext->LocalToString(params[2], &path); - - int64_t value; - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->PtrGetInt64(handle, path, &value, error, sizeof(error))) { - return pContext->ThrowNativeError("%s", error); - } - - char result[21]; - snprintf(result, sizeof(result), "%" PRId64, value); - pContext->StringToLocalUTF8(params[3], params[4], result, nullptr); - - return 1; -} - -static cell_t json_ptr_get_str(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - char* path; - pContext->LocalToString(params[2], &path); - - const char* str = nullptr; - size_t len = 0; - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->PtrGetString(handle, path, &str, &len, error, sizeof(error))) { - return pContext->ThrowNativeError("%s", error); - } - - size_t maxlen = static_cast(params[4]); - if (len + 1 > maxlen) { - return pContext->ThrowNativeError("Buffer is too small (need %d, have %d)", len + 1, maxlen); - } - - pContext->StringToLocalUTF8(params[3], maxlen, str, nullptr); - - return 1; -} - -static cell_t json_ptr_get_is_null(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - char* path; - pContext->LocalToString(params[2], &path); - - bool is_null; - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->PtrGetIsNull(handle, path, &is_null, error, sizeof(error))) { - return pContext->ThrowNativeError("%s", error); - } - - return is_null; -} - -static cell_t json_ptr_get_length(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - char* path; - pContext->LocalToString(params[2], &path); - - size_t len; - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->PtrGetLength(handle, path, &len, error, sizeof(error))) { - return pContext->ThrowNativeError("%s", error); - } - - return static_cast(len); -} - -static cell_t json_ptr_set_val(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle1 = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - YYJSONValue* handle2 = g_pYYJSONManager->GetFromHandle(pContext, params[3]); - - if (!handle1 || !handle2) return 0; - - if (!handle1->IsMutable()) { - return pContext->ThrowNativeError("Cannot set value in an immutable JSON document using pointer"); - } - - char* path; - pContext->LocalToString(params[2], &path); - - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->PtrSet(handle1, path, handle2, error, sizeof(error))) { - return pContext->ThrowNativeError("%s", error); - } - - return true; -} - -static cell_t json_ptr_set_bool(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot set value in an immutable JSON document using pointer"); - } - - char* path; - pContext->LocalToString(params[2], &path); - - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->PtrSetBool(handle, path, params[3], error, sizeof(error))) { - return pContext->ThrowNativeError("%s", error); - } - - return true; -} - -static cell_t json_ptr_set_float(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot set value in an immutable JSON document using pointer"); - } - - char* path; - pContext->LocalToString(params[2], &path); - - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->PtrSetFloat(handle, path, sp_ctof(params[3]), error, sizeof(error))) { - return pContext->ThrowNativeError("%s", error); - } - - return true; -} - -static cell_t json_ptr_set_int(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot set value in an immutable JSON document using pointer"); - } - - char* path; - pContext->LocalToString(params[2], &path); - - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->PtrSetInt(handle, path, params[3], error, sizeof(error))) { - return pContext->ThrowNativeError("%s", error); - } - - return true; -} - -static cell_t json_ptr_set_integer64(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot set value in an immutable JSON document using pointer"); - } - - char* path, * value; - pContext->LocalToString(params[2], &path); - pContext->LocalToString(params[3], &value); - - char* endptr; - errno = 0; - long long num = strtoll(value, &endptr, 10); - - if (errno == ERANGE || *endptr != '\0') { - return pContext->ThrowNativeError("Invalid integer64 value: %s", value); - } - - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->PtrSetInt64(handle, path, num, error, sizeof(error))) { - return pContext->ThrowNativeError("%s", error); - } - - return true; -} - -static cell_t json_ptr_set_str(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot set value in an immutable JSON document using pointer"); - } - - char* path, * str; - pContext->LocalToString(params[2], &path); - pContext->LocalToString(params[3], &str); - - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->PtrSetString(handle, path, str, error, sizeof(error))) { - return pContext->ThrowNativeError("%s", error); - } - - return true; -} - -static cell_t json_ptr_set_null(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot set value in an immutable JSON document using pointer"); - } - - char* path; - pContext->LocalToString(params[2], &path); - - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->PtrSetNull(handle, path, error, sizeof(error))) { - return pContext->ThrowNativeError("%s", error); - } - - return true; -} - -static cell_t json_ptr_add_val(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle1 = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - YYJSONValue* handle2 = g_pYYJSONManager->GetFromHandle(pContext, params[3]); - - if (!handle1 || !handle2) return 0; - - if (!handle1->IsMutable()) { - return pContext->ThrowNativeError("Cannot add value to an immutable JSON document using pointer"); - } - - char* path; - pContext->LocalToString(params[2], &path); - - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->PtrAdd(handle1, path, handle2, error, sizeof(error))) { - return pContext->ThrowNativeError("%s", error); - } - - return true; -} - -static cell_t json_ptr_add_bool(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot add value to an immutable JSON document using pointer"); - } - - char* path; - pContext->LocalToString(params[2], &path); - - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->PtrAddBool(handle, path, params[3], error, sizeof(error))) { - return pContext->ThrowNativeError("%s", error); - } - - return true; -} - -static cell_t json_ptr_add_float(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot add value to an immutable JSON document using pointer"); - } - - char* path; - pContext->LocalToString(params[2], &path); - - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->PtrAddFloat(handle, path, sp_ctof(params[3]), error, sizeof(error))) { - return pContext->ThrowNativeError("%s", error); - } - - return true; -} - -static cell_t json_ptr_add_int(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot add value to an immutable JSON document using pointer"); - } - - char* path; - pContext->LocalToString(params[2], &path); - - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->PtrAddInt(handle, path, params[3], error, sizeof(error))) { - return pContext->ThrowNativeError("%s", error); - } - - return true; -} - -static cell_t json_ptr_add_integer64(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot add value to an immutable JSON document using pointer"); - } - - char* path, * value; - pContext->LocalToString(params[2], &path); - pContext->LocalToString(params[3], &value); - - char* endptr; - errno = 0; - long long num = strtoll(value, &endptr, 10); - - if (errno == ERANGE || *endptr != '\0') { - return pContext->ThrowNativeError("Invalid integer64 value: %s", value); - } - - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->PtrAddInt64(handle, path, num, error, sizeof(error))) { - return pContext->ThrowNativeError("%s", error); - } - - return true; -} - -static cell_t json_ptr_add_str(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot add value to an immutable JSON document using pointer"); - } - - char* path, * str; - pContext->LocalToString(params[2], &path); - pContext->LocalToString(params[3], &str); - - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->PtrAddString(handle, path, str, error, sizeof(error))) { - return pContext->ThrowNativeError("%s", error); - } - - return true; -} - -static cell_t json_ptr_add_null(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot add value to an immutable JSON document using pointer"); - } - - char* path; - pContext->LocalToString(params[2], &path); - - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->PtrAddNull(handle, path, error, sizeof(error))) { - return pContext->ThrowNativeError("%s", error); - } - - return true; -} - -static cell_t json_ptr_remove_val(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot remove value from an immutable JSON document using pointer"); - } - - char* path; - pContext->LocalToString(params[2], &path); - - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->PtrRemove(handle, path, error, sizeof(error))) { - return pContext->ThrowNativeError("%s", error); - } - - return true; -} - -static cell_t json_ptr_try_get_val(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - char* path; - pContext->LocalToString(params[2], &path); - - YYJSONValue* pYYJSONValue = g_pYYJSONManager->PtrTryGet(handle, path); - - if (!pYYJSONValue) { - return 0; - } - - return CreateAndAssignHandle(pContext, pYYJSONValue, params[3], "JSON pointer value"); -} - -static cell_t json_ptr_try_get_bool(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - if (!handle) return 0; - - char* path; - pContext->LocalToString(params[2], &path); - - bool value; - if (!g_pYYJSONManager->PtrTryGetBool(handle, path, &value)) { - return 0; - } - - cell_t* addr; - pContext->LocalToPhysAddr(params[3], &addr); - *addr = value; - - return 1; -} - -static cell_t json_ptr_try_get_float(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - if (!handle) return 0; - - char* path; - pContext->LocalToString(params[2], &path); - - double value; - if (!g_pYYJSONManager->PtrTryGetFloat(handle, path, &value)) { - return 0; - } - - cell_t* addr; - pContext->LocalToPhysAddr(params[3], &addr); - *addr = sp_ftoc(static_cast(value)); - - return 1; -} - -static cell_t json_ptr_try_get_int(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - if (!handle) return 0; - - char* path; - pContext->LocalToString(params[2], &path); - - int value; - if (!g_pYYJSONManager->PtrTryGetInt(handle, path, &value)) { - return 0; - } - - cell_t* addr; - pContext->LocalToPhysAddr(params[3], &addr); - *addr = value; - - return 1; -} - -static cell_t json_ptr_try_get_integer64(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - if (!handle) return 0; - - char* path; - pContext->LocalToString(params[2], &path); - - int64_t value; - if (!g_pYYJSONManager->PtrTryGetInt64(handle, path, &value)) { - return 0; - } - - size_t maxlen = static_cast(params[4]); - char result[21]; - snprintf(result, sizeof(result), "%" PRId64, value); - pContext->StringToLocalUTF8(params[3], maxlen, result, nullptr); - return 1; -} - -static cell_t json_ptr_try_get_str(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - if (!handle) return 0; - - char* path; - pContext->LocalToString(params[2], &path); - - const char* str = nullptr; - size_t len = 0; - - if (!g_pYYJSONManager->PtrTryGetString(handle, path, &str, &len)) { - return 0; - } - - size_t maxlen = static_cast(params[4]); - if (len + 1 > maxlen) { - return 0; - } - - pContext->StringToLocalUTF8(params[3], maxlen, str, nullptr); - - return 1; -} - -static cell_t json_obj_foreach(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - if (!handle) return 0; - - const char* key = nullptr; - YYJSONValue* pYYJSONValue = nullptr; - - if (!g_pYYJSONManager->ObjectForeachNext(handle, &key, nullptr, &pYYJSONValue)) { - return false; - } - - pContext->StringToLocalUTF8(params[2], params[3], key, nullptr); - - return CreateAndAssignHandle(pContext, pYYJSONValue, params[4], "JSON object value"); -} - -static cell_t json_arr_foreach(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - if (!handle) return 0; - - size_t index = 0; - YYJSONValue* pYYJSONValue = nullptr; - - if (!g_pYYJSONManager->ArrayForeachNext(handle, &index, &pYYJSONValue)) { - return false; - } - - cell_t* indexPtr; - pContext->LocalToPhysAddr(params[2], &indexPtr); - *indexPtr = static_cast(index); - - return CreateAndAssignHandle(pContext, pYYJSONValue, params[3], "JSON array value"); -} - -static cell_t json_obj_foreach_key(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - if (!handle) return 0; - - const char* key = nullptr; - - if (!g_pYYJSONManager->ObjectForeachKeyNext(handle, &key, nullptr)) { - return false; - } - - pContext->StringToLocalUTF8(params[2], params[3], key, nullptr); - - return true; -} - -static cell_t json_arr_foreach_index(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - if (!handle) return 0; - - size_t index = 0; - - if (!g_pYYJSONManager->ArrayForeachIndexNext(handle, &index)) { - return false; - } - - cell_t* indexPtr; - pContext->LocalToPhysAddr(params[2], &indexPtr); - *indexPtr = static_cast(index); - - return true; -} - -static cell_t json_arr_sort(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot sort an immutable JSON array"); - } - - YYJSON_SORT_ORDER sort_mode = static_cast(params[2]); - if (sort_mode < YYJSON_SORT_ASC || sort_mode > YYJSON_SORT_RANDOM) { - return pContext->ThrowNativeError("Invalid sort mode: %d (expected 0=ascending, 1=descending, 2=random)", sort_mode); - } - - return g_pYYJSONManager->ArraySort(handle, sort_mode); -} - -static cell_t json_obj_sort(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot sort an immutable JSON object"); - } - - YYJSON_SORT_ORDER sort_mode = static_cast(params[2]); - if (sort_mode < YYJSON_SORT_ASC || sort_mode > YYJSON_SORT_RANDOM) { - return pContext->ThrowNativeError("Invalid sort mode: %d (expected 0=ascending, 1=descending, 2=random)", sort_mode); - } - - return g_pYYJSONManager->ObjectSort(handle, sort_mode); -} - -static cell_t json_doc_to_mutable(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (handle->IsMutable()) { - return pContext->ThrowNativeError("Document is already mutable"); - } - - YYJSONValue* pYYJSONValue = g_pYYJSONManager->ToMutable(handle); - - if (!pYYJSONValue) { - return pContext->ThrowNativeError("Failed to convert to mutable document"); - } - - return CreateAndReturnHandle(pContext, pYYJSONValue, "mutable JSON document"); -} - -static cell_t json_doc_to_immutable(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Document is already immutable"); - } - - YYJSONValue* pYYJSONValue = g_pYYJSONManager->ToImmutable(handle); - - if (!pYYJSONValue) { - return pContext->ThrowNativeError("Failed to convert to immutable document"); - } - - return CreateAndReturnHandle(pContext, pYYJSONValue, "immutable JSON document"); -} - -const sp_nativeinfo_t json_natives[] = -{ - // JSONObject - {"YYJSONObject.YYJSONObject", json_obj_init}, - {"YYJSONObject.FromStrings", json_obj_init_with_str}, - {"YYJSONObject.Size.get", json_obj_get_size}, - {"YYJSONObject.Get", json_obj_get_val}, - {"YYJSONObject.GetBool", json_obj_get_bool}, - {"YYJSONObject.GetFloat", json_obj_get_float}, - {"YYJSONObject.GetInt", json_obj_get_int}, - {"YYJSONObject.GetInt64", json_obj_get_integer64}, - {"YYJSONObject.GetString", json_obj_get_str}, - {"YYJSONObject.IsNull", json_obj_is_null}, - {"YYJSONObject.GetKey", json_obj_get_key}, - {"YYJSONObject.GetValueAt", json_obj_get_val_at}, - {"YYJSONObject.HasKey", json_obj_has_key}, - {"YYJSONObject.RenameKey", json_obj_rename_key}, - {"YYJSONObject.Set", json_obj_set_val}, - {"YYJSONObject.SetBool", json_obj_set_bool}, - {"YYJSONObject.SetFloat", json_obj_set_float}, - {"YYJSONObject.SetInt", json_obj_set_int}, - {"YYJSONObject.SetInt64", json_obj_set_integer64}, - {"YYJSONObject.SetNull", json_obj_set_null}, - {"YYJSONObject.SetString", json_obj_set_str}, - {"YYJSONObject.Remove", json_obj_remove}, - {"YYJSONObject.Clear", json_obj_clear}, - {"YYJSONObject.FromString", json_obj_parse_str}, - {"YYJSONObject.FromFile", json_obj_parse_file}, - {"YYJSONObject.Sort", json_obj_sort}, - - // JSONArray - {"YYJSONArray.YYJSONArray", json_arr_init}, - {"YYJSONArray.FromStrings", json_arr_init_with_str}, - {"YYJSONArray.Length.get", json_arr_get_size}, - {"YYJSONArray.Get", json_arr_get_val}, - {"YYJSONArray.First.get", json_arr_get_first}, - {"YYJSONArray.Last.get", json_arr_get_last}, - {"YYJSONArray.GetBool", json_arr_get_bool}, - {"YYJSONArray.GetFloat", json_arr_get_float}, - {"YYJSONArray.GetInt", json_arr_get_integer}, - {"YYJSONArray.GetInt64", json_arr_get_integer64}, - {"YYJSONArray.GetString", json_arr_get_str}, - {"YYJSONArray.IsNull", json_arr_is_null}, - {"YYJSONArray.Set", json_arr_replace_val}, - {"YYJSONArray.SetBool", json_arr_replace_bool}, - {"YYJSONArray.SetFloat", json_arr_replace_float}, - {"YYJSONArray.SetInt", json_arr_replace_integer}, - {"YYJSONArray.SetInt64", json_arr_replace_integer64}, - {"YYJSONArray.SetNull", json_arr_replace_null}, - {"YYJSONArray.SetString", json_arr_replace_str}, - {"YYJSONArray.Push", json_arr_append_val}, - {"YYJSONArray.PushBool", json_arr_append_bool}, - {"YYJSONArray.PushFloat", json_arr_append_float}, - {"YYJSONArray.PushInt", json_arr_append_int}, - {"YYJSONArray.PushInt64", json_arr_append_integer64}, - {"YYJSONArray.PushNull", json_arr_append_null}, - {"YYJSONArray.PushString", json_arr_append_str}, - {"YYJSONArray.Remove", json_arr_remove}, - {"YYJSONArray.RemoveFirst", json_arr_remove_first}, - {"YYJSONArray.RemoveLast", json_arr_remove_last}, - {"YYJSONArray.RemoveRange", json_arr_remove_range}, - {"YYJSONArray.Clear", json_arr_clear}, - {"YYJSONArray.FromString", json_arr_parse_str}, - {"YYJSONArray.FromFile", json_arr_parse_file}, - {"YYJSONArray.IndexOfBool", json_arr_index_of_bool}, - {"YYJSONArray.IndexOfString", json_arr_index_of_str}, - {"YYJSONArray.IndexOfInt", json_arr_index_of_int}, - {"YYJSONArray.IndexOfInt64", json_arr_index_of_integer64}, - {"YYJSONArray.IndexOfFloat", json_arr_index_of_float}, - {"YYJSONArray.Sort", json_arr_sort}, - - // JSON - {"YYJSON.ToString", json_doc_write_to_str}, - {"YYJSON.ToFile", json_doc_write_to_file}, - {"YYJSON.Parse", json_doc_parse}, - {"YYJSON.Equals", json_doc_equals}, - {"YYJSON.DeepCopy", json_doc_copy_deep}, - {"YYJSON.GetTypeDesc", json_val_get_type_desc}, - {"YYJSON.GetSerializedSize", json_val_get_serialized_size}, - {"YYJSON.ReadSize.get", json_val_get_read_size}, - {"YYJSON.Type.get", json_val_get_type}, - {"YYJSON.SubType.get", json_val_get_subtype}, - {"YYJSON.IsArray.get", json_val_is_array}, - {"YYJSON.IsObject.get", json_val_is_object}, - {"YYJSON.IsInt.get", json_val_is_int}, - {"YYJSON.IsUint.get", json_val_is_uint}, - {"YYJSON.IsSint.get", json_val_is_sint}, - {"YYJSON.IsNum.get", json_val_is_num}, - {"YYJSON.IsBool.get", json_val_is_bool}, - {"YYJSON.IsTrue.get", json_val_is_true}, - {"YYJSON.IsFalse.get", json_val_is_false}, - {"YYJSON.IsFloat.get", json_val_is_float}, - {"YYJSON.IsStr.get", json_val_is_str}, - {"YYJSON.IsNull.get", json_val_is_null}, - {"YYJSON.IsCtn.get", json_val_is_ctn}, - {"YYJSON.IsMutable.get", json_val_is_mutable}, - {"YYJSON.IsImmutable.get", json_val_is_immutable}, - {"YYJSON.ForeachObject", json_obj_foreach}, - {"YYJSON.ForeachArray", json_arr_foreach}, - {"YYJSON.ForeachKey", json_obj_foreach_key}, - {"YYJSON.ForeachIndex", json_arr_foreach_index}, - {"YYJSON.ToMutable", json_doc_to_mutable}, - {"YYJSON.ToImmutable", json_doc_to_immutable}, - - // JSON CREATE & GET - {"YYJSON.Pack", json_val_pack}, - {"YYJSON.CreateBool", json_val_create_bool}, - {"YYJSON.CreateFloat", json_val_create_float}, - {"YYJSON.CreateInt", json_val_create_int}, - {"YYJSON.CreateInt64", json_val_create_integer64}, - {"YYJSON.CreateNull", json_val_create_null}, - {"YYJSON.CreateString", json_val_create_str}, - {"YYJSON.GetBool", json_val_get_bool}, - {"YYJSON.GetFloat", json_val_get_float}, - {"YYJSON.GetInt", json_val_get_int}, - {"YYJSON.GetInt64", json_val_get_integer64}, - {"YYJSON.GetString", json_val_get_str}, - - // JSON POINTER - {"YYJSON.PtrGet", json_ptr_get_val}, - {"YYJSON.PtrGetBool", json_ptr_get_bool}, - {"YYJSON.PtrGetFloat", json_ptr_get_float}, - {"YYJSON.PtrGetInt", json_ptr_get_int}, - {"YYJSON.PtrGetInt64", json_ptr_get_integer64}, - {"YYJSON.PtrGetString", json_ptr_get_str}, - {"YYJSON.PtrGetIsNull", json_ptr_get_is_null}, - {"YYJSON.PtrGetLength", json_ptr_get_length}, - {"YYJSON.PtrSet", json_ptr_set_val}, - {"YYJSON.PtrSetBool", json_ptr_set_bool}, - {"YYJSON.PtrSetFloat", json_ptr_set_float}, - {"YYJSON.PtrSetInt", json_ptr_set_int}, - {"YYJSON.PtrSetInt64", json_ptr_set_integer64}, - {"YYJSON.PtrSetString", json_ptr_set_str}, - {"YYJSON.PtrSetNull", json_ptr_set_null}, - {"YYJSON.PtrAdd", json_ptr_add_val}, - {"YYJSON.PtrAddBool", json_ptr_add_bool}, - {"YYJSON.PtrAddFloat", json_ptr_add_float}, - {"YYJSON.PtrAddInt", json_ptr_add_int}, - {"YYJSON.PtrAddInt64", json_ptr_add_integer64}, - {"YYJSON.PtrAddString", json_ptr_add_str}, - {"YYJSON.PtrAddNull", json_ptr_add_null}, - {"YYJSON.PtrRemove", json_ptr_remove_val}, - {"YYJSON.PtrTryGetVal", json_ptr_try_get_val}, - {"YYJSON.PtrTryGetBool", json_ptr_try_get_bool}, - {"YYJSON.PtrTryGetFloat", json_ptr_try_get_float}, - {"YYJSON.PtrTryGetInt", json_ptr_try_get_int}, - {"YYJSON.PtrTryGetInt64", json_ptr_try_get_integer64}, - {"YYJSON.PtrTryGetString", json_ptr_try_get_str}, - {nullptr, nullptr} -}; \ No newline at end of file diff --git a/src/smsdk_config.h b/src/smsdk_config.h index acee57d..73184d5 100755 --- a/src/smsdk_config.h +++ b/src/smsdk_config.h @@ -1,12 +1,12 @@ #ifndef _INCLUDE_SOURCEMOD_EXTENSION_CONFIG_H_ #define _INCLUDE_SOURCEMOD_EXTENSION_CONFIG_H_ -#define SMEXT_CONF_NAME "SourceMod YYJSON Extension" +#define SMEXT_CONF_NAME "SourceMod JSON Extension" #define SMEXT_CONF_DESCRIPTION "Provide JSON Native" -#define SMEXT_CONF_VERSION "1.1.4a" +#define SMEXT_CONF_VERSION "1.1.5" #define SMEXT_CONF_AUTHOR "ProjectSky" #define SMEXT_CONF_URL "https://github.com/ProjectSky/sm-ext-yyjson" -#define SMEXT_CONF_LOGTAG "yyjson" +#define SMEXT_CONF_LOGTAG "json" #define SMEXT_CONF_LICENSE "GPL" #define SMEXT_CONF_DATESTRING __DATE__ diff --git a/third_party/yyjson/LICENSE b/third_party/yyjson/LICENSE old mode 100644 new mode 100755 diff --git a/third_party/yyjson/yyjson.c b/third_party/yyjson/yyjson.c old mode 100644 new mode 100755 diff --git a/third_party/yyjson/yyjson.h b/third_party/yyjson/yyjson.h old mode 100644 new mode 100755 From 847cd55bde63f420fbe32e274a1a0cba90566324 Mon Sep 17 00:00:00 2001 From: ProjectSky Date: Sun, 9 Nov 2025 11:28:32 +0800 Subject: [PATCH 2/8] refactor: streamline JSON error handling and update configuration - Introduced SetErrorSafe function to standardize error message handling across JSON operations, improving safety and consistency - Removed deprecated SetPackError function to reduce redundancy - Updated IJsonManager interface to include a default destructor fix: fix Windows compilation failure - Fix Windows build failure caused by missing C++17 specification --- AMBuildScript | 495 +++++++++++++++++------------------------- public/IJsonManager.h | 2 + src/JsonManager.cpp | 271 +++++++++++------------ src/JsonManager.h | 1 - src/JsonNatives.cpp | 9 +- src/smsdk_config.h | 2 +- 6 files changed, 344 insertions(+), 436 deletions(-) diff --git a/AMBuildScript b/AMBuildScript index 9e63058..d4eb46e 100755 --- a/AMBuildScript +++ b/AMBuildScript @@ -1,295 +1,204 @@ # vim: set sts=2 ts=8 sw=2 tw=99 et ft=python: import os -def ResolveEnvPath(env, folder): - if env in os.environ: - path = os.environ[env] - if os.path.isdir(path): - return path - return None - - head = os.getcwd() - oldhead = None - while head != None and head != oldhead: - path = os.path.join(head, folder) - if os.path.isdir(path): - return path - oldhead = head - head, tail = os.path.split(head) - - return None - -def Normalize(path): - return os.path.abspath(os.path.normpath(path)) - -def SetArchFlags(compiler): - if compiler.behavior == 'gcc': - if compiler.target.arch == 'x86_64': - compiler.cflags += ['-fPIC'] - else: - compiler.cflags += ['-fPIC'] - elif compiler.like('msvc'): - if compiler.target.arch == 'x86_64': - compiler.defines += ['WIN64'] - else: - compiler.defines += ['WIN32'] - class ExtensionConfig(object): - def __init__(self): - self.extensions = [] - self.sm_root = None - self.all_targets = [] - self.target_archs = set() - - if builder.options.targets: - target_archs = builder.options.targets.split(',') - else: - target_archs = ['x86'] - - for arch in target_archs: - try: - cxx = builder.DetectCxx(target_arch = arch) - self.target_archs.add(cxx.target.arch) - except Exception as e: - # Error if archs were manually overridden. - if builder.options.targets: - raise - print('Skipping target {}: {}'.format(arch, e)) - continue - self.all_targets.append(cxx) - - if not self.all_targets: - raise Exception('No suitable C/C++ compiler was found.') - - @property - def tag(self): - if builder.options.debug == '1': - return 'Debug' - return 'Release' - - def detectSourceMod(self): - if builder.options.sm_path: - self.sm_root = builder.options.sm_path - else: - self.sm_root = ResolveEnvPath('SOURCEMOD18', 'sourcemod-1.8') - if not self.sm_root: - self.sm_root = ResolveEnvPath('SOURCEMOD110', 'sourcemod-1.10') - if not self.sm_root: - self.sm_root = ResolveEnvPath('SOURCEMOD', 'sourcemod') - if not self.sm_root: - self.sm_root = ResolveEnvPath('SOURCEMOD_DEV', 'sourcemod-central') - - if not self.sm_root or not os.path.isdir(self.sm_root): - raise Exception('Could not find a source copy of SourceMod') - - self.sm_root = Normalize(self.sm_root) - - def configure(self): - for cxx in self.all_targets: - self.configure_cxx(cxx) - - def configure_cxx(self, cxx): - if cxx.family == 'msvc': - if cxx.version < 1900: - raise Exception('MSVC 2015 or later is required.') - elif cxx.family == 'gcc': - if cxx.version < 'gcc-4.9': - raise Exception('GCC 4.9 or later is required.') - elif cxx.family == 'clang': - if cxx.version < 'clang-3.4': - raise Exception('Clang 3.4 or later is required.') - - if cxx.like('gcc'): - self.configure_gcc(cxx) - elif cxx.family == 'msvc': - self.configure_msvc(cxx) - - # Optimization - if builder.options.opt == '1': - cxx.defines += ['NDEBUG'] - - # Debugging - if builder.options.debug == '1': - cxx.defines += ['DEBUG', '_DEBUG'] - - # Platform-specifics - if cxx.target.platform == 'linux': - self.configure_linux(cxx) - elif cxx.target.platform == 'mac': - self.configure_mac(cxx) - elif cxx.target.platform == 'windows': - self.configure_windows(cxx) - - # Finish up. - cxx.includes += [ - os.path.join(self.sm_root, 'public'), - ] - - def configure_gcc(self, cxx): - cxx.defines += [ - 'stricmp=strcasecmp', - '_stricmp=strcasecmp', - '_snprintf=snprintf', - '_vsnprintf=vsnprintf', - 'HAVE_STDINT_H', - 'GNUC', - ] - cxx.cflags += [ - '-pipe', - '-fno-strict-aliasing', - '-Wall', - '-Werror', - '-Wno-unused', - '-Wno-switch', - '-Wno-array-bounds', - '-msse', - '-fvisibility=hidden', - '-fexceptions', - ] - - if cxx.version == 'apple-clang-6.0' or cxx.version == 'clang-3.4': - cxx.cxxflags += ['-std=c++1y'] - else: - cxx.cxxflags += ['-std=c++17'] - - cxx.cxxflags += [ - '-fno-threadsafe-statics', - '-Wno-non-virtual-dtor', - '-Wno-overloaded-virtual', - '-fvisibility-inlines-hidden', - '-fexceptions', - ] - - have_gcc = cxx.family == 'gcc' - have_clang = cxx.family == 'clang' - if cxx.version >= 'clang-3.9' or cxx.version == 'clang-3.4' or cxx.version > 'apple-clang-6.0': - cxx.cxxflags += ['-Wno-expansion-to-defined'] - if cxx.version == 'clang-3.9' or cxx.version == 'apple-clang-8.0': - cxx.cflags += ['-Wno-varargs'] - if cxx.version >= 'clang-3.4' or cxx.version >= 'apple-clang-7.0': - cxx.cxxflags += ['-Wno-inconsistent-missing-override'] - if cxx.version >= 'clang-2.9' or cxx.version >= 'apple-clang-3.0': - cxx.cxxflags += ['-Wno-null-dereference'] - if have_clang or (cxx.version >= 'gcc-4.6'): - cxx.cflags += ['-Wno-narrowing'] - if have_clang or (cxx.version >= 'gcc-4.7'): - cxx.cxxflags += ['-Wno-delete-non-virtual-dtor'] - if cxx.version >= 'gcc-4.8': - cxx.cflags += ['-Wno-unused-result'] - if cxx.version >= 'gcc-9.0': - cxx.cxxflags += ['-Wno-class-memaccess', '-Wno-packed-not-aligned'] - - if have_clang: - cxx.cxxflags += ['-Wno-implicit-exception-spec-mismatch'] - if cxx.version >= 'apple-clang-5.1' or cxx.version >= 'clang-3.4': - cxx.cxxflags += ['-Wno-deprecated-register'] - else: - cxx.cxxflags += ['-Wno-deprecated'] - cxx.cflags += ['-Wno-sometimes-uninitialized'] - - if have_gcc: - cxx.cflags += ['-mfpmath=sse'] - cxx.cflags += ['-Wno-maybe-uninitialized'] - - if builder.options.opt == '1': - cxx.cflags += ['-O3'] - - # This needs to be after our optimization flags which could otherwise disable it. - # Don't omit the frame pointer. - cxx.cflags += ['-fno-omit-frame-pointer'] - - def configure_msvc(self, cxx): - if builder.options.debug == '1': - cxx.cflags += ['/MTd'] - cxx.linkflags += ['/NODEFAULTLIB:libcmt'] - else: - cxx.cflags += ['/MT'] - cxx.defines += [ - '_CRT_SECURE_NO_DEPRECATE', - '_CRT_SECURE_NO_WARNINGS', - '_CRT_NONSTDC_NO_DEPRECATE', - '_ITERATOR_DEBUG_LEVEL=0', - ] - cxx.cflags += [ - '/W3', - ] - cxx.cxxflags += [ - '/EHsc', - '/GR-', - '/TP', - ] - cxx.linkflags += [ - 'kernel32.lib', - 'user32.lib', - 'gdi32.lib', - 'winspool.lib', - 'comdlg32.lib', - 'advapi32.lib', - 'shell32.lib', - 'ole32.lib', - 'oleaut32.lib', - 'uuid.lib', - 'odbc32.lib', - 'odbccp32.lib', - ] - - if builder.options.opt == '1': - cxx.cflags += ['/Ox', '/Zo'] - cxx.linkflags += ['/OPT:ICF', '/OPT:REF'] - - if builder.options.debug == '1': - cxx.cflags += ['/Od', '/RTC1'] - - # This needs to be after our optimization flags which could otherwise disable it. - # Don't omit the frame pointer. - cxx.cflags += ['/Oy-'] - - def configure_linux(self, cxx): - cxx.defines += ['_LINUX', 'POSIX'] - cxx.linkflags += ['-Wl,--exclude-libs,ALL', '-lm', '-s'] - if cxx.family == 'gcc': - cxx.linkflags += ['-static-libgcc'] - elif cxx.family == 'clang': - cxx.linkflags += ['-lgcc_eh'] - cxx.linkflags += ['-static-libstdc++'] - - def configure_mac(self, cxx): - cxx.defines += ['OSX', '_OSX', 'POSIX'] - cxx.cflags += ['-mmacosx-version-min=10.7'] - cxx.linkflags += [ - '-mmacosx-version-min=10.7', - '-lc++', - '-stdlib=libc++', - ] - cxx.cxxflags += ['-stdlib=libc++'] - - def configure_windows(self, cxx): - cxx.defines += [ - '_WINDOWS', - '_WIN32_WINNT=0x0601', - ] - - def ConfigureForExtension(self, context, compiler): - compiler.cxxincludes += [ - os.path.join(context.currentSourcePath), - os.path.join(self.sm_root, 'public'), - os.path.join(self.sm_root, 'public', 'extensions'), - os.path.join(self.sm_root, 'sourcepawn', 'include'), - os.path.join(self.sm_root, 'public', 'amtl', 'amtl'), - os.path.join(self.sm_root, 'public', 'amtl'), - ] - return compiler - - def Library(self, context, compiler, name): - compiler = compiler.clone() - SetArchFlags(compiler) - self.ConfigureForExtension(context, compiler) - return compiler.Library(name) - - def StaticLibrary(self, context, compiler, name): - compiler = compiler.clone() - SetArchFlags(compiler) - return compiler.StaticLibrary(name) + def __init__(self): + self.extensions = [] + self.sm_root = None + self.all_targets = [] + self.target_archs = set() + + if builder.options.targets: + target_archs = builder.options.targets.split(',') + else: + target_archs = ['x86'] + + for arch in target_archs: + try: + cxx = builder.DetectCxx(target_arch=arch) + self.target_archs.add(cxx.target.arch) + except Exception as e: + if builder.options.targets: + raise + print('Skipping target {}: {}'.format(arch, e)) + continue + self.all_targets.append(cxx) + + if not self.all_targets: + raise Exception('No suitable C/C++ compiler was found.') + + def detectSourceMod(self): + if builder.options.sm_path: + self.sm_root = builder.options.sm_path + else: + raise Exception('SourceMod path not specified. Use --sm-path or set SOURCEMOD environment variable.') + + if not self.sm_root or not os.path.isdir(self.sm_root): + raise Exception('Could not find a source copy of SourceMod') + + self.sm_root = os.path.abspath(os.path.normpath(self.sm_root)) + + def configure(self): + for cxx in self.all_targets: + self.configure_cxx(cxx) + + def configure_cxx(self, cxx): + if cxx.family == 'msvc': + if cxx.version < 1914 and builder.options.generator != 'vs': + raise Exception('Only MSVC 2017 15.7 and later are supported, full C++17 support is required.') + elif cxx.family == 'gcc': + if cxx.version < 'gcc-9': + raise Exception('Only GCC versions 9 or later are supported, full C++17 support is required.') + elif cxx.family == 'clang': + if cxx.version < 'clang-5': + raise Exception('Only clang versions 5 or later are supported, full C++17 support is required.') + + if cxx.like('gcc'): + self.configure_gcc(cxx) + elif cxx.family == 'msvc': + self.configure_msvc(cxx) + + # Optimization + if builder.options.opt == '1': + cxx.defines += ['NDEBUG'] + + # Debugging + if builder.options.debug == '1': + cxx.defines += ['DEBUG', '_DEBUG'] + + # Platform-specifics + if cxx.target.platform == 'linux': + self.configure_linux(cxx) + elif cxx.target.platform == 'windows': + self.configure_windows(cxx) + + # Finish up. + cxx.includes += [ + os.path.join(self.sm_root, 'public'), + ] + + def configure_gcc(self, cxx): + cxx.defines += [ + 'stricmp=strcasecmp', + '_stricmp=strcasecmp', + '_snprintf=snprintf', + '_vsnprintf=vsnprintf', + 'HAVE_STDINT_H', + 'GNUC', + ] + + cxx.cflags += [ + '-pipe', + '-fno-strict-aliasing', + '-Wall', + '-fvisibility=hidden', + '-fPIC', + ] + + # Clang-specific warnings + if cxx.family == 'clang': + cxx.cflags += ['-Wno-sometimes-uninitialized'] + cxx.cxxflags += ['-Wno-implicit-exception-spec-mismatch'] + + cxx.cxxflags += ['-std=c++17'] + + cxx.cxxflags += [ + '-fno-threadsafe-statics', + '-fvisibility-inlines-hidden', + ] + + # Clang version-specific warnings + if cxx.family == 'clang': + if cxx.version >= 'clang-3.4' or cxx.version >= 'apple-clang-7.0': + cxx.cxxflags += ['-Wno-inconsistent-missing-override'] + if cxx.version >= 'apple-clang-5.1' or cxx.version >= 'clang-3.4': + cxx.cxxflags += ['-Wno-deprecated-register'] + + # GCC version-specific warnings + if cxx.family == 'gcc' and cxx.version >= 'gcc-9.0': + cxx.cxxflags += ['-Wno-class-memaccess'] + + if builder.options.opt == '1': + cxx.cflags += ['-O2'] + + # Keep frame pointer for debugging + cxx.cflags += ['-fno-omit-frame-pointer'] + + def configure_msvc(self, cxx): + if builder.options.debug == '1': + cxx.cflags += ['/MTd'] + else: + cxx.cflags += ['/MT'] + + cxx.defines += [ + '_CRT_SECURE_NO_WARNINGS', + '_CRT_NONSTDC_NO_DEPRECATE', + ] + + cxx.cflags += ['/W3'] + + cxx.cxxflags += [ + '/std:c++17', + '/EHsc', + '/GR-', + '/TP', + ] + + cxx.linkflags += [ + 'kernel32.lib', + 'user32.lib', + 'gdi32.lib', + ] + + if builder.options.opt == '1': + cxx.cflags += ['/Ox', '/Zo'] + cxx.linkflags += ['/OPT:ICF', '/OPT:REF'] + + if builder.options.debug == '1': + cxx.cflags += ['/Od', '/RTC1'] + + # Keep frame pointer + cxx.cflags += ['/Oy-'] + + def configure_linux(self, cxx): + cxx.defines += ['_LINUX', 'POSIX'] + cxx.linkflags += ['-Wl,--exclude-libs,ALL', '-lm'] + + if builder.options.opt == '1': + cxx.linkflags += ['-s'] + + if cxx.family == 'gcc': + cxx.linkflags += ['-static-libgcc'] + elif cxx.family == 'clang': + cxx.linkflags += ['-lgcc_eh'] + + cxx.linkflags += ['-static-libstdc++'] + + def configure_windows(self, cxx): + cxx.defines += [ + '_WINDOWS', + '_WIN32_WINNT=0x0601', + ] + + def Library(self, context, compiler, name): + compiler = compiler.clone() + self.ConfigureForExtension(context, compiler) + return compiler.Library(name) + + def StaticLibrary(self, context, compiler, name): + compiler = compiler.clone() + return compiler.StaticLibrary(name) + + def ConfigureForExtension(self, context, compiler): + compiler.cxxincludes += [ + os.path.join(context.currentSourcePath), + os.path.join(self.sm_root, 'public'), + os.path.join(self.sm_root, 'public', 'extensions'), + os.path.join(self.sm_root, 'sourcepawn', 'include'), + os.path.join(self.sm_root, 'public', 'amtl', 'amtl'), + os.path.join(self.sm_root, 'public', 'amtl'), + ] + return compiler Extension = ExtensionConfig() Extension.detectSourceMod() @@ -301,12 +210,12 @@ builder.targets = builder.CloneableList(Extension.all_targets) # Add additional buildscripts here BuildScripts = [ - 'AMBuilder', + 'AMBuilder', ] if builder.backend == 'amb2': - BuildScripts += [ - 'PackageScript', - ] + BuildScripts += [ + 'PackageScript', + ] -builder.Build(BuildScripts, {'Extension': Extension}) +builder.Build(BuildScripts, {'Extension': Extension}) \ No newline at end of file diff --git a/public/IJsonManager.h b/public/IJsonManager.h index d9c359a..a11a316 100755 --- a/public/IJsonManager.h +++ b/public/IJsonManager.h @@ -64,6 +64,8 @@ class IPackParamProvider class IJsonManager : public SMInterface { public: + virtual ~IJsonManager() = default; + virtual const char *GetInterfaceName() override { return SMINTERFACE_JSONMANAGER_NAME; } diff --git a/src/JsonManager.cpp b/src/JsonManager.cpp index 2d03a9e..df00ad7 100755 --- a/src/JsonManager.cpp +++ b/src/JsonManager.cpp @@ -17,6 +17,20 @@ static inline void ReadInt64FromMutVal(yyjson_mut_val* val, std::variant= static_cast(error_size)) { + error[error_size - 1] = '\0'; + } +} + std::unique_ptr JsonManager::CreateWrapper() { return std::make_unique(); } @@ -46,7 +60,7 @@ JsonValue* JsonManager::ParseJSON(const char* json_str, bool is_file, bool is_mu { if (!json_str) { if (error && error_size > 0) { - snprintf(error, error_size, "Invalid JSON string"); + SetErrorSafe(error, error_size, "Invalid JSON string"); } return nullptr; } @@ -66,10 +80,10 @@ JsonValue* JsonManager::ParseJSON(const char* json_str, bool is_file, bool is_mu if (!idoc || readError.code) { if (error && error_size > 0) { if (is_file) { - snprintf(error, error_size, "Failed to parse JSON file: %s (error code: %u, msg: %s, position: %zu)", + SetErrorSafe(error, error_size, "Failed to parse JSON file: %s (error code: %u, msg: %s, position: %zu)", json_str, readError.code, readError.msg, readError.pos); } else { - snprintf(error, error_size, "Failed to parse JSON str: %s (error code: %u, position: %zu)", + SetErrorSafe(error, error_size, "Failed to parse JSON str: %s (error code: %u, position: %zu)", readError.msg, readError.code, readError.pos); } } @@ -135,7 +149,7 @@ bool JsonManager::WriteToFile(JsonValue* handle, const char* path, yyjson_write_ { if (!handle || !path) { if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters"); + SetErrorSafe(error, error_size, "Invalid parameters"); } return false; } @@ -153,7 +167,7 @@ bool JsonManager::WriteToFile(JsonValue* handle, const char* path, yyjson_write_ } if (writeError.code && error && error_size > 0) { - snprintf(error, error_size, "Failed to write JSON to file: %s (error code: %u)", writeError.msg, writeError.code); + SetErrorSafe(error, error_size, "Failed to write JSON to file: %s (error code: %u)", writeError.msg, writeError.code); } return is_success; @@ -600,7 +614,7 @@ JsonValue* JsonManager::ObjectParseString(const char* str, yyjson_read_flag read { if (!str) { if (error && error_size > 0) { - snprintf(error, error_size, "Invalid string"); + SetErrorSafe(error, error_size, "Invalid string"); } return nullptr; } @@ -612,7 +626,7 @@ JsonValue* JsonManager::ObjectParseString(const char* str, yyjson_read_flag read if (!idoc || readError.code) { if (error && error_size > 0) { - snprintf(error, error_size, "Failed to parse JSON str: %s (error code: %u, position: %zu)", + SetErrorSafe(error, error_size, "Failed to parse JSON str: %s (error code: %u, position: %zu)", readError.msg, readError.code, readError.pos); } if (idoc) { @@ -625,7 +639,7 @@ JsonValue* JsonManager::ObjectParseString(const char* str, yyjson_read_flag read if (!yyjson_is_obj(root)) { if (error && error_size > 0) { - snprintf(error, error_size, "Root value is not an object (got %s)", yyjson_get_type_desc(root)); + SetErrorSafe(error, error_size, "Root value is not an object (got %s)", yyjson_get_type_desc(root)); } yyjson_doc_free(idoc); return nullptr; @@ -643,7 +657,7 @@ JsonValue* JsonManager::ObjectParseFile(const char* path, yyjson_read_flag read_ { if (!path) { if (error && error_size > 0) { - snprintf(error, error_size, "Invalid path"); + SetErrorSafe(error, error_size, "Invalid path"); } return nullptr; } @@ -657,7 +671,7 @@ JsonValue* JsonManager::ObjectParseFile(const char* path, yyjson_read_flag read_ if (!idoc || readError.code) { if (error && error_size > 0) { - snprintf(error, error_size, "Failed to parse JSON file: %s (error code: %u, msg: %s, position: %zu)", + SetErrorSafe(error, error_size, "Failed to parse JSON file: %s (error code: %u, msg: %s, position: %zu)", realpath, readError.code, readError.msg, readError.pos); } if (idoc) { @@ -670,7 +684,7 @@ JsonValue* JsonManager::ObjectParseFile(const char* path, yyjson_read_flag read_ if (!yyjson_is_obj(root)) { if (error && error_size > 0) { - snprintf(error, error_size, "Root value in file is not an object (got %s)", yyjson_get_type_desc(root)); + SetErrorSafe(error, error_size, "Root value in file is not an object (got %s)", yyjson_get_type_desc(root)); } yyjson_doc_free(idoc); return nullptr; @@ -1235,7 +1249,7 @@ JsonValue* JsonManager::ArrayInitWithInt64(const char** values, size_t count, ch { if (!values) { if (error && error_size > 0) { - snprintf(error, error_size, "Invalid values parameter"); + SetErrorSafe(error, error_size, "Invalid values parameter"); } return nullptr; } @@ -1254,7 +1268,7 @@ JsonValue* JsonManager::ArrayInitWithInt64(const char** values, size_t count, ch pJSONValue->m_pVal_mut = yyjson_mut_arr(doc); if (!pJSONValue->m_pVal_mut) { if (error && error_size > 0) { - snprintf(error, error_size, "Failed to create array"); + SetErrorSafe(error, error_size, "Failed to create array"); } return nullptr; } @@ -1277,7 +1291,7 @@ JsonValue* JsonManager::ArrayInitWithInt64(const char** values, size_t count, ch if (!val || !yyjson_mut_arr_append(pJSONValue->m_pVal_mut, val)) { if (error && error_size > 0) { - snprintf(error, error_size, "Failed to append value at index %zu", i); + SetErrorSafe(error, error_size, "Failed to append value at index %zu", i); } return nullptr; } @@ -1335,7 +1349,7 @@ JsonValue* JsonManager::ArrayParseString(const char* str, yyjson_read_flag read_ { if (!str) { if (error && error_size > 0) { - snprintf(error, error_size, "Invalid string"); + SetErrorSafe(error, error_size, "Invalid string"); } return nullptr; } @@ -1347,7 +1361,7 @@ JsonValue* JsonManager::ArrayParseString(const char* str, yyjson_read_flag read_ if (!idoc || readError.code) { if (error && error_size > 0) { - snprintf(error, error_size, "Failed to parse JSON string: %s (error code: %u, position: %zu)", + SetErrorSafe(error, error_size, "Failed to parse JSON string: %s (error code: %u, position: %zu)", readError.msg, readError.code, readError.pos); } if (idoc) { @@ -1360,7 +1374,7 @@ JsonValue* JsonManager::ArrayParseString(const char* str, yyjson_read_flag read_ if (!yyjson_is_arr(root)) { if (error && error_size > 0) { - snprintf(error, error_size, "Root value is not an array (got %s)", yyjson_get_type_desc(root)); + SetErrorSafe(error, error_size, "Root value is not an array (got %s)", yyjson_get_type_desc(root)); } yyjson_doc_free(idoc); return nullptr; @@ -1378,7 +1392,7 @@ JsonValue* JsonManager::ArrayParseFile(const char* path, yyjson_read_flag read_f { if (!path) { if (error && error_size > 0) { - snprintf(error, error_size, "Invalid path"); + SetErrorSafe(error, error_size, "Invalid path"); } return nullptr; } @@ -1392,7 +1406,7 @@ JsonValue* JsonManager::ArrayParseFile(const char* path, yyjson_read_flag read_f if (!idoc || readError.code) { if (error && error_size > 0) { - snprintf(error, error_size, "Failed to parse JSON file: %s (error code: %u, msg: %s, position: %zu)", + SetErrorSafe(error, error_size, "Failed to parse JSON file: %s (error code: %u, msg: %s, position: %zu)", realpath, readError.code, readError.msg, readError.pos); } if (idoc) { @@ -1405,7 +1419,7 @@ JsonValue* JsonManager::ArrayParseFile(const char* path, yyjson_read_flag read_f if (!yyjson_is_arr(root)) { if (error && error_size > 0) { - snprintf(error, error_size, "Root value in file is not an array (got %s)", yyjson_get_type_desc(root)); + SetErrorSafe(error, error_size, "Root value in file is not an array (got %s)", yyjson_get_type_desc(root)); } yyjson_doc_free(idoc); return nullptr; @@ -2473,24 +2487,13 @@ const char* JsonManager::SkipSeparators(const char* ptr) return ptr; } -void JsonManager::SetPackError(char* error, size_t error_size, const char* fmt, ...) -{ - if (error && error_size > 0) { - va_list args; - va_start(args, fmt); - vsnprintf(error, error_size, fmt, args); - va_end(args); - error[error_size - 1] = '\0'; - } -} - yyjson_mut_val* JsonManager::PackImpl(yyjson_mut_doc* doc, const char* format, IPackParamProvider* provider, char* error, size_t error_size, const char** out_end_ptr) { if (!doc || !format || !*format) { - SetPackError(error, error_size, "Invalid argument(s)"); + SetErrorSafe(error, error_size, "Invalid argument(s)"); return nullptr; } @@ -2506,12 +2509,12 @@ yyjson_mut_val* JsonManager::PackImpl(yyjson_mut_doc* doc, const char* format, root = yyjson_mut_arr(doc); ptr = SkipSeparators(ptr + 1); } else { - SetPackError(error, error_size, "Invalid format string: expected '{' or '['"); + SetErrorSafe(error, error_size, "Invalid format string: expected '{' or '['"); return nullptr; } if (!root) { - SetPackError(error, error_size, "Failed to create root object/array"); + SetErrorSafe(error, error_size, "Failed to create root object/array"); return nullptr; } @@ -2521,7 +2524,7 @@ yyjson_mut_val* JsonManager::PackImpl(yyjson_mut_doc* doc, const char* format, while (*ptr && *ptr != '}' && *ptr != ']') { if (is_obj) { if (*ptr != 's') { - SetPackError(error, error_size, "Object key must be string, got '%c'", *ptr); + SetErrorSafe(error, error_size, "Object key must be string, got '%c'", *ptr); return nullptr; } } @@ -2530,19 +2533,19 @@ yyjson_mut_val* JsonManager::PackImpl(yyjson_mut_doc* doc, const char* format, if (is_obj) { const char* key; if (!provider->GetNextString(&key)) { - SetPackError(error, error_size, "Invalid string key"); + SetErrorSafe(error, error_size, "Invalid string key"); return nullptr; } key_val = yyjson_mut_strcpy(doc, key); if (!key_val) { - SetPackError(error, error_size, "Failed to create key"); + SetErrorSafe(error, error_size, "Failed to create key"); return nullptr; } ptr = SkipSeparators(ptr + 1); if (*ptr != 's' && *ptr != 'i' && *ptr != 'f' && *ptr != 'b' && *ptr != 'n' && *ptr != '{' && *ptr != '[') { - SetPackError(error, error_size, "Invalid value type after key"); + SetErrorSafe(error, error_size, "Invalid value type after key"); return nullptr; } @@ -2556,7 +2559,7 @@ yyjson_mut_val* JsonManager::PackImpl(yyjson_mut_doc* doc, const char* format, case 's': { const char* val_str; if (!provider->GetNextString(&val_str)) { - SetPackError(error, error_size, "Invalid string value"); + SetErrorSafe(error, error_size, "Invalid string value"); return nullptr; } val = yyjson_mut_strcpy(doc, val_str); @@ -2566,7 +2569,7 @@ yyjson_mut_val* JsonManager::PackImpl(yyjson_mut_doc* doc, const char* format, case 'i': { int val_int; if (!provider->GetNextInt(&val_int)) { - SetPackError(error, error_size, "Invalid integer value"); + SetErrorSafe(error, error_size, "Invalid integer value"); return nullptr; } val = yyjson_mut_int(doc, val_int); @@ -2576,7 +2579,7 @@ yyjson_mut_val* JsonManager::PackImpl(yyjson_mut_doc* doc, const char* format, case 'f': { float val_float; if (!provider->GetNextFloat(&val_float)) { - SetPackError(error, error_size, "Invalid float value"); + SetErrorSafe(error, error_size, "Invalid float value"); return nullptr; } val = yyjson_mut_real(doc, val_float); @@ -2586,7 +2589,7 @@ yyjson_mut_val* JsonManager::PackImpl(yyjson_mut_doc* doc, const char* format, case 'b': { bool val_bool; if (!provider->GetNextBool(&val_bool)) { - SetPackError(error, error_size, "Invalid boolean value"); + SetErrorSafe(error, error_size, "Invalid boolean value"); return nullptr; } val = yyjson_mut_bool(doc, val_bool); @@ -2602,22 +2605,22 @@ yyjson_mut_val* JsonManager::PackImpl(yyjson_mut_doc* doc, const char* format, } if (!val) { - SetPackError(error, error_size, "Failed to create value"); + SetErrorSafe(error, error_size, "Failed to create value"); return nullptr; } if (!yyjson_mut_obj_add(root, key_val, val)) { - SetPackError(error, error_size, "Failed to add value to object"); + SetErrorSafe(error, error_size, "Failed to add value to object"); return nullptr; } } else { const char* val_str; if (!provider->GetNextString(&val_str)) { - SetPackError(error, error_size, "Invalid string value"); + SetErrorSafe(error, error_size, "Invalid string value"); return nullptr; } if (!yyjson_mut_arr_add_strcpy(doc, root, val_str)) { - SetPackError(error, error_size, "Failed to add string to array"); + SetErrorSafe(error, error_size, "Failed to add string to array"); return nullptr; } ptr++; @@ -2627,11 +2630,11 @@ yyjson_mut_val* JsonManager::PackImpl(yyjson_mut_doc* doc, const char* format, case 'i': { int val_int; if (!provider->GetNextInt(&val_int)) { - SetPackError(error, error_size, "Invalid integer value"); + SetErrorSafe(error, error_size, "Invalid integer value"); return nullptr; } if (!yyjson_mut_arr_add_int(doc, root, val_int)) { - SetPackError(error, error_size, "Failed to add integer to array"); + SetErrorSafe(error, error_size, "Failed to add integer to array"); return nullptr; } ptr++; @@ -2640,11 +2643,11 @@ yyjson_mut_val* JsonManager::PackImpl(yyjson_mut_doc* doc, const char* format, case 'b': { bool val_bool; if (!provider->GetNextBool(&val_bool)) { - SetPackError(error, error_size, "Invalid boolean value"); + SetErrorSafe(error, error_size, "Invalid boolean value"); return nullptr; } if (!yyjson_mut_arr_add_bool(doc, root, val_bool)) { - SetPackError(error, error_size, "Failed to add boolean to array"); + SetErrorSafe(error, error_size, "Failed to add boolean to array"); return nullptr; } ptr++; @@ -2652,7 +2655,7 @@ yyjson_mut_val* JsonManager::PackImpl(yyjson_mut_doc* doc, const char* format, } case 'n': { if (!yyjson_mut_arr_add_null(doc, root)) { - SetPackError(error, error_size, "Failed to add null to array"); + SetErrorSafe(error, error_size, "Failed to add null to array"); return nullptr; } ptr++; @@ -2661,11 +2664,11 @@ yyjson_mut_val* JsonManager::PackImpl(yyjson_mut_doc* doc, const char* format, case 'f': { float val_float; if (!provider->GetNextFloat(&val_float)) { - SetPackError(error, error_size, "Invalid float value"); + SetErrorSafe(error, error_size, "Invalid float value"); return nullptr; } if (!yyjson_mut_arr_add_real(doc, root, val_float)) { - SetPackError(error, error_size, "Failed to add float to array"); + SetErrorSafe(error, error_size, "Failed to add float to array"); return nullptr; } ptr++; @@ -2678,13 +2681,13 @@ yyjson_mut_val* JsonManager::PackImpl(yyjson_mut_doc* doc, const char* format, return nullptr; } if (!yyjson_mut_arr_append(root, val)) { - SetPackError(error, error_size, "Failed to add nested value to array"); + SetErrorSafe(error, error_size, "Failed to add nested value to array"); return nullptr; } break; } default: { - SetPackError(error, error_size, "Invalid format character: %c", *ptr); + SetErrorSafe(error, error_size, "Invalid format character: %c", *ptr); return nullptr; } } @@ -2692,7 +2695,7 @@ yyjson_mut_val* JsonManager::PackImpl(yyjson_mut_doc* doc, const char* format, } if (*ptr != (is_obj ? '}' : ']')) { - SetPackError(error, error_size, "Unexpected end of format string"); + SetErrorSafe(error, error_size, "Unexpected end of format string"); return nullptr; } @@ -2706,7 +2709,7 @@ yyjson_mut_val* JsonManager::PackImpl(yyjson_mut_doc* doc, const char* format, JsonValue* JsonManager::Pack(const char* format, IPackParamProvider* param_provider, char* error, size_t error_size) { if (!format || !param_provider) { - SetPackError(error, error_size, "Invalid arguments"); + SetErrorSafe(error, error_size, "Invalid arguments"); return nullptr; } @@ -2714,7 +2717,7 @@ JsonValue* JsonManager::Pack(const char* format, IPackParamProvider* param_provi pJSONValue->m_pDocument_mut = CreateDocument(); if (!pJSONValue->m_pDocument_mut) { - SetPackError(error, error_size, "Failed to create document"); + SetErrorSafe(error, error_size, "Failed to create document"); return nullptr; } @@ -2936,7 +2939,7 @@ JsonValue* JsonManager::PtrGet(JsonValue* handle, const char* path, char* error, { if (!handle || !path) { if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters"); + SetErrorSafe(error, error_size, "Invalid parameters"); } return nullptr; } @@ -2949,7 +2952,7 @@ JsonValue* JsonManager::PtrGet(JsonValue* handle, const char* path, char* error, if (!val || ptrGetError.code) { if (error && error_size > 0) { - snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + SetErrorSafe(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); } return nullptr; @@ -2962,7 +2965,7 @@ JsonValue* JsonManager::PtrGet(JsonValue* handle, const char* path, char* error, if (!val || ptrGetError.code) { if (error && error_size > 0) { - snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + SetErrorSafe(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); } return nullptr; @@ -2979,7 +2982,7 @@ bool JsonManager::PtrGetBool(JsonValue* handle, const char* path, bool* out_valu { if (!handle || !path || !out_value) { if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters"); + SetErrorSafe(error, error_size, "Invalid parameters"); } return false; } @@ -2991,7 +2994,7 @@ bool JsonManager::PtrGetBool(JsonValue* handle, const char* path, bool* out_valu if (ptrGetError.code) { if (error && error_size > 0) { - snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + SetErrorSafe(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); } return false; @@ -2999,7 +3002,7 @@ bool JsonManager::PtrGetBool(JsonValue* handle, const char* path, bool* out_valu if (!yyjson_mut_is_bool(val)) { if (error && error_size > 0) { - snprintf(error, error_size, "Type mismatch at path '%s': expected boolean value, got %s", path, yyjson_mut_get_type_desc(val)); + SetErrorSafe(error, error_size, "Type mismatch at path '%s': expected boolean value, got %s", path, yyjson_mut_get_type_desc(val)); } return false; } @@ -3011,7 +3014,7 @@ bool JsonManager::PtrGetBool(JsonValue* handle, const char* path, bool* out_valu if (ptrGetError.code) { if (error && error_size > 0) { - snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + SetErrorSafe(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); } return false; @@ -3019,7 +3022,7 @@ bool JsonManager::PtrGetBool(JsonValue* handle, const char* path, bool* out_valu if (!yyjson_is_bool(val)) { if (error && error_size > 0) { - snprintf(error, error_size, "Type mismatch at path '%s': expected boolean value, got %s", path, yyjson_get_type_desc(val)); + SetErrorSafe(error, error_size, "Type mismatch at path '%s': expected boolean value, got %s", path, yyjson_get_type_desc(val)); } return false; } @@ -3033,7 +3036,7 @@ bool JsonManager::PtrGetFloat(JsonValue* handle, const char* path, double* out_v { if (!handle || !path || !out_value) { if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters"); + SetErrorSafe(error, error_size, "Invalid parameters"); } return false; } @@ -3045,7 +3048,7 @@ bool JsonManager::PtrGetFloat(JsonValue* handle, const char* path, double* out_v if (ptrGetError.code) { if (error && error_size > 0) { - snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + SetErrorSafe(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); } return false; @@ -3053,7 +3056,7 @@ bool JsonManager::PtrGetFloat(JsonValue* handle, const char* path, double* out_v if (!yyjson_mut_is_real(val)) { if (error && error_size > 0) { - snprintf(error, error_size, "Type mismatch at path '%s': expected float value, got %s", path, yyjson_mut_get_type_desc(val)); + SetErrorSafe(error, error_size, "Type mismatch at path '%s': expected float value, got %s", path, yyjson_mut_get_type_desc(val)); } return false; } @@ -3065,7 +3068,7 @@ bool JsonManager::PtrGetFloat(JsonValue* handle, const char* path, double* out_v if (ptrGetError.code) { if (error && error_size > 0) { - snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + SetErrorSafe(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); } return false; @@ -3073,7 +3076,7 @@ bool JsonManager::PtrGetFloat(JsonValue* handle, const char* path, double* out_v if (!yyjson_is_real(val)) { if (error && error_size > 0) { - snprintf(error, error_size, "Type mismatch at path '%s': expected float value, got %s", path, yyjson_get_type_desc(val)); + SetErrorSafe(error, error_size, "Type mismatch at path '%s': expected float value, got %s", path, yyjson_get_type_desc(val)); } return false; } @@ -3087,7 +3090,7 @@ bool JsonManager::PtrGetInt(JsonValue* handle, const char* path, int* out_value, { if (!handle || !path || !out_value) { if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters"); + SetErrorSafe(error, error_size, "Invalid parameters"); } return false; } @@ -3099,7 +3102,7 @@ bool JsonManager::PtrGetInt(JsonValue* handle, const char* path, int* out_value, if (ptrGetError.code) { if (error && error_size > 0) { - snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + SetErrorSafe(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); } return false; @@ -3107,7 +3110,7 @@ bool JsonManager::PtrGetInt(JsonValue* handle, const char* path, int* out_value, if (!yyjson_mut_is_int(val)) { if (error && error_size > 0) { - snprintf(error, error_size, "Type mismatch at path '%s': expected integer value, got %s", path, yyjson_mut_get_type_desc(val)); + SetErrorSafe(error, error_size, "Type mismatch at path '%s': expected integer value, got %s", path, yyjson_mut_get_type_desc(val)); } return false; } @@ -3119,7 +3122,7 @@ bool JsonManager::PtrGetInt(JsonValue* handle, const char* path, int* out_value, if (ptrGetError.code) { if (error && error_size > 0) { - snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + SetErrorSafe(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); } return false; @@ -3127,7 +3130,7 @@ bool JsonManager::PtrGetInt(JsonValue* handle, const char* path, int* out_value, if (!yyjson_is_int(val)) { if (error && error_size > 0) { - snprintf(error, error_size, "Type mismatch at path '%s': expected integer value, got %s", path, yyjson_get_type_desc(val)); + SetErrorSafe(error, error_size, "Type mismatch at path '%s': expected integer value, got %s", path, yyjson_get_type_desc(val)); } return false; } @@ -3141,7 +3144,7 @@ bool JsonManager::PtrGetInt64(JsonValue* handle, const char* path, std::variant< { if (!handle || !path || !out_value) { if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters"); + SetErrorSafe(error, error_size, "Invalid parameters"); } return false; } @@ -3153,7 +3156,7 @@ bool JsonManager::PtrGetInt64(JsonValue* handle, const char* path, std::variant< if (!val || ptrGetError.code) { if (error && error_size > 0) { - snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + SetErrorSafe(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); } return false; @@ -3161,7 +3164,7 @@ bool JsonManager::PtrGetInt64(JsonValue* handle, const char* path, std::variant< if (!yyjson_mut_is_int(val)) { if (error && error_size > 0) { - snprintf(error, error_size, "Type mismatch at path '%s': expected integer64 value, got %s", path, yyjson_mut_get_type_desc(val)); + SetErrorSafe(error, error_size, "Type mismatch at path '%s': expected integer64 value, got %s", path, yyjson_mut_get_type_desc(val)); } return false; } @@ -3173,7 +3176,7 @@ bool JsonManager::PtrGetInt64(JsonValue* handle, const char* path, std::variant< if (!val || ptrGetError.code) { if (error && error_size > 0) { - snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + SetErrorSafe(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); } return false; @@ -3181,7 +3184,7 @@ bool JsonManager::PtrGetInt64(JsonValue* handle, const char* path, std::variant< if (!yyjson_is_int(val)) { if (error && error_size > 0) { - snprintf(error, error_size, "Type mismatch at path '%s': expected integer64 value, got %s", path, yyjson_get_type_desc(val)); + SetErrorSafe(error, error_size, "Type mismatch at path '%s': expected integer64 value, got %s", path, yyjson_get_type_desc(val)); } return false; } @@ -3195,7 +3198,7 @@ bool JsonManager::PtrGetString(JsonValue* handle, const char* path, const char** { if (!handle || !path || !out_str) { if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters"); + SetErrorSafe(error, error_size, "Invalid parameters"); } return false; } @@ -3207,7 +3210,7 @@ bool JsonManager::PtrGetString(JsonValue* handle, const char* path, const char** if (ptrGetError.code) { if (error && error_size > 0) { - snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + SetErrorSafe(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); } return false; @@ -3215,7 +3218,7 @@ bool JsonManager::PtrGetString(JsonValue* handle, const char* path, const char** if (!yyjson_mut_is_str(val)) { if (error && error_size > 0) { - snprintf(error, error_size, "Type mismatch at path '%s': expected string value, got %s", path, yyjson_mut_get_type_desc(val)); + SetErrorSafe(error, error_size, "Type mismatch at path '%s': expected string value, got %s", path, yyjson_mut_get_type_desc(val)); } return false; } @@ -3230,7 +3233,7 @@ bool JsonManager::PtrGetString(JsonValue* handle, const char* path, const char** if (ptrGetError.code) { if (error && error_size > 0) { - snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + SetErrorSafe(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); } return false; @@ -3238,7 +3241,7 @@ bool JsonManager::PtrGetString(JsonValue* handle, const char* path, const char** if (!yyjson_is_str(val)) { if (error && error_size > 0) { - snprintf(error, error_size, "Type mismatch at path '%s': expected string value, got %s", path, yyjson_get_type_desc(val)); + SetErrorSafe(error, error_size, "Type mismatch at path '%s': expected string value, got %s", path, yyjson_get_type_desc(val)); } return false; } @@ -3255,7 +3258,7 @@ bool JsonManager::PtrGetIsNull(JsonValue* handle, const char* path, bool* out_is { if (!handle || !path || !out_is_null) { if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters"); + SetErrorSafe(error, error_size, "Invalid parameters"); } return false; } @@ -3267,7 +3270,7 @@ bool JsonManager::PtrGetIsNull(JsonValue* handle, const char* path, bool* out_is if (ptrGetError.code) { if (error && error_size > 0) { - snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + SetErrorSafe(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); } return false; @@ -3280,7 +3283,7 @@ bool JsonManager::PtrGetIsNull(JsonValue* handle, const char* path, bool* out_is if (ptrGetError.code) { if (error && error_size > 0) { - snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + SetErrorSafe(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); } return false; @@ -3295,7 +3298,7 @@ bool JsonManager::PtrGetLength(JsonValue* handle, const char* path, size_t* out_ { if (!handle || !path || !out_len) { if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters"); + SetErrorSafe(error, error_size, "Invalid parameters"); } return false; } @@ -3307,7 +3310,7 @@ bool JsonManager::PtrGetLength(JsonValue* handle, const char* path, size_t* out_ if (ptrGetError.code) { if (error && error_size > 0) { - snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + SetErrorSafe(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); } return false; @@ -3324,7 +3327,7 @@ bool JsonManager::PtrGetLength(JsonValue* handle, const char* path, size_t* out_ if (ptrGetError.code) { if (error && error_size > 0) { - snprintf(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + SetErrorSafe(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); } return false; @@ -3343,7 +3346,7 @@ bool JsonManager::PtrSet(JsonValue* handle, const char* path, JsonValue* value, { if (!handle || !handle->IsMutable() || !path || !value) { if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters or immutable document"); + SetErrorSafe(error, error_size, "Invalid parameters or immutable document"); } return false; } @@ -3357,7 +3360,7 @@ bool JsonManager::PtrSet(JsonValue* handle, const char* path, JsonValue* value, if (!val_copy) { if (error && error_size > 0) { - snprintf(error, error_size, "Failed to copy JSON value"); + SetErrorSafe(error, error_size, "Failed to copy JSON value"); } return false; } @@ -3366,7 +3369,7 @@ bool JsonManager::PtrSet(JsonValue* handle, const char* path, JsonValue* value, bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut.get(), path, strlen(path), val_copy, true, nullptr, &ptrSetError); if (ptrSetError.code && error && error_size > 0) { - snprintf(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", + SetErrorSafe(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrSetError.msg, ptrSetError.code, ptrSetError.pos, path); } @@ -3377,7 +3380,7 @@ bool JsonManager::PtrSetBool(JsonValue* handle, const char* path, bool value, ch { if (!handle || !handle->IsMutable() || !path) { if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters or immutable document"); + SetErrorSafe(error, error_size, "Invalid parameters or immutable document"); } return false; } @@ -3386,7 +3389,7 @@ bool JsonManager::PtrSetBool(JsonValue* handle, const char* path, bool value, ch bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_bool(handle->m_pDocument_mut.get(), value), true, nullptr, &ptrSetError); if (ptrSetError.code && error && error_size > 0) { - snprintf(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", + SetErrorSafe(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrSetError.msg, ptrSetError.code, ptrSetError.pos, path); } @@ -3397,7 +3400,7 @@ bool JsonManager::PtrSetFloat(JsonValue* handle, const char* path, double value, { if (!handle || !handle->IsMutable() || !path) { if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters or immutable document"); + SetErrorSafe(error, error_size, "Invalid parameters or immutable document"); } return false; } @@ -3406,7 +3409,7 @@ bool JsonManager::PtrSetFloat(JsonValue* handle, const char* path, double value, bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_real(handle->m_pDocument_mut.get(), value), true, nullptr, &ptrSetError); if (ptrSetError.code && error && error_size > 0) { - snprintf(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", + SetErrorSafe(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrSetError.msg, ptrSetError.code, ptrSetError.pos, path); } @@ -3417,7 +3420,7 @@ bool JsonManager::PtrSetInt(JsonValue* handle, const char* path, int value, char { if (!handle || !handle->IsMutable() || !path) { if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters or immutable document"); + SetErrorSafe(error, error_size, "Invalid parameters or immutable document"); } return false; } @@ -3426,7 +3429,7 @@ bool JsonManager::PtrSetInt(JsonValue* handle, const char* path, int value, char bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_int(handle->m_pDocument_mut.get(), value), true, nullptr, &ptrSetError); if (ptrSetError.code && error && error_size > 0) { - snprintf(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", + SetErrorSafe(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrSetError.msg, ptrSetError.code, ptrSetError.pos, path); } @@ -3437,7 +3440,7 @@ bool JsonManager::PtrSetInt64(JsonValue* handle, const char* path, std::variant< { if (!handle || !handle->IsMutable() || !path) { if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters or immutable document"); + SetErrorSafe(error, error_size, "Invalid parameters or immutable document"); } return false; } @@ -3454,7 +3457,7 @@ bool JsonManager::PtrSetInt64(JsonValue* handle, const char* path, std::variant< }, value); if (!success && ptrSetError.code && error && error_size > 0) { - snprintf(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", + SetErrorSafe(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrSetError.msg, ptrSetError.code, ptrSetError.pos, path); } @@ -3465,7 +3468,7 @@ bool JsonManager::PtrSetString(JsonValue* handle, const char* path, const char* { if (!handle || !handle->IsMutable() || !path || !value) { if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters or immutable document"); + SetErrorSafe(error, error_size, "Invalid parameters or immutable document"); } return false; } @@ -3474,7 +3477,7 @@ bool JsonManager::PtrSetString(JsonValue* handle, const char* path, const char* bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_strcpy(handle->m_pDocument_mut.get(), value), true, nullptr, &ptrSetError); if (ptrSetError.code && error && error_size > 0) { - snprintf(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", + SetErrorSafe(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrSetError.msg, ptrSetError.code, ptrSetError.pos, path); } @@ -3485,7 +3488,7 @@ bool JsonManager::PtrSetNull(JsonValue* handle, const char* path, char* error, s { if (!handle || !handle->IsMutable() || !path) { if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters or immutable document"); + SetErrorSafe(error, error_size, "Invalid parameters or immutable document"); } return false; } @@ -3494,7 +3497,7 @@ bool JsonManager::PtrSetNull(JsonValue* handle, const char* path, char* error, s bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_null(handle->m_pDocument_mut.get()), true, nullptr, &ptrSetError); if (ptrSetError.code && error && error_size > 0) { - snprintf(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", + SetErrorSafe(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrSetError.msg, ptrSetError.code, ptrSetError.pos, path); } @@ -3505,7 +3508,7 @@ bool JsonManager::PtrAdd(JsonValue* handle, const char* path, JsonValue* value, { if (!handle || !handle->IsMutable() || !path || !value) { if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters or immutable document"); + SetErrorSafe(error, error_size, "Invalid parameters or immutable document"); } return false; } @@ -3519,7 +3522,7 @@ bool JsonManager::PtrAdd(JsonValue* handle, const char* path, JsonValue* value, if (!val_copy) { if (error && error_size > 0) { - snprintf(error, error_size, "Failed to copy JSON value"); + SetErrorSafe(error, error_size, "Failed to copy JSON value"); } return false; } @@ -3528,7 +3531,7 @@ bool JsonManager::PtrAdd(JsonValue* handle, const char* path, JsonValue* value, bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut.get(), path, strlen(path), val_copy, true, nullptr, &ptrAddError); if (ptrAddError.code && error && error_size > 0) { - snprintf(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", + SetErrorSafe(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrAddError.msg, ptrAddError.code, ptrAddError.pos, path); } @@ -3539,7 +3542,7 @@ bool JsonManager::PtrAddBool(JsonValue* handle, const char* path, bool value, ch { if (!handle || !handle->IsMutable() || !path) { if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters or immutable document"); + SetErrorSafe(error, error_size, "Invalid parameters or immutable document"); } return false; } @@ -3548,7 +3551,7 @@ bool JsonManager::PtrAddBool(JsonValue* handle, const char* path, bool value, ch bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_bool(handle->m_pDocument_mut.get(), value), true, nullptr, &ptrAddError); if (ptrAddError.code && error && error_size > 0) { - snprintf(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", + SetErrorSafe(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrAddError.msg, ptrAddError.code, ptrAddError.pos, path); } @@ -3559,7 +3562,7 @@ bool JsonManager::PtrAddFloat(JsonValue* handle, const char* path, double value, { if (!handle || !handle->IsMutable() || !path) { if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters or immutable document"); + SetErrorSafe(error, error_size, "Invalid parameters or immutable document"); } return false; } @@ -3568,7 +3571,7 @@ bool JsonManager::PtrAddFloat(JsonValue* handle, const char* path, double value, bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_real(handle->m_pDocument_mut.get(), value), true, nullptr, &ptrAddError); if (ptrAddError.code && error && error_size > 0) { - snprintf(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", + SetErrorSafe(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrAddError.msg, ptrAddError.code, ptrAddError.pos, path); } @@ -3579,7 +3582,7 @@ bool JsonManager::PtrAddInt(JsonValue* handle, const char* path, int value, char { if (!handle || !handle->IsMutable() || !path) { if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters or immutable document"); + SetErrorSafe(error, error_size, "Invalid parameters or immutable document"); } return false; } @@ -3588,7 +3591,7 @@ bool JsonManager::PtrAddInt(JsonValue* handle, const char* path, int value, char bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_int(handle->m_pDocument_mut.get(), value), true, nullptr, &ptrAddError); if (ptrAddError.code && error && error_size > 0) { - snprintf(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", + SetErrorSafe(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrAddError.msg, ptrAddError.code, ptrAddError.pos, path); } @@ -3599,7 +3602,7 @@ bool JsonManager::PtrAddInt64(JsonValue* handle, const char* path, std::variant< { if (!handle || !handle->IsMutable() || !path) { if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters or immutable document"); + SetErrorSafe(error, error_size, "Invalid parameters or immutable document"); } return false; } @@ -3616,7 +3619,7 @@ bool JsonManager::PtrAddInt64(JsonValue* handle, const char* path, std::variant< }, value); if (ptrAddError.code && error && error_size > 0) { - snprintf(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", + SetErrorSafe(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrAddError.msg, ptrAddError.code, ptrAddError.pos, path); } @@ -3627,7 +3630,7 @@ bool JsonManager::PtrAddString(JsonValue* handle, const char* path, const char* { if (!handle || !handle->IsMutable() || !path || !value) { if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters or immutable document"); + SetErrorSafe(error, error_size, "Invalid parameters or immutable document"); } return false; } @@ -3636,7 +3639,7 @@ bool JsonManager::PtrAddString(JsonValue* handle, const char* path, const char* bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_strcpy(handle->m_pDocument_mut.get(), value), true, nullptr, &ptrAddError); if (ptrAddError.code && error && error_size > 0) { - snprintf(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", + SetErrorSafe(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrAddError.msg, ptrAddError.code, ptrAddError.pos, path); } @@ -3647,7 +3650,7 @@ bool JsonManager::PtrAddNull(JsonValue* handle, const char* path, char* error, s { if (!handle || !handle->IsMutable() || !path) { if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters or immutable document"); + SetErrorSafe(error, error_size, "Invalid parameters or immutable document"); } return false; } @@ -3656,7 +3659,7 @@ bool JsonManager::PtrAddNull(JsonValue* handle, const char* path, char* error, s bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_null(handle->m_pDocument_mut.get()), true, nullptr, &ptrAddError); if (ptrAddError.code && error && error_size > 0) { - snprintf(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", + SetErrorSafe(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrAddError.msg, ptrAddError.code, ptrAddError.pos, path); } @@ -3667,7 +3670,7 @@ bool JsonManager::PtrRemove(JsonValue* handle, const char* path, char* error, si { if (!handle || !handle->IsMutable() || !path) { if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters or immutable document"); + SetErrorSafe(error, error_size, "Invalid parameters or immutable document"); } return false; } @@ -3676,7 +3679,7 @@ bool JsonManager::PtrRemove(JsonValue* handle, const char* path, char* error, si bool success = yyjson_mut_doc_ptr_removex(handle->m_pDocument_mut.get(), path, strlen(path), nullptr, &ptrRemoveError) != nullptr; if (ptrRemoveError.code && error && error_size > 0) { - snprintf(error, error_size, "Failed to remove JSON pointer: %s (error code: %u, position: %zu, path: %s)", + SetErrorSafe(error, error_size, "Failed to remove JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrRemoveError.msg, ptrRemoveError.code, ptrRemoveError.pos, path); } @@ -4410,7 +4413,7 @@ JsonValue* JsonManager::ReadNumber(const char* dat, uint32_t read_flg, char* err { if (!dat) { if (error && error_size > 0) { - snprintf(error, error_size, "Invalid input data"); + SetErrorSafe(error, error_size, "Invalid input data"); } return nullptr; } @@ -4421,7 +4424,7 @@ JsonValue* JsonManager::ReadNumber(const char* dat, uint32_t read_flg, char* err yyjson_mut_val* val = yyjson_mut_int(pJSONValue->m_pDocument_mut.get(), 0); if (!val) { if (error && error_size > 0) { - snprintf(error, error_size, "Failed to create number value"); + SetErrorSafe(error, error_size, "Failed to create number value"); } return nullptr; } @@ -4432,7 +4435,7 @@ JsonValue* JsonManager::ReadNumber(const char* dat, uint32_t read_flg, char* err if (!end_ptr || readError.code) { if (error && error_size > 0) { - snprintf(error, error_size, "Failed to read number: %s (error code: %u, position: %zu)", + SetErrorSafe(error, error_size, "Failed to read number: %s (error code: %u, position: %zu)", readError.msg, readError.code, readError.pos); } return nullptr; @@ -4605,14 +4608,14 @@ bool JsonManager::ParseInt64Variant(const char* value, std::variant 0) { - snprintf(error, error_size, "Empty integer64 value"); + SetErrorSafe(error, error_size, "Empty integer64 value"); } return false; } if (!out_value) { if (error && error_size > 0) { - snprintf(error, error_size, "Invalid output parameter"); + SetErrorSafe(error, error_size, "Invalid output parameter"); } return false; } @@ -4631,9 +4634,9 @@ bool JsonManager::ParseInt64Variant(const char* value, std::variant 0) { if (result.ec == std::errc::result_out_of_range) { - snprintf(error, error_size, "Integer64 value out of range: %s", value); + SetErrorSafe(error, error_size, "Integer64 value out of range: %s", value); } else { - snprintf(error, error_size, "Invalid integer64 value: %s", value); + SetErrorSafe(error, error_size, "Invalid integer64 value: %s", value); } } return false; @@ -4658,16 +4661,16 @@ bool JsonManager::ParseInt64Variant(const char* value, std::variant 0) { if (unsigned_result.ec == std::errc::result_out_of_range) { - snprintf(error, error_size, "Integer64 value out of range: %s", value); + SetErrorSafe(error, error_size, "Integer64 value out of range: %s", value); } else { - snprintf(error, error_size, "Invalid integer64 value: %s", value); + SetErrorSafe(error, error_size, "Invalid integer64 value: %s", value); } } return false; } if (error && error_size > 0) { - snprintf(error, error_size, "Invalid integer64 value: %s", value); + SetErrorSafe(error, error_size, "Invalid integer64 value: %s", value); } return false; } \ No newline at end of file diff --git a/src/JsonManager.h b/src/JsonManager.h index 76c356e..deae931 100755 --- a/src/JsonManager.h +++ b/src/JsonManager.h @@ -385,7 +385,6 @@ class JsonManager : public IJsonManager // Pack helper methods static const char* SkipSeparators(const char* ptr); - static void SetPackError(char* error, size_t error_size, const char* fmt, ...); static yyjson_mut_val* PackImpl(yyjson_mut_doc* doc, const char* format, IPackParamProvider* provider, char* error, size_t error_size, const char** out_end_ptr); diff --git a/src/JsonNatives.cpp b/src/JsonNatives.cpp index e5c86d9..8c2eca1 100755 --- a/src/JsonNatives.cpp +++ b/src/JsonNatives.cpp @@ -836,9 +836,8 @@ static cell_t json_arr_init_with_bool(IPluginContext* pContext, const cell_t* pa pContext->LocalToPhysAddr(params[1], &addr); cell_t array_size = params[2]; - // Use unique_ptr for automatic memory management - // Note: std::vector is specialized and doesn't work with .data() - std::unique_ptr values(new bool[array_size]); + // std::vector is specialized and doesn't work with .data() so we use a unique_ptr + auto values = std::make_unique(array_size); for (cell_t i = 0; i < array_size; i++) { values[i] = (addr[i] != 0); @@ -1254,8 +1253,6 @@ static cell_t json_arr_append_str(IPluginContext* pContext, const cell_t* params return g_pJsonManager->ArrayAppendString(handle, str); } -// ========== Array Insert Operations ========== - static cell_t json_arr_insert(IPluginContext* pContext, const cell_t* params) { JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); @@ -1369,8 +1366,6 @@ static cell_t json_arr_insert_null(IPluginContext* pContext, const cell_t* param return g_pJsonManager->ArrayInsertNull(handle, index); } -// ========== Array Prepend Operations ========== - static cell_t json_arr_prepend(IPluginContext* pContext, const cell_t* params) { JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); diff --git a/src/smsdk_config.h b/src/smsdk_config.h index 73184d5..25e6d39 100755 --- a/src/smsdk_config.h +++ b/src/smsdk_config.h @@ -3,7 +3,7 @@ #define SMEXT_CONF_NAME "SourceMod JSON Extension" #define SMEXT_CONF_DESCRIPTION "Provide JSON Native" -#define SMEXT_CONF_VERSION "1.1.5" +#define SMEXT_CONF_VERSION "1.1.5a" #define SMEXT_CONF_AUTHOR "ProjectSky" #define SMEXT_CONF_URL "https://github.com/ProjectSky/sm-ext-yyjson" #define SMEXT_CONF_LOGTAG "json" From 3f5d30d706ddb61284cd2bf3239d72daa91ad809 Mon Sep 17 00:00:00 2001 From: ProjectSky Date: Sun, 9 Nov 2025 13:20:35 +0800 Subject: [PATCH 3/8] fix: fix Windows compilation failure again --- AMBuildScript | 10 +++++++++- public/IJsonManager.h | 2 +- src/JsonManager.cpp | 4 ++-- src/JsonManager.h | 4 ++-- src/JsonNatives.cpp | 2 +- src/smsdk_config.h | 2 +- 6 files changed, 16 insertions(+), 8 deletions(-) diff --git a/AMBuildScript b/AMBuildScript index d4eb46e..ad0cead 100755 --- a/AMBuildScript +++ b/AMBuildScript @@ -1,6 +1,13 @@ # vim: set sts=2 ts=8 sw=2 tw=99 et ft=python: import os +def SetArchFlags(compiler): + if compiler.like('msvc'): + if compiler.target.arch == 'x86_64': + compiler.defines += ['WIN64'] + else: + compiler.defines += ['WIN32'] + class ExtensionConfig(object): def __init__(self): self.extensions = [] @@ -31,7 +38,7 @@ class ExtensionConfig(object): if builder.options.sm_path: self.sm_root = builder.options.sm_path else: - raise Exception('SourceMod path not specified. Use --sm-path or set SOURCEMOD environment variable.') + raise Exception('SourceMod path not specified') if not self.sm_root or not os.path.isdir(self.sm_root): raise Exception('Could not find a source copy of SourceMod') @@ -182,6 +189,7 @@ class ExtensionConfig(object): def Library(self, context, compiler, name): compiler = compiler.clone() + SetArchFlags(compiler) self.ConfigureForExtension(context, compiler) return compiler.Library(name) diff --git a/public/IJsonManager.h b/public/IJsonManager.h index a11a316..e06e9d8 100755 --- a/public/IJsonManager.h +++ b/public/IJsonManager.h @@ -591,7 +591,7 @@ class IJsonManager : public SMInterface /** * Create a JSON array from float values - * @param values Array of double values + * @param values Array of float values * @param count Number of values * @return New JSON array or nullptr on failure */ diff --git a/src/JsonManager.cpp b/src/JsonManager.cpp index df00ad7..3103c0b 100755 --- a/src/JsonManager.cpp +++ b/src/JsonManager.cpp @@ -4572,9 +4572,9 @@ bool JsonManager::SetFloat(JsonValue* handle, double value) } if (handle->IsMutable()) { - return yyjson_mut_set_float(handle->m_pVal_mut, value); + return yyjson_mut_set_real(handle->m_pVal_mut, value); } else { - return yyjson_set_float(handle->m_pVal, value); + return yyjson_set_real(handle->m_pVal, value); } } diff --git a/src/JsonManager.h b/src/JsonManager.h index deae931..0de2e79 100755 --- a/src/JsonManager.h +++ b/src/JsonManager.h @@ -17,10 +17,10 @@ class JsonValue { public: JsonValue() = default; ~JsonValue() { - if (m_pDocument_mut.unique()) { + if (m_pDocument_mut.use_count() == 1) { yyjson_mut_doc_free(m_pDocument_mut.get()); } - if (m_pDocument.unique()) { + if (m_pDocument.use_count() == 1) { yyjson_doc_free(m_pDocument.get()); } } diff --git a/src/JsonNatives.cpp b/src/JsonNatives.cpp index 8c2eca1..c90eba5 100755 --- a/src/JsonNatives.cpp +++ b/src/JsonNatives.cpp @@ -3005,7 +3005,7 @@ static cell_t json_set_float(IPluginContext* pContext, const cell_t* params) JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); if (!handle) return 0; - double value = sp_ctof(params[2]); + float value = sp_ctof(params[2]); if (!g_pJsonManager->SetFloat(handle, value)) { return pContext->ThrowNativeError("Failed to set value to float (value is object or array)"); } diff --git a/src/smsdk_config.h b/src/smsdk_config.h index 25e6d39..71f6197 100755 --- a/src/smsdk_config.h +++ b/src/smsdk_config.h @@ -3,7 +3,7 @@ #define SMEXT_CONF_NAME "SourceMod JSON Extension" #define SMEXT_CONF_DESCRIPTION "Provide JSON Native" -#define SMEXT_CONF_VERSION "1.1.5a" +#define SMEXT_CONF_VERSION "1.1.5b" #define SMEXT_CONF_AUTHOR "ProjectSky" #define SMEXT_CONF_URL "https://github.com/ProjectSky/sm-ext-yyjson" #define SMEXT_CONF_LOGTAG "json" From 520d9140ef060a17274dea4c3c75233076438762 Mon Sep 17 00:00:00 2001 From: ProjectSky Date: Mon, 10 Nov 2025 23:59:25 +0800 Subject: [PATCH 4/8] feat: add performance-optimized JSON serialization method - Introduced WriteToStringPtr method in IJsonManager and JsonManager for efficient JSON serialization, avoiding intermediate buffer allocations. - Updated documentation to highlight performance considerations and usage recommendations. --- public/IJsonManager.h | 13 +++++++++++++ src/JsonManager.cpp | 22 ++++++++++++++++++++++ src/JsonManager.h | 1 + src/JsonNatives.cpp | 22 ++++++++++++---------- src/smsdk_config.h | 2 +- 5 files changed, 49 insertions(+), 11 deletions(-) diff --git a/public/IJsonManager.h b/public/IJsonManager.h index e06e9d8..093e640 100755 --- a/public/IJsonManager.h +++ b/public/IJsonManager.h @@ -99,10 +99,23 @@ class IJsonManager : public SMInterface * * @note The out_size parameter returns the size including null terminator * @note Use GetSerializedSize() with the same write_flg to determine buffer size + * @warning This method performs multiple memory allocations and copies, resulting in poor performance. + * For better performance, use WriteToStringPtr() instead, which avoids intermediate buffers */ virtual bool WriteToString(JsonValue* handle, char* buffer, size_t buffer_size, uint32_t write_flg = 0, size_t* out_size = nullptr) = 0; + /** + * Write JSON to string and return allocated string (performance-optimized version) + * @param handle JSON value + * @param write_flg Write flags (YYJSON_WRITE_FLAG values, default: 0) + * @param out_size Pointer to receive actual size written (including null terminator) optional + * @return Allocated string pointer on success, nullptr on error. Caller must free() the returned pointer + * + * @note This is the recommended method for serialization as it avoids intermediate buffer allocations + */ + virtual char* WriteToStringPtr(JsonValue* handle, uint32_t write_flg = 0, size_t* out_size = nullptr) = 0; + /** * Write JSON to file * @param handle JSON value diff --git a/src/JsonManager.cpp b/src/JsonManager.cpp index 3103c0b..68439e6 100755 --- a/src/JsonManager.cpp +++ b/src/JsonManager.cpp @@ -144,6 +144,28 @@ bool JsonManager::WriteToString(JsonValue* handle, char* buffer, size_t buffer_s return true; } +char* JsonManager::WriteToStringPtr(JsonValue* handle, yyjson_write_flag write_flg, size_t* out_size) +{ + if (!handle) { + return nullptr; + } + + size_t json_size = 0; + char* json_str = nullptr; + + if (handle->IsMutable()) { + json_str = yyjson_mut_val_write(handle->m_pVal_mut, write_flg, &json_size); + } else { + json_str = yyjson_val_write(handle->m_pVal, write_flg, &json_size); + } + + if (json_str && out_size) { + *out_size = json_size + 1; + } + + return json_str; +} + bool JsonManager::WriteToFile(JsonValue* handle, const char* path, yyjson_write_flag write_flg, char* error, size_t error_size) { diff --git a/src/JsonManager.h b/src/JsonManager.h index 0de2e79..a43db25 100755 --- a/src/JsonManager.h +++ b/src/JsonManager.h @@ -139,6 +139,7 @@ class JsonManager : public IJsonManager yyjson_read_flag read_flg, char* error, size_t error_size) override; virtual bool WriteToString(JsonValue* handle, char* buffer, size_t buffer_size, yyjson_write_flag write_flg, size_t* out_size) override; + virtual char* WriteToStringPtr(JsonValue* handle, yyjson_write_flag write_flg, size_t* out_size); virtual bool WriteToFile(JsonValue* handle, const char* path, yyjson_write_flag write_flg, char* error, size_t error_size) override; virtual bool Equals(JsonValue* handle1, JsonValue* handle2) override; diff --git a/src/JsonNatives.cpp b/src/JsonNatives.cpp index c90eba5..ea63f30 100755 --- a/src/JsonNatives.cpp +++ b/src/JsonNatives.cpp @@ -1549,20 +1549,22 @@ static cell_t json_doc_write_to_str(IPluginContext* pContext, const cell_t* para size_t buffer_size = static_cast(params[3]); yyjson_write_flag write_flg = static_cast(params[4]); - char* temp_buffer = (char*)malloc(buffer_size); - if (!temp_buffer) { - return pContext->ThrowNativeError("Failed to allocate buffer"); + size_t json_size = 0; + char* json_str = g_pJsonManager->WriteToStringPtr(handle, write_flg, &json_size); + + if (!json_str) { + return pContext->ThrowNativeError("Failed to serialize JSON"); } - size_t output_len = 0; - if (!g_pJsonManager->WriteToString(handle, temp_buffer, buffer_size, write_flg, &output_len)) { - free(temp_buffer); - return pContext->ThrowNativeError("Buffer too small or write failed"); + if (json_size > buffer_size) { + free(json_str); + return pContext->ThrowNativeError("Buffer too small (need %d, have %d)", json_size, buffer_size); } - pContext->StringToLocalUTF8(params[2], buffer_size, temp_buffer, nullptr); - free(temp_buffer); - return static_cast(output_len); + pContext->StringToLocalUTF8(params[2], buffer_size, json_str, nullptr); + free(json_str); + + return static_cast(json_size); } static cell_t json_doc_write_to_file(IPluginContext* pContext, const cell_t* params) diff --git a/src/smsdk_config.h b/src/smsdk_config.h index 71f6197..eff4e02 100755 --- a/src/smsdk_config.h +++ b/src/smsdk_config.h @@ -3,7 +3,7 @@ #define SMEXT_CONF_NAME "SourceMod JSON Extension" #define SMEXT_CONF_DESCRIPTION "Provide JSON Native" -#define SMEXT_CONF_VERSION "1.1.5b" +#define SMEXT_CONF_VERSION "1.1.5c" #define SMEXT_CONF_AUTHOR "ProjectSky" #define SMEXT_CONF_URL "https://github.com/ProjectSky/sm-ext-yyjson" #define SMEXT_CONF_LOGTAG "json" From e89e3a6468e56e7414266f1a40741ba65496b576 Mon Sep 17 00:00:00 2001 From: ProjectSky Date: Tue, 11 Nov 2025 00:06:22 +0800 Subject: [PATCH 5/8] feat: add reference counting to JSON documents - Introduced RefCounted and RefPtr classes for intrusive reference counting of JSON documents - Updated IJsonManager to include GetRefCount method for retrieving the reference count of JSON values - Refactored JsonManager to utilize RefPtr for managing mutable and immutable JSON documents, enhancing memory management - Updated documentation to reflect changes in JSON value handling and reference counting --- public/IJsonManager.h | 8 ++ scripting/include/json.inc | 70 +++++++++- src/JsonManager.cpp | 274 +++++++++++++++++++------------------ src/JsonManager.h | 201 ++++++++++++++++++++++++--- src/JsonNatives.cpp | 10 ++ src/smsdk_config.h | 2 +- 6 files changed, 408 insertions(+), 157 deletions(-) diff --git a/public/IJsonManager.h b/public/IJsonManager.h index 093e640..bb00a00 100755 --- a/public/IJsonManager.h +++ b/public/IJsonManager.h @@ -329,6 +329,14 @@ class IJsonManager : public SMInterface */ virtual size_t GetReadSize(JsonValue* handle) = 0; + /** + * Get the reference count of the document + * @param handle JSON value + * @return Reference count of the document (number of JsonValue objects sharing this document) + * @note Returns 0 if handle is null or has no document + */ + virtual int GetRefCount(JsonValue* handle) = 0; + /** * Create an empty mutable JSON object * @return New mutable JSON object or nullptr on failure diff --git a/scripting/include/json.inc b/scripting/include/json.inc index 6d81096..c372ae5 100755 --- a/scripting/include/json.inc +++ b/scripting/include/json.inc @@ -381,14 +381,13 @@ methodmap JSON < Handle /** * Returns the JSON value's type description * - * @param value JSON handle * @param buffer String buffer to write to * @param maxlength Maximum length of the string buffer * * @return The return value should be one of these strings: "raw", "null", "string", * "array", "object", "true", "false", "uint", "sint", "real", "unknown" */ - public static native void GetTypeDesc(const JSON value, char[] buffer, int maxlength); + public native void GetTypeDesc(char[] buffer, int maxlength); /** * Returns whether two JSON values are equal (deep compare) @@ -396,8 +395,8 @@ methodmap JSON < Handle * @note This function is recursive and may cause a stack overflow if the object level is too deep * @note the result may be inaccurate if object has duplicate keys * - * @param value1 JSON handle - * @param value2 JSON handle + * @param value1 First JSON value to compare + * @param value2 Second JSON value to compare * * @return True if they are the same, false otherwise */ @@ -537,6 +536,7 @@ methodmap JSON < Handle * Get value by a JSON Pointer * * @note Needs to be freed using delete or CloseHandle() + * @note JSON Pointer paths are always resolved from the document root, not from the current value * * @param path The JSON pointer string * @@ -547,6 +547,8 @@ methodmap JSON < Handle /** * Get boolean value by a JSON Pointer * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * * @param path The JSON pointer string * * @return boolean value referenced by the JSON pointer @@ -556,6 +558,8 @@ methodmap JSON < Handle /** * Get float value by a JSON Pointer * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * * @param path The JSON pointer string * * @return float value referenced by the JSON pointer @@ -565,6 +569,8 @@ methodmap JSON < Handle /** * Get integer value by a JSON Pointer * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * * @param path The JSON pointer string * * @return integer value referenced by the JSON pointer @@ -574,17 +580,21 @@ methodmap JSON < Handle /** * Get integer64 value by a JSON Pointer (auto-detects signed/unsigned) * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * * @param path The JSON pointer string * @param buffer Buffer to copy to * @param maxlength Maximum size of the buffer * - * @return integer64 value referenced by the JSON pointer + * @return True on success, false on failure */ public native bool PtrGetInt64(const char[] path, char[] buffer, int maxlength); /** * Get string value by a JSON Pointer * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * * @param path The JSON pointer string * @param buffer Buffer to copy to * @param maxlength Maximum size of the buffer @@ -596,6 +606,8 @@ methodmap JSON < Handle /** * Get value is null by a JSON Pointer * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * * @param path The JSON pointer string * * @return True if the value is null, false otherwise @@ -605,6 +617,7 @@ methodmap JSON < Handle /** * Get JSON content length (string length, array size, object size) * + * @note JSON Pointer paths are always resolved from the document root, not from the current value * @note For strings: returns string length including null-terminator * @note For arrays/objects: returns number of elements * @note Returns 0 if value is null or type is not string/array/object @@ -618,6 +631,7 @@ methodmap JSON < Handle /** * Set value by a JSON Pointer * + * @note JSON Pointer paths are always resolved from the document root, not from the current value * @note The parent nodes will be created if they do not exist. * If the target value already exists, it will be replaced by the new value * @@ -631,6 +645,7 @@ methodmap JSON < Handle /** * Set boolean value by a JSON Pointer * + * @note JSON Pointer paths are always resolved from the document root, not from the current value * @note The parent nodes will be created if they do not exist. * If the target value already exists, it will be replaced by the new value * @@ -644,6 +659,7 @@ methodmap JSON < Handle /** * Set float value by a JSON Pointer * + * @note JSON Pointer paths are always resolved from the document root, not from the current value * @note The parent nodes will be created if they do not exist. * If the target value already exists, it will be replaced by the new value * @@ -657,6 +673,7 @@ methodmap JSON < Handle /** * Set integer value by a JSON Pointer * + * @note JSON Pointer paths are always resolved from the document root, not from the current value * @note The parent nodes will be created if they do not exist. * If the target value already exists, it will be replaced by the new value * @@ -670,6 +687,7 @@ methodmap JSON < Handle /** * Set integer64 value by a JSON Pointer * + * @note JSON Pointer paths are always resolved from the document root, not from the current value * @note The parent nodes will be created if they do not exist. * If the target value already exists, it will be replaced by the new value * @note This function auto-detects whether the value is signed or unsigned @@ -684,6 +702,7 @@ methodmap JSON < Handle /** * Set string value by a JSON Pointer * + * @note JSON Pointer paths are always resolved from the document root, not from the current value * @note The parent nodes will be created if they do not exist. * If the target value already exists, it will be replaced by the new value * @@ -697,6 +716,7 @@ methodmap JSON < Handle /** * Set null value by a JSON Pointer * + * @note JSON Pointer paths are always resolved from the document root, not from the current value * @note The parent nodes will be created if they do not exist. * If the target value already exists, it will be replaced by the new value * @@ -709,6 +729,8 @@ methodmap JSON < Handle /** * Add (insert) value by a JSON pointer * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * * @param path The JSON pointer string * @param value The value to be added * @@ -719,6 +741,8 @@ methodmap JSON < Handle /** * Add (insert) boolean value by a JSON pointer * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * * @param path The JSON pointer string * @param value The boolean value to be added * @@ -729,6 +753,8 @@ methodmap JSON < Handle /** * Add (insert) float value by a JSON pointer * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * * @param path The JSON pointer string * @param value The float value to be added * @@ -739,6 +765,8 @@ methodmap JSON < Handle /** * Add (insert) integer value by a JSON pointer * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * * @param path The JSON pointer string * @param value The int value to be added * @@ -749,6 +777,8 @@ methodmap JSON < Handle /** * Add (insert) integer64 value by a JSON pointer * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * * @param path The JSON pointer string * @param value The integer64 value to be added * @@ -759,6 +789,8 @@ methodmap JSON < Handle /** * Add (insert) string value by a JSON pointer * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * * @param path The JSON pointer string * @param value The str value to be added * @@ -769,6 +801,8 @@ methodmap JSON < Handle /** * Add (insert) null value by a JSON pointer * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * * @param path The JSON pointer string * * @return true if JSON pointer is valid and new value is set, false otherwise @@ -778,6 +812,8 @@ methodmap JSON < Handle /** * Remove value by a JSON pointer * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * * @param path The JSON pointer string * * @return true if removed value, false otherwise @@ -787,6 +823,8 @@ methodmap JSON < Handle /** * Try to get value by a JSON Pointer * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * * @param path The JSON pointer string * @param value Handle to store value * @@ -797,6 +835,8 @@ methodmap JSON < Handle /** * Try to get boolean value by a JSON Pointer * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * * @param path The JSON pointer string * @param value Store the boolean value * @@ -807,6 +847,8 @@ methodmap JSON < Handle /** * Try to get float value by a JSON Pointer * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * * @param path The JSON pointer string * @param value Store the float value * @@ -817,6 +859,8 @@ methodmap JSON < Handle /** * Try to get integer value by a JSON Pointer * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * * @param path The JSON pointer string * @param value Store the integer value * @@ -827,6 +871,8 @@ methodmap JSON < Handle /** * Try to get integer64 value by a JSON Pointer (automatically detects signed/unsigned) * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * * @param path The JSON pointer string * @param buffer Buffer to store the integer64 value * @param maxlength Maximum length of the buffer @@ -838,6 +884,8 @@ methodmap JSON < Handle /** * Try to get string value by a JSON Pointer * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * * @param path The JSON pointer string * @param buffer Buffer to store the string value * @param maxlength Maximum length of the buffer @@ -978,6 +1026,17 @@ methodmap JSON < Handle property int ReadSize { public native get(); } + + /** + * Get the reference count of the document (debugging purpose) + * + * @return Reference count (number of JSON objects sharing this document) + * + * @note Returns 0 if the handle is invalid + */ + property int RefCount { + public native get(); + } }; methodmap JSONObject < JSON @@ -2055,6 +2114,7 @@ public void __pl_json_SetNTVOptional() MarkNativeAsOptional("JSON.GetTypeDesc"); MarkNativeAsOptional("JSON.GetSerializedSize"); MarkNativeAsOptional("JSON.ReadSize.get"); + MarkNativeAsOptional("JSON.RefCount.get"); MarkNativeAsOptional("JSON.Type.get"); MarkNativeAsOptional("JSON.SubType.get"); MarkNativeAsOptional("JSON.IsArray.get"); diff --git a/src/JsonManager.cpp b/src/JsonManager.cpp index 68439e6..a66330d 100755 --- a/src/JsonManager.cpp +++ b/src/JsonManager.cpp @@ -35,20 +35,20 @@ std::unique_ptr JsonManager::CreateWrapper() { return std::make_unique(); } -std::shared_ptr JsonManager::WrapDocument(yyjson_mut_doc* doc) { - return std::shared_ptr(doc, [](yyjson_mut_doc*){}); +RefPtr JsonManager::WrapDocument(yyjson_mut_doc* doc) { + return RefPtr(new RefCountedMutDoc(doc)); } -std::shared_ptr JsonManager::CopyDocument(yyjson_doc* doc) { +RefPtr JsonManager::CopyDocument(yyjson_doc* doc) { return WrapDocument(yyjson_doc_mut_copy(doc, nullptr)); } -std::shared_ptr JsonManager::CreateDocument() { +RefPtr JsonManager::CreateDocument() { return WrapDocument(yyjson_mut_doc_new(nullptr)); } -std::shared_ptr JsonManager::WrapImmutableDocument(yyjson_doc* doc) { - return std::shared_ptr(doc, [](yyjson_doc*){}); +RefPtr JsonManager::WrapImmutableDocument(yyjson_doc* doc) { + return RefPtr(new RefCountedImmutableDoc(doc)); } JsonManager::JsonManager(): m_randomGenerator(m_randomDevice()) {} @@ -97,7 +97,7 @@ JsonValue* JsonManager::ParseJSON(const char* json_str, bool is_file, bool is_mu if (is_mutable) { pJSONValue->m_pDocument_mut = CopyDocument(idoc); - pJSONValue->m_pVal_mut = yyjson_mut_doc_get_root(pJSONValue->m_pDocument_mut.get()); + pJSONValue->m_pVal_mut = yyjson_mut_doc_get_root(pJSONValue->m_pDocument_mut->get()); yyjson_doc_free(idoc); } else { pJSONValue->m_pDocument = WrapImmutableDocument(idoc); @@ -183,9 +183,9 @@ bool JsonManager::WriteToFile(JsonValue* handle, const char* path, yyjson_write_ bool is_success; if (handle->IsMutable()) { - is_success = yyjson_mut_write_file(realpath, handle->m_pDocument_mut.get(), write_flg, nullptr, &writeError); + is_success = yyjson_mut_write_file(realpath, handle->m_pDocument_mut->get(), write_flg, nullptr, &writeError); } else { - is_success = yyjson_write_file(realpath, handle->m_pDocument.get(), write_flg, nullptr, &writeError); + is_success = yyjson_write_file(realpath, handle->m_pDocument->get(), write_flg, nullptr, &writeError); } if (writeError.code && error && error_size > 0) { @@ -206,15 +206,15 @@ bool JsonManager::Equals(JsonValue* handle1, JsonValue* handle2) } if (!handle1->IsMutable() && !handle2->IsMutable()) { - auto doc1_mut = CopyDocument(handle1->m_pDocument.get()); - auto doc2_mut = CopyDocument(handle2->m_pDocument.get()); + auto doc1_mut = CopyDocument(handle1->m_pDocument->get()); + auto doc2_mut = CopyDocument(handle2->m_pDocument->get()); if (!doc1_mut || !doc2_mut) { return false; } - yyjson_mut_val* val1_mut = yyjson_mut_doc_get_root(doc1_mut.get()); - yyjson_mut_val* val2_mut = yyjson_mut_doc_get_root(doc2_mut.get()); + yyjson_mut_val* val1_mut = yyjson_mut_doc_get_root(doc1_mut->get()); + yyjson_mut_val* val2_mut = yyjson_mut_doc_get_root(doc2_mut->get()); if (!val1_mut || !val2_mut) { return false; @@ -226,12 +226,12 @@ bool JsonManager::Equals(JsonValue* handle1, JsonValue* handle2) JsonValue* immutable = handle1->IsMutable() ? handle2 : handle1; JsonValue* mutable_doc = handle1->IsMutable() ? handle1 : handle2; - auto doc_mut = CopyDocument(immutable->m_pDocument.get()); + auto doc_mut = CopyDocument(immutable->m_pDocument->get()); if (!doc_mut) { return false; } - yyjson_mut_val* val_mut = yyjson_mut_doc_get_root(doc_mut.get()); + yyjson_mut_val* val_mut = yyjson_mut_doc_get_root(doc_mut->get()); if (!val_mut) { return false; } @@ -265,16 +265,16 @@ JsonValue* JsonManager::DeepCopy(JsonValue* targetDoc, JsonValue* sourceValue) yyjson_mut_val* val_copy = nullptr; if (sourceValue->IsMutable()) { - val_copy = yyjson_mut_val_mut_copy(pJSONValue->m_pDocument_mut.get(), sourceValue->m_pVal_mut); + val_copy = yyjson_mut_val_mut_copy(pJSONValue->m_pDocument_mut->get(), sourceValue->m_pVal_mut); } else { - val_copy = yyjson_val_mut_copy(pJSONValue->m_pDocument_mut.get(), sourceValue->m_pVal); + val_copy = yyjson_val_mut_copy(pJSONValue->m_pDocument_mut->get(), sourceValue->m_pVal); } if (!val_copy) { return nullptr; } - yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut.get(), val_copy); + yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut->get(), val_copy); pJSONValue->m_pVal_mut = val_copy; } else { yyjson_mut_doc* temp_doc = yyjson_mut_doc_new(nullptr); @@ -353,8 +353,8 @@ JsonValue* JsonManager::ToMutable(JsonValue* handle) } auto pJSONValue = CreateWrapper(); - pJSONValue->m_pDocument_mut = CopyDocument(handle->m_pDocument.get()); - pJSONValue->m_pVal_mut = yyjson_mut_doc_get_root(pJSONValue->m_pDocument_mut.get()); + pJSONValue->m_pDocument_mut = CopyDocument(handle->m_pDocument->get()); + pJSONValue->m_pVal_mut = yyjson_mut_doc_get_root(pJSONValue->m_pDocument_mut->get()); return pJSONValue.release(); } @@ -366,9 +366,9 @@ JsonValue* JsonManager::ToImmutable(JsonValue* handle) } auto pJSONValue = CreateWrapper(); - yyjson_doc* mdoc = yyjson_mut_doc_imut_copy(handle->m_pDocument_mut.get(), nullptr); + yyjson_doc* mdoc = yyjson_mut_doc_imut_copy(handle->m_pDocument_mut->get(), nullptr); pJSONValue->m_pDocument = WrapImmutableDocument(mdoc); - pJSONValue->m_pVal = yyjson_doc_get_root(pJSONValue->m_pDocument.get()); + pJSONValue->m_pVal = yyjson_doc_get_root(pJSONValue->m_pDocument->get()); return pJSONValue.release(); } @@ -599,12 +599,20 @@ size_t JsonManager::GetReadSize(JsonValue* handle) return handle->m_readSize + 1; } +int JsonManager::GetRefCount(JsonValue* handle) +{ + if (!handle) { + return 0; + } + return handle->GetDocumentRefCount(); +} + JsonValue* JsonManager::ObjectInit() { auto pJSONValue = CreateWrapper(); pJSONValue->m_pDocument_mut = CreateDocument(); - pJSONValue->m_pVal_mut = yyjson_mut_obj(pJSONValue->m_pDocument_mut.get()); - yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut.get(), pJSONValue->m_pVal_mut); + pJSONValue->m_pVal_mut = yyjson_mut_obj(pJSONValue->m_pDocument_mut->get()); + yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut->get(), pJSONValue->m_pVal_mut); return pJSONValue.release(); } @@ -618,8 +626,8 @@ JsonValue* JsonManager::ObjectInitWithStrings(const char** pairs, size_t count) auto pJSONValue = CreateWrapper(); pJSONValue->m_pDocument_mut = CreateDocument(); - pJSONValue->m_pVal_mut = yyjson_mut_obj_with_kv( - pJSONValue->m_pDocument_mut.get(), + pJSONValue->m_pVal_mut = yyjson_mut_obj_with_kv( + pJSONValue->m_pDocument_mut->get(), pairs, count ); @@ -1022,14 +1030,14 @@ bool JsonManager::ObjectHasKey(JsonValue* handle, const char* key, bool use_poin if (handle->IsMutable()) { if (use_pointer) { - return yyjson_mut_doc_ptr_get(handle->m_pDocument_mut.get(), key) != nullptr; + return yyjson_mut_doc_ptr_get(handle->m_pDocument_mut->get(), key) != nullptr; } else { yyjson_mut_obj_iter iter = yyjson_mut_obj_iter_with(handle->m_pVal_mut); return yyjson_mut_obj_iter_get(&iter, key) != nullptr; } } else { if (use_pointer) { - return yyjson_doc_ptr_get(handle->m_pDocument.get(), key) != nullptr; + return yyjson_doc_ptr_get(handle->m_pDocument->get(), key) != nullptr; } else { yyjson_obj_iter iter = yyjson_obj_iter_with(handle->m_pVal); return yyjson_obj_iter_get(&iter, key) != nullptr; @@ -1051,7 +1059,7 @@ bool JsonManager::ObjectRenameKey(JsonValue* handle, const char* old_key, const return false; } - return yyjson_mut_obj_rename_key(handle->m_pDocument_mut.get(), handle->m_pVal_mut, old_key, new_key); + return yyjson_mut_obj_rename_key(handle->m_pDocument_mut->get(), handle->m_pVal_mut, old_key, new_key); } bool JsonManager::ObjectSet(JsonValue* handle, const char* key, JsonValue* value) @@ -1062,16 +1070,16 @@ bool JsonManager::ObjectSet(JsonValue* handle, const char* key, JsonValue* value yyjson_mut_val* val_copy = nullptr; if (value->IsMutable()) { - val_copy = yyjson_mut_val_mut_copy(handle->m_pDocument_mut.get(), value->m_pVal_mut); + val_copy = yyjson_mut_val_mut_copy(handle->m_pDocument_mut->get(), value->m_pVal_mut); } else { - val_copy = yyjson_val_mut_copy(handle->m_pDocument_mut.get(), value->m_pVal); + val_copy = yyjson_val_mut_copy(handle->m_pDocument_mut->get(), value->m_pVal); } if (!val_copy) { return false; } - return yyjson_mut_obj_put(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut.get(), key), val_copy); + return yyjson_mut_obj_put(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut->get(), key), val_copy); } bool JsonManager::ObjectSetBool(JsonValue* handle, const char* key, bool value) @@ -1080,7 +1088,7 @@ bool JsonManager::ObjectSetBool(JsonValue* handle, const char* key, bool value) return false; } - return yyjson_mut_obj_put(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut.get(), key), yyjson_mut_bool(handle->m_pDocument_mut.get(), value)); + return yyjson_mut_obj_put(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut->get(), key), yyjson_mut_bool(handle->m_pDocument_mut->get(), value)); } bool JsonManager::ObjectSetFloat(JsonValue* handle, const char* key, double value) @@ -1089,7 +1097,7 @@ bool JsonManager::ObjectSetFloat(JsonValue* handle, const char* key, double valu return false; } - return yyjson_mut_obj_put(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut.get(), key), yyjson_mut_real(handle->m_pDocument_mut.get(), value)); + return yyjson_mut_obj_put(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut->get(), key), yyjson_mut_real(handle->m_pDocument_mut->get(), value)); } bool JsonManager::ObjectSetInt(JsonValue* handle, const char* key, int value) @@ -1098,7 +1106,7 @@ bool JsonManager::ObjectSetInt(JsonValue* handle, const char* key, int value) return false; } - return yyjson_mut_obj_put(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut.get(), key), yyjson_mut_int(handle->m_pDocument_mut.get(), value)); + return yyjson_mut_obj_put(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut->get(), key), yyjson_mut_int(handle->m_pDocument_mut->get(), value)); } bool JsonManager::ObjectSetInt64(JsonValue* handle, const char* key, std::variant value) @@ -1110,9 +1118,9 @@ bool JsonManager::ObjectSetInt64(JsonValue* handle, const char* key, std::varian return std::visit([&](auto&& val) -> bool { using T = std::decay_t; if constexpr (std::is_same_v) { - return yyjson_mut_obj_put(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut.get(), key), yyjson_mut_sint(handle->m_pDocument_mut.get(), val)); + return yyjson_mut_obj_put(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut->get(), key), yyjson_mut_sint(handle->m_pDocument_mut->get(), val)); } else if constexpr (std::is_same_v) { - return yyjson_mut_obj_put(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut.get(), key), yyjson_mut_uint(handle->m_pDocument_mut.get(), val)); + return yyjson_mut_obj_put(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut->get(), key), yyjson_mut_uint(handle->m_pDocument_mut->get(), val)); } return false; }, value); @@ -1124,7 +1132,7 @@ bool JsonManager::ObjectSetNull(JsonValue* handle, const char* key) return false; } - return yyjson_mut_obj_put(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut.get(), key), yyjson_mut_null(handle->m_pDocument_mut.get())); + return yyjson_mut_obj_put(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut->get(), key), yyjson_mut_null(handle->m_pDocument_mut->get())); } bool JsonManager::ObjectSetString(JsonValue* handle, const char* key, const char* value) @@ -1133,7 +1141,7 @@ bool JsonManager::ObjectSetString(JsonValue* handle, const char* key, const char return false; } - return yyjson_mut_obj_put(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut.get(), key), yyjson_mut_strcpy(handle->m_pDocument_mut.get(), value)); + return yyjson_mut_obj_put(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut->get(), key), yyjson_mut_strcpy(handle->m_pDocument_mut->get(), value)); } bool JsonManager::ObjectRemove(JsonValue* handle, const char* key) @@ -1217,8 +1225,8 @@ JsonValue* JsonManager::ArrayInit() { auto pJSONValue = CreateWrapper(); pJSONValue->m_pDocument_mut = CreateDocument(); - pJSONValue->m_pVal_mut = yyjson_mut_arr(pJSONValue->m_pDocument_mut.get()); - yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut.get(), pJSONValue->m_pVal_mut); + pJSONValue->m_pVal_mut = yyjson_mut_arr(pJSONValue->m_pDocument_mut->get()); + yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut->get(), pJSONValue->m_pVal_mut); return pJSONValue.release(); } @@ -1232,8 +1240,8 @@ JsonValue* JsonManager::ArrayInitWithStrings(const char** strings, size_t count) auto pJSONValue = CreateWrapper(); pJSONValue->m_pDocument_mut = CreateDocument(); - pJSONValue->m_pVal_mut = yyjson_mut_arr_with_strcpy( - pJSONValue->m_pDocument_mut.get(), + pJSONValue->m_pVal_mut = yyjson_mut_arr_with_strcpy( + pJSONValue->m_pDocument_mut->get(), strings, count ); @@ -1254,8 +1262,8 @@ JsonValue* JsonManager::ArrayInitWithInt32(const int32_t* values, size_t count) auto pJSONValue = CreateWrapper(); pJSONValue->m_pDocument_mut = CreateDocument(); - pJSONValue->m_pVal_mut = yyjson_mut_arr_with_sint32( - pJSONValue->m_pDocument_mut.get(), + pJSONValue->m_pVal_mut = yyjson_mut_arr_with_sint32( + pJSONValue->m_pDocument_mut->get(), values, count ); @@ -1279,13 +1287,13 @@ JsonValue* JsonManager::ArrayInitWithInt64(const char** values, size_t count, ch if (count == 0) { auto pJSONValue = CreateWrapper(); pJSONValue->m_pDocument_mut = CreateDocument(); - pJSONValue->m_pVal_mut = yyjson_mut_arr(pJSONValue->m_pDocument_mut.get()); + pJSONValue->m_pVal_mut = yyjson_mut_arr(pJSONValue->m_pDocument_mut->get()); return pJSONValue.release(); } auto pJSONValue = CreateWrapper(); pJSONValue->m_pDocument_mut = CreateDocument(); - auto doc = pJSONValue->m_pDocument_mut.get(); + auto doc = pJSONValue->m_pDocument_mut->get(); pJSONValue->m_pVal_mut = yyjson_mut_arr(doc); if (!pJSONValue->m_pVal_mut) { @@ -1331,8 +1339,8 @@ JsonValue* JsonManager::ArrayInitWithBool(const bool* values, size_t count) auto pJSONValue = CreateWrapper(); pJSONValue->m_pDocument_mut = CreateDocument(); - pJSONValue->m_pVal_mut = yyjson_mut_arr_with_bool( - pJSONValue->m_pDocument_mut.get(), + pJSONValue->m_pVal_mut = yyjson_mut_arr_with_bool( + pJSONValue->m_pDocument_mut->get(), values, count ); @@ -1353,8 +1361,8 @@ JsonValue* JsonManager::ArrayInitWithFloat(const double* values, size_t count) auto pJSONValue = CreateWrapper(); pJSONValue->m_pDocument_mut = CreateDocument(); - pJSONValue->m_pVal_mut = yyjson_mut_arr_with_real( - pJSONValue->m_pDocument_mut.get(), + pJSONValue->m_pVal_mut = yyjson_mut_arr_with_real( + pJSONValue->m_pDocument_mut->get(), values, count ); @@ -1803,9 +1811,9 @@ bool JsonManager::ArrayReplace(JsonValue* handle, size_t index, JsonValue* value yyjson_mut_val* val_copy = nullptr; if (value->IsMutable()) { - val_copy = yyjson_mut_val_mut_copy(handle->m_pDocument_mut.get(), value->m_pVal_mut); + val_copy = yyjson_mut_val_mut_copy(handle->m_pDocument_mut->get(), value->m_pVal_mut); } else { - val_copy = yyjson_val_mut_copy(handle->m_pDocument_mut.get(), value->m_pVal); + val_copy = yyjson_val_mut_copy(handle->m_pDocument_mut->get(), value->m_pVal); } if (!val_copy) { @@ -1826,7 +1834,7 @@ bool JsonManager::ArrayReplaceBool(JsonValue* handle, size_t index, bool value) return false; } - return yyjson_mut_arr_replace(handle->m_pVal_mut, index, yyjson_mut_bool(handle->m_pDocument_mut.get(), value)) != nullptr; + return yyjson_mut_arr_replace(handle->m_pVal_mut, index, yyjson_mut_bool(handle->m_pDocument_mut->get(), value)) != nullptr; } bool JsonManager::ArrayReplaceFloat(JsonValue* handle, size_t index, double value) @@ -1840,7 +1848,7 @@ bool JsonManager::ArrayReplaceFloat(JsonValue* handle, size_t index, double valu return false; } - return yyjson_mut_arr_replace(handle->m_pVal_mut, index, yyjson_mut_real(handle->m_pDocument_mut.get(), value)) != nullptr; + return yyjson_mut_arr_replace(handle->m_pVal_mut, index, yyjson_mut_real(handle->m_pDocument_mut->get(), value)) != nullptr; } bool JsonManager::ArrayReplaceInt(JsonValue* handle, size_t index, int value) @@ -1854,7 +1862,7 @@ bool JsonManager::ArrayReplaceInt(JsonValue* handle, size_t index, int value) return false; } - return yyjson_mut_arr_replace(handle->m_pVal_mut, index, yyjson_mut_int(handle->m_pDocument_mut.get(), value)) != nullptr; + return yyjson_mut_arr_replace(handle->m_pVal_mut, index, yyjson_mut_int(handle->m_pDocument_mut->get(), value)) != nullptr; } bool JsonManager::ArrayReplaceInt64(JsonValue* handle, size_t index, std::variant value) @@ -1871,9 +1879,9 @@ bool JsonManager::ArrayReplaceInt64(JsonValue* handle, size_t index, std::varian return std::visit([&](auto&& val) -> bool { using T = std::decay_t; if constexpr (std::is_same_v) { - return yyjson_mut_arr_replace(handle->m_pVal_mut, index, yyjson_mut_sint(handle->m_pDocument_mut.get(), val)) != nullptr; + return yyjson_mut_arr_replace(handle->m_pVal_mut, index, yyjson_mut_sint(handle->m_pDocument_mut->get(), val)) != nullptr; } else if constexpr (std::is_same_v) { - return yyjson_mut_arr_replace(handle->m_pVal_mut, index, yyjson_mut_uint(handle->m_pDocument_mut.get(), val)) != nullptr; + return yyjson_mut_arr_replace(handle->m_pVal_mut, index, yyjson_mut_uint(handle->m_pDocument_mut->get(), val)) != nullptr; } return false; }, value); @@ -1890,7 +1898,7 @@ bool JsonManager::ArrayReplaceNull(JsonValue* handle, size_t index) return false; } - return yyjson_mut_arr_replace(handle->m_pVal_mut, index, yyjson_mut_null(handle->m_pDocument_mut.get())) != nullptr; + return yyjson_mut_arr_replace(handle->m_pVal_mut, index, yyjson_mut_null(handle->m_pDocument_mut->get())) != nullptr; } bool JsonManager::ArrayReplaceString(JsonValue* handle, size_t index, const char* value) @@ -1904,7 +1912,7 @@ bool JsonManager::ArrayReplaceString(JsonValue* handle, size_t index, const char return false; } - return yyjson_mut_arr_replace(handle->m_pVal_mut, index, yyjson_mut_strcpy(handle->m_pDocument_mut.get(), value)) != nullptr; + return yyjson_mut_arr_replace(handle->m_pVal_mut, index, yyjson_mut_strcpy(handle->m_pDocument_mut->get(), value)) != nullptr; } bool JsonManager::ArrayAppend(JsonValue* handle, JsonValue* value) @@ -1915,9 +1923,9 @@ bool JsonManager::ArrayAppend(JsonValue* handle, JsonValue* value) yyjson_mut_val* val_copy = nullptr; if (value->IsMutable()) { - val_copy = yyjson_mut_val_mut_copy(handle->m_pDocument_mut.get(), value->m_pVal_mut); + val_copy = yyjson_mut_val_mut_copy(handle->m_pDocument_mut->get(), value->m_pVal_mut); } else { - val_copy = yyjson_val_mut_copy(handle->m_pDocument_mut.get(), value->m_pVal); + val_copy = yyjson_val_mut_copy(handle->m_pDocument_mut->get(), value->m_pVal); } if (!val_copy) { @@ -1933,7 +1941,7 @@ bool JsonManager::ArrayAppendBool(JsonValue* handle, bool value) return false; } - return yyjson_mut_arr_append(handle->m_pVal_mut, yyjson_mut_bool(handle->m_pDocument_mut.get(), value)); + return yyjson_mut_arr_append(handle->m_pVal_mut, yyjson_mut_bool(handle->m_pDocument_mut->get(), value)); } bool JsonManager::ArrayAppendFloat(JsonValue* handle, double value) @@ -1942,7 +1950,7 @@ bool JsonManager::ArrayAppendFloat(JsonValue* handle, double value) return false; } - return yyjson_mut_arr_append(handle->m_pVal_mut, yyjson_mut_real(handle->m_pDocument_mut.get(), value)); + return yyjson_mut_arr_append(handle->m_pVal_mut, yyjson_mut_real(handle->m_pDocument_mut->get(), value)); } bool JsonManager::ArrayAppendInt(JsonValue* handle, int value) @@ -1951,7 +1959,7 @@ bool JsonManager::ArrayAppendInt(JsonValue* handle, int value) return false; } - return yyjson_mut_arr_append(handle->m_pVal_mut, yyjson_mut_int(handle->m_pDocument_mut.get(), value)); + return yyjson_mut_arr_append(handle->m_pVal_mut, yyjson_mut_int(handle->m_pDocument_mut->get(), value)); } bool JsonManager::ArrayAppendInt64(JsonValue* handle, std::variant value) @@ -1963,9 +1971,9 @@ bool JsonManager::ArrayAppendInt64(JsonValue* handle, std::variant bool { using T = std::decay_t; if constexpr (std::is_same_v) { - return yyjson_mut_arr_append(handle->m_pVal_mut, yyjson_mut_sint(handle->m_pDocument_mut.get(), val)); + return yyjson_mut_arr_append(handle->m_pVal_mut, yyjson_mut_sint(handle->m_pDocument_mut->get(), val)); } else if constexpr (std::is_same_v) { - return yyjson_mut_arr_append(handle->m_pVal_mut, yyjson_mut_uint(handle->m_pDocument_mut.get(), val)); + return yyjson_mut_arr_append(handle->m_pVal_mut, yyjson_mut_uint(handle->m_pDocument_mut->get(), val)); } return false; }, value); @@ -1977,7 +1985,7 @@ bool JsonManager::ArrayAppendNull(JsonValue* handle) return false; } - return yyjson_mut_arr_append(handle->m_pVal_mut, yyjson_mut_null(handle->m_pDocument_mut.get())); + return yyjson_mut_arr_append(handle->m_pVal_mut, yyjson_mut_null(handle->m_pDocument_mut->get())); } bool JsonManager::ArrayAppendString(JsonValue* handle, const char* value) @@ -1986,7 +1994,7 @@ bool JsonManager::ArrayAppendString(JsonValue* handle, const char* value) return false; } - return yyjson_mut_arr_append(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut.get(), value)); + return yyjson_mut_arr_append(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut->get(), value)); } bool JsonManager::ArrayInsert(JsonValue* handle, size_t index, JsonValue* value) @@ -2004,7 +2012,7 @@ bool JsonManager::ArrayInsertBool(JsonValue* handle, size_t index, bool value) return false; } - return yyjson_mut_arr_insert(handle->m_pVal_mut, yyjson_mut_bool(handle->m_pDocument_mut.get(), value), index); + return yyjson_mut_arr_insert(handle->m_pVal_mut, yyjson_mut_bool(handle->m_pDocument_mut->get(), value), index); } bool JsonManager::ArrayInsertInt(JsonValue* handle, size_t index, int value) @@ -2013,7 +2021,7 @@ bool JsonManager::ArrayInsertInt(JsonValue* handle, size_t index, int value) return false; } - return yyjson_mut_arr_insert(handle->m_pVal_mut, yyjson_mut_sint(handle->m_pDocument_mut.get(), value), index); + return yyjson_mut_arr_insert(handle->m_pVal_mut, yyjson_mut_sint(handle->m_pDocument_mut->get(), value), index); } bool JsonManager::ArrayInsertInt64(JsonValue* handle, size_t index, std::variant value) @@ -2026,9 +2034,9 @@ bool JsonManager::ArrayInsertInt64(JsonValue* handle, size_t index, std::variant std::visit([&](auto&& arg) { using T = std::decay_t; if constexpr (std::is_same_v) { - val = yyjson_mut_sint(handle->m_pDocument_mut.get(), arg); + val = yyjson_mut_sint(handle->m_pDocument_mut->get(), arg); } else if constexpr (std::is_same_v) { - val = yyjson_mut_uint(handle->m_pDocument_mut.get(), arg); + val = yyjson_mut_uint(handle->m_pDocument_mut->get(), arg); } }, value); @@ -2045,7 +2053,7 @@ bool JsonManager::ArrayInsertFloat(JsonValue* handle, size_t index, double value return false; } - return yyjson_mut_arr_insert(handle->m_pVal_mut, yyjson_mut_real(handle->m_pDocument_mut.get(), value), index); + return yyjson_mut_arr_insert(handle->m_pVal_mut, yyjson_mut_real(handle->m_pDocument_mut->get(), value), index); } bool JsonManager::ArrayInsertString(JsonValue* handle, size_t index, const char* value) @@ -2054,7 +2062,7 @@ bool JsonManager::ArrayInsertString(JsonValue* handle, size_t index, const char* return false; } - return yyjson_mut_arr_insert(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut.get(), value), index); + return yyjson_mut_arr_insert(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut->get(), value), index); } bool JsonManager::ArrayInsertNull(JsonValue* handle, size_t index) @@ -2063,7 +2071,7 @@ bool JsonManager::ArrayInsertNull(JsonValue* handle, size_t index) return false; } - return yyjson_mut_arr_insert(handle->m_pVal_mut, yyjson_mut_null(handle->m_pDocument_mut.get()), index); + return yyjson_mut_arr_insert(handle->m_pVal_mut, yyjson_mut_null(handle->m_pDocument_mut->get()), index); } bool JsonManager::ArrayPrepend(JsonValue* handle, JsonValue* value) @@ -2081,7 +2089,7 @@ bool JsonManager::ArrayPrependBool(JsonValue* handle, bool value) return false; } - return yyjson_mut_arr_prepend(handle->m_pVal_mut, yyjson_mut_bool(handle->m_pDocument_mut.get(), value)); + return yyjson_mut_arr_prepend(handle->m_pVal_mut, yyjson_mut_bool(handle->m_pDocument_mut->get(), value)); } bool JsonManager::ArrayPrependInt(JsonValue* handle, int value) @@ -2090,7 +2098,7 @@ bool JsonManager::ArrayPrependInt(JsonValue* handle, int value) return false; } - return yyjson_mut_arr_prepend(handle->m_pVal_mut, yyjson_mut_sint(handle->m_pDocument_mut.get(), value)); + return yyjson_mut_arr_prepend(handle->m_pVal_mut, yyjson_mut_sint(handle->m_pDocument_mut->get(), value)); } bool JsonManager::ArrayPrependInt64(JsonValue* handle, std::variant value) @@ -2103,9 +2111,9 @@ bool JsonManager::ArrayPrependInt64(JsonValue* handle, std::variant; if constexpr (std::is_same_v) { - val = yyjson_mut_sint(handle->m_pDocument_mut.get(), arg); + val = yyjson_mut_sint(handle->m_pDocument_mut->get(), arg); } else if constexpr (std::is_same_v) { - val = yyjson_mut_uint(handle->m_pDocument_mut.get(), arg); + val = yyjson_mut_uint(handle->m_pDocument_mut->get(), arg); } }, value); @@ -2122,7 +2130,7 @@ bool JsonManager::ArrayPrependFloat(JsonValue* handle, double value) return false; } - return yyjson_mut_arr_prepend(handle->m_pVal_mut, yyjson_mut_real(handle->m_pDocument_mut.get(), value)); + return yyjson_mut_arr_prepend(handle->m_pVal_mut, yyjson_mut_real(handle->m_pDocument_mut->get(), value)); } bool JsonManager::ArrayPrependString(JsonValue* handle, const char* value) @@ -2131,7 +2139,7 @@ bool JsonManager::ArrayPrependString(JsonValue* handle, const char* value) return false; } - return yyjson_mut_arr_prepend(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut.get(), value)); + return yyjson_mut_arr_prepend(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut->get(), value)); } bool JsonManager::ArrayPrependNull(JsonValue* handle) @@ -2140,7 +2148,7 @@ bool JsonManager::ArrayPrependNull(JsonValue* handle) return false; } - return yyjson_mut_arr_prepend(handle->m_pVal_mut, yyjson_mut_null(handle->m_pDocument_mut.get())); + return yyjson_mut_arr_prepend(handle->m_pVal_mut, yyjson_mut_null(handle->m_pDocument_mut->get())); } bool JsonManager::ArrayRemove(JsonValue* handle, size_t index) @@ -2744,14 +2752,14 @@ JsonValue* JsonManager::Pack(const char* format, IPackParamProvider* param_provi } const char* end_ptr = nullptr; - pJSONValue->m_pVal_mut = PackImpl(pJSONValue->m_pDocument_mut.get(), format, + pJSONValue->m_pVal_mut = PackImpl(pJSONValue->m_pDocument_mut->get(), format, param_provider, error, error_size, &end_ptr); if (!pJSONValue->m_pVal_mut) { return nullptr; } - yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut.get(), pJSONValue->m_pVal_mut); + yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut->get(), pJSONValue->m_pVal_mut); return pJSONValue.release(); } @@ -2760,7 +2768,7 @@ JsonValue* JsonManager::CreateBool(bool value) { auto pJSONValue = CreateWrapper(); pJSONValue->m_pDocument_mut = CreateDocument(); - pJSONValue->m_pVal_mut = yyjson_mut_bool(pJSONValue->m_pDocument_mut.get(), value); + pJSONValue->m_pVal_mut = yyjson_mut_bool(pJSONValue->m_pDocument_mut->get(), value); if (!pJSONValue->m_pVal_mut) { return nullptr; @@ -2773,7 +2781,7 @@ JsonValue* JsonManager::CreateFloat(double value) { auto pJSONValue = CreateWrapper(); pJSONValue->m_pDocument_mut = CreateDocument(); - pJSONValue->m_pVal_mut = yyjson_mut_real(pJSONValue->m_pDocument_mut.get(), value); + pJSONValue->m_pVal_mut = yyjson_mut_real(pJSONValue->m_pDocument_mut->get(), value); if (!pJSONValue->m_pVal_mut) { return nullptr; @@ -2786,7 +2794,7 @@ JsonValue* JsonManager::CreateInt(int value) { auto pJSONValue = CreateWrapper(); pJSONValue->m_pDocument_mut = CreateDocument(); - pJSONValue->m_pVal_mut = yyjson_mut_int(pJSONValue->m_pDocument_mut.get(), value); + pJSONValue->m_pVal_mut = yyjson_mut_int(pJSONValue->m_pDocument_mut->get(), value); if (!pJSONValue->m_pVal_mut) { return nullptr; @@ -2803,9 +2811,9 @@ JsonValue* JsonManager::CreateInt64(std::variant value) std::visit([&](auto&& val) { using T = std::decay_t; if constexpr (std::is_same_v) { - pJSONValue->m_pVal_mut = yyjson_mut_sint(pJSONValue->m_pDocument_mut.get(), val); + pJSONValue->m_pVal_mut = yyjson_mut_sint(pJSONValue->m_pDocument_mut->get(), val); } else if constexpr (std::is_same_v) { - pJSONValue->m_pVal_mut = yyjson_mut_uint(pJSONValue->m_pDocument_mut.get(), val); + pJSONValue->m_pVal_mut = yyjson_mut_uint(pJSONValue->m_pDocument_mut->get(), val); } }, value); @@ -2820,7 +2828,7 @@ JsonValue* JsonManager::CreateNull() { auto pJSONValue = CreateWrapper(); pJSONValue->m_pDocument_mut = CreateDocument(); - pJSONValue->m_pVal_mut = yyjson_mut_null(pJSONValue->m_pDocument_mut.get()); + pJSONValue->m_pVal_mut = yyjson_mut_null(pJSONValue->m_pDocument_mut->get()); if (!pJSONValue->m_pVal_mut) { return nullptr; @@ -2837,7 +2845,7 @@ JsonValue* JsonManager::CreateString(const char* value) auto pJSONValue = CreateWrapper(); pJSONValue->m_pDocument_mut = CreateDocument(); - pJSONValue->m_pVal_mut = yyjson_mut_strcpy(pJSONValue->m_pDocument_mut.get(), value); + pJSONValue->m_pVal_mut = yyjson_mut_strcpy(pJSONValue->m_pDocument_mut->get(), value); if (!pJSONValue->m_pVal_mut) { return nullptr; @@ -2970,7 +2978,7 @@ JsonValue* JsonManager::PtrGet(JsonValue* handle, const char* path, char* error, yyjson_ptr_err ptrGetError; if (handle->IsMutable()) { - yyjson_mut_val* val = yyjson_mut_doc_ptr_getx(handle->m_pDocument_mut.get(), path, strlen(path), nullptr, &ptrGetError); + yyjson_mut_val* val = yyjson_mut_doc_ptr_getx(handle->m_pDocument_mut->get(), path, strlen(path), nullptr, &ptrGetError); if (!val || ptrGetError.code) { if (error && error_size > 0) { @@ -2983,7 +2991,7 @@ JsonValue* JsonManager::PtrGet(JsonValue* handle, const char* path, char* error, pJSONValue->m_pDocument_mut = handle->m_pDocument_mut; pJSONValue->m_pVal_mut = val; } else { - yyjson_val* val = yyjson_doc_ptr_getx(handle->m_pDocument.get(), path, strlen(path), &ptrGetError); + yyjson_val* val = yyjson_doc_ptr_getx(handle->m_pDocument->get(), path, strlen(path), &ptrGetError); if (!val || ptrGetError.code) { if (error && error_size > 0) { @@ -3012,7 +3020,7 @@ bool JsonManager::PtrGetBool(JsonValue* handle, const char* path, bool* out_valu yyjson_ptr_err ptrGetError; if (handle->IsMutable()) { - yyjson_mut_val* val = yyjson_mut_doc_ptr_getx(handle->m_pDocument_mut.get(), path, strlen(path), nullptr, &ptrGetError); + yyjson_mut_val* val = yyjson_mut_doc_ptr_getx(handle->m_pDocument_mut->get(), path, strlen(path), nullptr, &ptrGetError); if (ptrGetError.code) { if (error && error_size > 0) { @@ -3032,7 +3040,7 @@ bool JsonManager::PtrGetBool(JsonValue* handle, const char* path, bool* out_valu *out_value = yyjson_mut_get_bool(val); return true; } else { - yyjson_val* val = yyjson_doc_ptr_getx(handle->m_pDocument.get(), path, strlen(path), &ptrGetError); + yyjson_val* val = yyjson_doc_ptr_getx(handle->m_pDocument->get(), path, strlen(path), &ptrGetError); if (ptrGetError.code) { if (error && error_size > 0) { @@ -3066,7 +3074,7 @@ bool JsonManager::PtrGetFloat(JsonValue* handle, const char* path, double* out_v yyjson_ptr_err ptrGetError; if (handle->IsMutable()) { - yyjson_mut_val* val = yyjson_mut_doc_ptr_getx(handle->m_pDocument_mut.get(), path, strlen(path), nullptr, &ptrGetError); + yyjson_mut_val* val = yyjson_mut_doc_ptr_getx(handle->m_pDocument_mut->get(), path, strlen(path), nullptr, &ptrGetError); if (ptrGetError.code) { if (error && error_size > 0) { @@ -3086,7 +3094,7 @@ bool JsonManager::PtrGetFloat(JsonValue* handle, const char* path, double* out_v *out_value = yyjson_mut_get_real(val); return true; } else { - yyjson_val* val = yyjson_doc_ptr_getx(handle->m_pDocument.get(), path, strlen(path), &ptrGetError); + yyjson_val* val = yyjson_doc_ptr_getx(handle->m_pDocument->get(), path, strlen(path), &ptrGetError); if (ptrGetError.code) { if (error && error_size > 0) { @@ -3120,7 +3128,7 @@ bool JsonManager::PtrGetInt(JsonValue* handle, const char* path, int* out_value, yyjson_ptr_err ptrGetError; if (handle->IsMutable()) { - yyjson_mut_val* val = yyjson_mut_doc_ptr_getx(handle->m_pDocument_mut.get(), path, strlen(path), nullptr, &ptrGetError); + yyjson_mut_val* val = yyjson_mut_doc_ptr_getx(handle->m_pDocument_mut->get(), path, strlen(path), nullptr, &ptrGetError); if (ptrGetError.code) { if (error && error_size > 0) { @@ -3140,7 +3148,7 @@ bool JsonManager::PtrGetInt(JsonValue* handle, const char* path, int* out_value, *out_value = yyjson_mut_get_int(val); return true; } else { - yyjson_val* val = yyjson_doc_ptr_getx(handle->m_pDocument.get(), path, strlen(path), &ptrGetError); + yyjson_val* val = yyjson_doc_ptr_getx(handle->m_pDocument->get(), path, strlen(path), &ptrGetError); if (ptrGetError.code) { if (error && error_size > 0) { @@ -3174,7 +3182,7 @@ bool JsonManager::PtrGetInt64(JsonValue* handle, const char* path, std::variant< yyjson_ptr_err ptrGetError; if (handle->IsMutable()) { - yyjson_mut_val* val = yyjson_mut_doc_ptr_getx(handle->m_pDocument_mut.get(), path, strlen(path), nullptr, &ptrGetError); + yyjson_mut_val* val = yyjson_mut_doc_ptr_getx(handle->m_pDocument_mut->get(), path, strlen(path), nullptr, &ptrGetError); if (!val || ptrGetError.code) { if (error && error_size > 0) { @@ -3194,7 +3202,7 @@ bool JsonManager::PtrGetInt64(JsonValue* handle, const char* path, std::variant< ReadInt64FromMutVal(val, out_value); return true; } else { - yyjson_val* val = yyjson_doc_ptr_getx(handle->m_pDocument.get(), path, strlen(path), &ptrGetError); + yyjson_val* val = yyjson_doc_ptr_getx(handle->m_pDocument->get(), path, strlen(path), &ptrGetError); if (!val || ptrGetError.code) { if (error && error_size > 0) { @@ -3228,7 +3236,7 @@ bool JsonManager::PtrGetString(JsonValue* handle, const char* path, const char** yyjson_ptr_err ptrGetError; if (handle->IsMutable()) { - yyjson_mut_val* val = yyjson_mut_doc_ptr_getx(handle->m_pDocument_mut.get(), path, strlen(path), nullptr, &ptrGetError); + yyjson_mut_val* val = yyjson_mut_doc_ptr_getx(handle->m_pDocument_mut->get(), path, strlen(path), nullptr, &ptrGetError); if (ptrGetError.code) { if (error && error_size > 0) { @@ -3251,7 +3259,7 @@ bool JsonManager::PtrGetString(JsonValue* handle, const char* path, const char** } return true; } else { - yyjson_val* val = yyjson_doc_ptr_getx(handle->m_pDocument.get(), path, strlen(path), &ptrGetError); + yyjson_val* val = yyjson_doc_ptr_getx(handle->m_pDocument->get(), path, strlen(path), &ptrGetError); if (ptrGetError.code) { if (error && error_size > 0) { @@ -3288,7 +3296,7 @@ bool JsonManager::PtrGetIsNull(JsonValue* handle, const char* path, bool* out_is yyjson_ptr_err ptrGetError; if (handle->IsMutable()) { - yyjson_mut_val* val = yyjson_mut_doc_ptr_getx(handle->m_pDocument_mut.get(), path, strlen(path), nullptr, &ptrGetError); + yyjson_mut_val* val = yyjson_mut_doc_ptr_getx(handle->m_pDocument_mut->get(), path, strlen(path), nullptr, &ptrGetError); if (ptrGetError.code) { if (error && error_size > 0) { @@ -3301,7 +3309,7 @@ bool JsonManager::PtrGetIsNull(JsonValue* handle, const char* path, bool* out_is *out_is_null = yyjson_mut_is_null(val); return true; } else { - yyjson_val* val = yyjson_doc_ptr_getx(handle->m_pDocument.get(), path, strlen(path), &ptrGetError); + yyjson_val* val = yyjson_doc_ptr_getx(handle->m_pDocument->get(), path, strlen(path), &ptrGetError); if (ptrGetError.code) { if (error && error_size > 0) { @@ -3328,7 +3336,7 @@ bool JsonManager::PtrGetLength(JsonValue* handle, const char* path, size_t* out_ yyjson_ptr_err ptrGetError; if (handle->IsMutable()) { - yyjson_mut_val* val = yyjson_mut_doc_ptr_getx(handle->m_pDocument_mut.get(), path, strlen(path), nullptr, &ptrGetError); + yyjson_mut_val* val = yyjson_mut_doc_ptr_getx(handle->m_pDocument_mut->get(), path, strlen(path), nullptr, &ptrGetError); if (ptrGetError.code) { if (error && error_size > 0) { @@ -3345,7 +3353,7 @@ bool JsonManager::PtrGetLength(JsonValue* handle, const char* path, size_t* out_ } return true; } else { - yyjson_val* val = yyjson_doc_ptr_getx(handle->m_pDocument.get(), path, strlen(path), &ptrGetError); + yyjson_val* val = yyjson_doc_ptr_getx(handle->m_pDocument->get(), path, strlen(path), &ptrGetError); if (ptrGetError.code) { if (error && error_size > 0) { @@ -3375,9 +3383,9 @@ bool JsonManager::PtrSet(JsonValue* handle, const char* path, JsonValue* value, yyjson_mut_val* val_copy = nullptr; if (value->IsMutable()) { - val_copy = yyjson_mut_val_mut_copy(handle->m_pDocument_mut.get(), value->m_pVal_mut); + val_copy = yyjson_mut_val_mut_copy(handle->m_pDocument_mut->get(), value->m_pVal_mut); } else { - val_copy = yyjson_val_mut_copy(handle->m_pDocument_mut.get(), value->m_pVal); + val_copy = yyjson_val_mut_copy(handle->m_pDocument_mut->get(), value->m_pVal); } if (!val_copy) { @@ -3388,7 +3396,7 @@ bool JsonManager::PtrSet(JsonValue* handle, const char* path, JsonValue* value, } yyjson_ptr_err ptrSetError; - bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut.get(), path, strlen(path), val_copy, true, nullptr, &ptrSetError); + bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut->get(), path, strlen(path), val_copy, true, nullptr, &ptrSetError); if (ptrSetError.code && error && error_size > 0) { SetErrorSafe(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", @@ -3408,7 +3416,7 @@ bool JsonManager::PtrSetBool(JsonValue* handle, const char* path, bool value, ch } yyjson_ptr_err ptrSetError; - bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_bool(handle->m_pDocument_mut.get(), value), true, nullptr, &ptrSetError); + bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut->get(), path, strlen(path), yyjson_mut_bool(handle->m_pDocument_mut->get(), value), true, nullptr, &ptrSetError); if (ptrSetError.code && error && error_size > 0) { SetErrorSafe(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", @@ -3428,7 +3436,7 @@ bool JsonManager::PtrSetFloat(JsonValue* handle, const char* path, double value, } yyjson_ptr_err ptrSetError; - bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_real(handle->m_pDocument_mut.get(), value), true, nullptr, &ptrSetError); + bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut->get(), path, strlen(path), yyjson_mut_real(handle->m_pDocument_mut->get(), value), true, nullptr, &ptrSetError); if (ptrSetError.code && error && error_size > 0) { SetErrorSafe(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", @@ -3448,7 +3456,7 @@ bool JsonManager::PtrSetInt(JsonValue* handle, const char* path, int value, char } yyjson_ptr_err ptrSetError; - bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_int(handle->m_pDocument_mut.get(), value), true, nullptr, &ptrSetError); + bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut->get(), path, strlen(path), yyjson_mut_int(handle->m_pDocument_mut->get(), value), true, nullptr, &ptrSetError); if (ptrSetError.code && error && error_size > 0) { SetErrorSafe(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", @@ -3471,9 +3479,9 @@ bool JsonManager::PtrSetInt64(JsonValue* handle, const char* path, std::variant< bool success = std::visit([&](auto&& val) -> bool { using T = std::decay_t; if constexpr (std::is_same_v) { - return yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_sint(handle->m_pDocument_mut.get(), val), true, nullptr, &ptrSetError); + return yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut->get(), path, strlen(path), yyjson_mut_sint(handle->m_pDocument_mut->get(), val), true, nullptr, &ptrSetError); } else if constexpr (std::is_same_v) { - return yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_uint(handle->m_pDocument_mut.get(), val), true, nullptr, &ptrSetError); + return yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut->get(), path, strlen(path), yyjson_mut_uint(handle->m_pDocument_mut->get(), val), true, nullptr, &ptrSetError); } return false; }, value); @@ -3496,7 +3504,7 @@ bool JsonManager::PtrSetString(JsonValue* handle, const char* path, const char* } yyjson_ptr_err ptrSetError; - bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_strcpy(handle->m_pDocument_mut.get(), value), true, nullptr, &ptrSetError); + bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut->get(), path, strlen(path), yyjson_mut_strcpy(handle->m_pDocument_mut->get(), value), true, nullptr, &ptrSetError); if (ptrSetError.code && error && error_size > 0) { SetErrorSafe(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", @@ -3516,7 +3524,7 @@ bool JsonManager::PtrSetNull(JsonValue* handle, const char* path, char* error, s } yyjson_ptr_err ptrSetError; - bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_null(handle->m_pDocument_mut.get()), true, nullptr, &ptrSetError); + bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut->get(), path, strlen(path), yyjson_mut_null(handle->m_pDocument_mut->get()), true, nullptr, &ptrSetError); if (ptrSetError.code && error && error_size > 0) { SetErrorSafe(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", @@ -3537,9 +3545,9 @@ bool JsonManager::PtrAdd(JsonValue* handle, const char* path, JsonValue* value, yyjson_mut_val* val_copy = nullptr; if (value->IsMutable()) { - val_copy = yyjson_mut_val_mut_copy(handle->m_pDocument_mut.get(), value->m_pVal_mut); + val_copy = yyjson_mut_val_mut_copy(handle->m_pDocument_mut->get(), value->m_pVal_mut); } else { - val_copy = yyjson_val_mut_copy(handle->m_pDocument_mut.get(), value->m_pVal); + val_copy = yyjson_val_mut_copy(handle->m_pDocument_mut->get(), value->m_pVal); } if (!val_copy) { @@ -3550,7 +3558,7 @@ bool JsonManager::PtrAdd(JsonValue* handle, const char* path, JsonValue* value, } yyjson_ptr_err ptrAddError; - bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut.get(), path, strlen(path), val_copy, true, nullptr, &ptrAddError); + bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut->get(), path, strlen(path), val_copy, true, nullptr, &ptrAddError); if (ptrAddError.code && error && error_size > 0) { SetErrorSafe(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", @@ -3570,7 +3578,7 @@ bool JsonManager::PtrAddBool(JsonValue* handle, const char* path, bool value, ch } yyjson_ptr_err ptrAddError; - bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_bool(handle->m_pDocument_mut.get(), value), true, nullptr, &ptrAddError); + bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut->get(), path, strlen(path), yyjson_mut_bool(handle->m_pDocument_mut->get(), value), true, nullptr, &ptrAddError); if (ptrAddError.code && error && error_size > 0) { SetErrorSafe(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", @@ -3590,7 +3598,7 @@ bool JsonManager::PtrAddFloat(JsonValue* handle, const char* path, double value, } yyjson_ptr_err ptrAddError; - bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_real(handle->m_pDocument_mut.get(), value), true, nullptr, &ptrAddError); + bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut->get(), path, strlen(path), yyjson_mut_real(handle->m_pDocument_mut->get(), value), true, nullptr, &ptrAddError); if (ptrAddError.code && error && error_size > 0) { SetErrorSafe(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", @@ -3610,7 +3618,7 @@ bool JsonManager::PtrAddInt(JsonValue* handle, const char* path, int value, char } yyjson_ptr_err ptrAddError; - bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_int(handle->m_pDocument_mut.get(), value), true, nullptr, &ptrAddError); + bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut->get(), path, strlen(path), yyjson_mut_int(handle->m_pDocument_mut->get(), value), true, nullptr, &ptrAddError); if (ptrAddError.code && error && error_size > 0) { SetErrorSafe(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", @@ -3633,9 +3641,9 @@ bool JsonManager::PtrAddInt64(JsonValue* handle, const char* path, std::variant< bool success = std::visit([&](auto&& val) -> bool { using T = std::decay_t; if constexpr (std::is_same_v) { - return yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_sint(handle->m_pDocument_mut.get(), val), true, nullptr, &ptrAddError); + return yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut->get(), path, strlen(path), yyjson_mut_sint(handle->m_pDocument_mut->get(), val), true, nullptr, &ptrAddError); } else if constexpr (std::is_same_v) { - return yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_uint(handle->m_pDocument_mut.get(), val), true, nullptr, &ptrAddError); + return yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut->get(), path, strlen(path), yyjson_mut_uint(handle->m_pDocument_mut->get(), val), true, nullptr, &ptrAddError); } return false; }, value); @@ -3658,7 +3666,7 @@ bool JsonManager::PtrAddString(JsonValue* handle, const char* path, const char* } yyjson_ptr_err ptrAddError; - bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_strcpy(handle->m_pDocument_mut.get(), value), true, nullptr, &ptrAddError); + bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut->get(), path, strlen(path), yyjson_mut_strcpy(handle->m_pDocument_mut->get(), value), true, nullptr, &ptrAddError); if (ptrAddError.code && error && error_size > 0) { SetErrorSafe(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", @@ -3678,7 +3686,7 @@ bool JsonManager::PtrAddNull(JsonValue* handle, const char* path, char* error, s } yyjson_ptr_err ptrAddError; - bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_null(handle->m_pDocument_mut.get()), true, nullptr, &ptrAddError); + bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut->get(), path, strlen(path), yyjson_mut_null(handle->m_pDocument_mut->get()), true, nullptr, &ptrAddError); if (ptrAddError.code && error && error_size > 0) { SetErrorSafe(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", @@ -3698,7 +3706,7 @@ bool JsonManager::PtrRemove(JsonValue* handle, const char* path, char* error, si } yyjson_ptr_err ptrRemoveError; - bool success = yyjson_mut_doc_ptr_removex(handle->m_pDocument_mut.get(), path, strlen(path), nullptr, &ptrRemoveError) != nullptr; + bool success = yyjson_mut_doc_ptr_removex(handle->m_pDocument_mut->get(), path, strlen(path), nullptr, &ptrRemoveError) != nullptr; if (ptrRemoveError.code && error && error_size > 0) { SetErrorSafe(error, error_size, "Failed to remove JSON pointer: %s (error code: %u, position: %zu, path: %s)", @@ -3720,12 +3728,12 @@ JsonManager::PtrGetValueResult JsonManager::PtrGetValueInternal(JsonValue* handl yyjson_ptr_err ptrGetError; if (handle->IsMutable()) { - result.mut_val = yyjson_mut_doc_ptr_getx(handle->m_pDocument_mut.get(), path, strlen(path), nullptr, &ptrGetError); + result.mut_val = yyjson_mut_doc_ptr_getx(handle->m_pDocument_mut->get(), path, strlen(path), nullptr, &ptrGetError); if (result.mut_val && !ptrGetError.code) { result.success = true; } } else { - result.imm_val = yyjson_doc_ptr_getx(handle->m_pDocument.get(), path, strlen(path), &ptrGetError); + result.imm_val = yyjson_doc_ptr_getx(handle->m_pDocument->get(), path, strlen(path), &ptrGetError); if (result.imm_val && !ptrGetError.code) { result.success = true; } @@ -4443,7 +4451,7 @@ JsonValue* JsonManager::ReadNumber(const char* dat, uint32_t read_flg, char* err auto pJSONValue = CreateWrapper(); pJSONValue->m_pDocument_mut = CreateDocument(); - yyjson_mut_val* val = yyjson_mut_int(pJSONValue->m_pDocument_mut.get(), 0); + yyjson_mut_val* val = yyjson_mut_int(pJSONValue->m_pDocument_mut->get(), 0); if (!val) { if (error && error_size > 0) { SetErrorSafe(error, error_size, "Failed to create number value"); @@ -4468,7 +4476,7 @@ JsonValue* JsonManager::ReadNumber(const char* dat, uint32_t read_flg, char* err } pJSONValue->m_pVal_mut = val; - yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut.get(), val); + yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut->get(), val); return pJSONValue.release(); } diff --git a/src/JsonManager.h b/src/JsonManager.h index a43db25..a2b8ca5 100755 --- a/src/JsonManager.h +++ b/src/JsonManager.h @@ -7,6 +7,168 @@ #include #include +/** + * @brief Base class for intrusive reference counting + * + * Objects inheriting from this class can be managed by RefPtr. + * Reference count starts at 0 and is incremented when RefPtr takes ownership. + */ +class RefCounted { +private: + mutable int ref_count_ = 0; + +protected: + virtual ~RefCounted() = default; + + RefCounted() = default; + RefCounted(const RefCounted &) : ref_count_(0) {} + RefCounted &operator=(const RefCounted &) { return *this; } + RefCounted(RefCounted &&) noexcept : ref_count_(0) {} + RefCounted &operator=(RefCounted &&) noexcept { return *this; } + +public: + void add_ref() const noexcept { ++ref_count_; } + + void release() const noexcept { + if (--ref_count_ == 0) { + delete this; + } + } + + int use_count() const noexcept { return ref_count_; } +}; + +/** + * @brief Smart pointer for intrusive reference counting + * + * Similar to std::shared_ptr but uses intrusive reference counting. + * Automatically manages object lifetime through add_ref() and release(). + */ +template class RefPtr { +private: + T *ptr_; + +public: + RefPtr() noexcept : ptr_(nullptr) {} + + explicit RefPtr(T *p) noexcept : ptr_(p) { + if (ptr_) + ptr_->add_ref(); + } + + RefPtr(const RefPtr &other) noexcept : ptr_(other.ptr_) { + if (ptr_) + ptr_->add_ref(); + } + + RefPtr(RefPtr &&other) noexcept : ptr_(other.ptr_) { other.ptr_ = nullptr; } + + ~RefPtr() { + if (ptr_) + ptr_->release(); + } + + RefPtr &operator=(const RefPtr &other) noexcept { + if (ptr_ != other.ptr_) { + if (other.ptr_) + other.ptr_->add_ref(); + if (ptr_) + ptr_->release(); + ptr_ = other.ptr_; + } + return *this; + } + + RefPtr &operator=(RefPtr &&other) noexcept { + if (this != &other) { + if (ptr_) + ptr_->release(); + ptr_ = other.ptr_; + other.ptr_ = nullptr; + } + return *this; + } + + RefPtr &operator=(T *p) noexcept { + reset(p); + return *this; + } + + T *operator->() const noexcept { return ptr_; } + T &operator*() const noexcept { return *ptr_; } + T *get() const noexcept { return ptr_; } + + explicit operator bool() const noexcept { return ptr_ != nullptr; } + + int use_count() const noexcept { return ptr_ ? ptr_->use_count() : 0; } + + void reset(T *p = nullptr) noexcept { + if (ptr_ != p) { + if (p) + p->add_ref(); + if (ptr_) + ptr_->release(); + ptr_ = p; + } + } + + bool operator==(const RefPtr &other) const noexcept { return ptr_ == other.ptr_; } + bool operator!=(const RefPtr &other) const noexcept { return ptr_ != other.ptr_; } + bool operator==(std::nullptr_t) const noexcept { return ptr_ == nullptr; } + bool operator!=(std::nullptr_t) const noexcept { return ptr_ != nullptr; } +}; + +/** + * @brief Factory function to create RefPtr from new object + */ +template RefPtr make_ref(Args &&...args) { + return RefPtr(new T(std::forward(args)...)); +} + +/** + * @brief Wrapper for yyjson_mut_doc with intrusive reference counting + */ +class RefCountedMutDoc : public RefCounted { +private: + yyjson_mut_doc *doc_; + +public: + explicit RefCountedMutDoc(yyjson_mut_doc *doc) noexcept : doc_(doc) {} + + RefCountedMutDoc(const RefCountedMutDoc &) = delete; + RefCountedMutDoc &operator=(const RefCountedMutDoc &) = delete; + + ~RefCountedMutDoc() noexcept override { + if (doc_) { + yyjson_mut_doc_free(doc_); + } + } + + yyjson_mut_doc *get() const noexcept { return doc_; } +}; + +/** + * @brief Wrapper for yyjson_doc with intrusive reference counting + */ +class RefCountedImmutableDoc : public RefCounted { +private: + yyjson_doc *doc_; + +public: + explicit RefCountedImmutableDoc(yyjson_doc *doc) noexcept : doc_(doc) {} + + RefCountedImmutableDoc(const RefCountedImmutableDoc &) = delete; + RefCountedImmutableDoc &operator=(const RefCountedImmutableDoc &) = delete; + + ~RefCountedImmutableDoc() noexcept override { + if (doc_) { + yyjson_doc_free(doc_); + } + } + + yyjson_doc *get() const noexcept { return doc_; } +}; + /** * @brief JSON value wrapper * @@ -16,14 +178,7 @@ class JsonValue { public: JsonValue() = default; - ~JsonValue() { - if (m_pDocument_mut.use_count() == 1) { - yyjson_mut_doc_free(m_pDocument_mut.get()); - } - if (m_pDocument.use_count() == 1) { - yyjson_doc_free(m_pDocument.get()); - } - } + ~JsonValue() = default; JsonValue(const JsonValue&) = delete; JsonValue& operator=(const JsonValue&) = delete; @@ -45,12 +200,21 @@ class JsonValue { return m_pDocument != nullptr; } + int GetDocumentRefCount() const { + if (m_pDocument_mut) { + return m_pDocument_mut.use_count(); + } else if (m_pDocument) { + return m_pDocument.use_count(); + } + return 0; + } + // Mutable document - std::shared_ptr m_pDocument_mut; + RefPtr m_pDocument_mut; yyjson_mut_val* m_pVal_mut{ nullptr }; // Immutable document - std::shared_ptr m_pDocument; + RefPtr m_pDocument; yyjson_val* m_pVal{ nullptr }; // Mutable document iterators @@ -84,8 +248,8 @@ class JsonArrIter { return m_isMutable; } - std::shared_ptr m_pDocument_mut; - std::shared_ptr m_pDocument; + RefPtr m_pDocument_mut; + RefPtr m_pDocument; yyjson_mut_arr_iter m_iterMut; @@ -113,8 +277,8 @@ class JsonObjIter { return m_isMutable; } - std::shared_ptr m_pDocument_mut; - std::shared_ptr m_pDocument; + RefPtr m_pDocument_mut; + RefPtr m_pDocument; yyjson_mut_obj_iter m_iterMut; @@ -167,6 +331,7 @@ class JsonManager : public IJsonManager virtual bool IsMutable(JsonValue* handle) override; virtual bool IsImmutable(JsonValue* handle) override; virtual size_t GetReadSize(JsonValue* handle) override; + virtual int GetRefCount(JsonValue* handle) override; // ========== Object Operations ========== virtual JsonValue* ObjectInit() override; @@ -379,10 +544,10 @@ class JsonManager : public IJsonManager // Helper methods static std::unique_ptr CreateWrapper(); - static std::shared_ptr WrapDocument(yyjson_mut_doc* doc); - static std::shared_ptr CopyDocument(yyjson_doc* doc); - static std::shared_ptr CreateDocument(); - static std::shared_ptr WrapImmutableDocument(yyjson_doc* doc); + static RefPtr WrapDocument(yyjson_mut_doc* doc); + static RefPtr CopyDocument(yyjson_doc* doc); + static RefPtr CreateDocument(); + static RefPtr WrapImmutableDocument(yyjson_doc* doc); // Pack helper methods static const char* SkipSeparators(const char* ptr); diff --git a/src/JsonNatives.cpp b/src/JsonNatives.cpp index ea63f30..c53182a 100755 --- a/src/JsonNatives.cpp +++ b/src/JsonNatives.cpp @@ -747,6 +747,15 @@ static cell_t json_get_read_size(IPluginContext* pContext, const cell_t* params) return static_cast(size); } +static cell_t json_get_ref_count(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + + if (!handle) return 0; + + return g_pJsonManager->GetRefCount(handle); +} + static cell_t json_create_null(IPluginContext* pContext, const cell_t* params) { JsonValue* pJSONValue = g_pJsonManager->CreateNull(); @@ -3142,6 +3151,7 @@ const sp_nativeinfo_t g_JsonNatives[] = {"JSON.GetTypeDesc", json_get_type_desc}, {"JSON.GetSerializedSize", json_get_serialized_size}, {"JSON.ReadSize.get", json_get_read_size}, + {"JSON.RefCount.get", json_get_ref_count}, {"JSON.Type.get", json_get_type}, {"JSON.SubType.get", json_get_subtype}, {"JSON.IsArray.get", json_is_array}, diff --git a/src/smsdk_config.h b/src/smsdk_config.h index eff4e02..eba1b81 100755 --- a/src/smsdk_config.h +++ b/src/smsdk_config.h @@ -3,7 +3,7 @@ #define SMEXT_CONF_NAME "SourceMod JSON Extension" #define SMEXT_CONF_DESCRIPTION "Provide JSON Native" -#define SMEXT_CONF_VERSION "1.1.5c" +#define SMEXT_CONF_VERSION "1.1.5d" #define SMEXT_CONF_AUTHOR "ProjectSky" #define SMEXT_CONF_URL "https://github.com/ProjectSky/sm-ext-yyjson" #define SMEXT_CONF_LOGTAG "json" From 06ef42a05ec278695d0c5ce7dc95dd64118a7a86 Mon Sep 17 00:00:00 2001 From: ProjectSky Date: Fri, 14 Nov 2025 23:00:35 +0800 Subject: [PATCH 6/8] feat: add JSON Patch and Merge Patch support - Implemented ApplyJsonPatch and JsonPatchInPlace methods in IJsonManager and JsonManager for applying JSON Patch operations - Introduced ApplyMergePatch and MergePatchInPlace methods for JSON Merge Patch operations - Updated README to include support for JSON Patch and Merge Patch features - Enhanced JSON iterator functionality with reset capabilities for both array and object iterators - Refactored related methods to improve error handling and memory management - Updated tests to cover new patch functionalities and ensure correctness --- README.md | 4 +- public/IJsonManager.h | 96 +++- scripting/include/json.inc | 113 ++++- scripting/test_json.sp | 248 +++++++++-- src/JsonManager.cpp | 837 +++++++++++++++++++++++++++++------ src/JsonManager.h | 50 ++- src/JsonNatives.cpp | 432 ++++++++++++++---- src/extension.cpp | 1 - src/smsdk_config.h | 2 +- third_party/yyjson/AMBuilder | 20 - 10 files changed, 1472 insertions(+), 331 deletions(-) delete mode 100755 third_party/yyjson/AMBuilder diff --git a/README.md b/README.md index dbac652..2717459 100755 --- a/README.md +++ b/README.md @@ -6,6 +6,8 @@ A [SourceMod](http://www.sourcemod.net/) extension that provides comprehensive J ## Key Features * High-performance JSON parsing and serialization using YYJSON * Support for [JSON Pointer](https://datatracker.ietf.org/doc/html/rfc6901) operations +* Support for [JSON Patch](https://datatracker.ietf.org/doc/html/rfc6902) operations +* Support for [JSON Merge Patch](https://datatracker.ietf.org/doc/html/rfc7386) operations * x64 support * Easy-to-use API for both objects and arrays * Pretty printing and writing support @@ -208,7 +210,7 @@ JSON value; // Method 1: Using Array Iterator (Recommended) JSONArrIter iter = new JSONArrIter(arr); -while ((value = iter.Next()) != null) { +while ((value = iter.Next) != null) { PrintToServer("Index: %d", iter.Index); delete value; } diff --git a/public/IJsonManager.h b/public/IJsonManager.h index bb00a00..12746f9 100755 --- a/public/IJsonManager.h +++ b/public/IJsonManager.h @@ -1,5 +1,5 @@ -#ifndef _INCLUDE_SM_JSON_IJSONMANAGER_H_ -#define _INCLUDE_SM_JSON_IJSONMANAGER_H_ +#ifndef _INCLUDE_IJSONMANAGER_H_ +#define _INCLUDE_IJSONMANAGER_H_ #include #include @@ -15,7 +15,7 @@ class JsonArrIter; class JsonObjIter; #define SMINTERFACE_JSONMANAGER_NAME "IJsonManager" -#define SMINTERFACE_JSONMANAGER_VERSION 1 +#define SMINTERFACE_JSONMANAGER_VERSION 2 #define JSON_PACK_ERROR_SIZE 256 #define JSON_ERROR_BUFFER_SIZE 256 #define JSON_INT64_BUFFER_SIZE 32 @@ -116,6 +116,52 @@ class IJsonManager : public SMInterface */ virtual char* WriteToStringPtr(JsonValue* handle, uint32_t write_flg = 0, size_t* out_size = nullptr) = 0; + /** + * Apply JSON Patch (RFC 6902) and return a new JSON value + * @param target Target JSON value + * @param patch JSON Patch document + * @param result_mutable true to return mutable result, false for immutable + * @param error Error buffer (optional) + * @param error_size Error buffer size + * @return Patched JSON value on success, nullptr on failure + */ + virtual JsonValue* ApplyJsonPatch(JsonValue* target, JsonValue* patch, bool result_mutable, + char* error = nullptr, size_t error_size = 0) = 0; + + /** + * Apply JSON Patch in place (target must be mutable) + * @param target Target JSON value (mutable document) + * @param patch JSON Patch document + * @param error Error buffer (optional) + * @param error_size Error buffer size + * @return true on success, false on failure + */ + virtual bool JsonPatchInPlace(JsonValue* target, JsonValue* patch, + char* error = nullptr, size_t error_size = 0) = 0; + + /** + * Apply JSON Merge Patch (RFC 7396) and return a new JSON value + * @param target Target JSON value + * @param patch JSON Merge Patch document + * @param result_mutable true to return mutable result, false for immutable + * @param error Error buffer (optional) + * @param error_size Error buffer size + * @return Patched JSON value on success, nullptr on failure + */ + virtual JsonValue* ApplyMergePatch(JsonValue* target, JsonValue* patch, bool result_mutable, + char* error = nullptr, size_t error_size = 0) = 0; + + /** + * Apply JSON Merge Patch in place (target must be mutable) + * @param target Target JSON value (mutable document) + * @param patch JSON Merge Patch document + * @param error Error buffer (optional) + * @param error_size Error buffer size + * @return true on success, false on failure + */ + virtual bool MergePatchInPlace(JsonValue* target, JsonValue* patch, + char* error = nullptr, size_t error_size = 0) = 0; + /** * Write JSON to file * @param handle JSON value @@ -335,7 +381,7 @@ class IJsonManager : public SMInterface * @return Reference count of the document (number of JsonValue objects sharing this document) * @note Returns 0 if handle is null or has no document */ - virtual int GetRefCount(JsonValue* handle) = 0; + virtual size_t GetRefCount(JsonValue* handle) = 0; /** * Create an empty mutable JSON object @@ -985,10 +1031,10 @@ class IJsonManager : public SMInterface * Remove range of elements (mutable only) * @param handle JSON array * @param start_index Start index (inclusive) - * @param end_index End index (exclusive) + * @param count Number of elements to remove * @return true on success */ - virtual bool ArrayRemoveRange(JsonValue* handle, size_t start_index, size_t end_index) = 0; + virtual bool ArrayRemoveRange(JsonValue* handle, size_t start_index, size_t count) = 0; /** * Remove all elements (mutable only) @@ -1022,20 +1068,12 @@ class IJsonManager : public SMInterface virtual int ArrayIndexOfInt(JsonValue* handle, int search_value) = 0; /** - * Find index of 64-bit integer value - * @param handle JSON array - * @param search_value 64-bit integer value to search for - * @return Index of first match, or -1 if not found - */ - virtual int ArrayIndexOfInt64(JsonValue* handle, int64_t search_value) = 0; - - /** - * Find index of 64-bit unsigned integer value + * Find index of 64-bit integer value (auto-detects signed/unsigned) * @param handle JSON array - * @param search_value 64-bit unsigned integer value to search for + * @param search_value 64-bit integer value to search for (std::variant) * @return Index of first match, or -1 if not found */ - virtual int ArrayIndexOfUint64(JsonValue* handle, uint64_t search_value) = 0; + virtual int ArrayIndexOfInt64(JsonValue* handle, std::variant search_value) = 0; /** * Find index of float value @@ -1559,6 +1597,8 @@ class IJsonManager : public SMInterface * Initialize an array iterator (same as ArrIterWith but returns pointer) * @param handle JSON array value * @return New array iterator or nullptr on error + * @note Caller must release the iterator using ReleaseArrIter() once finished + * @note Iterators are single-pass; once ArrIterNext() returns nullptr, create a new iterator or call ArrIterReset() to iterate again */ virtual JsonArrIter* ArrIterInit(JsonValue* handle) = 0; @@ -1566,9 +1606,18 @@ class IJsonManager : public SMInterface * Create an array iterator with an array * @param handle JSON array value * @return New array iterator or nullptr on error + * @note Caller must release the iterator using ReleaseArrIter() once finished + * @note Iterators are single-pass; once ArrIterNext() returns nullptr, create a new iterator or call ArrIterReset() to iterate again */ virtual JsonArrIter* ArrIterWith(JsonValue* handle) = 0; + /** + * Reset an array iterator to the beginning + * @param iter Array iterator + * @return true on success, false if iterator is invalid or reset failed + */ + virtual bool ArrIterReset(JsonArrIter* iter) = 0; + /** * Get next element from array iterator * @param iter Array iterator @@ -1601,6 +1650,8 @@ class IJsonManager : public SMInterface * Initialize an object iterator (same as ObjIterWith but returns pointer) * @param handle JSON object value * @return New object iterator or nullptr on error + * @note Caller must release the iterator using ReleaseObjIter() once finished + * @note Iterators are single-pass; once ObjIterNext() returns nullptr, create a new iterator or call ObjIterReset() to iterate again */ virtual JsonObjIter* ObjIterInit(JsonValue* handle) = 0; @@ -1608,9 +1659,18 @@ class IJsonManager : public SMInterface * Create an object iterator with an object * @param handle JSON object value * @return New object iterator or nullptr on error + * @note Caller must release the iterator using ReleaseObjIter() once finished + * @note Iterators are single-pass; once ObjIterNext() returns nullptr, create a new iterator or call ObjIterReset() to iterate again */ virtual JsonObjIter* ObjIterWith(JsonValue* handle) = 0; + /** + * Reset an object iterator to the beginning + * @param iter Object iterator + * @return true on success, false if iterator is invalid or reset failed + */ + virtual bool ObjIterReset(JsonObjIter* iter) = 0; + /** * Get next key from object iterator * @param iter Object iterator @@ -1815,4 +1875,4 @@ class IJsonManager : public SMInterface char* error = nullptr, size_t error_size = 0) = 0; }; -#endif // _INCLUDE_SM_JSON_IJSONMANAGER_H_ \ No newline at end of file +#endif // _INCLUDE_IJSONMANAGER_H_ \ No newline at end of file diff --git a/scripting/include/json.inc b/scripting/include/json.inc index c372ae5..81bc1e6 100755 --- a/scripting/include/json.inc +++ b/scripting/include/json.inc @@ -191,6 +191,48 @@ methodmap JSON < Handle */ public native any ToImmutable(); + /** + * Apply a JSON Patch (RFC 6902) to this value and return a new JSON handle + * + * @param patch JSON Patch document + * @param resultMutable True to return a mutable result, false for immutable + * + * @return New JSON handle on success + * @error Throws if the patch cannot be applied + */ + public native any ApplyJsonPatch(const JSON patch, bool resultMutable = false); + + /** + * Apply a JSON Patch (RFC 6902) to this value in place + * + * @param patch JSON Patch document + * + * @return True on success, false otherwise + * @error Throws if this value is immutable or patch failed + */ + public native bool JsonPatchInPlace(const JSON patch); + + /** + * Apply a JSON Merge Patch (RFC 7396) to this value and return a new JSON handle + * + * @param patch JSON Merge Patch document + * @param resultMutable True to return a mutable result, false for immutable + * + * @return New JSON handle on success + * @error Throws if the merge patch cannot be applied + */ + public native any ApplyMergePatch(const JSON patch, bool resultMutable = false); + + /** + * Apply a JSON Merge Patch (RFC 7396) to this value in place + * + * @param patch JSON Merge Patch document + * + * @return True on success, false otherwise + * @error Throws if this value is immutable or merge patch failed + */ + public native bool MergePatchInPlace(const JSON patch); + /** * Write a document to JSON file with options * @@ -1427,9 +1469,9 @@ methodmap JSONArray < JSON * @param buffer Buffer to copy to * @param maxlength Maximum size of the buffer * - * @return 64-bit integer + * @return True on success, false on failure */ - public native void GetInt64(int index, char[] buffer, int maxlength); + public native bool GetInt64(int index, char[] buffer, int maxlength); /** * Gets string data from the array @@ -1529,7 +1571,7 @@ methodmap JSONArray < JSON * * @param value JSON handle to set * - * @return The value to be inserted. Returns false if it is null + * @return True if succeed, false otherwise */ public native bool Push(const JSON value); @@ -1538,7 +1580,7 @@ methodmap JSONArray < JSON * * @param value Boolean value to set * - * @return The value to be inserted. Returns false if it is null + * @return True if succeed, false otherwise */ public native bool PushBool(bool value); @@ -1547,7 +1589,7 @@ methodmap JSONArray < JSON * * @param value float to set * - * @return The value to be inserted. Returns false if it is null + * @return True if succeed, false otherwise */ public native bool PushFloat(float value); @@ -1556,7 +1598,7 @@ methodmap JSONArray < JSON * * @param value integer to set * - * @return The value to be inserted. Returns false if it is null + * @return True if succeed, false otherwise */ public native bool PushInt(int value); @@ -1565,7 +1607,7 @@ methodmap JSONArray < JSON * * @param value integer64 value * - * @return The value to be inserted. Returns false if it is null + * @return True if succeed, false otherwise */ public native bool PushInt64(const char[] value); @@ -1574,20 +1616,22 @@ methodmap JSONArray < JSON * * @param value String to copy * - * @return The value to be inserted. Returns false if it is null + * @return True if succeed, false otherwise */ public native bool PushString(const char[] value); /** * Inserts a null value at the end of the array * - * @return The value to be inserted. Returns false if it is null + * @return True if succeed, false otherwise */ public native bool PushNull(); /** * Inserts a JSON value at specific index * + * @note This function takes a linear search time + * * @param index Position to insert (0 to size, size means append) * @param value JSON handle to insert * @@ -1598,6 +1642,8 @@ methodmap JSONArray < JSON /** * Inserts a boolean value at specific index * + * @note This function takes a linear search time + * * @param index Position to insert * @param value Boolean value * @@ -1608,6 +1654,8 @@ methodmap JSONArray < JSON /** * Inserts an integer value at specific index * + * @note This function takes a linear search time + * * @param index Position to insert * @param value Integer value * @@ -1618,6 +1666,8 @@ methodmap JSONArray < JSON /** * Inserts an integer64 value at specific index * + * @note This function takes a linear search time + * * @param index Position to insert * @param value Integer64 value (as string) * @@ -1628,6 +1678,8 @@ methodmap JSONArray < JSON /** * Inserts a float value at specific index * + * @note This function takes a linear search time + * * @param index Position to insert * @param value Float value * @@ -1638,6 +1690,8 @@ methodmap JSONArray < JSON /** * Inserts a string value at specific index * + * @note This function takes a linear search time + * * @param index Position to insert * @param value String value * @@ -1648,6 +1702,8 @@ methodmap JSONArray < JSON /** * Inserts a null value at specific index * + * @note This function takes a linear search time + * * @param index Position to insert * * @return True if succeed, false otherwise @@ -1734,7 +1790,7 @@ methodmap JSONArray < JSON public native bool RemoveFirst(); /** - * Removes and returns the last value in this array + * Removes the last value in this array * * @return True if succeed, false otherwise */ @@ -1746,12 +1802,11 @@ methodmap JSONArray < JSON * @note This function takes a linear search time * * @param start_index The start index of the range (0 is the first) - * @param end_index The number of items in the range (can be 0, but do nothing) - * + * @param count Number of items to remove (can be 0, in which case nothing happens) * * @return True if succeed, false otherwise */ - public native bool RemoveRange(int start_index, int end_index); + public native bool RemoveRange(int start_index, int count); /** * Searches for a boolean value in the array and returns its index @@ -1861,6 +1916,8 @@ methodmap JSONArrIter < Handle * Creates an array iterator from a JSON array value * * @note Needs to be freed using delete or CloseHandle() + * @note Iterators are single-pass. Once Next returns null, call Reset() or create + * a new iterator to traverse the array again * * @param array JSON array value to iterate * @@ -1869,6 +1926,16 @@ methodmap JSONArrIter < Handle */ public native JSONArrIter(JSON array); + /** + * Resets the iterator to the beginning of the array + * + * @note Only resets the iterator, does not free the iterator handle + * + * @return True on success, false on failure + * @error Invalid iterator handle + */ + public native bool Reset(); + /** * Moves the iterator to the next element * @@ -1917,6 +1984,8 @@ methodmap JSONObjIter < Handle * Creates an object iterator from a JSON object value * * @note Needs to be freed using delete or CloseHandle() + * @note Iterators are single-pass. Once Next returns null, call Reset() or create + * a new iterator to traverse the object again * * @param obj JSON object value to iterate * @@ -1925,6 +1994,16 @@ methodmap JSONObjIter < Handle */ public native JSONObjIter(JSON obj); + /** + * Resets the iterator to the beginning of the object + * + * @note Only resets the iterator, does not free the iterator handle + * + * @return True on success, false on failure + * @error Invalid iterator handle + */ + public native bool Reset(); + /** * Moves the iterator to the next key * @@ -2138,6 +2217,10 @@ public void __pl_json_SetNTVOptional() MarkNativeAsOptional("JSON.ForeachIndex"); MarkNativeAsOptional("JSON.ToMutable"); MarkNativeAsOptional("JSON.ToImmutable"); + MarkNativeAsOptional("JSON.ApplyJsonPatch"); + MarkNativeAsOptional("JSON.JsonPatchInPlace"); + MarkNativeAsOptional("JSON.ApplyMergePatch"); + MarkNativeAsOptional("JSON.MergePatchInPlace"); // JSON CREATE & GET MarkNativeAsOptional("JSON.Pack"); @@ -2200,14 +2283,16 @@ public void __pl_json_SetNTVOptional() MarkNativeAsOptional("JSONArrIter.HasNext.get"); MarkNativeAsOptional("JSONArrIter.Index.get"); MarkNativeAsOptional("JSONArrIter.Remove"); + MarkNativeAsOptional("JSONArrIter.Reset"); // JSONObjIter MarkNativeAsOptional("JSONObjIter.JSONObjIter"); MarkNativeAsOptional("JSONObjIter.Next"); - MarkNativeAsOptional("JSONObjIter.HasNext"); + MarkNativeAsOptional("JSONObjIter.HasNext.get"); MarkNativeAsOptional("JSONObjIter.Value.get"); MarkNativeAsOptional("JSONObjIter.Get"); MarkNativeAsOptional("JSONObjIter.Index.get"); MarkNativeAsOptional("JSONObjIter.Remove"); + MarkNativeAsOptional("JSONObjIter.Reset"); } #endif \ No newline at end of file diff --git a/scripting/test_json.sp b/scripting/test_json.sp index d732b26..2c0b149 100755 --- a/scripting/test_json.sp +++ b/scripting/test_json.sp @@ -405,19 +405,19 @@ void Test_BasicValues() char buffer[32]; - JSON.GetTypeDesc(boolVal, buffer, sizeof(buffer)); + boolVal.GetTypeDesc(buffer, sizeof(buffer)); AssertStrEq(buffer, "true"); - JSON.GetTypeDesc(intVal, buffer, sizeof(buffer)); + intVal.GetTypeDesc(buffer, sizeof(buffer)); AssertTrue(strcmp(buffer, "uint") == 0 || strcmp(buffer, "sint") == 0); - JSON.GetTypeDesc(floatVal, buffer, sizeof(buffer)); + floatVal.GetTypeDesc(buffer, sizeof(buffer)); AssertStrEq(buffer, "real"); - JSON.GetTypeDesc(strVal, buffer, sizeof(buffer)); + strVal.GetTypeDesc(buffer, sizeof(buffer)); AssertStrEq(buffer, "string"); - JSON.GetTypeDesc(nullVal, buffer, sizeof(buffer)); + nullVal.GetTypeDesc(buffer, sizeof(buffer)); AssertStrEq(buffer, "null"); delete boolVal; @@ -1547,7 +1547,7 @@ void Test_ParseAndSerialize() TestStart("Parse_MutableDocument"); { - JSON json = JSON.Parse("{\"key\":\"value\"}", false, true); + JSON json = JSON.Parse("{\"key\":\"value\"}", .is_mutable_doc = true); AssertValidHandle(json); AssertTrue(json.IsMutable); AssertFalse(json.IsImmutable); @@ -1607,7 +1607,7 @@ void Test_ParseAndSerialize() // Test read flags TestStart("Parse_WithTrailingCommas"); { - JSON json = JSON.Parse("[1,2,3,]", false, false, JSON_READ_ALLOW_TRAILING_COMMAS); + JSON json = JSON.Parse("[1,2,3,]", .flag = JSON_READ_ALLOW_TRAILING_COMMAS); AssertValidHandle(json); delete json; } @@ -1615,7 +1615,7 @@ void Test_ParseAndSerialize() TestStart("Parse_WithComments"); { - JSON json = JSON.Parse("/* comment */ {\"key\":\"value\"}", false, false, JSON_READ_ALLOW_COMMENTS); + JSON json = JSON.Parse("/* comment */ {\"key\":\"value\"}", .flag = JSON_READ_ALLOW_COMMENTS); AssertValidHandle(json); delete json; } @@ -1687,16 +1687,30 @@ void Test_Iterators() int count = 0; char key[32]; - JSON value; + JSONObjIter iter = new JSONObjIter(obj); - while (obj.ForeachObject(key, sizeof(key), value)) + while (iter.Next(key, sizeof(key))) { count++; + JSON value = iter.Value; AssertValidHandle(value); delete value; } + AssertFalse(iter.HasNext); + AssertEq(count, 3); + + AssertTrue(iter.Reset()); + count = 0; + while (iter.Next(key, sizeof(key))) + { + count++; + JSON value = iter.Value; + AssertValidHandle(value); + delete value; + } AssertEq(count, 3); + delete iter; delete obj; } TestEnd(); @@ -1710,18 +1724,32 @@ void Test_Iterators() arr.PushInt(30); int count = 0; - int index; - JSON value; + JSONArrIter iter = new JSONArrIter(arr); - while (arr.ForeachArray(index, value)) + while (iter.HasNext) { - AssertEq(index, count); + JSON value = iter.Next; AssertValidHandle(value); + AssertEq(iter.Index, count); delete value; count++; } + AssertFalse(iter.HasNext); + AssertEq(count, 3); + + AssertTrue(iter.Reset()); + count = 0; + while (iter.HasNext) + { + JSON value = iter.Next; + AssertValidHandle(value); + AssertEq(iter.Index, count); + delete value; + count++; + } AssertEq(count, 3); + delete iter; delete arr; } TestEnd(); @@ -1736,14 +1764,26 @@ void Test_Iterators() int count = 0; char key[32]; + JSONObjIter iter = new JSONObjIter(obj); - while (obj.ForeachKey(key, sizeof(key))) + while (iter.Next(key, sizeof(key))) { AssertTrue(strlen(key) > 0); count++; } + AssertFalse(iter.HasNext); + AssertEq(count, 3); + + AssertTrue(iter.Reset()); + count = 0; + while (iter.Next(key, sizeof(key))) + { + AssertTrue(strlen(key) > 0); + count++; + } AssertEq(count, 3); + delete iter; delete obj; } TestEnd(); @@ -1757,15 +1797,30 @@ void Test_Iterators() arr.PushInt(3); int count = 0; - int index; + JSONArrIter iter = new JSONArrIter(arr); - while (arr.ForeachIndex(index)) + while (iter.HasNext) { - AssertEq(index, count); + JSON value = iter.Next; + AssertEq(iter.Index, count); + delete value; count++; } + AssertFalse(iter.HasNext); + AssertEq(count, 3); + AssertTrue(iter.Reset()); + + count = 0; + while (iter.HasNext) + { + JSON value = iter.Next; + AssertEq(iter.Index, count); + delete value; + count++; + } AssertEq(count, 3); + delete iter; delete arr; } TestEnd(); @@ -1774,10 +1829,12 @@ void Test_Iterators() TestStart("Iterator_EmptyObject"); { JSONObject obj = new JSONObject(); - char key[32]; - JSON value; - AssertFalse(obj.ForeachObject(key, sizeof(key), value)); + JSONObjIter iter = new JSONObjIter(obj); + AssertFalse(iter.Next(key, sizeof(key))); + AssertTrue(iter.Reset()); + AssertFalse(iter.Next(key, sizeof(key))); + delete iter; delete obj; } @@ -1786,10 +1843,11 @@ void Test_Iterators() TestStart("Iterator_EmptyArray"); { JSONArray arr = new JSONArray(); - - int index; - JSON value; - AssertFalse(arr.ForeachArray(index, value)); + JSONArrIter iter = new JSONArrIter(arr); + AssertFalse(iter.HasNext); + AssertTrue(iter.Reset()); + AssertFalse(iter.HasNext); + delete iter; delete arr; } @@ -2146,6 +2204,144 @@ void Test_AdvancedFeatures() } TestEnd(); + // Test ApplyJsonPatch (new value, immutable result) + TestStart("Advanced_ApplyJsonPatch"); + { + JSONObject original = new JSONObject(); + original.SetInt("score", 10); + original.SetString("name", "bot"); + + JSON patch = JSON.Parse("[{\"op\":\"replace\",\"path\":\"/score\",\"value\":42}]"); + AssertValidHandle(patch); + + JSON result = original.ApplyJsonPatch(patch); + AssertValidHandle(result); + AssertTrue(result.IsImmutable); + + AssertEq(result.PtrGetInt("/score"), 42); + char buffer[32]; + result.PtrGetString("/name", buffer, sizeof(buffer)); + AssertStrEq(buffer, "bot"); + + // ensure original unchanged + AssertEq(original.GetInt("score"), 10); + + delete result; + delete patch; + delete original; + } + TestEnd(); + + // Test ApplyJsonPatch resultMutable = true + TestStart("Advanced_ApplyJsonPatch_MutableResult"); + { + JSONObject original = new JSONObject(); + original.SetInt("count", 1); + + JSON patch = JSON.Parse("[{\"op\":\"add\",\"path\":\"/newField\",\"value\":\"hello\"}]"); + AssertValidHandle(patch); + + JSON result = original.ApplyJsonPatch(patch, true); + AssertValidHandle(result); + AssertTrue(result.IsMutable); + + char buffer[16]; + result.PtrGetString("/newField", buffer, sizeof(buffer)); + AssertStrEq(buffer, "hello"); + + delete result; + delete patch; + delete original; + } + TestEnd(); + + // Test JsonPatchInPlace + TestStart("Advanced_JsonPatchInPlace"); + { + JSONObject target = new JSONObject(); + target.SetInt("score", 5); + target.SetInt("lives", 3); + + JSON patch = JSON.Parse("[{\"op\":\"remove\",\"path\":\"/lives\"},{\"op\":\"replace\",\"path\":\"/score\",\"value\":9}]"); + AssertValidHandle(patch); + + AssertTrue(target.JsonPatchInPlace(patch)); + AssertEq(target.GetInt("score"), 9); + AssertFalse(target.HasKey("lives")); + + delete patch; + delete target; + } + TestEnd(); + + // Test ApplyMergePatch (immutable result) + TestStart("Advanced_ApplyMergePatch"); + { + JSONObject original = new JSONObject(); + original.PtrSetString("/settings/mode", "coop"); + original.PtrSetInt("/settings/difficulty", 1); + + JSON mergePatch = JSON.Parse("{\"settings\":{\"difficulty\":3,\"friendlyFire\":true}}"); + AssertValidHandle(mergePatch); + + JSON result = original.ApplyMergePatch(mergePatch); + AssertValidHandle(result); + AssertTrue(result.IsImmutable); + + AssertEq(result.PtrGetInt("/settings/difficulty"), 3); + AssertTrue(result.PtrGetBool("/settings/friendlyFire")); + + delete result; + delete mergePatch; + delete original; + } + TestEnd(); + + // Test ApplyMergePatch resultMutable = true + TestStart("Advanced_ApplyMergePatch_MutableResult"); + { + JSONObject original = new JSONObject(); + original.PtrSetString("/profile/name", "player"); + + JSON mergePatch = JSON.Parse("{\"profile\":{\"rank\":10}}"); + AssertValidHandle(mergePatch); + + JSON result = original.ApplyMergePatch(mergePatch, true); + AssertValidHandle(result); + AssertTrue(result.IsMutable); + + AssertEq(result.PtrGetInt("/profile/rank"), 10); + char buffer[16]; + result.PtrGetString("/profile/name", buffer, sizeof(buffer)); + AssertStrEq(buffer, "player"); + + delete result; + delete mergePatch; + delete original; + } + TestEnd(); + + // Test MergePatchInPlace + TestStart("Advanced_MergePatchInPlace"); + { + JSONObject target = new JSONObject(); + target.PtrSetString("/config/mode", "coop"); + target.PtrSetInt("/config/players", 4); + + JSON mergePatch = JSON.Parse("{\"config\":{\"players\":6,\"region\":\"EU\"}}"); + AssertValidHandle(mergePatch); + + AssertTrue(target.MergePatchInPlace(mergePatch)); + AssertEq(target.PtrGetInt("/config/players"), 6); + char buffer[16]; + target.PtrGetString("/config/region", buffer, sizeof(buffer)); + AssertStrEq(buffer, "EU"); + + delete mergePatch; + delete target; + } + TestEnd(); + // Test Pack TestStart("Advanced_Pack_SimpleObject"); { @@ -2210,7 +2406,7 @@ void Test_AdvancedFeatures() // Test mixed type array sorting TestStart("Advanced_MixedTypeSort"); { - JSONArray json = JSON.Parse("[true, 42, \"hello\", 1.5, false]", false, true); + JSONArray json = JSON.Parse("[true, 42, \"hello\", 1.5, false]", .is_mutable_doc = true); JSONArray arr = json; AssertTrue(arr.Sort(JSON_SORT_ASC)); diff --git a/src/JsonManager.cpp b/src/JsonManager.cpp index a66330d..2867549 100755 --- a/src/JsonManager.cpp +++ b/src/JsonManager.cpp @@ -36,7 +36,10 @@ std::unique_ptr JsonManager::CreateWrapper() { } RefPtr JsonManager::WrapDocument(yyjson_mut_doc* doc) { - return RefPtr(new RefCountedMutDoc(doc)); + if (!doc) { + return RefPtr(); + } + return make_ref(doc); } RefPtr JsonManager::CopyDocument(yyjson_doc* doc) { @@ -48,7 +51,54 @@ RefPtr JsonManager::CreateDocument() { } RefPtr JsonManager::WrapImmutableDocument(yyjson_doc* doc) { - return RefPtr(new RefCountedImmutableDoc(doc)); + if (!doc) { + return RefPtr(); + } + return make_ref(doc); +} + +RefPtr JsonManager::CloneValueToMutable(JsonValue* value) { + if (!value) { + return RefPtr(); + } + + if (value->IsMutable()) { + yyjson_mut_doc* dup = yyjson_mut_doc_mut_copy(value->m_pDocument_mut->get(), nullptr); + return WrapDocument(dup); + } + + if (!value->m_pDocument) { + return RefPtr(); + } + + return CopyDocument(value->m_pDocument->get()); +} + +static yyjson_mut_val* CopyValueIntoDoc(JsonValue* value, yyjson_mut_doc* doc, char* error, size_t error_size) { + if (!value || !doc) { + SetErrorSafe(error, error_size, "Invalid JSON value or document"); + return nullptr; + } + + yyjson_mut_val* copy = nullptr; + if (value->IsMutable()) { + if (!value->m_pVal_mut) { + SetErrorSafe(error, error_size, "Mutable JSON value has no root"); + return nullptr; + } + copy = yyjson_mut_val_mut_copy(doc, value->m_pVal_mut); + } else { + if (!value->m_pVal) { + SetErrorSafe(error, error_size, "Immutable JSON value has no root"); + return nullptr; + } + copy = yyjson_val_mut_copy(doc, value->m_pVal); + } + + if (!copy) { + SetErrorSafe(error, error_size, "Failed to copy JSON value"); + } + return copy; } JsonManager::JsonManager(): m_randomGenerator(m_randomDevice()) {} @@ -97,11 +147,29 @@ JsonValue* JsonManager::ParseJSON(const char* json_str, bool is_file, bool is_mu if (is_mutable) { pJSONValue->m_pDocument_mut = CopyDocument(idoc); - pJSONValue->m_pVal_mut = yyjson_mut_doc_get_root(pJSONValue->m_pDocument_mut->get()); yyjson_doc_free(idoc); + if (!pJSONValue->m_pDocument_mut) { + SetErrorSafe(error, error_size, "Failed to create mutable JSON document"); + return nullptr; + } + pJSONValue->m_pVal_mut = yyjson_mut_doc_get_root(pJSONValue->m_pDocument_mut->get()); + if (!pJSONValue->m_pVal_mut) { + SetErrorSafe(error, error_size, "Mutable JSON document has no root value"); + return nullptr; + } } else { pJSONValue->m_pDocument = WrapImmutableDocument(idoc); + if (!pJSONValue->m_pDocument) { + yyjson_doc_free(idoc); + SetErrorSafe(error, error_size, "Failed to create immutable JSON document"); + return nullptr; + } pJSONValue->m_pVal = yyjson_doc_get_root(idoc); + if (!pJSONValue->m_pVal) { + yyjson_doc_free(idoc); + SetErrorSafe(error, error_size, "Immutable JSON document has no root value"); + return nullptr; + } } return pJSONValue.release(); @@ -114,33 +182,27 @@ bool JsonManager::WriteToString(JsonValue* handle, char* buffer, size_t buffer_s return false; } - size_t json_size; - char* json_str; + size_t written; if (handle->IsMutable()) { - json_str = yyjson_mut_val_write(handle->m_pVal_mut, write_flg, &json_size); + written = yyjson_mut_val_write_buf(buffer, buffer_size, handle->m_pVal_mut, write_flg, nullptr); } else { - json_str = yyjson_val_write(handle->m_pVal, write_flg, &json_size); + written = yyjson_val_write_buf(buffer, buffer_size, handle->m_pVal, write_flg, nullptr); } - if (!json_str) { + if (written == 0) { return false; } - size_t needed_size = json_size + 1; - if (needed_size > buffer_size) { - free(json_str); + if (written + 1 > buffer_size) { return false; } - memcpy(buffer, json_str, json_size); - buffer[json_size] = '\0'; - free(json_str); + buffer[written] = '\0'; if (out_size) { - *out_size = needed_size; + *out_size = written + 1; } - return true; } @@ -151,7 +213,7 @@ char* JsonManager::WriteToStringPtr(JsonValue* handle, yyjson_write_flag write_f } size_t json_size = 0; - char* json_str = nullptr; + char* json_str; if (handle->IsMutable()) { json_str = yyjson_mut_val_write(handle->m_pVal_mut, write_flg, &json_size); @@ -166,6 +228,202 @@ char* JsonManager::WriteToStringPtr(JsonValue* handle, yyjson_write_flag write_f return json_str; } +JsonValue* JsonManager::ApplyJsonPatch(JsonValue* target, JsonValue* patch, bool result_mutable, + char* error, size_t error_size) +{ + if (!target || !patch) { + SetErrorSafe(error, error_size, "Target or patch JSON value is null"); + return nullptr; + } + + auto docRef = CloneValueToMutable(target); + if (!docRef) { + SetErrorSafe(error, error_size, "Failed to clone target JSON value"); + return nullptr; + } + + yyjson_mut_doc* doc = docRef->get(); + yyjson_mut_val* root = yyjson_mut_doc_get_root(doc); + if (!root) { + SetErrorSafe(error, error_size, "Target JSON has no root value"); + return nullptr; + } + + yyjson_mut_val* patchCopy = CopyValueIntoDoc(patch, doc, error, error_size); + if (!patchCopy) { + return nullptr; + } + + yyjson_patch_err patch_err = {0}; + yyjson_mut_val* resultRoot = yyjson_mut_patch(doc, root, patchCopy, &patch_err); + if (!resultRoot) { + SetErrorSafe(error, error_size, "JSON patch failed (code %u, op index %zu, message: %s)", + patch_err.code, patch_err.idx, patch_err); + return nullptr; + } + + yyjson_mut_doc_set_root(doc, resultRoot); + + if (result_mutable) { + auto wrapper = CreateWrapper(); + wrapper->m_pDocument_mut = docRef; + wrapper->m_pVal_mut = yyjson_mut_doc_get_root(doc); + docRef.reset(); + return wrapper.release(); + } + + yyjson_doc* imutDoc = yyjson_mut_doc_imut_copy(doc, nullptr); + if (!imutDoc) { + SetErrorSafe(error, error_size, "Failed to convert patched JSON to immutable document"); + return nullptr; + } + + auto wrapper = CreateWrapper(); + wrapper->m_pDocument = WrapImmutableDocument(imutDoc); + if (!wrapper->m_pDocument) { + yyjson_doc_free(imutDoc); + SetErrorSafe(error, error_size, "Failed to wrap immutable JSON document"); + return nullptr; + } + wrapper->m_pVal = yyjson_doc_get_root(imutDoc); + return wrapper.release(); +} + +bool JsonManager::JsonPatchInPlace(JsonValue* target, JsonValue* patch, + char* error, size_t error_size) +{ + if (!target || !patch) { + SetErrorSafe(error, error_size, "Target or patch JSON value is null"); + return false; + } + + if (!target->IsMutable()) { + SetErrorSafe(error, error_size, "Target JSON must be mutable for in-place JSON Patch"); + return false; + } + + yyjson_mut_doc* doc = target->m_pDocument_mut->get(); + yyjson_mut_val* root = target->m_pVal_mut; + + if (!doc || !root) { + SetErrorSafe(error, error_size, "Target JSON has no root value"); + return false; + } + + yyjson_mut_val* patchCopy = CopyValueIntoDoc(patch, doc, error, error_size); + if (!patchCopy) { + return false; + } + + yyjson_patch_err patch_err = {0}; + yyjson_mut_val* resultRoot = yyjson_mut_patch(doc, root, patchCopy, &patch_err); + if (!resultRoot) { + SetErrorSafe(error, error_size, "JSON patch failed (code %u, op index %zu, message: %s)", + patch_err.code, patch_err.idx, patch_err); + return false; + } + + yyjson_mut_doc_set_root(doc, resultRoot); + target->m_pVal_mut = yyjson_mut_doc_get_root(doc); + return true; +} + +JsonValue* JsonManager::ApplyMergePatch(JsonValue* target, JsonValue* patch, bool result_mutable, + char* error, size_t error_size) +{ + if (!target || !patch) { + SetErrorSafe(error, error_size, "Target or patch JSON value is null"); + return nullptr; + } + + auto docRef = CloneValueToMutable(target); + if (!docRef) { + SetErrorSafe(error, error_size, "Failed to clone target JSON value"); + return nullptr; + } + + yyjson_mut_doc* doc = docRef->get(); + yyjson_mut_val* root = yyjson_mut_doc_get_root(doc); + if (!root) { + SetErrorSafe(error, error_size, "Target JSON has no root value"); + return nullptr; + } + + yyjson_mut_val* patchCopy = CopyValueIntoDoc(patch, doc, error, error_size); + if (!patchCopy) { + return nullptr; + } + + yyjson_mut_val* resultRoot = yyjson_mut_merge_patch(doc, root, patchCopy); + if (!resultRoot) { + SetErrorSafe(error, error_size, "Failed to apply JSON Merge Patch"); + return nullptr; + } + + yyjson_mut_doc_set_root(doc, resultRoot); + + if (result_mutable) { + auto wrapper = CreateWrapper(); + wrapper->m_pDocument_mut = docRef; + wrapper->m_pVal_mut = yyjson_mut_doc_get_root(doc); + docRef.reset(); + return wrapper.release(); + } + + yyjson_doc* imutDoc = yyjson_mut_doc_imut_copy(doc, nullptr); + if (!imutDoc) { + SetErrorSafe(error, error_size, "Failed to convert patched JSON to immutable document"); + return nullptr; + } + + auto wrapper = CreateWrapper(); + wrapper->m_pDocument = WrapImmutableDocument(imutDoc); + if (!wrapper->m_pDocument) { + yyjson_doc_free(imutDoc); + SetErrorSafe(error, error_size, "Failed to wrap immutable JSON document"); + return nullptr; + } + wrapper->m_pVal = yyjson_doc_get_root(imutDoc); + return wrapper.release(); +} + +bool JsonManager::MergePatchInPlace(JsonValue* target, JsonValue* patch, + char* error, size_t error_size) +{ + if (!target || !patch) { + SetErrorSafe(error, error_size, "Target or patch JSON value is null"); + return false; + } + + if (!target->IsMutable()) { + SetErrorSafe(error, error_size, "Target JSON must be mutable for in-place merge patch"); + return false; + } + + yyjson_mut_doc* doc = target->m_pDocument_mut->get(); + yyjson_mut_val* root = target->m_pVal_mut; + + if (!doc || !root) { + SetErrorSafe(error, error_size, "Target JSON has no root value"); + return false; + } + + yyjson_mut_val* patchCopy = CopyValueIntoDoc(patch, doc, error, error_size); + if (!patchCopy) { + return false; + } + + yyjson_mut_val* resultRoot = yyjson_mut_merge_patch(doc, root, patchCopy); + if (!resultRoot) { + SetErrorSafe(error, error_size, "Failed to apply JSON Merge Patch in place"); + return false; + } + + yyjson_mut_doc_set_root(doc, resultRoot); + target->m_pVal_mut = yyjson_mut_doc_get_root(doc); + return true; +} + bool JsonManager::WriteToFile(JsonValue* handle, const char* path, yyjson_write_flag write_flg, char* error, size_t error_size) { @@ -262,8 +520,11 @@ JsonValue* JsonManager::DeepCopy(JsonValue* targetDoc, JsonValue* sourceValue) if (targetDoc->IsMutable()) { pJSONValue->m_pDocument_mut = CreateDocument(); + if (!pJSONValue->m_pDocument_mut) { + return nullptr; + } - yyjson_mut_val* val_copy = nullptr; + yyjson_mut_val* val_copy; if (sourceValue->IsMutable()) { val_copy = yyjson_mut_val_mut_copy(pJSONValue->m_pDocument_mut->get(), sourceValue->m_pVal_mut); } else { @@ -282,7 +543,7 @@ JsonValue* JsonManager::DeepCopy(JsonValue* targetDoc, JsonValue* sourceValue) return nullptr; } - yyjson_mut_val* temp_val = nullptr; + yyjson_mut_val* temp_val; if (sourceValue->IsMutable()) { temp_val = yyjson_mut_val_mut_copy(temp_doc, sourceValue->m_pVal_mut); } else { @@ -354,7 +615,13 @@ JsonValue* JsonManager::ToMutable(JsonValue* handle) auto pJSONValue = CreateWrapper(); pJSONValue->m_pDocument_mut = CopyDocument(handle->m_pDocument->get()); + if (!pJSONValue->m_pDocument_mut) { + return nullptr; + } pJSONValue->m_pVal_mut = yyjson_mut_doc_get_root(pJSONValue->m_pDocument_mut->get()); + if (!pJSONValue->m_pVal_mut) { + return nullptr; + } return pJSONValue.release(); } @@ -367,7 +634,14 @@ JsonValue* JsonManager::ToImmutable(JsonValue* handle) auto pJSONValue = CreateWrapper(); yyjson_doc* mdoc = yyjson_mut_doc_imut_copy(handle->m_pDocument_mut->get(), nullptr); + if (!mdoc) { + return nullptr; + } pJSONValue->m_pDocument = WrapImmutableDocument(mdoc); + if (!pJSONValue->m_pDocument) { + yyjson_doc_free(mdoc); + return nullptr; + } pJSONValue->m_pVal = yyjson_doc_get_root(pJSONValue->m_pDocument->get()); return pJSONValue.release(); @@ -599,7 +873,7 @@ size_t JsonManager::GetReadSize(JsonValue* handle) return handle->m_readSize + 1; } -int JsonManager::GetRefCount(JsonValue* handle) +size_t JsonManager::GetRefCount(JsonValue* handle) { if (!handle) { return 0; @@ -611,7 +885,17 @@ JsonValue* JsonManager::ObjectInit() { auto pJSONValue = CreateWrapper(); pJSONValue->m_pDocument_mut = CreateDocument(); + + if (!pJSONValue->m_pDocument_mut) { + return nullptr; + } + pJSONValue->m_pVal_mut = yyjson_mut_obj(pJSONValue->m_pDocument_mut->get()); + + if (!pJSONValue->m_pVal_mut) { + return nullptr; + } + yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut->get(), pJSONValue->m_pVal_mut); return pJSONValue.release(); @@ -626,8 +910,12 @@ JsonValue* JsonManager::ObjectInitWithStrings(const char** pairs, size_t count) auto pJSONValue = CreateWrapper(); pJSONValue->m_pDocument_mut = CreateDocument(); - pJSONValue->m_pVal_mut = yyjson_mut_obj_with_kv( - pJSONValue->m_pDocument_mut->get(), + if (!pJSONValue->m_pDocument_mut) { + return nullptr; + } + + pJSONValue->m_pVal_mut = yyjson_mut_obj_with_kv( + pJSONValue->m_pDocument_mut->get(), pairs, count ); @@ -636,6 +924,8 @@ JsonValue* JsonManager::ObjectInitWithStrings(const char** pairs, size_t count) return nullptr; } + yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut->get(), pJSONValue->m_pVal_mut); + return pJSONValue.release(); } @@ -822,7 +1112,7 @@ JsonValue* JsonManager::ObjectGetValueAt(JsonValue* handle, size_t index) yyjson_obj_iter iter; yyjson_obj_iter_init(handle->m_pVal, &iter); - yyjson_val* key = nullptr; + yyjson_val* key; for (size_t i = 0; i <= index; i++) { key = yyjson_obj_iter_next(&iter); if (!key) { @@ -1068,7 +1358,7 @@ bool JsonManager::ObjectSet(JsonValue* handle, const char* key, JsonValue* value return false; } - yyjson_mut_val* val_copy = nullptr; + yyjson_mut_val* val_copy; if (value->IsMutable()) { val_copy = yyjson_mut_val_mut_copy(handle->m_pDocument_mut->get(), value->m_pVal_mut); } else { @@ -1225,7 +1515,17 @@ JsonValue* JsonManager::ArrayInit() { auto pJSONValue = CreateWrapper(); pJSONValue->m_pDocument_mut = CreateDocument(); + + if (!pJSONValue->m_pDocument_mut) { + return nullptr; + } + pJSONValue->m_pVal_mut = yyjson_mut_arr(pJSONValue->m_pDocument_mut->get()); + + if (!pJSONValue->m_pVal_mut) { + return nullptr; + } + yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut->get(), pJSONValue->m_pVal_mut); return pJSONValue.release(); @@ -1240,8 +1540,12 @@ JsonValue* JsonManager::ArrayInitWithStrings(const char** strings, size_t count) auto pJSONValue = CreateWrapper(); pJSONValue->m_pDocument_mut = CreateDocument(); - pJSONValue->m_pVal_mut = yyjson_mut_arr_with_strcpy( - pJSONValue->m_pDocument_mut->get(), + if (!pJSONValue->m_pDocument_mut) { + return nullptr; + } + + pJSONValue->m_pVal_mut = yyjson_mut_arr_with_strcpy( + pJSONValue->m_pDocument_mut->get(), strings, count ); @@ -1250,6 +1554,8 @@ JsonValue* JsonManager::ArrayInitWithStrings(const char** strings, size_t count) return nullptr; } + yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut->get(), pJSONValue->m_pVal_mut); + return pJSONValue.release(); } @@ -1262,8 +1568,12 @@ JsonValue* JsonManager::ArrayInitWithInt32(const int32_t* values, size_t count) auto pJSONValue = CreateWrapper(); pJSONValue->m_pDocument_mut = CreateDocument(); - pJSONValue->m_pVal_mut = yyjson_mut_arr_with_sint32( - pJSONValue->m_pDocument_mut->get(), + if (!pJSONValue->m_pDocument_mut) { + return nullptr; + } + + pJSONValue->m_pVal_mut = yyjson_mut_arr_with_sint32( + pJSONValue->m_pDocument_mut->get(), values, count ); @@ -1272,6 +1582,8 @@ JsonValue* JsonManager::ArrayInitWithInt32(const int32_t* values, size_t count) return nullptr; } + yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut->get(), pJSONValue->m_pVal_mut); + return pJSONValue.release(); } @@ -1287,15 +1599,35 @@ JsonValue* JsonManager::ArrayInitWithInt64(const char** values, size_t count, ch if (count == 0) { auto pJSONValue = CreateWrapper(); pJSONValue->m_pDocument_mut = CreateDocument(); + + if (!pJSONValue->m_pDocument_mut) { + return nullptr; + } + pJSONValue->m_pVal_mut = yyjson_mut_arr(pJSONValue->m_pDocument_mut->get()); + + if (!pJSONValue->m_pVal_mut) { + return nullptr; + } + + yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut->get(), pJSONValue->m_pVal_mut); + return pJSONValue.release(); } auto pJSONValue = CreateWrapper(); pJSONValue->m_pDocument_mut = CreateDocument(); - auto doc = pJSONValue->m_pDocument_mut->get(); + if (!pJSONValue->m_pDocument_mut) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to create document"); + } + return nullptr; + } + + auto doc = pJSONValue->m_pDocument_mut->get(); pJSONValue->m_pVal_mut = yyjson_mut_arr(doc); + if (!pJSONValue->m_pVal_mut) { if (error && error_size > 0) { SetErrorSafe(error, error_size, "Failed to create array"); @@ -1309,14 +1641,14 @@ JsonValue* JsonManager::ArrayInitWithInt64(const char** values, size_t count, ch return nullptr; } - yyjson_mut_val* val = nullptr; - std::visit([&](auto&& arg) { + yyjson_mut_val* val = std::visit([&](auto&& arg) -> yyjson_mut_val* { using T = std::decay_t; if constexpr (std::is_same_v) { - val = yyjson_mut_sint(doc, arg); + return yyjson_mut_sint(doc, arg); } else if constexpr (std::is_same_v) { - val = yyjson_mut_uint(doc, arg); + return yyjson_mut_uint(doc, arg); } + return nullptr; }, variant_value); if (!val || !yyjson_mut_arr_append(pJSONValue->m_pVal_mut, val)) { @@ -1327,6 +1659,8 @@ JsonValue* JsonManager::ArrayInitWithInt64(const char** values, size_t count, ch } } + yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut->get(), pJSONValue->m_pVal_mut); + return pJSONValue.release(); } @@ -1339,8 +1673,12 @@ JsonValue* JsonManager::ArrayInitWithBool(const bool* values, size_t count) auto pJSONValue = CreateWrapper(); pJSONValue->m_pDocument_mut = CreateDocument(); - pJSONValue->m_pVal_mut = yyjson_mut_arr_with_bool( - pJSONValue->m_pDocument_mut->get(), + if (!pJSONValue->m_pDocument_mut) { + return nullptr; + } + + pJSONValue->m_pVal_mut = yyjson_mut_arr_with_bool( + pJSONValue->m_pDocument_mut->get(), values, count ); @@ -1349,6 +1687,8 @@ JsonValue* JsonManager::ArrayInitWithBool(const bool* values, size_t count) return nullptr; } + yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut->get(), pJSONValue->m_pVal_mut); + return pJSONValue.release(); } @@ -1361,8 +1701,12 @@ JsonValue* JsonManager::ArrayInitWithFloat(const double* values, size_t count) auto pJSONValue = CreateWrapper(); pJSONValue->m_pDocument_mut = CreateDocument(); - pJSONValue->m_pVal_mut = yyjson_mut_arr_with_real( - pJSONValue->m_pDocument_mut->get(), + if (!pJSONValue->m_pDocument_mut) { + return nullptr; + } + + pJSONValue->m_pVal_mut = yyjson_mut_arr_with_real( + pJSONValue->m_pDocument_mut->get(), values, count ); @@ -1371,6 +1715,8 @@ JsonValue* JsonManager::ArrayInitWithFloat(const double* values, size_t count) return nullptr; } + yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut->get(), pJSONValue->m_pVal_mut); + return pJSONValue.release(); } @@ -1809,7 +2155,7 @@ bool JsonManager::ArrayReplace(JsonValue* handle, size_t index, JsonValue* value return false; } - yyjson_mut_val* val_copy = nullptr; + yyjson_mut_val* val_copy; if (value->IsMutable()) { val_copy = yyjson_mut_val_mut_copy(handle->m_pDocument_mut->get(), value->m_pVal_mut); } else { @@ -1921,7 +2267,7 @@ bool JsonManager::ArrayAppend(JsonValue* handle, JsonValue* value) return false; } - yyjson_mut_val* val_copy = nullptr; + yyjson_mut_val* val_copy; if (value->IsMutable()) { val_copy = yyjson_mut_val_mut_copy(handle->m_pDocument_mut->get(), value->m_pVal_mut); } else { @@ -2003,7 +2349,23 @@ bool JsonManager::ArrayInsert(JsonValue* handle, size_t index, JsonValue* value) return false; } - return yyjson_mut_arr_insert(handle->m_pVal_mut, value->m_pVal_mut, index); + size_t arr_size = yyjson_mut_arr_size(handle->m_pVal_mut); + if (index > arr_size) { + return false; + } + + yyjson_mut_val* val_copy; + if (value->IsMutable()) { + val_copy = yyjson_mut_val_mut_copy(handle->m_pDocument_mut->get(), value->m_pVal_mut); + } else { + val_copy = yyjson_val_mut_copy(handle->m_pDocument_mut->get(), value->m_pVal); + } + + if (!val_copy) { + return false; + } + + return yyjson_mut_arr_insert(handle->m_pVal_mut, val_copy, index); } bool JsonManager::ArrayInsertBool(JsonValue* handle, size_t index, bool value) @@ -2030,14 +2392,14 @@ bool JsonManager::ArrayInsertInt64(JsonValue* handle, size_t index, std::variant return false; } - yyjson_mut_val* val = nullptr; - std::visit([&](auto&& arg) { + yyjson_mut_val* val = std::visit([&](auto&& arg) -> yyjson_mut_val* { using T = std::decay_t; if constexpr (std::is_same_v) { - val = yyjson_mut_sint(handle->m_pDocument_mut->get(), arg); + return yyjson_mut_sint(handle->m_pDocument_mut->get(), arg); } else if constexpr (std::is_same_v) { - val = yyjson_mut_uint(handle->m_pDocument_mut->get(), arg); + return yyjson_mut_uint(handle->m_pDocument_mut->get(), arg); } + return nullptr; }, value); if (!val) { @@ -2080,7 +2442,18 @@ bool JsonManager::ArrayPrepend(JsonValue* handle, JsonValue* value) return false; } - return yyjson_mut_arr_prepend(handle->m_pVal_mut, value->m_pVal_mut); + yyjson_mut_val* val_copy; + if (value->IsMutable()) { + val_copy = yyjson_mut_val_mut_copy(handle->m_pDocument_mut->get(), value->m_pVal_mut); + } else { + val_copy = yyjson_val_mut_copy(handle->m_pDocument_mut->get(), value->m_pVal); + } + + if (!val_copy) { + return false; + } + + return yyjson_mut_arr_prepend(handle->m_pVal_mut, val_copy); } bool JsonManager::ArrayPrependBool(JsonValue* handle, bool value) @@ -2107,14 +2480,14 @@ bool JsonManager::ArrayPrependInt64(JsonValue* handle, std::variant yyjson_mut_val* { using T = std::decay_t; if constexpr (std::is_same_v) { - val = yyjson_mut_sint(handle->m_pDocument_mut->get(), arg); + return yyjson_mut_sint(handle->m_pDocument_mut->get(), arg); } else if constexpr (std::is_same_v) { - val = yyjson_mut_uint(handle->m_pDocument_mut->get(), arg); + return yyjson_mut_uint(handle->m_pDocument_mut->get(), arg); } + return nullptr; }, value); if (!val) { @@ -2191,7 +2564,7 @@ bool JsonManager::ArrayRemoveLast(JsonValue* handle) return yyjson_mut_arr_remove_last(handle->m_pVal_mut) != nullptr; } -bool JsonManager::ArrayRemoveRange(JsonValue* handle, size_t start_index, size_t end_index) +bool JsonManager::ArrayRemoveRange(JsonValue* handle, size_t start_index, size_t count) { if (!handle || !handle->IsMutable()) { return false; @@ -2199,11 +2572,19 @@ bool JsonManager::ArrayRemoveRange(JsonValue* handle, size_t start_index, size_t size_t arr_size = yyjson_mut_arr_size(handle->m_pVal_mut); - if (start_index >= arr_size || end_index > arr_size || start_index > end_index) { + if (start_index >= arr_size) { + return false; + } + + if (count == 0) { + return true; + } + + if (count > (arr_size - start_index)) { return false; } - return yyjson_mut_arr_remove_range(handle->m_pVal_mut, start_index, end_index); + return yyjson_mut_arr_remove_range(handle->m_pVal_mut, start_index, count); } bool JsonManager::ArrayClear(JsonValue* handle) @@ -2296,53 +2677,44 @@ int JsonManager::ArrayIndexOfInt(JsonValue* handle, int search_value) return -1; } -int JsonManager::ArrayIndexOfInt64(JsonValue* handle, int64_t search_value) +int JsonManager::ArrayIndexOfInt64(JsonValue* handle, std::variant search_value) { if (!handle) { return -1; } - if (handle->IsMutable()) { - size_t idx, max; - yyjson_mut_val *val; - yyjson_mut_arr_foreach(handle->m_pVal_mut, idx, max, val) { - if (yyjson_mut_is_int(val) && yyjson_mut_get_sint(val) == search_value) { - return static_cast(idx); - } - } - } else { - size_t idx, max; - yyjson_val *val; - yyjson_arr_foreach(handle->m_pVal, idx, max, val) { - if (yyjson_is_int(val) && yyjson_get_sint(val) == search_value) { - return static_cast(idx); - } - } - } - - return -1; -} - -int JsonManager::ArrayIndexOfUint64(JsonValue* handle, uint64_t search_value) -{ - if (!handle) { - return -1; - } + bool is_unsigned = std::holds_alternative(search_value); if (handle->IsMutable()) { size_t idx, max; yyjson_mut_val *val; yyjson_mut_arr_foreach(handle->m_pVal_mut, idx, max, val) { - if (yyjson_mut_is_int(val) && yyjson_mut_get_uint(val) == search_value) { - return static_cast(idx); + if (yyjson_mut_is_int(val)) { + if (is_unsigned) { + if (yyjson_mut_get_uint(val) == std::get(search_value)) { + return static_cast(idx); + } + } else { + if (yyjson_mut_get_sint(val) == std::get(search_value)) { + return static_cast(idx); + } + } } } } else { size_t idx, max; yyjson_val *val; yyjson_arr_foreach(handle->m_pVal, idx, max, val) { - if (yyjson_is_int(val) && yyjson_get_uint(val) == search_value) { - return static_cast(idx); + if (yyjson_is_int(val)) { + if (is_unsigned) { + if (yyjson_get_uint(val) == std::get(search_value)) { + return static_cast(idx); + } + } else { + if (yyjson_get_sint(val) == std::get(search_value)) { + return static_cast(idx); + } + } } } } @@ -2527,7 +2899,7 @@ yyjson_mut_val* JsonManager::PackImpl(yyjson_mut_doc* doc, const char* format, return nullptr; } - yyjson_mut_val* root = nullptr; + yyjson_mut_val* root; const char* ptr = format; bool is_obj = false; @@ -2548,8 +2920,8 @@ yyjson_mut_val* JsonManager::PackImpl(yyjson_mut_doc* doc, const char* format, return nullptr; } - yyjson_mut_val* key_val = nullptr; - yyjson_mut_val* val = nullptr; + yyjson_mut_val* key_val; + yyjson_mut_val* val; while (*ptr && *ptr != '}' && *ptr != ']') { if (is_obj) { @@ -2751,7 +3123,7 @@ JsonValue* JsonManager::Pack(const char* format, IPackParamProvider* param_provi return nullptr; } - const char* end_ptr = nullptr; + const char* end_ptr; pJSONValue->m_pVal_mut = PackImpl(pJSONValue->m_pDocument_mut->get(), format, param_provider, error, error_size, &end_ptr); @@ -2768,12 +3140,19 @@ JsonValue* JsonManager::CreateBool(bool value) { auto pJSONValue = CreateWrapper(); pJSONValue->m_pDocument_mut = CreateDocument(); + + if (!pJSONValue->m_pDocument_mut) { + return nullptr; + } + pJSONValue->m_pVal_mut = yyjson_mut_bool(pJSONValue->m_pDocument_mut->get(), value); if (!pJSONValue->m_pVal_mut) { return nullptr; } + yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut->get(), pJSONValue->m_pVal_mut); + return pJSONValue.release(); } @@ -2781,12 +3160,19 @@ JsonValue* JsonManager::CreateFloat(double value) { auto pJSONValue = CreateWrapper(); pJSONValue->m_pDocument_mut = CreateDocument(); + + if (!pJSONValue->m_pDocument_mut) { + return nullptr; + } + pJSONValue->m_pVal_mut = yyjson_mut_real(pJSONValue->m_pDocument_mut->get(), value); if (!pJSONValue->m_pVal_mut) { return nullptr; } + yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut->get(), pJSONValue->m_pVal_mut); + return pJSONValue.release(); } @@ -2794,12 +3180,19 @@ JsonValue* JsonManager::CreateInt(int value) { auto pJSONValue = CreateWrapper(); pJSONValue->m_pDocument_mut = CreateDocument(); + + if (!pJSONValue->m_pDocument_mut) { + return nullptr; + } + pJSONValue->m_pVal_mut = yyjson_mut_int(pJSONValue->m_pDocument_mut->get(), value); if (!pJSONValue->m_pVal_mut) { return nullptr; } + yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut->get(), pJSONValue->m_pVal_mut); + return pJSONValue.release(); } @@ -2808,12 +3201,18 @@ JsonValue* JsonManager::CreateInt64(std::variant value) auto pJSONValue = CreateWrapper(); pJSONValue->m_pDocument_mut = CreateDocument(); + if (!pJSONValue->m_pDocument_mut) { + return nullptr; + } + + auto* doc = pJSONValue->m_pDocument_mut->get(); + std::visit([&](auto&& val) { using T = std::decay_t; if constexpr (std::is_same_v) { - pJSONValue->m_pVal_mut = yyjson_mut_sint(pJSONValue->m_pDocument_mut->get(), val); + pJSONValue->m_pVal_mut = yyjson_mut_sint(doc, val); } else if constexpr (std::is_same_v) { - pJSONValue->m_pVal_mut = yyjson_mut_uint(pJSONValue->m_pDocument_mut->get(), val); + pJSONValue->m_pVal_mut = yyjson_mut_uint(doc, val); } }, value); @@ -2821,6 +3220,8 @@ JsonValue* JsonManager::CreateInt64(std::variant value) return nullptr; } + yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut->get(), pJSONValue->m_pVal_mut); + return pJSONValue.release(); } @@ -2828,12 +3229,19 @@ JsonValue* JsonManager::CreateNull() { auto pJSONValue = CreateWrapper(); pJSONValue->m_pDocument_mut = CreateDocument(); + + if (!pJSONValue->m_pDocument_mut) { + return nullptr; + } + pJSONValue->m_pVal_mut = yyjson_mut_null(pJSONValue->m_pDocument_mut->get()); if (!pJSONValue->m_pVal_mut) { return nullptr; } + yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut->get(), pJSONValue->m_pVal_mut); + return pJSONValue.release(); } @@ -2845,12 +3253,19 @@ JsonValue* JsonManager::CreateString(const char* value) auto pJSONValue = CreateWrapper(); pJSONValue->m_pDocument_mut = CreateDocument(); + + if (!pJSONValue->m_pDocument_mut) { + return nullptr; + } + pJSONValue->m_pVal_mut = yyjson_mut_strcpy(pJSONValue->m_pDocument_mut->get(), value); if (!pJSONValue->m_pVal_mut) { return nullptr; } + yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut->get(), pJSONValue->m_pVal_mut); + return pJSONValue.release(); } @@ -3381,7 +3796,7 @@ bool JsonManager::PtrSet(JsonValue* handle, const char* path, JsonValue* value, return false; } - yyjson_mut_val* val_copy = nullptr; + yyjson_mut_val* val_copy; if (value->IsMutable()) { val_copy = yyjson_mut_val_mut_copy(handle->m_pDocument_mut->get(), value->m_pVal_mut); } else { @@ -3398,7 +3813,7 @@ bool JsonManager::PtrSet(JsonValue* handle, const char* path, JsonValue* value, yyjson_ptr_err ptrSetError; bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut->get(), path, strlen(path), val_copy, true, nullptr, &ptrSetError); - if (ptrSetError.code && error && error_size > 0) { + if (!success && ptrSetError.code && error && error_size > 0) { SetErrorSafe(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrSetError.msg, ptrSetError.code, ptrSetError.pos, path); } @@ -3415,10 +3830,18 @@ bool JsonManager::PtrSetBool(JsonValue* handle, const char* path, bool value, ch return false; } + yyjson_mut_val* val = yyjson_mut_bool(handle->m_pDocument_mut->get(), value); + if (!val) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to create JSON value"); + } + return false; + } + yyjson_ptr_err ptrSetError; - bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut->get(), path, strlen(path), yyjson_mut_bool(handle->m_pDocument_mut->get(), value), true, nullptr, &ptrSetError); + bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut->get(), path, strlen(path), val, true, nullptr, &ptrSetError); - if (ptrSetError.code && error && error_size > 0) { + if (!success && ptrSetError.code && error && error_size > 0) { SetErrorSafe(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrSetError.msg, ptrSetError.code, ptrSetError.pos, path); } @@ -3435,10 +3858,18 @@ bool JsonManager::PtrSetFloat(JsonValue* handle, const char* path, double value, return false; } + yyjson_mut_val* val = yyjson_mut_real(handle->m_pDocument_mut->get(), value); + if (!val) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to create JSON value"); + } + return false; + } + yyjson_ptr_err ptrSetError; - bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut->get(), path, strlen(path), yyjson_mut_real(handle->m_pDocument_mut->get(), value), true, nullptr, &ptrSetError); + bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut->get(), path, strlen(path), val, true, nullptr, &ptrSetError); - if (ptrSetError.code && error && error_size > 0) { + if (!success && ptrSetError.code && error && error_size > 0) { SetErrorSafe(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrSetError.msg, ptrSetError.code, ptrSetError.pos, path); } @@ -3455,10 +3886,18 @@ bool JsonManager::PtrSetInt(JsonValue* handle, const char* path, int value, char return false; } + yyjson_mut_val* val = yyjson_mut_int(handle->m_pDocument_mut->get(), value); + if (!val) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to create JSON value"); + } + return false; + } + yyjson_ptr_err ptrSetError; - bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut->get(), path, strlen(path), yyjson_mut_int(handle->m_pDocument_mut->get(), value), true, nullptr, &ptrSetError); + bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut->get(), path, strlen(path), val, true, nullptr, &ptrSetError); - if (ptrSetError.code && error && error_size > 0) { + if (!success && ptrSetError.code && error && error_size > 0) { SetErrorSafe(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrSetError.msg, ptrSetError.code, ptrSetError.pos, path); } @@ -3475,16 +3914,23 @@ bool JsonManager::PtrSetInt64(JsonValue* handle, const char* path, std::variant< return false; } - yyjson_ptr_err ptrSetError; - bool success = std::visit([&](auto&& val) -> bool { - using T = std::decay_t; + yyjson_mut_val* val = std::visit([&](auto&& val_input) -> yyjson_mut_val* { + using T = std::decay_t; if constexpr (std::is_same_v) { - return yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut->get(), path, strlen(path), yyjson_mut_sint(handle->m_pDocument_mut->get(), val), true, nullptr, &ptrSetError); - } else if constexpr (std::is_same_v) { - return yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut->get(), path, strlen(path), yyjson_mut_uint(handle->m_pDocument_mut->get(), val), true, nullptr, &ptrSetError); + return yyjson_mut_sint(handle->m_pDocument_mut->get(), val_input); + } else { + return yyjson_mut_uint(handle->m_pDocument_mut->get(), val_input); } - return false; }, value); + if (!val) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to create JSON value"); + } + return false; + } + + yyjson_ptr_err ptrSetError; + bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut->get(), path, strlen(path), val, true, nullptr, &ptrSetError); if (!success && ptrSetError.code && error && error_size > 0) { SetErrorSafe(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", @@ -3503,10 +3949,18 @@ bool JsonManager::PtrSetString(JsonValue* handle, const char* path, const char* return false; } + yyjson_mut_val* val = yyjson_mut_strcpy(handle->m_pDocument_mut->get(), value); + if (!val) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to create JSON value"); + } + return false; + } + yyjson_ptr_err ptrSetError; - bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut->get(), path, strlen(path), yyjson_mut_strcpy(handle->m_pDocument_mut->get(), value), true, nullptr, &ptrSetError); + bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut->get(), path, strlen(path), val, true, nullptr, &ptrSetError); - if (ptrSetError.code && error && error_size > 0) { + if (!success && ptrSetError.code && error && error_size > 0) { SetErrorSafe(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrSetError.msg, ptrSetError.code, ptrSetError.pos, path); } @@ -3523,10 +3977,18 @@ bool JsonManager::PtrSetNull(JsonValue* handle, const char* path, char* error, s return false; } + yyjson_mut_val* val = yyjson_mut_null(handle->m_pDocument_mut->get()); + if (!val) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to create JSON value"); + } + return false; + } + yyjson_ptr_err ptrSetError; - bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut->get(), path, strlen(path), yyjson_mut_null(handle->m_pDocument_mut->get()), true, nullptr, &ptrSetError); + bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut->get(), path, strlen(path), val, true, nullptr, &ptrSetError); - if (ptrSetError.code && error && error_size > 0) { + if (!success && ptrSetError.code && error && error_size > 0) { SetErrorSafe(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrSetError.msg, ptrSetError.code, ptrSetError.pos, path); } @@ -3543,7 +4005,7 @@ bool JsonManager::PtrAdd(JsonValue* handle, const char* path, JsonValue* value, return false; } - yyjson_mut_val* val_copy = nullptr; + yyjson_mut_val* val_copy; if (value->IsMutable()) { val_copy = yyjson_mut_val_mut_copy(handle->m_pDocument_mut->get(), value->m_pVal_mut); } else { @@ -3560,7 +4022,7 @@ bool JsonManager::PtrAdd(JsonValue* handle, const char* path, JsonValue* value, yyjson_ptr_err ptrAddError; bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut->get(), path, strlen(path), val_copy, true, nullptr, &ptrAddError); - if (ptrAddError.code && error && error_size > 0) { + if (!success && ptrAddError.code && error && error_size > 0) { SetErrorSafe(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrAddError.msg, ptrAddError.code, ptrAddError.pos, path); } @@ -3577,10 +4039,18 @@ bool JsonManager::PtrAddBool(JsonValue* handle, const char* path, bool value, ch return false; } + yyjson_mut_val* val = yyjson_mut_bool(handle->m_pDocument_mut->get(), value); + if (!val) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to create JSON value"); + } + return false; + } + yyjson_ptr_err ptrAddError; - bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut->get(), path, strlen(path), yyjson_mut_bool(handle->m_pDocument_mut->get(), value), true, nullptr, &ptrAddError); + bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut->get(), path, strlen(path), val, true, nullptr, &ptrAddError); - if (ptrAddError.code && error && error_size > 0) { + if (!success && ptrAddError.code && error && error_size > 0) { SetErrorSafe(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrAddError.msg, ptrAddError.code, ptrAddError.pos, path); } @@ -3597,10 +4067,18 @@ bool JsonManager::PtrAddFloat(JsonValue* handle, const char* path, double value, return false; } + yyjson_mut_val* val = yyjson_mut_real(handle->m_pDocument_mut->get(), value); + if (!val) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to create JSON value"); + } + return false; + } + yyjson_ptr_err ptrAddError; - bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut->get(), path, strlen(path), yyjson_mut_real(handle->m_pDocument_mut->get(), value), true, nullptr, &ptrAddError); + bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut->get(), path, strlen(path), val, true, nullptr, &ptrAddError); - if (ptrAddError.code && error && error_size > 0) { + if (!success && ptrAddError.code && error && error_size > 0) { SetErrorSafe(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrAddError.msg, ptrAddError.code, ptrAddError.pos, path); } @@ -3617,10 +4095,18 @@ bool JsonManager::PtrAddInt(JsonValue* handle, const char* path, int value, char return false; } + yyjson_mut_val* val = yyjson_mut_int(handle->m_pDocument_mut->get(), value); + if (!val) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to create JSON value"); + } + return false; + } + yyjson_ptr_err ptrAddError; - bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut->get(), path, strlen(path), yyjson_mut_int(handle->m_pDocument_mut->get(), value), true, nullptr, &ptrAddError); + bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut->get(), path, strlen(path), val, true, nullptr, &ptrAddError); - if (ptrAddError.code && error && error_size > 0) { + if (!success && ptrAddError.code && error && error_size > 0) { SetErrorSafe(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrAddError.msg, ptrAddError.code, ptrAddError.pos, path); } @@ -3637,18 +4123,25 @@ bool JsonManager::PtrAddInt64(JsonValue* handle, const char* path, std::variant< return false; } - yyjson_ptr_err ptrAddError; - bool success = std::visit([&](auto&& val) -> bool { - using T = std::decay_t; + yyjson_mut_val* val = std::visit([&](auto&& val_input) -> yyjson_mut_val* { + using T = std::decay_t; if constexpr (std::is_same_v) { - return yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut->get(), path, strlen(path), yyjson_mut_sint(handle->m_pDocument_mut->get(), val), true, nullptr, &ptrAddError); - } else if constexpr (std::is_same_v) { - return yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut->get(), path, strlen(path), yyjson_mut_uint(handle->m_pDocument_mut->get(), val), true, nullptr, &ptrAddError); + return yyjson_mut_sint(handle->m_pDocument_mut->get(), val_input); + } else { + return yyjson_mut_uint(handle->m_pDocument_mut->get(), val_input); } - return false; }, value); + if (!val) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to create JSON value"); + } + return false; + } - if (ptrAddError.code && error && error_size > 0) { + yyjson_ptr_err ptrAddError; + bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut->get(), path, strlen(path), val, true, nullptr, &ptrAddError); + + if (!success && ptrAddError.code && error && error_size > 0) { SetErrorSafe(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrAddError.msg, ptrAddError.code, ptrAddError.pos, path); } @@ -3665,10 +4158,18 @@ bool JsonManager::PtrAddString(JsonValue* handle, const char* path, const char* return false; } + yyjson_mut_val* val = yyjson_mut_strcpy(handle->m_pDocument_mut->get(), value); + if (!val) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to create JSON value"); + } + return false; + } + yyjson_ptr_err ptrAddError; - bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut->get(), path, strlen(path), yyjson_mut_strcpy(handle->m_pDocument_mut->get(), value), true, nullptr, &ptrAddError); + bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut->get(), path, strlen(path), val, true, nullptr, &ptrAddError); - if (ptrAddError.code && error && error_size > 0) { + if (!success && ptrAddError.code && error && error_size > 0) { SetErrorSafe(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrAddError.msg, ptrAddError.code, ptrAddError.pos, path); } @@ -3685,10 +4186,18 @@ bool JsonManager::PtrAddNull(JsonValue* handle, const char* path, char* error, s return false; } + yyjson_mut_val* val = yyjson_mut_null(handle->m_pDocument_mut->get()); + if (!val) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to create JSON value"); + } + return false; + } + yyjson_ptr_err ptrAddError; - bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut->get(), path, strlen(path), yyjson_mut_null(handle->m_pDocument_mut->get()), true, nullptr, &ptrAddError); + bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut->get(), path, strlen(path), val, true, nullptr, &ptrAddError); - if (ptrAddError.code && error && error_size > 0) { + if (!success && ptrAddError.code && error && error_size > 0) { SetErrorSafe(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrAddError.msg, ptrAddError.code, ptrAddError.pos, path); } @@ -3708,7 +4217,7 @@ bool JsonManager::PtrRemove(JsonValue* handle, const char* path, char* error, si yyjson_ptr_err ptrRemoveError; bool success = yyjson_mut_doc_ptr_removex(handle->m_pDocument_mut->get(), path, strlen(path), nullptr, &ptrRemoveError) != nullptr; - if (ptrRemoveError.code && error && error_size > 0) { + if (!success && ptrRemoveError.code && error && error_size > 0) { SetErrorSafe(error, error_size, "Failed to remove JSON pointer: %s (error code: %u, position: %zu, path: %s)", ptrRemoveError.msg, ptrRemoveError.code, ptrRemoveError.pos, path); } @@ -4142,14 +4651,18 @@ JsonArrIter* JsonManager::ArrIterWith(JsonValue* handle) iter->m_isMutable = handle->IsMutable(); iter->m_pDocument_mut = handle->m_pDocument_mut; iter->m_pDocument = handle->m_pDocument; + iter->m_rootMut = nullptr; + iter->m_rootImm = nullptr; if (handle->IsMutable()) { - if (!yyjson_mut_arr_iter_init(handle->m_pVal_mut, &iter->m_iterMut)) { + iter->m_rootMut = handle->m_pVal_mut; + if (!iter->m_rootMut || !yyjson_mut_arr_iter_init(iter->m_rootMut, &iter->m_iterMut)) { delete iter; return nullptr; } } else { - if (!yyjson_arr_iter_init(handle->m_pVal, &iter->m_iterImm)) { + iter->m_rootImm = handle->m_pVal; + if (!iter->m_rootImm || !yyjson_arr_iter_init(iter->m_rootImm, &iter->m_iterImm)) { delete iter; return nullptr; } @@ -4159,13 +4672,34 @@ JsonArrIter* JsonManager::ArrIterWith(JsonValue* handle) return iter; } +bool JsonManager::ArrIterReset(JsonArrIter* iter) +{ + if (!iter || !iter->m_initialized) { + return false; + } + + bool success; + if (iter->m_isMutable) { + success = iter->m_rootMut && yyjson_mut_arr_iter_init(iter->m_rootMut, &iter->m_iterMut); + } else { + success = iter->m_rootImm && yyjson_arr_iter_init(iter->m_rootImm, &iter->m_iterImm); + } + + if (!success) { + iter->m_initialized = false; + return false; + } + + return true; +} + JsonValue* JsonManager::ArrIterNext(JsonArrIter* iter) { if (!iter || !iter->m_initialized) { return nullptr; } - JsonValue* val = nullptr; + JsonValue* val; if (iter->m_isMutable) { yyjson_mut_val* raw_val = yyjson_mut_arr_iter_next(&iter->m_iterMut); @@ -4248,14 +4782,18 @@ JsonObjIter* JsonManager::ObjIterWith(JsonValue* handle) iter->m_isMutable = handle->IsMutable(); iter->m_pDocument_mut = handle->m_pDocument_mut; iter->m_pDocument = handle->m_pDocument; + iter->m_rootMut = nullptr; + iter->m_rootImm = nullptr; if (handle->IsMutable()) { - if (!yyjson_mut_obj_iter_init(handle->m_pVal_mut, &iter->m_iterMut)) { + iter->m_rootMut = handle->m_pVal_mut; + if (!iter->m_rootMut || !yyjson_mut_obj_iter_init(iter->m_rootMut, &iter->m_iterMut)) { delete iter; return nullptr; } } else { - if (!yyjson_obj_iter_init(handle->m_pVal, &iter->m_iterImm)) { + iter->m_rootImm = handle->m_pVal; + if (!iter->m_rootImm || !yyjson_obj_iter_init(iter->m_rootImm, &iter->m_iterImm)) { delete iter; return nullptr; } @@ -4265,6 +4803,28 @@ JsonObjIter* JsonManager::ObjIterWith(JsonValue* handle) return iter; } +bool JsonManager::ObjIterReset(JsonObjIter* iter) +{ + if (!iter || !iter->m_initialized) { + return false; + } + + bool success; + if (iter->m_isMutable) { + success = iter->m_rootMut && yyjson_mut_obj_iter_init(iter->m_rootMut, &iter->m_iterMut); + } else { + success = iter->m_rootImm && yyjson_obj_iter_init(iter->m_rootImm, &iter->m_iterImm); + } + + if (!success) { + iter->m_initialized = false; + return false; + } + + iter->m_currentKey = nullptr; + return true; +} + void* JsonManager::ObjIterNext(JsonObjIter* iter) { if (!iter || !iter->m_initialized) { @@ -4451,6 +5011,13 @@ JsonValue* JsonManager::ReadNumber(const char* dat, uint32_t read_flg, char* err auto pJSONValue = CreateWrapper(); pJSONValue->m_pDocument_mut = CreateDocument(); + if (!pJSONValue->m_pDocument_mut) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to create number document"); + } + return nullptr; + } + yyjson_mut_val* val = yyjson_mut_int(pJSONValue->m_pDocument_mut->get(), 0); if (!val) { if (error && error_size > 0) { @@ -4491,7 +5058,7 @@ bool JsonManager::WriteNumber(JsonValue* handle, char* buffer, size_t buffer_siz return false; } - char* result = nullptr; + char* result; if (handle->IsMutable()) { result = yyjson_mut_write_number(handle->m_pVal_mut, buffer); } else { diff --git a/src/JsonManager.h b/src/JsonManager.h index a2b8ca5..2fc6a14 100755 --- a/src/JsonManager.h +++ b/src/JsonManager.h @@ -1,5 +1,5 @@ -#ifndef _INCLUDE_SM_JSON_JSONMANAGER_H_ -#define _INCLUDE_SM_JSON_JSONMANAGER_H_ +#ifndef _INCLUDE_JSONMANAGER_H_ +#define _INCLUDE_JSONMANAGER_H_ #include #include @@ -15,14 +15,15 @@ */ class RefCounted { private: - mutable int ref_count_ = 0; + mutable size_t ref_count_ = 0; protected: virtual ~RefCounted() = default; - RefCounted() = default; - RefCounted(const RefCounted &) : ref_count_(0) {} - RefCounted &operator=(const RefCounted &) { return *this; } + + RefCounted(const RefCounted &) = delete; + RefCounted &operator=(const RefCounted &) = delete; + RefCounted(RefCounted &&) noexcept : ref_count_(0) {} RefCounted &operator=(RefCounted &&) noexcept { return *this; } @@ -30,12 +31,13 @@ class RefCounted { void add_ref() const noexcept { ++ref_count_; } void release() const noexcept { + assert(ref_count_ > 0 && "Reference count underflow"); if (--ref_count_ == 0) { delete this; } } - int use_count() const noexcept { return ref_count_; } + size_t use_count() const noexcept { return ref_count_; } }; /** @@ -43,6 +45,8 @@ class RefCounted { * * Similar to std::shared_ptr but uses intrusive reference counting. * Automatically manages object lifetime through add_ref() and release(). + * @warning This implementation is not thread-safe. It must only be used + * in single-threaded contexts or with external synchronization. */ template class RefPtr { private: @@ -100,7 +104,7 @@ template class RefPtr { explicit operator bool() const noexcept { return ptr_ != nullptr; } - int use_count() const noexcept { return ptr_ ? ptr_->use_count() : 0; } + size_t use_count() const noexcept { return ptr_ ? ptr_->use_count() : 0; } void reset(T *p = nullptr) noexcept { if (ptr_ != p) { @@ -200,7 +204,7 @@ class JsonValue { return m_pDocument != nullptr; } - int GetDocumentRefCount() const { + size_t GetDocumentRefCount() const { if (m_pDocument_mut) { return m_pDocument_mut.use_count(); } else if (m_pDocument) { @@ -252,8 +256,9 @@ class JsonArrIter { RefPtr m_pDocument; yyjson_mut_arr_iter m_iterMut; - yyjson_arr_iter m_iterImm; + yyjson_mut_val* m_rootMut{ nullptr }; + yyjson_val* m_rootImm{ nullptr }; Handle_t m_handle{ BAD_HANDLE }; bool m_isMutable{ false }; @@ -281,8 +286,9 @@ class JsonObjIter { RefPtr m_pDocument; yyjson_mut_obj_iter m_iterMut; - yyjson_obj_iter m_iterImm; + yyjson_mut_val* m_rootMut{ nullptr }; + yyjson_val* m_rootImm{ nullptr }; void* m_currentKey{ nullptr }; @@ -303,7 +309,15 @@ class JsonManager : public IJsonManager yyjson_read_flag read_flg, char* error, size_t error_size) override; virtual bool WriteToString(JsonValue* handle, char* buffer, size_t buffer_size, yyjson_write_flag write_flg, size_t* out_size) override; - virtual char* WriteToStringPtr(JsonValue* handle, yyjson_write_flag write_flg, size_t* out_size); + virtual char* WriteToStringPtr(JsonValue* handle, yyjson_write_flag write_flg, size_t* out_size) override; + virtual JsonValue* ApplyJsonPatch(JsonValue* target, JsonValue* patch, bool result_mutable, + char* error, size_t error_size) override; + virtual bool JsonPatchInPlace(JsonValue* target, JsonValue* patch, + char* error, size_t error_size) override; + virtual JsonValue* ApplyMergePatch(JsonValue* target, JsonValue* patch, bool result_mutable, + char* error, size_t error_size) override; + virtual bool MergePatchInPlace(JsonValue* target, JsonValue* patch, + char* error, size_t error_size) override; virtual bool WriteToFile(JsonValue* handle, const char* path, yyjson_write_flag write_flg, char* error, size_t error_size) override; virtual bool Equals(JsonValue* handle1, JsonValue* handle2) override; @@ -331,7 +345,7 @@ class JsonManager : public IJsonManager virtual bool IsMutable(JsonValue* handle) override; virtual bool IsImmutable(JsonValue* handle) override; virtual size_t GetReadSize(JsonValue* handle) override; - virtual int GetRefCount(JsonValue* handle) override; + virtual size_t GetRefCount(JsonValue* handle) override; // ========== Object Operations ========== virtual JsonValue* ObjectInit() override; @@ -416,13 +430,12 @@ class JsonManager : public IJsonManager virtual bool ArrayRemove(JsonValue* handle, size_t index) override; virtual bool ArrayRemoveFirst(JsonValue* handle) override; virtual bool ArrayRemoveLast(JsonValue* handle) override; - virtual bool ArrayRemoveRange(JsonValue* handle, size_t start_index, size_t end_index) override; + virtual bool ArrayRemoveRange(JsonValue* handle, size_t start_index, size_t count) override; virtual bool ArrayClear(JsonValue* handle) override; virtual int ArrayIndexOfBool(JsonValue* handle, bool search_value) override; virtual int ArrayIndexOfString(JsonValue* handle, const char* search_value) override; virtual int ArrayIndexOfInt(JsonValue* handle, int search_value) override; - virtual int ArrayIndexOfInt64(JsonValue* handle, int64_t search_value) override; - virtual int ArrayIndexOfUint64(JsonValue* handle, uint64_t search_value) override; + virtual int ArrayIndexOfInt64(JsonValue* handle, std::variant search_value) override; virtual int ArrayIndexOfFloat(JsonValue* handle, double search_value) override; virtual bool ArraySort(JsonValue* handle, JSON_SORT_ORDER sort_mode) override; @@ -483,6 +496,7 @@ class JsonManager : public IJsonManager // ========== Array Iterator Operations ========== virtual JsonArrIter* ArrIterInit(JsonValue* handle) override; virtual JsonArrIter* ArrIterWith(JsonValue* handle) override; + virtual bool ArrIterReset(JsonArrIter* iter) override; virtual JsonValue* ArrIterNext(JsonArrIter* iter) override; virtual bool ArrIterHasNext(JsonArrIter* iter) override; virtual size_t ArrIterGetIndex(JsonArrIter* iter) override; @@ -491,6 +505,7 @@ class JsonManager : public IJsonManager // ========== Object Iterator Operations ========== virtual JsonObjIter* ObjIterInit(JsonValue* handle) override; virtual JsonObjIter* ObjIterWith(JsonValue* handle) override; + virtual bool ObjIterReset(JsonObjIter* iter) override; virtual void* ObjIterNext(JsonObjIter* iter) override; virtual bool ObjIterHasNext(JsonObjIter* iter) override; virtual JsonValue* ObjIterGetVal(JsonObjIter* iter, void* key) override; @@ -548,6 +563,7 @@ class JsonManager : public IJsonManager static RefPtr CopyDocument(yyjson_doc* doc); static RefPtr CreateDocument(); static RefPtr WrapImmutableDocument(yyjson_doc* doc); + static RefPtr CloneValueToMutable(JsonValue* value); // Pack helper methods static const char* SkipSeparators(const char* ptr); @@ -564,4 +580,4 @@ class JsonManager : public IJsonManager static PtrGetValueResult PtrGetValueInternal(JsonValue* handle, const char* path); }; -#endif // _INCLUDE_SM_JSON_JSONMANAGER_H_ \ No newline at end of file +#endif // _INCLUDE_JSONMANAGER_H_ \ No newline at end of file diff --git a/src/JsonNatives.cpp b/src/JsonNatives.cpp index c53182a..c9b207c 100755 --- a/src/JsonNatives.cpp +++ b/src/JsonNatives.cpp @@ -170,7 +170,7 @@ static cell_t json_doc_parse(IPluginContext* pContext, const cell_t* params) bool is_file = params[2]; bool is_mutable_doc = params[3]; - yyjson_read_flag read_flg = static_cast(params[4]); + uint32_t read_flg = static_cast(params[4]); char error[JSON_ERROR_BUFFER_SIZE]; JsonValue* pJSONValue = g_pJsonManager->ParseJSON(str, is_file, is_mutable_doc, read_flg, error, sizeof(error)); @@ -224,7 +224,7 @@ static cell_t json_obj_parse_str(IPluginContext* pContext, const cell_t* params) { char* str; pContext->LocalToString(params[1], &str); - yyjson_read_flag read_flg = static_cast(params[2]); + uint32_t read_flg = static_cast(params[2]); char error[JSON_PACK_ERROR_SIZE]; JsonValue* pJSONValue = g_pJsonManager->ObjectParseString(str, read_flg, error, sizeof(error)); @@ -240,7 +240,7 @@ static cell_t json_obj_parse_file(IPluginContext* pContext, const cell_t* params { char* path; pContext->LocalToString(params[1], &path); - yyjson_read_flag read_flg = static_cast(params[2]); + uint32_t read_flg = static_cast(params[2]); char error[JSON_PACK_ERROR_SIZE]; JsonValue* pJSONValue = g_pJsonManager->ObjectParseFile(path, read_flg, error, sizeof(error)); @@ -256,7 +256,7 @@ static cell_t json_arr_parse_str(IPluginContext* pContext, const cell_t* params) { char* str; pContext->LocalToString(params[1], &str); - yyjson_read_flag read_flg = static_cast(params[2]); + uint32_t read_flg = static_cast(params[2]); char error[JSON_PACK_ERROR_SIZE]; JsonValue* pJSONValue = g_pJsonManager->ArrayParseString(str, read_flg, error, sizeof(error)); @@ -272,7 +272,7 @@ static cell_t json_arr_parse_file(IPluginContext* pContext, const cell_t* params { char* path; pContext->LocalToString(params[1], &path); - yyjson_read_flag read_flg = static_cast(params[2]); + uint32_t read_flg = static_cast(params[2]); char error[JSON_PACK_ERROR_SIZE]; JsonValue* pJSONValue = g_pJsonManager->ArrayParseFile(path, read_flg, error, sizeof(error)); @@ -325,35 +325,14 @@ static cell_t json_arr_index_of_integer64(IPluginContext* pContext, const cell_t char* searchStr; pContext->LocalToString(params[2], &searchStr); - char* endptr; - errno = 0; - long long searchValue = strtoll(searchStr, &endptr, 10); - - if (errno == ERANGE || *endptr != '\0') { - return pContext->ThrowNativeError("Invalid integer64 value: %s", searchStr); - } - - return g_pJsonManager->ArrayIndexOfInt64(handle, searchValue); -} - -static cell_t json_arr_index_of_uint64(IPluginContext* pContext, const cell_t* params) -{ - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - char* searchStr; - pContext->LocalToString(params[2], &searchStr); - - char* endptr; - errno = 0; - unsigned long long searchValue = strtoull(searchStr, &endptr, 10); + std::variant variant_value; + char error[JSON_ERROR_BUFFER_SIZE]; - if (errno == ERANGE || *endptr != '\0') { - return pContext->ThrowNativeError("Invalid unsigned integer64 value: %s", searchStr); + if (!g_pJsonManager->ParseInt64Variant(searchStr, &variant_value, error, sizeof(error))) { + return pContext->ThrowNativeError("%s", error); } - return g_pJsonManager->ArrayIndexOfUint64(handle, searchValue); + return g_pJsonManager->ArrayIndexOfInt64(handle, variant_value); } static cell_t json_arr_index_of_float(IPluginContext* pContext, const cell_t* params) @@ -597,6 +576,7 @@ static cell_t json_create_integer64(IPluginContext* pContext, const cell_t* para std::variant variant_value; char error[JSON_ERROR_BUFFER_SIZE]; + if (!g_pJsonManager->ParseInt64Variant(value, &variant_value, error, sizeof(error))) { return pContext->ThrowNativeError("%s", error); } @@ -694,8 +674,8 @@ static cell_t json_get_str(IPluginContext* pContext, const cell_t* params) if (!handle) return 0; - const char* str = nullptr; - size_t len = 0; + const char* str; + size_t len; if (!g_pJsonManager->GetString(handle, &str, &len)) { return pContext->ThrowNativeError("Type mismatch: expected string value"); @@ -729,7 +709,7 @@ static cell_t json_get_serialized_size(IPluginContext* pContext, const cell_t* p if (!handle) return 0; - yyjson_write_flag write_flg = static_cast(params[2]); + uint32_t write_flg = static_cast(params[2]); size_t size = g_pJsonManager->GetSerializedSize(handle, write_flg); return static_cast(size); @@ -899,7 +879,11 @@ static cell_t json_arr_get_val(IPluginContext* pContext, const cell_t* params) if (!handle) return 0; - size_t index = static_cast(params[2]); + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); JsonValue* pJSONValue = g_pJsonManager->ArrayGet(handle, index); @@ -946,7 +930,11 @@ static cell_t json_arr_get_bool(IPluginContext* pContext, const cell_t* params) if (!handle) return 0; - size_t index = static_cast(params[2]); + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); bool value; if (!g_pJsonManager->ArrayGetBool(handle, index, &value)) { @@ -962,7 +950,11 @@ static cell_t json_arr_get_float(IPluginContext* pContext, const cell_t* params) if (!handle) return 0; - size_t index = static_cast(params[2]); + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); double value; if (!g_pJsonManager->ArrayGetFloat(handle, index, &value)) { @@ -978,7 +970,11 @@ static cell_t json_arr_get_integer(IPluginContext* pContext, const cell_t* param if (!handle) return 0; - size_t index = static_cast(params[2]); + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); int value; if (!g_pJsonManager->ArrayGetInt(handle, index, &value)) { @@ -994,9 +990,13 @@ static cell_t json_arr_get_integer64(IPluginContext* pContext, const cell_t* par if (!handle) return 0; - size_t index = static_cast(params[2]); - std::variant value; + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); + std::variant value; if (!g_pJsonManager->ArrayGetInt64(handle, index, &value)) { return pContext->ThrowNativeError("Failed to get integer64 at index %d", index); } @@ -1018,10 +1018,14 @@ static cell_t json_arr_get_str(IPluginContext* pContext, const cell_t* params) if (!handle) return 0; - size_t index = static_cast(params[2]); - const char* str = nullptr; - size_t len = 0; + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); + const char* str; + size_t len; if (!g_pJsonManager->ArrayGetString(handle, index, &str, &len)) { return pContext->ThrowNativeError("Failed to get string at index %d", index); } @@ -1042,7 +1046,11 @@ static cell_t json_arr_is_null(IPluginContext* pContext, const cell_t* params) if (!handle) return 0; - size_t index = static_cast(params[2]); + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); return g_pJsonManager->ArrayIsNull(handle, index); } @@ -1058,7 +1066,12 @@ static cell_t json_arr_replace_val(IPluginContext* pContext, const cell_t* param return pContext->ThrowNativeError("Cannot replace value in an immutable JSON array"); } - size_t index = static_cast(params[2]); + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); + return g_pJsonManager->ArrayReplace(handle1, index, handle2); } @@ -1072,7 +1085,12 @@ static cell_t json_arr_replace_bool(IPluginContext* pContext, const cell_t* para return pContext->ThrowNativeError("Cannot replace value in an immutable JSON array"); } - size_t index = static_cast(params[2]); + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); + return g_pJsonManager->ArrayReplaceBool(handle, index, params[3]); } @@ -1086,7 +1104,12 @@ static cell_t json_arr_replace_float(IPluginContext* pContext, const cell_t* par return pContext->ThrowNativeError("Cannot replace value in an immutable JSON array"); } - size_t index = static_cast(params[2]); + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); + return g_pJsonManager->ArrayReplaceFloat(handle, index, sp_ctof(params[3])); } @@ -1100,7 +1123,12 @@ static cell_t json_arr_replace_integer(IPluginContext* pContext, const cell_t* p return pContext->ThrowNativeError("Cannot replace value in an immutable JSON array"); } - size_t index = static_cast(params[2]); + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); + return g_pJsonManager->ArrayReplaceInt(handle, index, params[3]); } @@ -1119,11 +1147,17 @@ static cell_t json_arr_replace_integer64(IPluginContext* pContext, const cell_t* std::variant variant_value; char error[JSON_ERROR_BUFFER_SIZE]; + if (!g_pJsonManager->ParseInt64Variant(value, &variant_value, error, sizeof(error))) { return pContext->ThrowNativeError("%s", error); } - size_t index = static_cast(params[2]); + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); + return g_pJsonManager->ArrayReplaceInt64(handle, index, variant_value); } @@ -1137,7 +1171,12 @@ static cell_t json_arr_replace_null(IPluginContext* pContext, const cell_t* para return pContext->ThrowNativeError("Cannot replace value in an immutable JSON array"); } - size_t index = static_cast(params[2]); + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); + return g_pJsonManager->ArrayReplaceNull(handle, index); } @@ -1154,7 +1193,12 @@ static cell_t json_arr_replace_str(IPluginContext* pContext, const cell_t* param char* val; pContext->LocalToString(params[3], &val); - size_t index = static_cast(params[2]); + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); + return g_pJsonManager->ArrayReplaceString(handle, index, val); } @@ -1226,6 +1270,7 @@ static cell_t json_arr_append_integer64(IPluginContext* pContext, const cell_t* std::variant variant_value; char error[JSON_ERROR_BUFFER_SIZE]; + if (!g_pJsonManager->ParseInt64Variant(value, &variant_value, error, sizeof(error))) { return pContext->ThrowNativeError("%s", error); } @@ -1271,7 +1316,17 @@ static cell_t json_arr_insert(IPluginContext* pContext, const cell_t* params) return pContext->ThrowNativeError("Cannot insert value into an immutable JSON array"); } - size_t index = params[2]; + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); + + size_t arr_size = g_pJsonManager->ArrayGetSize(handle); + if (index > arr_size) { + return pContext->ThrowNativeError("Index is out of bounds (got %d, max %zu)", index, arr_size); + } + JsonValue* value = g_pJsonManager->GetFromHandle(pContext, params[3]); if (!value) return 0; @@ -1287,7 +1342,17 @@ static cell_t json_arr_insert_bool(IPluginContext* pContext, const cell_t* param return pContext->ThrowNativeError("Cannot insert value into an immutable JSON array"); } - size_t index = params[2]; + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); + + size_t arr_size = g_pJsonManager->ArrayGetSize(handle); + if (index > arr_size) { + return pContext->ThrowNativeError("Index is out of bounds (got %d, max %zu)", index, arr_size); + } + bool value = params[3] != 0; return g_pJsonManager->ArrayInsertBool(handle, index, value); @@ -1302,7 +1367,17 @@ static cell_t json_arr_insert_int(IPluginContext* pContext, const cell_t* params return pContext->ThrowNativeError("Cannot insert value into an immutable JSON array"); } - size_t index = params[2]; + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); + + size_t arr_size = g_pJsonManager->ArrayGetSize(handle); + if (index > arr_size) { + return pContext->ThrowNativeError("Index is out of bounds (got %d, max %zu)", index, arr_size); + } + int value = params[3]; return g_pJsonManager->ArrayInsertInt(handle, index, value); @@ -1317,12 +1392,23 @@ static cell_t json_arr_insert_int64(IPluginContext* pContext, const cell_t* para return pContext->ThrowNativeError("Cannot insert value into an immutable JSON array"); } - size_t index = params[2]; + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); + + size_t arr_size = g_pJsonManager->ArrayGetSize(handle); + if (index > arr_size) { + return pContext->ThrowNativeError("Index is out of bounds (got %d, max %zu)", index, arr_size); + } + char* value; pContext->LocalToString(params[3], &value); std::variant variant_value; char error[JSON_ERROR_BUFFER_SIZE]; + if (!g_pJsonManager->ParseInt64Variant(value, &variant_value, error, sizeof(error))) { return pContext->ThrowNativeError("%s", error); } @@ -1339,7 +1425,17 @@ static cell_t json_arr_insert_float(IPluginContext* pContext, const cell_t* para return pContext->ThrowNativeError("Cannot insert value into an immutable JSON array"); } - size_t index = params[2]; + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); + + size_t arr_size = g_pJsonManager->ArrayGetSize(handle); + if (index > arr_size) { + return pContext->ThrowNativeError("Index is out of bounds (got %d, max %zu)", index, arr_size); + } + double value = sp_ctof(params[3]); return g_pJsonManager->ArrayInsertFloat(handle, index, value); @@ -1354,7 +1450,17 @@ static cell_t json_arr_insert_str(IPluginContext* pContext, const cell_t* params return pContext->ThrowNativeError("Cannot insert value into an immutable JSON array"); } - size_t index = params[2]; + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); + + size_t arr_size = g_pJsonManager->ArrayGetSize(handle); + if (index > arr_size) { + return pContext->ThrowNativeError("Index is out of bounds (got %d, max %zu)", index, arr_size); + } + char* str; pContext->LocalToString(params[3], &str); @@ -1370,7 +1476,17 @@ static cell_t json_arr_insert_null(IPluginContext* pContext, const cell_t* param return pContext->ThrowNativeError("Cannot insert value into an immutable JSON array"); } - size_t index = params[2]; + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); + + size_t arr_size = g_pJsonManager->ArrayGetSize(handle); + if (index > arr_size) { + return pContext->ThrowNativeError("Index is out of bounds (got %d, max %zu)", index, arr_size); + } + return g_pJsonManager->ArrayInsertNull(handle, index); } @@ -1432,6 +1548,7 @@ static cell_t json_arr_prepend_int64(IPluginContext* pContext, const cell_t* par std::variant variant_value; char error[JSON_ERROR_BUFFER_SIZE]; + if (!g_pJsonManager->ParseInt64Variant(value, &variant_value, error, sizeof(error))) { return pContext->ThrowNativeError("%s", error); } @@ -1490,7 +1607,12 @@ static cell_t json_arr_remove(IPluginContext* pContext, const cell_t* params) return pContext->ThrowNativeError("Cannot remove value from an immutable JSON array"); } - size_t index = static_cast(params[2]); + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); + return g_pJsonManager->ArrayRemove(handle, index); } @@ -1530,10 +1652,18 @@ static cell_t json_arr_remove_range(IPluginContext* pContext, const cell_t* para return pContext->ThrowNativeError("Cannot remove value from an immutable JSON array"); } - size_t start_index = static_cast(params[2]); - size_t end_index = static_cast(params[3]); + cell_t start_index_param = params[2]; + cell_t count_param = params[3]; + if (start_index_param < 0) { + return pContext->ThrowNativeError("Start index must be >= 0 (got %d)", start_index_param); + } + if (count_param < 0) { + return pContext->ThrowNativeError("Count must be >= 0 (got %d)", count_param); + } - return g_pJsonManager->ArrayRemoveRange(handle, start_index, end_index); + size_t start_index = static_cast(start_index_param); + size_t count = static_cast(count_param); + return g_pJsonManager->ArrayRemoveRange(handle, start_index, count); } static cell_t json_arr_clear(IPluginContext* pContext, const cell_t* params) @@ -1556,9 +1686,9 @@ static cell_t json_doc_write_to_str(IPluginContext* pContext, const cell_t* para if (!handle) return 0; size_t buffer_size = static_cast(params[3]); - yyjson_write_flag write_flg = static_cast(params[4]); + uint32_t write_flg = static_cast(params[4]); - size_t json_size = 0; + size_t json_size; char* json_str = g_pJsonManager->WriteToStringPtr(handle, write_flg, &json_size); if (!json_str) { @@ -1584,7 +1714,7 @@ static cell_t json_doc_write_to_file(IPluginContext* pContext, const cell_t* par char* path; pContext->LocalToString(params[2], &path); - yyjson_write_flag write_flg = static_cast(params[3]); + uint32_t write_flg = static_cast(params[3]); char error[JSON_PACK_ERROR_SIZE]; if (!g_pJsonManager->WriteToFile(handle, path, write_flg, error, sizeof(error))) { @@ -1610,8 +1740,12 @@ static cell_t json_obj_get_key(IPluginContext* pContext, const cell_t* params) if (!handle) return 0; - size_t index = static_cast(params[2]); - const char* key = nullptr; + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); + const char* key; if (!g_pJsonManager->ObjectGetKey(handle, index, &key)) { return pContext->ThrowNativeError("Index %d is out of bounds", index); @@ -1627,7 +1761,11 @@ static cell_t json_obj_get_val_at(IPluginContext* pContext, const cell_t* params if (!handle) return 0; - size_t index = static_cast(params[2]); + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); JsonValue* pJSONValue = g_pJsonManager->ObjectGetValueAt(handle, index); @@ -1741,8 +1879,8 @@ static cell_t json_obj_get_str(IPluginContext* pContext, const cell_t* params) char* key; pContext->LocalToString(params[2], &key); - const char* str = nullptr; - size_t len = 0; + const char* str; + size_t len; if (!g_pJsonManager->ObjectGetString(handle, key, &str, &len)) { return pContext->ThrowNativeError("Failed to get string for key '%s'", key); } @@ -1779,7 +1917,7 @@ static cell_t json_obj_is_null(IPluginContext* pContext, const cell_t* params) char* key; pContext->LocalToString(params[2], &key); - bool is_null = false; + bool is_null; if (!g_pJsonManager->ObjectIsNull(handle, key, &is_null)) { return pContext->ThrowNativeError("Key not found: %s", key); } @@ -1907,6 +2045,7 @@ static cell_t json_obj_set_integer64(IPluginContext* pContext, const cell_t* par std::variant variant_value; char error[JSON_ERROR_BUFFER_SIZE]; + if (!g_pJsonManager->ParseInt64Variant(value, &variant_value, error, sizeof(error))) { return pContext->ThrowNativeError("%s", error); } @@ -2071,8 +2210,8 @@ static cell_t json_ptr_get_str(IPluginContext* pContext, const cell_t* params) char* path; pContext->LocalToString(params[2], &path); - const char* str = nullptr; - size_t len = 0; + const char* str; + size_t len; char error[JSON_PACK_ERROR_SIZE]; if (!g_pJsonManager->PtrGetString(handle, path, &str, &len, error, sizeof(error))) { return pContext->ThrowNativeError("%s", error); @@ -2225,6 +2364,7 @@ static cell_t json_ptr_set_integer64(IPluginContext* pContext, const cell_t* par std::variant variant_value; char error[JSON_PACK_ERROR_SIZE]; + if (!g_pJsonManager->ParseInt64Variant(value, &variant_value, error, sizeof(error))) { return pContext->ThrowNativeError("%s", error); } @@ -2380,6 +2520,7 @@ static cell_t json_ptr_add_integer64(IPluginContext* pContext, const cell_t* par std::variant variant_value; char error[JSON_PACK_ERROR_SIZE]; + if (!g_pJsonManager->ParseInt64Variant(value, &variant_value, error, sizeof(error))) { return pContext->ThrowNativeError("%s", error); } @@ -2565,8 +2706,8 @@ static cell_t json_ptr_try_get_str(IPluginContext* pContext, const cell_t* param char* path; pContext->LocalToString(params[2], &path); - const char* str = nullptr; - size_t len = 0; + const char* str; + size_t len; if (!g_pJsonManager->PtrTryGetString(handle, path, &str, &len)) { return 0; @@ -2587,8 +2728,8 @@ static cell_t json_obj_foreach(IPluginContext* pContext, const cell_t* params) JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); if (!handle) return 0; - const char* key = nullptr; - JsonValue* pJSONValue = nullptr; + const char* key; + JsonValue* pJSONValue; if (!g_pJsonManager->ObjectForeachNext(handle, &key, nullptr, &pJSONValue)) { return false; @@ -2604,8 +2745,8 @@ static cell_t json_arr_foreach(IPluginContext* pContext, const cell_t* params) JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); if (!handle) return 0; - size_t index = 0; - JsonValue* pJSONValue = nullptr; + size_t index; + JsonValue* pJSONValue; if (!g_pJsonManager->ArrayForeachNext(handle, &index, &pJSONValue)) { return false; @@ -2623,7 +2764,7 @@ static cell_t json_obj_foreach_key(IPluginContext* pContext, const cell_t* param JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); if (!handle) return 0; - const char* key = nullptr; + const char* key; if (!g_pJsonManager->ObjectForeachKeyNext(handle, &key, nullptr)) { return false; @@ -2639,7 +2780,7 @@ static cell_t json_arr_foreach_index(IPluginContext* pContext, const cell_t* par JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); if (!handle) return 0; - size_t index = 0; + size_t index; if (!g_pJsonManager->ArrayForeachIndexNext(handle, &index)) { return false; @@ -2726,6 +2867,84 @@ static cell_t json_doc_to_immutable(IPluginContext* pContext, const cell_t* para return CreateAndReturnHandle(pContext, pJSONValue, "immutable JSON document"); } +static cell_t json_apply_json_patch(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* target = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* patch = g_pJsonManager->GetFromHandle(pContext, params[2]); + + if (!target || !patch) return 0; + + bool resultMutable = params[3] != 0; + char error[JSON_ERROR_BUFFER_SIZE] = {0}; + + JsonValue* result = g_pJsonManager->ApplyJsonPatch(target, patch, resultMutable, error, sizeof(error)); + if (!result) { + if (error[0] != '\0') { + return pContext->ThrowNativeError("%s", error); + } + return pContext->ThrowNativeError("Failed to apply JSON Patch"); + } + + return CreateAndReturnHandle(pContext, result, "JSON patch result"); +} + +static cell_t json_json_patch_in_place(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* target = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* patch = g_pJsonManager->GetFromHandle(pContext, params[2]); + + if (!target || !patch) return 0; + + char error[JSON_ERROR_BUFFER_SIZE] = {0}; + if (!g_pJsonManager->JsonPatchInPlace(target, patch, error, sizeof(error))) { + if (error[0] != '\0') { + return pContext->ThrowNativeError("%s", error); + } + return pContext->ThrowNativeError("Failed to apply JSON Patch in place"); + } + + return 1; +} + +static cell_t json_apply_merge_patch(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* target = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* patch = g_pJsonManager->GetFromHandle(pContext, params[2]); + + if (!target || !patch) return 0; + + bool resultMutable = params[3] != 0; + char error[JSON_ERROR_BUFFER_SIZE] = {0}; + + JsonValue* result = g_pJsonManager->ApplyMergePatch(target, patch, resultMutable, error, sizeof(error)); + if (!result) { + if (error[0] != '\0') { + return pContext->ThrowNativeError("%s", error); + } + return pContext->ThrowNativeError("Failed to apply JSON Merge Patch"); + } + + return CreateAndReturnHandle(pContext, result, "JSON merge patch result"); +} + +static cell_t json_merge_patch_in_place(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* target = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* patch = g_pJsonManager->GetFromHandle(pContext, params[2]); + + if (!target || !patch) return 0; + + char error[JSON_ERROR_BUFFER_SIZE] = {0}; + if (!g_pJsonManager->MergePatchInPlace(target, patch, error, sizeof(error))) { + if (error[0] != '\0') { + return pContext->ThrowNativeError("%s", error); + } + return pContext->ThrowNativeError("Failed to apply JSON Merge Patch in place"); + } + + return 1; +} + static cell_t json_arr_iter_init(IPluginContext* pContext, const cell_t* params) { JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); @@ -2780,6 +2999,14 @@ static cell_t json_arr_iter_remove(IPluginContext* pContext, const cell_t* param return removed != nullptr; } +static cell_t json_arr_iter_reset(IPluginContext* pContext, const cell_t* params) +{ + JsonArrIter* iter = g_pJsonManager->GetArrIterFromHandle(pContext, params[1]); + if (!iter) return 0; + + return g_pJsonManager->ArrIterReset(iter); +} + static cell_t json_obj_iter_init(IPluginContext* pContext, const cell_t* params) { JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); @@ -2798,12 +3025,8 @@ static cell_t json_obj_iter_next(IPluginContext* pContext, const cell_t* params) if (!key) return 0; const char* key_str; - if (iter->IsMutable()) { - key_str = yyjson_mut_get_str(reinterpret_cast(key)); - } else { - key_str = yyjson_get_str(reinterpret_cast(key)); - } - + size_t key_len; + g_pJsonManager->GetString(reinterpret_cast(key), &key_str, &key_len); pContext->StringToLocalUTF8(params[2], params[3], key_str, nullptr); return 1; } @@ -2821,13 +3044,7 @@ static cell_t json_obj_iter_get_val(IPluginContext* pContext, const cell_t* para JsonObjIter* iter = g_pJsonManager->GetObjIterFromHandle(pContext, params[1]); if (!iter) return 0; - void* key = nullptr; - if (iter->IsMutable()) { - key = iter->m_currentKey; - } else { - key = iter->m_currentKey; - } - + void* key = iter->m_currentKey; if (!key) { return pContext->ThrowNativeError("Iterator not positioned at a valid key (call Next() first)"); } @@ -2882,14 +3099,22 @@ static cell_t json_obj_iter_remove(IPluginContext* pContext, const cell_t* param return removed != nullptr; } +static cell_t json_obj_iter_reset(IPluginContext* pContext, const cell_t* params) +{ + JsonObjIter* iter = g_pJsonManager->GetObjIterFromHandle(pContext, params[1]); + if (!iter) return 0; + + return g_pJsonManager->ObjIterReset(iter); +} + static cell_t json_read_number(IPluginContext* pContext, const cell_t* params) { char* dat; pContext->LocalToString(params[1], &dat); - yyjson_read_flag read_flg = static_cast(params[2]); + uint32_t read_flg = static_cast(params[2]); char error[JSON_ERROR_BUFFER_SIZE]; - size_t consumed = 0; + size_t consumed; JsonValue* pJSONValue = g_pJsonManager->ReadNumber(dat, read_flg, error, sizeof(error), &consumed); if (!pJSONValue) { @@ -2912,7 +3137,12 @@ static cell_t json_write_number(IPluginContext* pContext, const cell_t* params) JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); if (!handle) return 0; - size_t buffer_size = static_cast(params[3]); + cell_t buffer_size_param = params[3]; + if (buffer_size_param <= 0) { + return pContext->ThrowNativeError("Buffer size must be > 0 (got %d)", buffer_size_param); + } + + size_t buffer_size = static_cast(buffer_size_param); char* temp_buffer = (char*)malloc(buffer_size); if (!temp_buffer) { return pContext->ThrowNativeError("Failed to allocate buffer"); @@ -3000,6 +3230,7 @@ static cell_t json_set_int64(IPluginContext* pContext, const cell_t* params) std::variant variant_value; char error[JSON_ERROR_BUFFER_SIZE]; + if (!g_pJsonManager->ParseInt64Variant(str, &variant_value, error, sizeof(error))) { return pContext->ThrowNativeError("%s", error); } @@ -3137,7 +3368,6 @@ const sp_nativeinfo_t g_JsonNatives[] = {"JSONArray.IndexOfString", json_arr_index_of_str}, {"JSONArray.IndexOfInt", json_arr_index_of_int}, {"JSONArray.IndexOfInt64", json_arr_index_of_integer64}, - {"JSONArray.IndexOfUint64", json_arr_index_of_uint64}, {"JSONArray.IndexOfFloat", json_arr_index_of_float}, {"JSONArray.Sort", json_arr_sort}, @@ -3175,6 +3405,10 @@ const sp_nativeinfo_t g_JsonNatives[] = {"JSON.ForeachIndex", json_arr_foreach_index}, {"JSON.ToMutable", json_doc_to_mutable}, {"JSON.ToImmutable", json_doc_to_immutable}, + {"JSON.ApplyJsonPatch", json_apply_json_patch}, + {"JSON.JsonPatchInPlace", json_json_patch_in_place}, + {"JSON.ApplyMergePatch", json_apply_merge_patch}, + {"JSON.MergePatchInPlace", json_merge_patch_in_place}, {"JSON.ReadNumber", json_read_number}, {"JSON.WriteNumber", json_write_number}, {"JSON.SetFpToFloat", json_set_fp_to_float}, @@ -3237,6 +3471,7 @@ const sp_nativeinfo_t g_JsonNatives[] = {"JSONArrIter.HasNext.get", json_arr_iter_has_next}, {"JSONArrIter.Index.get", json_arr_iter_get_index}, {"JSONArrIter.Remove", json_arr_iter_remove}, + {"JSONArrIter.Reset", json_arr_iter_reset}, // JSONObjIter {"JSONObjIter.JSONObjIter", json_obj_iter_init}, @@ -3246,6 +3481,7 @@ const sp_nativeinfo_t g_JsonNatives[] = {"JSONObjIter.Get", json_obj_iter_get}, {"JSONObjIter.Index.get", json_obj_iter_get_index}, {"JSONObjIter.Remove", json_obj_iter_remove}, + {"JSONObjIter.Reset", json_obj_iter_reset}, {nullptr, nullptr} }; \ No newline at end of file diff --git a/src/extension.cpp b/src/extension.cpp index 3a3ed0c..2c624ac 100755 --- a/src/extension.cpp +++ b/src/extension.cpp @@ -42,7 +42,6 @@ bool JsonExtension::SDK_OnLoad(char* error, size_t maxlen, bool late) return false; } - // Delete the existing instance if it exists if (g_pJsonManager) { delete g_pJsonManager; g_pJsonManager = nullptr; diff --git a/src/smsdk_config.h b/src/smsdk_config.h index eba1b81..7abb641 100755 --- a/src/smsdk_config.h +++ b/src/smsdk_config.h @@ -3,7 +3,7 @@ #define SMEXT_CONF_NAME "SourceMod JSON Extension" #define SMEXT_CONF_DESCRIPTION "Provide JSON Native" -#define SMEXT_CONF_VERSION "1.1.5d" +#define SMEXT_CONF_VERSION "1.1.5e" #define SMEXT_CONF_AUTHOR "ProjectSky" #define SMEXT_CONF_URL "https://github.com/ProjectSky/sm-ext-yyjson" #define SMEXT_CONF_LOGTAG "json" diff --git a/third_party/yyjson/AMBuilder b/third_party/yyjson/AMBuilder deleted file mode 100755 index e9d9a49..0000000 --- a/third_party/yyjson/AMBuilder +++ /dev/null @@ -1,20 +0,0 @@ -# vim: sts=2 ts=8 sw=2 tw=99 et ft=python: -import os - -builder.SetBuildFolder('yyjson') - -rvalue = {} -for cxx in builder.targets: - binary = Extension.StaticLibrary(builder, cxx, 'yyjson') - binary.compiler.includes += [ - os.path.join(builder.sourcePath, 'third_party', 'yyjson'), - ] - - if binary.compiler.family == 'clang': - binary.compiler.cflags += ['-Wno-attributes'] - - binary.sources += [ - 'yyjson.c', - ] - - rvalue[binary.compiler.target.arch] = builder.Add(binary) From e6eb59e436bf893b481af0972fe853dd6c86c699 Mon Sep 17 00:00:00 2001 From: ProjectSky Date: Sun, 16 Nov 2025 00:22:46 +0800 Subject: [PATCH 7/8] feat: enhance JSON floating-point handling and documentation - Updated SetFpToFloat and SetFpToFixed methods to clarify serialization effects in documentation - Adjusted precision limits for SetFpToFixed from 1-15 to 1-7 - Improved error handling for parameter limits in json_pack and related methods - Added notes across various JSON methods to emphasize memory management requirements --- public/IJsonManager.h | 4 ++- scripting/include/json.inc | 57 ++++++++++++++++++++++++++++++++------ src/JsonNatives.cpp | 21 ++++++++++---- 3 files changed, 68 insertions(+), 14 deletions(-) diff --git a/public/IJsonManager.h b/public/IJsonManager.h index 12746f9..ed4090e 100755 --- a/public/IJsonManager.h +++ b/public/IJsonManager.h @@ -1788,16 +1788,18 @@ class IJsonManager : public SMInterface * @param flt true to use single-precision (float), false to use double-precision (double) * @return true on success, false if handle is not a floating-point number * @note Only works on floating-point numbers (not integers) + * @note This affects how the number is serialized in all write operations */ virtual bool SetFpToFloat(JsonValue* handle, bool flt) = 0; /** * Set floating-point number's output format to fixed-point notation * @param handle JSON floating-point number value - * @param prec Precision (1-15), similar to ECMAScript Number.prototype.toFixed(prec) but with trailing zeros removed + * @param prec Precision (1-15), similar to ECMAScript `Number.prototype.toFixed(prec)` but with trailing zeros removed * @return true on success, false if handle is not a floating-point number or prec is out of range * @note Only works on floating-point numbers (not integers) * @note This will produce shorter output but may lose some precision + * @note This affects how the number is serialized in all write operations */ virtual bool SetFpToFixed(JsonValue* handle, int prec) = 0; diff --git a/scripting/include/json.inc b/scripting/include/json.inc index 81bc1e6..10a298a 100755 --- a/scripting/include/json.inc +++ b/scripting/include/json.inc @@ -71,12 +71,17 @@ enum JSON_WRITE_FLAG JSON_WRITE_FP_TO_FLOAT = 1 << 27 // Write floating-point numbers using single-precision (float) } -/** Write floating-point number using fixed-point notation - - This is similar to ECMAScript Number.prototype.toFixed(prec) but with trailing zeros removed. The prec ranges from 1 to 15 - - This will produce shorter output but may lose some precision -*/ +/** + * Write floating-point number using fixed-point notation + * This is similar to ECMAScript Number.prototype.toFixed(prec) but with trailing zeros removed + * + * @param n Precision digits (1-7) + * @return JSON write flag with precision setting + * @note This will produce shorter output but may lose some precision + */ stock JSON_WRITE_FLAG JSON_WRITE_FP_TO_FIXED(int n) { + n = (n < 1) ? 1 : (n > 7 ? 7 : n); return view_as(n << 28); } @@ -103,6 +108,10 @@ methodmap JSON < Handle * - [: start array * - ]: end array * + * @note Needs to be freed using delete or CloseHandle() + * @note There is a limit of 32 parameters (defined in SourcePawn) + * including the format string, so the number of arguments must be less than or equal to 31 + * * @param format Format string * @param ... Arguments based on format string * @@ -178,6 +187,8 @@ methodmap JSON < Handle /** * Converts an immutable JSON document to a mutable one * + * @note Needs to be freed using delete or CloseHandle() + * * @return Handle to the new mutable JSON document, INVALID_HANDLE on failure * @error If the document is already mutable */ @@ -186,6 +197,8 @@ methodmap JSON < Handle /** * Converts a mutable JSON document to an immutable one * + * @note Needs to be freed using delete or CloseHandle() + * * @return Handle to the new immutable JSON document, INVALID_HANDLE on failure * @error If the document is already immutable */ @@ -194,6 +207,8 @@ methodmap JSON < Handle /** * Apply a JSON Patch (RFC 6902) to this value and return a new JSON handle * + * @note Needs to be freed using delete or CloseHandle() + * * @param patch JSON Patch document * @param resultMutable True to return a mutable result, false for immutable * @@ -215,6 +230,8 @@ methodmap JSON < Handle /** * Apply a JSON Merge Patch (RFC 7396) to this value and return a new JSON handle * + * @note Needs to be freed using delete or CloseHandle() + * * @param patch JSON Merge Patch document * @param resultMutable True to return a mutable result, false for immutable * @@ -275,7 +292,7 @@ methodmap JSON < Handle * Set floating-point number's output format to single-precision * * @note Only works on floating-point numbers (not integers) - * @note This affects how the number is serialized when using ToString() + * @note This affects how the number is serialized in all write operations * * @param flt True to use single-precision (float), false to use double-precision (double) * @@ -290,12 +307,12 @@ methodmap JSON < Handle * @note Only works on floating-point numbers (not integers) * @note This is similar to ECMAScript Number.prototype.toFixed(prec) but with trailing zeros removed * @note This will produce shorter output but may lose some precision - * @note This affects how the number is serialized when using ToString() + * @note This affects how the number is serialized in all write operations * - * @param prec Precision (1-15) + * @param prec Precision (1-7) * * @return True on success, false if handle is not a floating-point number or prec is out of range - * @error Invalid handle, handle is not a floating-point number, or precision out of range (1-15) + * @error Invalid handle, handle is not a floating-point number, or precision out of range (1-7) */ public native bool SetFpToFixed(int prec); @@ -1096,6 +1113,8 @@ methodmap JSONObject < JSON * The array must contain an even number of strings, where even indices are keys * and odd indices are values. Keys cannot be empty strings * + * @note Needs to be freed using delete or CloseHandle() + * * @param pairs Array of strings containing alternating keys and values * @param size Total size of the array (must be even) * @@ -1108,6 +1127,8 @@ methodmap JSONObject < JSON /** * Loads a JSON object from a file * + * @note Needs to be freed using delete or CloseHandle() + * * @param file File to read from * @param flag The JSON read options * @@ -1118,6 +1139,8 @@ methodmap JSONObject < JSON /** * Loads a JSON object from a string * + * @note Needs to be freed using delete or CloseHandle() + * * @param buffer String buffer to load into the JSON object * @param flag The JSON read options * @@ -1128,6 +1151,7 @@ methodmap JSONObject < JSON /** * Gets a value from the object * + * @note Needs to be freed using delete or CloseHandle() * @note This function takes a linear search time * * @param key Key name @@ -1363,6 +1387,8 @@ methodmap JSONArray < JSON /** * Creates a new JSON array from an array of strings * + * @note Needs to be freed using delete or CloseHandle() + * * @param strings Array of strings to create array from * @param size Size of the array * @return New JSON array handle, or null if creation failed @@ -1372,6 +1398,8 @@ methodmap JSONArray < JSON /** * Creates a new JSON array from an array of integer values * + * @note Needs to be freed using delete or CloseHandle() + * * @param values Array of int values to create array from * @param size Size of the array * @return New JSON array handle, or null if creation failed @@ -1381,6 +1409,8 @@ methodmap JSONArray < JSON /** * Creates a new JSON array from an array of 64-bit integer strings (auto-detects signed/unsigned) * + * @note Needs to be freed using delete or CloseHandle() + * * @param values Array of int64 string values (can be signed or unsigned) * @param size Size of the array * @return New JSON array handle, or null if creation failed @@ -1391,6 +1421,8 @@ methodmap JSONArray < JSON /** * Creates a new JSON array from an array of boolean values * + * @note Needs to be freed using delete or CloseHandle() + * * @param values Array of bool values to create array from * @param size Size of the array * @return New JSON array handle, or null if creation failed @@ -1400,6 +1432,8 @@ methodmap JSONArray < JSON /** * Creates a new JSON array from an array of float values * + * @note Needs to be freed using delete or CloseHandle() + * * @param values Array of float values to create array from * @param size Size of the array * @return New JSON array handle, or null if creation failed @@ -1409,6 +1443,8 @@ methodmap JSONArray < JSON /** * Loads a JSON array from a file * + * @note Needs to be freed using delete or CloseHandle() + * * @param file File to read from * @param flag Read flag * @return Array handle, or null on failure @@ -1418,6 +1454,8 @@ methodmap JSONArray < JSON /** * Loads a JSON array from a string * + * @note Needs to be freed using delete or CloseHandle() + * * @param buffer String buffer to load into the JSON array * @param flag Read flag * @return Array handle, or null on failure @@ -1427,6 +1465,7 @@ methodmap JSONArray < JSON /** * Gets a value from the array * + * @note Needs to be freed using delete or CloseHandle() * @note This function takes a linear search time * * @param index Position in the array (starting from 0) @@ -1939,6 +1978,8 @@ methodmap JSONArrIter < Handle /** * Moves the iterator to the next element * + * @note Needs to be freed using delete or CloseHandle() + * * @return JSON value handle for the next element, or null if iteration is complete * @error Invalid iterator handle */ diff --git a/src/JsonNatives.cpp b/src/JsonNatives.cpp index c9b207c..c964a43 100755 --- a/src/JsonNatives.cpp +++ b/src/JsonNatives.cpp @@ -147,7 +147,14 @@ static cell_t CreateAndReturnObjIterHandle(IPluginContext* pContext, JsonObjIter return handle; } -static cell_t json_pack(IPluginContext* pContext, const cell_t* params) { +static cell_t json_pack(IPluginContext* pContext, const cell_t* params) +{ + // SourcePawn has a limit of 32 parameters (defined SP_MAX_EXEC_PARAMS) + // including the format string, so we need to check if the number of parameters is less than the limit + if (params[0] > SP_MAX_EXEC_PARAMS - 1) { + return pContext->ThrowNativeError("Too many parameters (max %d)", SP_MAX_EXEC_PARAMS - 1); + } + char* fmt; pContext->LocalToString(params[1], &fmt); @@ -341,7 +348,7 @@ static cell_t json_arr_index_of_float(IPluginContext* pContext, const cell_t* pa if (!handle) return 0; - double searchValue = static_cast(sp_ctof(params[2])); + float searchValue = sp_ctof(params[2]); return g_pJsonManager->ArrayIndexOfFloat(handle, searchValue); } @@ -1436,7 +1443,7 @@ static cell_t json_arr_insert_float(IPluginContext* pContext, const cell_t* para return pContext->ThrowNativeError("Index is out of bounds (got %d, max %zu)", index, arr_size); } - double value = sp_ctof(params[3]); + float value = sp_ctof(params[3]); return g_pJsonManager->ArrayInsertFloat(handle, index, value); } @@ -1565,7 +1572,7 @@ static cell_t json_arr_prepend_float(IPluginContext* pContext, const cell_t* par return pContext->ThrowNativeError("Cannot prepend value to an immutable JSON array"); } - double value = sp_ctof(params[2]); + float value = sp_ctof(params[2]); return g_pJsonManager->ArrayPrependFloat(handle, value); } @@ -3187,8 +3194,12 @@ static cell_t json_set_fp_to_fixed(IPluginContext* pContext, const cell_t* param if (!handle) return 0; int prec = params[2]; + if (prec < 1 || prec > 7) { + return pContext->ThrowNativeError("Precision out of range (1-7)"); + } + if (!g_pJsonManager->SetFpToFixed(handle, prec)) { - return pContext->ThrowNativeError("Failed to set floating-point format to fixed (value is not a floating-point number or precision out of range 1-15)"); + return pContext->ThrowNativeError("Failed to set floating-point format to fixed (value is not a floating-point number or precision out of range 1-7)"); } return 1; From 54410c8789eb6a8a949441d40ffadfbfcf7ee4ed Mon Sep 17 00:00:00 2001 From: ProjectSky Date: Thu, 20 Nov 2025 23:53:46 +0800 Subject: [PATCH 8/8] feat: enhance JSON management with new rotation and value counting features - Added ObjectRotate and ArrayRotate methods for rotating key-value pairs in JSON objects and arrays, respectively - Introduced GetValCount method to retrieve the total number of values in immutable JSON documents - Fixed TypeAccess configuration to allow external extensions to create JSON handles by setting HTypeAccess_Create flag in handle type registration - Renamed GetHandleType to GetJsonHandleType GetFromHandle to GetValueFromHandle for better clarity and consistency - Updated documentation to clarify usage and performance considerations for new methods - Enhanced tests to cover rotation functionality and value counting in various JSON structures --- public/IJsonManager.h | 177 ++++++++---- scripting/include/json.inc | 54 +++- scripting/test_json.sp | 197 ++++++++++++++ src/JsonManager.cpp | 300 ++++++++++---------- src/JsonManager.h | 40 +-- src/JsonNatives.cpp | 544 ++++++++++++++++++++----------------- src/extension.cpp | 20 +- 7 files changed, 854 insertions(+), 478 deletions(-) diff --git a/public/IJsonManager.h b/public/IJsonManager.h index ed4090e..f43fdbe 100755 --- a/public/IJsonManager.h +++ b/public/IJsonManager.h @@ -15,8 +15,7 @@ class JsonArrIter; class JsonObjIter; #define SMINTERFACE_JSONMANAGER_NAME "IJsonManager" -#define SMINTERFACE_JSONMANAGER_VERSION 2 -#define JSON_PACK_ERROR_SIZE 256 +#define SMINTERFACE_JSONMANAGER_VERSION 3 #define JSON_ERROR_BUFFER_SIZE 256 #define JSON_INT64_BUFFER_SIZE 32 @@ -98,21 +97,36 @@ class IJsonManager : public SMInterface * @return true on success, false if buffer is too small or on error * * @note The out_size parameter returns the size including null terminator - * @note Use GetSerializedSize() with the same write_flg to determine buffer size - * @warning This method performs multiple memory allocations and copies, resulting in poor performance. - * For better performance, use WriteToStringPtr() instead, which avoids intermediate buffers + * + * @warning Buffer Size Requirements: + * This function does not allocate memory, but the buffer must be larger than + * the final JSON size to allow temporary space. + * + * The extra space is needed temporarily for each value while it is written, + * and is reused for later values: + * - Number: 40 bytes + * - String: 16 + (str_len * 6) bytes + * - Other values: 16 bytes + * - Nesting depth: 16 * max_json_depth bytes + * + * @note GetSerializedSize() only returns the final JSON size, NOT including the temporary space + * You must manually add extra buffer space based on your JSON content + * @note For most use cases, prefer WriteToStringPtr() for simplicity + * Use this method only when you need to avoid heap allocation (e.g., stack buffers, pre-allocated pools) */ virtual bool WriteToString(JsonValue* handle, char* buffer, size_t buffer_size, uint32_t write_flg = 0, size_t* out_size = nullptr) = 0; /** - * Write JSON to string and return allocated string (performance-optimized version) + * Write JSON to string and return allocated string * @param handle JSON value * @param write_flg Write flags (YYJSON_WRITE_FLAG values, default: 0) * @param out_size Pointer to receive actual size written (including null terminator) optional * @return Allocated string pointer on success, nullptr on error. Caller must free() the returned pointer * - * @note This is the recommended method for serialization as it avoids intermediate buffer allocations + * @note This function handles memory allocation internally, + * avoiding the complexity of manual buffer size calculation required by WriteToString() + * @note This is the recommended method for most use cases due to its simplicity and safety */ virtual char* WriteToStringPtr(JsonValue* handle, uint32_t write_flg = 0, size_t* out_size = nullptr) = 0; @@ -383,6 +397,17 @@ class IJsonManager : public SMInterface */ virtual size_t GetRefCount(JsonValue* handle) = 0; + /** + * Get the total number of values in a document + * @param handle JSON value + * @return Total number of values in the document tree, 0 if handle is null or mutable + * @note Only works on immutable documents (parsed from JSON) + * @note Useful for performance planning and memory estimation + * @note Counts ALL values including containers, keys, and leaf values + * Example: {"a":1,"b":2,"c":3} returns 7 (1 object + 3 keys + 3 values) + */ + virtual size_t GetValCount(JsonValue* handle) = 0; + /** * Create an empty mutable JSON object * @return New mutable JSON object or nullptr on failure @@ -466,10 +491,11 @@ class IJsonManager : public SMInterface * Get float value by key * @param handle JSON object * @param key Key name - * @param out_value Pointer to receive float value + * @param out_value Pointer to receive double value * @return true on success, false if key not found or type mismatch + * @note Integers values are auto converted to double */ - virtual bool ObjectGetFloat(JsonValue* handle, const char* key, double* out_value) = 0; + virtual bool ObjectGetDouble(JsonValue* handle, const char* key, double* out_value) = 0; /** * Get integer value by key @@ -547,13 +573,13 @@ class IJsonManager : public SMInterface virtual bool ObjectSetBool(JsonValue* handle, const char* key, bool value) = 0; /** - * Set float value by key (mutable only) + * Set double value by key (mutable only) * @param handle Mutable JSON object * @param key Key name - * @param value Float value + * @param value Double value * @return true on success */ - virtual bool ObjectSetFloat(JsonValue* handle, const char* key, double value) = 0; + virtual bool ObjectSetDouble(JsonValue* handle, const char* key, double value) = 0; /** * Set integer value by key (mutable only) @@ -614,6 +640,18 @@ class IJsonManager : public SMInterface */ virtual bool ObjectSort(JsonValue* handle, JSON_SORT_ORDER sort_mode) = 0; + /** + * Rotate key-value pairs in the object + * @param handle Mutable JSON object + * @param idx Number of positions to rotate (must be less than object size) + * @return true on success, false if idx >= object size or object is immutable + * @note Only works on mutable objects + * @note Example: {"a":1,"b":2,"c":3,"d":4} rotate 1 becomes {"b":2,"c":3,"d":4,"a":1} + * @note Valid range: 0 <= idx < object size + * @warning This function takes linear time proportional to the rotation amount + */ + virtual bool ObjectRotate(JsonValue* handle, size_t idx) = 0; + /** * Create an empty mutable JSON array * @return New mutable JSON array or nullptr on failure @@ -657,12 +695,12 @@ class IJsonManager : public SMInterface virtual JsonValue* ArrayInitWithBool(const bool* values, size_t count) = 0; /** - * Create a JSON array from float values - * @param values Array of float values + * Create a JSON array from double values + * @param values Array of double values * @param count Number of values * @return New JSON array or nullptr on failure */ - virtual JsonValue* ArrayInitWithFloat(const double* values, size_t count) = 0; + virtual JsonValue* ArrayInitWithDouble(const double* values, size_t count) = 0; /** * Parse a JSON array from string @@ -727,13 +765,14 @@ class IJsonManager : public SMInterface virtual bool ArrayGetBool(JsonValue* handle, size_t index, bool* out_value) = 0; /** - * Get float value at index + * Get double value at index * @param handle JSON array * @param index Element index - * @param out_value Pointer to receive float value + * @param out_value Pointer to receive double value * @return true on success, false if index out of bounds or type mismatch + * @note Integers values are auto converted to double */ - virtual bool ArrayGetFloat(JsonValue* handle, size_t index, double* out_value) = 0; + virtual bool ArrayGetDouble(JsonValue* handle, size_t index, double* out_value) = 0; /** * Get integer value at index @@ -790,13 +829,13 @@ class IJsonManager : public SMInterface virtual bool ArrayReplaceBool(JsonValue* handle, size_t index, bool value) = 0; /** - * Replace element at index with float (mutable only) + * Replace element at index with double (mutable only) * @param handle Mutable JSON array * @param index Element index - * @param value Float value + * @param value Double value * @return true on success */ - virtual bool ArrayReplaceFloat(JsonValue* handle, size_t index, double value) = 0; + virtual bool ArrayReplaceDouble(JsonValue* handle, size_t index, double value) = 0; /** * Replace element at index with integer (mutable only) @@ -850,12 +889,12 @@ class IJsonManager : public SMInterface virtual bool ArrayAppendBool(JsonValue* handle, bool value) = 0; /** - * Append float to end of array (mutable only) + * Append double to end of array (mutable only) * @param handle Mutable JSON array - * @param value Float value + * @param value Double value * @return true on success */ - virtual bool ArrayAppendFloat(JsonValue* handle, double value) = 0; + virtual bool ArrayAppendDouble(JsonValue* handle, double value) = 0; /** * Append integer to end of array (mutable only) @@ -925,13 +964,13 @@ class IJsonManager : public SMInterface virtual bool ArrayInsertInt64(JsonValue* handle, size_t index, std::variant value) = 0; /** - * Insert float at specific index (mutable only) + * Insert double at specific index (mutable only) * @param handle Mutable JSON array * @param index Element index - * @param value Float value + * @param value Double value * @return true on success */ - virtual bool ArrayInsertFloat(JsonValue* handle, size_t index, double value) = 0; + virtual bool ArrayInsertDouble(JsonValue* handle, size_t index, double value) = 0; /** * Insert string at specific index (mutable only) @@ -983,12 +1022,12 @@ class IJsonManager : public SMInterface virtual bool ArrayPrependInt64(JsonValue* handle, std::variant value) = 0; /** - * Prepend float to beginning of array (mutable only) + * Prepend double to beginning of array (mutable only) * @param handle Mutable JSON array - * @param value Float value + * @param value Double value * @return true on success */ - virtual bool ArrayPrependFloat(JsonValue* handle, double value) = 0; + virtual bool ArrayPrependDouble(JsonValue* handle, double value) = 0; /** * Prepend string to beginning of array (mutable only) @@ -1076,12 +1115,12 @@ class IJsonManager : public SMInterface virtual int ArrayIndexOfInt64(JsonValue* handle, std::variant search_value) = 0; /** - * Find index of float value + * Find index of double value * @param handle JSON array - * @param search_value Float value to search for + * @param search_value Double value to search for * @return Index of first match, or -1 if not found */ - virtual int ArrayIndexOfFloat(JsonValue* handle, double search_value) = 0; + virtual int ArrayIndexOfDouble(JsonValue* handle, double search_value) = 0; /** * Sort array elements @@ -1092,6 +1131,18 @@ class IJsonManager : public SMInterface */ virtual bool ArraySort(JsonValue* handle, JSON_SORT_ORDER sort_mode) = 0; + /** + * Rotate array elements + * @param handle Mutable JSON array + * @param idx Number of positions to rotate (must be less than array length) + * @return true on success, false if idx >= array length or array is immutable + * @note Only works on mutable arrays + * @note Example: [1,2,3,4,5] rotate 2 becomes [3,4,5,1,2] + * @note Valid range: 0 <= idx < array length + * @warning This function takes linear time proportional to the rotation amount + */ + virtual bool ArrayRotate(JsonValue* handle, size_t idx) = 0; + /** * Create JSON value from format string and parameters * @param format Format string (e.g., "{s:i,s:s}", "[i,s,b]") @@ -1120,11 +1171,11 @@ class IJsonManager : public SMInterface virtual JsonValue* CreateBool(bool value) = 0; /** - * Create a JSON float value - * @param value Float value - * @return New JSON float or nullptr on failure + * Create a JSON double value + * @param value Double value + * @return New JSON double or nullptr on failure */ - virtual JsonValue* CreateFloat(double value) = 0; + virtual JsonValue* CreateDouble(double value) = 0; /** * Create a JSON integer value @@ -1162,12 +1213,13 @@ class IJsonManager : public SMInterface virtual bool GetBool(JsonValue* handle, bool* out_value) = 0; /** - * Get float value from JSON + * Get double value from JSON * @param handle JSON value - * @param out_value Pointer to receive float value + * @param out_value Pointer to receive double value * @return true on success, false on type mismatch + * @note Integers values are auto converted to double */ - virtual bool GetFloat(JsonValue* handle, double* out_value) = 0; + virtual bool GetDouble(JsonValue* handle, double* out_value) = 0; /** * Get integer value from JSON @@ -1218,15 +1270,16 @@ class IJsonManager : public SMInterface char* error = nullptr, size_t error_size = 0) = 0; /** - * Get float value using JSON Pointer + * Get double value using JSON Pointer * @param handle JSON value * @param path JSON Pointer path - * @param out_value Pointer to receive float value + * @param out_value Pointer to receive double value * @param error Error buffer (optional) * @param error_size Error buffer size * @return true on success, false on error + * @note Integers values are auto converted to double */ - virtual bool PtrGetFloat(JsonValue* handle, const char* path, double* out_value, + virtual bool PtrGetDouble(JsonValue* handle, const char* path, double* out_value, char* error = nullptr, size_t error_size = 0) = 0; /** @@ -1315,15 +1368,15 @@ class IJsonManager : public SMInterface char* error = nullptr, size_t error_size = 0) = 0; /** - * Set float value using JSON Pointer (mutable only) + * Set double value using JSON Pointer (mutable only) * @param handle Mutable JSON value * @param path JSON Pointer path - * @param value Float value + * @param value Double value * @param error Error buffer (optional) * @param error_size Error buffer size * @return true on success, false on error */ - virtual bool PtrSetFloat(JsonValue* handle, const char* path, double value, + virtual bool PtrSetDouble(JsonValue* handle, const char* path, double value, char* error = nullptr, size_t error_size = 0) = 0; /** @@ -1398,15 +1451,15 @@ class IJsonManager : public SMInterface char* error = nullptr, size_t error_size = 0) = 0; /** - * Add float to array using JSON Pointer (mutable only) + * Add double to array using JSON Pointer (mutable only) * @param handle Mutable JSON value * @param path JSON Pointer path to array - * @param value Float value + * @param value Double value * @param error Error buffer (optional) * @param error_size Error buffer size * @return true on success, false on error */ - virtual bool PtrAddFloat(JsonValue* handle, const char* path, double value, + virtual bool PtrAddDouble(JsonValue* handle, const char* path, double value, char* error = nullptr, size_t error_size = 0) = 0; /** @@ -1485,13 +1538,14 @@ class IJsonManager : public SMInterface virtual bool PtrTryGetBool(JsonValue* handle, const char* path, bool* out_value) = 0; /** - * Try to get float value using JSON Pointer (returns false on failure) + * Try to get double value using JSON Pointer (returns false on failure) * @param handle JSON value * @param path JSON Pointer path - * @param out_value Pointer to receive float value + * @param out_value Pointer to receive double value * @return true on success, false if not found or type mismatch + * @note Integers values are auto converted to double */ - virtual bool PtrTryGetFloat(JsonValue* handle, const char* path, double* out_value) = 0; + virtual bool PtrTryGetDouble(JsonValue* handle, const char* path, double* out_value) = 0; /** * Try to get integer value using JSON Pointer (returns false on failure) @@ -1583,7 +1637,7 @@ class IJsonManager : public SMInterface * External extensions MUST use this method to obtain the handle type * @return The HandleType_t for JSON handles */ - virtual HandleType_t GetHandleType() = 0; + virtual HandleType_t GetJsonHandleType() = 0; /** * Read JsonValue from a SourceMod handle @@ -1591,7 +1645,7 @@ class IJsonManager : public SMInterface * @param handle Handle to read from * @return JsonValue pointer, or nullptr on error (error will be reported to context) */ - virtual JsonValue* GetFromHandle(IPluginContext* pContext, Handle_t handle) = 0; + virtual JsonValue* GetValueFromHandle(IPluginContext* pContext, Handle_t handle) = 0; /** * Initialize an array iterator (same as ArrIterWith but returns pointer) @@ -1717,6 +1771,17 @@ class IJsonManager : public SMInterface */ virtual void* ObjIterRemove(JsonObjIter* iter) = 0; + /** + * Get key string from object iterator key pointer + * @param iter Object iterator + * @param key Key pointer (returned from ObjIterNext) + * @param out_str Pointer to receive key string + * @param out_len Pointer to receive key length (optional) + * @return true on success, false on error + * @note Do not free the returned string - it is owned by the JSON document + */ + virtual bool ObjIterGetKeyString(JsonObjIter* iter, void* key, const char** out_str, size_t* out_len = nullptr) = 0; + /** * Release an array iterator * @param iter Iterator to release @@ -1836,12 +1901,12 @@ class IJsonManager : public SMInterface /** * Directly modify a JSON value to floating-point type * @param handle JSON value to modify (cannot be object or array) - * @param value Float value + * @param value Double value * @return true on success, false if handle is object or array * @warning For immutable documents, this breaks immutability. Use with caution. * @note This modifies the value in-place without creating a new value */ - virtual bool SetFloat(JsonValue* handle, double value) = 0; + virtual bool SetDouble(JsonValue* handle, double value) = 0; /** * Directly modify a JSON value to string type diff --git a/scripting/include/json.inc b/scripting/include/json.inc index 10a298a..bbf2bc8 100755 --- a/scripting/include/json.inc +++ b/scripting/include/json.inc @@ -109,8 +109,8 @@ methodmap JSON < Handle * - ]: end array * * @note Needs to be freed using delete or CloseHandle() - * @note There is a limit of 32 parameters (defined in SourcePawn) - * including the format string, so the number of arguments must be less than or equal to 31 + * @note SourcePawn imposes a limit of 32 parameters per function (defined by SP_MAX_EXEC_PARAMS), + * which precludes the construction of complex objects with this method. * * @param format Format string * @param ... Arguments based on format string @@ -546,6 +546,8 @@ methodmap JSON < Handle /** * Get float value * + * @note Integers values are auto converted to float + * * @return float value */ public native float GetFloat(); @@ -618,6 +620,7 @@ methodmap JSON < Handle * Get float value by a JSON Pointer * * @note JSON Pointer paths are always resolved from the document root, not from the current value + * @note Integers values are auto converted to float * * @param path The JSON pointer string * @@ -907,6 +910,7 @@ methodmap JSON < Handle * Try to get float value by a JSON Pointer * * @note JSON Pointer paths are always resolved from the document root, not from the current value + * @note Integers values are auto converted to float * * @param path The JSON pointer string * @param value Store the float value @@ -1096,6 +1100,17 @@ methodmap JSON < Handle property int RefCount { public native get(); } + + /** + * Get the total number of values in the document + * + * @note Only works on immutable documents (parsed from JSON) + * @note Returns 0 for mutable documents or null handles + * @note Useful for performance planning and memory estimation + */ + property int ValCount { + public native get(); + } }; methodmap JSONObject < JSON @@ -1182,6 +1197,8 @@ methodmap JSONObject < JSON /** * Gets a float value from the object * + * @note Integers values are auto converted to float + * * @param key Key name * * @return Float value @@ -1367,6 +1384,20 @@ methodmap JSONObject < JSON */ public native bool Sort(JSON_SORT_ORDER order = JSON_SORT_ASC); + /** + * Rotates key-value pairs in the object + * + * @note This function takes a linear search time + * @note Only works on mutable objects + * @note Example: {"a":1,"b":2,"c":3,"d":4} rotate 1 becomes {"b":2,"c":3,"d":4,"a":1} + * + * @param idx Number of positions to rotate + * + * @return True on success, false if object is immutable or operation failed + * @error Invalid handle or attempting to rotate an immutable object + */ + public native bool Rotate(int idx); + /** * Retrieves the size of the object */ @@ -1486,6 +1517,8 @@ methodmap JSONArray < JSON /** * Gets a float value from the array * + * @note Integers values are auto converted to float + * * @param index Position in the array (starting from 0) * * @return The number as float @@ -1921,6 +1954,20 @@ methodmap JSONArray < JSON */ public native bool Sort(JSON_SORT_ORDER order = JSON_SORT_ASC); + /** + * Rotates array elements + * + * @note This function takes a linear search time + * @note Only works on mutable arrays + * @note Example: [1,2,3,4,5] rotate 2 becomes [3,4,5,1,2] + * + * @param idx Number of positions to rotate + * + * @return True on success, false if array is immutable or operation failed + * @error Invalid handle or attempting to rotate an immutable array + */ + public native bool Rotate(int idx); + /** * Retrieves the size of the array */ @@ -2164,6 +2211,7 @@ public void __pl_json_SetNTVOptional() MarkNativeAsOptional("JSONObject.FromString"); MarkNativeAsOptional("JSONObject.FromFile"); MarkNativeAsOptional("JSONObject.Sort"); + MarkNativeAsOptional("JSONObject.Rotate"); // JSONArray MarkNativeAsOptional("JSONArray.JSONArray"); @@ -2223,6 +2271,7 @@ public void __pl_json_SetNTVOptional() MarkNativeAsOptional("JSONArray.IndexOfInt64"); MarkNativeAsOptional("JSONArray.IndexOfFloat"); MarkNativeAsOptional("JSONArray.Sort"); + MarkNativeAsOptional("JSONArray.Rotate"); // JSON MarkNativeAsOptional("JSON.ToString"); @@ -2235,6 +2284,7 @@ public void __pl_json_SetNTVOptional() MarkNativeAsOptional("JSON.GetSerializedSize"); MarkNativeAsOptional("JSON.ReadSize.get"); MarkNativeAsOptional("JSON.RefCount.get"); + MarkNativeAsOptional("JSON.ValCount.get"); MarkNativeAsOptional("JSON.Type.get"); MarkNativeAsOptional("JSON.SubType.get"); MarkNativeAsOptional("JSON.IsArray.get"); diff --git a/scripting/test_json.sp b/scripting/test_json.sp index 2c0b149..761f21b 100755 --- a/scripting/test_json.sp +++ b/scripting/test_json.sp @@ -653,6 +653,64 @@ void Test_ObjectOperations() } TestEnd(); + // Test Rotate + TestStart("Object_Rotate_Forward"); + { + JSONObject obj = new JSONObject(); + obj.SetInt("a", 1); + obj.SetInt("b", 2); + obj.SetInt("c", 3); + obj.SetInt("d", 4); + + AssertTrue(obj.Rotate(1)); + + char key[32]; + obj.GetKey(0, key, sizeof(key)); + AssertStrEq(key, "b", "First key should be 'b' after rotate 1"); + obj.GetKey(3, key, sizeof(key)); + AssertStrEq(key, "a", "Last key should be 'a' after rotate 1"); + + delete obj; + } + TestEnd(); + + TestStart("Object_Rotate_Multiple"); + { + JSONObject obj = new JSONObject(); + obj.SetInt("first", 1); + obj.SetInt("second", 2); + obj.SetInt("third", 3); + obj.SetInt("fourth", 4); + obj.SetInt("fifth", 5); + + AssertTrue(obj.Rotate(2)); + + char key[32]; + obj.GetKey(0, key, sizeof(key)); + AssertStrEq(key, "third", "First key should be 'third' after rotate 2"); + obj.GetKey(4, key, sizeof(key)); + AssertStrEq(key, "second", "Last key should be 'second' after rotate 2"); + + delete obj; + } + TestEnd(); + + TestStart("Object_Rotate_Zero"); + { + JSONObject obj = new JSONObject(); + obj.SetInt("x", 1); + obj.SetInt("y", 2); + + AssertTrue(obj.Rotate(0)); + + char key[32]; + obj.GetKey(0, key, sizeof(key)); + AssertStrEq(key, "x", "Order should not change after rotate 0"); + + delete obj; + } + TestEnd(); + // Test Set with handle TestStart("Object_SetWithHandle"); { @@ -1013,6 +1071,83 @@ void Test_ArrayOperations() } TestEnd(); + // Test Rotate + TestStart("Array_Rotate_Forward"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(1); + arr.PushInt(2); + arr.PushInt(3); + arr.PushInt(4); + arr.PushInt(5); + + AssertTrue(arr.Rotate(2)); + AssertEq(arr.GetInt(0), 3, "First element should be 3 after rotate 2"); + AssertEq(arr.GetInt(1), 4, "Second element should be 4"); + AssertEq(arr.GetInt(2), 5, "Third element should be 5"); + AssertEq(arr.GetInt(3), 1, "Fourth element should be 1"); + AssertEq(arr.GetInt(4), 2, "Fifth element should be 2"); + + delete arr; + } + TestEnd(); + + TestStart("Array_Rotate_Single"); + { + JSONArray arr = new JSONArray(); + arr.PushString("a"); + arr.PushString("b"); + arr.PushString("c"); + arr.PushString("d"); + + AssertTrue(arr.Rotate(1)); + + char buffer[32]; + arr.GetString(0, buffer, sizeof(buffer)); + AssertStrEq(buffer, "b", "First element should be 'b' after rotate 1"); + arr.GetString(3, buffer, sizeof(buffer)); + AssertStrEq(buffer, "a", "Last element should be 'a' after rotate 1"); + + delete arr; + } + TestEnd(); + + TestStart("Array_Rotate_Zero"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(10); + arr.PushInt(20); + arr.PushInt(30); + + AssertTrue(arr.Rotate(0)); + AssertEq(arr.GetInt(0), 10, "Order should not change after rotate 0"); + AssertEq(arr.GetInt(1), 20); + AssertEq(arr.GetInt(2), 30); + + delete arr; + } + TestEnd(); + + TestStart("Array_Rotate_LargeIndex"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(1); + arr.PushInt(2); + arr.PushInt(3); + + // Rotate by array length or greater should fail (idx must be < length) + AssertFalse(arr.Rotate(3), "Rotate by array length should fail"); + AssertFalse(arr.Rotate(10), "Rotate by value > length should fail"); + + // Values should remain unchanged + AssertEq(arr.GetInt(0), 1); + AssertEq(arr.GetInt(1), 2); + AssertEq(arr.GetInt(2), 3); + + delete arr; + } + TestEnd(); + // Test Push with handle TestStart("Array_PushHandle"); { @@ -1555,6 +1690,68 @@ void Test_ParseAndSerialize() } TestEnd(); + // Test ValCount (only works on immutable documents) + TestStart("Parse_ValCount_SimpleObject"); + { + JSON json = JSON.Parse("{\"a\":1,\"b\":2,\"c\":3}"); + AssertValidHandle(json); + int valCount = json.ValCount; + // Object has 7 values: 1 object + 3 keys ("a","b","c") + 3 integer values (1,2,3) + AssertEq(valCount, 7, "Simple object should have 7 values (object + keys + values)"); + delete json; + } + TestEnd(); + + TestStart("Parse_ValCount_NestedStructure"); + { + // {"user":{"name":"John","age":30},"items":[1,2,3]} + // Root object: 1 + // "user" key + nested object: 2 + // "name" key + "John" value: 2 + // "age" key + 30 value: 2 + // "items" key + array: 2 + // Three integers in array: 3 + // Total: 1 + 2 + 2 + 2 + 2 + 3 = 12 + JSON json = JSON.Parse("{\"user\":{\"name\":\"John\",\"age\":30},\"items\":[1,2,3]}"); + AssertValidHandle(json); + int valCount = json.ValCount; + AssertTrue(valCount > 0, "Nested structure should have multiple values"); + delete json; + } + TestEnd(); + + TestStart("Parse_ValCount_Array"); + { + JSON json = JSON.Parse("[1,2,3,4,5]"); + AssertValidHandle(json); + int valCount = json.ValCount; + // Array itself + 5 integers = 6 + AssertEq(valCount, 6, "Array with 5 elements should have 6 values"); + delete json; + } + TestEnd(); + + TestStart("Parse_ValCount_MutableReturnsZero"); + { + JSON json = JSON.Parse("{\"key\":\"value\"}", .is_mutable_doc = true); + AssertValidHandle(json); + int valCount = json.ValCount; + AssertEq(valCount, 0, "ValCount should return 0 for mutable documents"); + delete json; + } + TestEnd(); + + TestStart("Parse_ValCount_CreatedObjectReturnsZero"); + { + JSONObject obj = new JSONObject(); + obj.SetInt("a", 1); + obj.SetInt("b", 2); + int valCount = obj.ValCount; + AssertEq(valCount, 0, "ValCount should return 0 for created (mutable) objects"); + delete obj; + } + TestEnd(); + // Test ToString TestStart("Serialize_ToString"); { diff --git a/src/JsonManager.cpp b/src/JsonManager.cpp index 2867549..e95a93f 100755 --- a/src/JsonManager.cpp +++ b/src/JsonManager.cpp @@ -137,9 +137,6 @@ JsonValue* JsonManager::ParseJSON(const char* json_str, bool is_file, bool is_mu readError.msg, readError.code, readError.pos); } } - if (idoc) { - yyjson_doc_free(idoc); - } return nullptr; } @@ -258,7 +255,7 @@ JsonValue* JsonManager::ApplyJsonPatch(JsonValue* target, JsonValue* patch, bool yyjson_mut_val* resultRoot = yyjson_mut_patch(doc, root, patchCopy, &patch_err); if (!resultRoot) { SetErrorSafe(error, error_size, "JSON patch failed (code %u, op index %zu, message: %s)", - patch_err.code, patch_err.idx, patch_err); + patch_err.code, patch_err.idx, patch_err.msg); return nullptr; } @@ -881,6 +878,14 @@ size_t JsonManager::GetRefCount(JsonValue* handle) return handle->GetDocumentRefCount(); } +size_t JsonManager::GetValCount(JsonValue* handle) +{ + if (!handle || !handle->IsImmutable()) { + return 0; + } + return yyjson_doc_get_val_count(handle->m_pDocument->get()); +} + JsonValue* JsonManager::ObjectInit() { auto pJSONValue = CreateWrapper(); @@ -949,9 +954,6 @@ JsonValue* JsonManager::ObjectParseString(const char* str, yyjson_read_flag read SetErrorSafe(error, error_size, "Failed to parse JSON str: %s (error code: %u, position: %zu)", readError.msg, readError.code, readError.pos); } - if (idoc) { - yyjson_doc_free(idoc); - } return nullptr; } @@ -994,9 +996,6 @@ JsonValue* JsonManager::ObjectParseFile(const char* path, yyjson_read_flag read_ SetErrorSafe(error, error_size, "Failed to parse JSON file: %s (error code: %u, msg: %s, position: %zu)", realpath, readError.code, readError.msg, readError.pos); } - if (idoc) { - yyjson_doc_free(idoc); - } return nullptr; } @@ -1181,7 +1180,7 @@ bool JsonManager::ObjectGetBool(JsonValue* handle, const char* key, bool* out_va } } -bool JsonManager::ObjectGetFloat(JsonValue* handle, const char* key, double* out_value) +bool JsonManager::ObjectGetDouble(JsonValue* handle, const char* key, double* out_value) { if (!handle || !key || !out_value) { return false; @@ -1189,19 +1188,19 @@ bool JsonManager::ObjectGetFloat(JsonValue* handle, const char* key, double* out if (handle->IsMutable()) { yyjson_mut_val* val = yyjson_mut_obj_get(handle->m_pVal_mut, key); - if (!val || !yyjson_mut_is_real(val)) { + if (!val || !yyjson_mut_is_num(val)) { return false; } - *out_value = yyjson_mut_get_real(val); + *out_value = yyjson_mut_get_num(val); return true; } else { yyjson_val* val = yyjson_obj_get(handle->m_pVal, key); - if (!val || !yyjson_is_real(val)) { + if (!val || !yyjson_is_num(val)) { return false; } - *out_value = yyjson_get_real(val); + *out_value = yyjson_get_num(val); return true; } } @@ -1381,7 +1380,7 @@ bool JsonManager::ObjectSetBool(JsonValue* handle, const char* key, bool value) return yyjson_mut_obj_put(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut->get(), key), yyjson_mut_bool(handle->m_pDocument_mut->get(), value)); } -bool JsonManager::ObjectSetFloat(JsonValue* handle, const char* key, double value) +bool JsonManager::ObjectSetDouble(JsonValue* handle, const char* key, double value) { if (!handle || !handle->IsMutable() || !key) { return false; @@ -1405,15 +1404,11 @@ bool JsonManager::ObjectSetInt64(JsonValue* handle, const char* key, std::varian return false; } - return std::visit([&](auto&& val) -> bool { - using T = std::decay_t; - if constexpr (std::is_same_v) { - return yyjson_mut_obj_put(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut->get(), key), yyjson_mut_sint(handle->m_pDocument_mut->get(), val)); - } else if constexpr (std::is_same_v) { - return yyjson_mut_obj_put(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut->get(), key), yyjson_mut_uint(handle->m_pDocument_mut->get(), val)); - } - return false; - }, value); + if (std::holds_alternative(value)) { + return yyjson_mut_obj_put(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut->get(), key), yyjson_mut_sint(handle->m_pDocument_mut->get(), std::get(value))); + } else { + return yyjson_mut_obj_put(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut->get(), key), yyjson_mut_uint(handle->m_pDocument_mut->get(), std::get(value))); + } } bool JsonManager::ObjectSetNull(JsonValue* handle, const char* key) @@ -1511,6 +1506,19 @@ bool JsonManager::ObjectSort(JsonValue* handle, JSON_SORT_ORDER sort_mode) return true; } +bool JsonManager::ObjectRotate(JsonValue* handle, size_t idx) +{ + if (!handle || !handle->IsMutable()) { + return false; + } + + if (!yyjson_mut_is_obj(handle->m_pVal_mut)) { + return false; + } + + return yyjson_mut_obj_rotate(handle->m_pVal_mut, idx); +} + JsonValue* JsonManager::ArrayInit() { auto pJSONValue = CreateWrapper(); @@ -1641,15 +1649,12 @@ JsonValue* JsonManager::ArrayInitWithInt64(const char** values, size_t count, ch return nullptr; } - yyjson_mut_val* val = std::visit([&](auto&& arg) -> yyjson_mut_val* { - using T = std::decay_t; - if constexpr (std::is_same_v) { - return yyjson_mut_sint(doc, arg); - } else if constexpr (std::is_same_v) { - return yyjson_mut_uint(doc, arg); - } - return nullptr; - }, variant_value); + yyjson_mut_val* val; + if (std::holds_alternative(variant_value)) { + val = yyjson_mut_sint(doc, std::get(variant_value)); + } else { + val = yyjson_mut_uint(doc, std::get(variant_value)); + } if (!val || !yyjson_mut_arr_append(pJSONValue->m_pVal_mut, val)) { if (error && error_size > 0) { @@ -1692,7 +1697,7 @@ JsonValue* JsonManager::ArrayInitWithBool(const bool* values, size_t count) return pJSONValue.release(); } -JsonValue* JsonManager::ArrayInitWithFloat(const double* values, size_t count) +JsonValue* JsonManager::ArrayInitWithDouble(const double* values, size_t count) { if (!values) { return nullptr; @@ -1740,9 +1745,6 @@ JsonValue* JsonManager::ArrayParseString(const char* str, yyjson_read_flag read_ SetErrorSafe(error, error_size, "Failed to parse JSON string: %s (error code: %u, position: %zu)", readError.msg, readError.code, readError.pos); } - if (idoc) { - yyjson_doc_free(idoc); - } return nullptr; } @@ -1785,9 +1787,6 @@ JsonValue* JsonManager::ArrayParseFile(const char* path, yyjson_read_flag read_f SetErrorSafe(error, error_size, "Failed to parse JSON file: %s (error code: %u, msg: %s, position: %zu)", realpath, readError.code, readError.msg, readError.pos); } - if (idoc) { - yyjson_doc_free(idoc); - } return nullptr; } @@ -1973,7 +1972,7 @@ bool JsonManager::ArrayGetBool(JsonValue* handle, size_t index, bool* out_value) } } -bool JsonManager::ArrayGetFloat(JsonValue* handle, size_t index, double* out_value) +bool JsonManager::ArrayGetDouble(JsonValue* handle, size_t index, double* out_value) { if (!handle || !out_value) { return false; @@ -1986,11 +1985,11 @@ bool JsonManager::ArrayGetFloat(JsonValue* handle, size_t index, double* out_val } yyjson_mut_val* val = yyjson_mut_arr_get(handle->m_pVal_mut, index); - if (!yyjson_mut_is_real(val)) { + if (!yyjson_mut_is_num(val)) { return false; } - *out_value = yyjson_mut_get_real(val); + *out_value = yyjson_mut_get_num(val); return true; } else { size_t arr_size = yyjson_arr_size(handle->m_pVal); @@ -1999,11 +1998,11 @@ bool JsonManager::ArrayGetFloat(JsonValue* handle, size_t index, double* out_val } yyjson_val* val = yyjson_arr_get(handle->m_pVal, index); - if (!yyjson_is_real(val)) { + if (!yyjson_is_num(val)) { return false; } - *out_value = yyjson_get_real(val); + *out_value = yyjson_get_num(val); return true; } } @@ -2183,7 +2182,7 @@ bool JsonManager::ArrayReplaceBool(JsonValue* handle, size_t index, bool value) return yyjson_mut_arr_replace(handle->m_pVal_mut, index, yyjson_mut_bool(handle->m_pDocument_mut->get(), value)) != nullptr; } -bool JsonManager::ArrayReplaceFloat(JsonValue* handle, size_t index, double value) +bool JsonManager::ArrayReplaceDouble(JsonValue* handle, size_t index, double value) { if (!handle || !handle->IsMutable()) { return false; @@ -2222,15 +2221,11 @@ bool JsonManager::ArrayReplaceInt64(JsonValue* handle, size_t index, std::varian return false; } - return std::visit([&](auto&& val) -> bool { - using T = std::decay_t; - if constexpr (std::is_same_v) { - return yyjson_mut_arr_replace(handle->m_pVal_mut, index, yyjson_mut_sint(handle->m_pDocument_mut->get(), val)) != nullptr; - } else if constexpr (std::is_same_v) { - return yyjson_mut_arr_replace(handle->m_pVal_mut, index, yyjson_mut_uint(handle->m_pDocument_mut->get(), val)) != nullptr; - } - return false; - }, value); + if (std::holds_alternative(value)) { + return yyjson_mut_arr_replace(handle->m_pVal_mut, index, yyjson_mut_sint(handle->m_pDocument_mut->get(), std::get(value))) != nullptr; + } else { + return yyjson_mut_arr_replace(handle->m_pVal_mut, index, yyjson_mut_uint(handle->m_pDocument_mut->get(), std::get(value))) != nullptr; + } } bool JsonManager::ArrayReplaceNull(JsonValue* handle, size_t index) @@ -2290,7 +2285,7 @@ bool JsonManager::ArrayAppendBool(JsonValue* handle, bool value) return yyjson_mut_arr_append(handle->m_pVal_mut, yyjson_mut_bool(handle->m_pDocument_mut->get(), value)); } -bool JsonManager::ArrayAppendFloat(JsonValue* handle, double value) +bool JsonManager::ArrayAppendDouble(JsonValue* handle, double value) { if (!handle || !handle->IsMutable()) { return false; @@ -2314,15 +2309,11 @@ bool JsonManager::ArrayAppendInt64(JsonValue* handle, std::variant bool { - using T = std::decay_t; - if constexpr (std::is_same_v) { - return yyjson_mut_arr_append(handle->m_pVal_mut, yyjson_mut_sint(handle->m_pDocument_mut->get(), val)); - } else if constexpr (std::is_same_v) { - return yyjson_mut_arr_append(handle->m_pVal_mut, yyjson_mut_uint(handle->m_pDocument_mut->get(), val)); - } - return false; - }, value); + if (std::holds_alternative(value)) { + return yyjson_mut_arr_append(handle->m_pVal_mut, yyjson_mut_sint(handle->m_pDocument_mut->get(), std::get(value))); + } else { + return yyjson_mut_arr_append(handle->m_pVal_mut, yyjson_mut_uint(handle->m_pDocument_mut->get(), std::get(value))); + } } bool JsonManager::ArrayAppendNull(JsonValue* handle) @@ -2392,15 +2383,12 @@ bool JsonManager::ArrayInsertInt64(JsonValue* handle, size_t index, std::variant return false; } - yyjson_mut_val* val = std::visit([&](auto&& arg) -> yyjson_mut_val* { - using T = std::decay_t; - if constexpr (std::is_same_v) { - return yyjson_mut_sint(handle->m_pDocument_mut->get(), arg); - } else if constexpr (std::is_same_v) { - return yyjson_mut_uint(handle->m_pDocument_mut->get(), arg); - } - return nullptr; - }, value); + yyjson_mut_val* val; + if (std::holds_alternative(value)) { + val = yyjson_mut_sint(handle->m_pDocument_mut->get(), std::get(value)); + } else { + val = yyjson_mut_uint(handle->m_pDocument_mut->get(), std::get(value)); + } if (!val) { return false; @@ -2409,7 +2397,7 @@ bool JsonManager::ArrayInsertInt64(JsonValue* handle, size_t index, std::variant return yyjson_mut_arr_insert(handle->m_pVal_mut, val, index); } -bool JsonManager::ArrayInsertFloat(JsonValue* handle, size_t index, double value) +bool JsonManager::ArrayInsertDouble(JsonValue* handle, size_t index, double value) { if (!handle || !handle->IsMutable()) { return false; @@ -2480,15 +2468,12 @@ bool JsonManager::ArrayPrependInt64(JsonValue* handle, std::variant yyjson_mut_val* { - using T = std::decay_t; - if constexpr (std::is_same_v) { - return yyjson_mut_sint(handle->m_pDocument_mut->get(), arg); - } else if constexpr (std::is_same_v) { - return yyjson_mut_uint(handle->m_pDocument_mut->get(), arg); - } - return nullptr; - }, value); + yyjson_mut_val* val; + if (std::holds_alternative(value)) { + val = yyjson_mut_sint(handle->m_pDocument_mut->get(), std::get(value)); + } else { + val = yyjson_mut_uint(handle->m_pDocument_mut->get(), std::get(value)); + } if (!val) { return false; @@ -2497,7 +2482,7 @@ bool JsonManager::ArrayPrependInt64(JsonValue* handle, std::variantm_pVal_mut, val); } -bool JsonManager::ArrayPrependFloat(JsonValue* handle, double value) +bool JsonManager::ArrayPrependDouble(JsonValue* handle, double value) { if (!handle || !handle->IsMutable()) { return false; @@ -2722,7 +2707,7 @@ int JsonManager::ArrayIndexOfInt64(JsonValue* handle, std::variant values; @@ -2881,6 +2866,19 @@ bool JsonManager::ArraySort(JsonValue* handle, JSON_SORT_ORDER sort_mode) return true; } +bool JsonManager::ArrayRotate(JsonValue* handle, size_t idx) +{ + if (!handle || !handle->IsMutable()) { + return false; + } + + if (!yyjson_mut_is_arr(handle->m_pVal_mut)) { + return false; + } + + return yyjson_mut_arr_rotate(handle->m_pVal_mut, idx); +} + const char* JsonManager::SkipSeparators(const char* ptr) { while (*ptr && (isspace(*ptr) || *ptr == ':' || *ptr == ',')) { @@ -3156,7 +3154,7 @@ JsonValue* JsonManager::CreateBool(bool value) return pJSONValue.release(); } -JsonValue* JsonManager::CreateFloat(double value) +JsonValue* JsonManager::CreateDouble(double value) { auto pJSONValue = CreateWrapper(); pJSONValue->m_pDocument_mut = CreateDocument(); @@ -3207,14 +3205,11 @@ JsonValue* JsonManager::CreateInt64(std::variant value) auto* doc = pJSONValue->m_pDocument_mut->get(); - std::visit([&](auto&& val) { - using T = std::decay_t; - if constexpr (std::is_same_v) { - pJSONValue->m_pVal_mut = yyjson_mut_sint(doc, val); - } else if constexpr (std::is_same_v) { - pJSONValue->m_pVal_mut = yyjson_mut_uint(doc, val); - } - }, value); + if (std::holds_alternative(value)) { + pJSONValue->m_pVal_mut = yyjson_mut_sint(doc, std::get(value)); + } else { + pJSONValue->m_pVal_mut = yyjson_mut_uint(doc, std::get(value)); + } if (!pJSONValue->m_pVal_mut) { return nullptr; @@ -3290,23 +3285,23 @@ bool JsonManager::GetBool(JsonValue* handle, bool* out_value) } } -bool JsonManager::GetFloat(JsonValue* handle, double* out_value) +bool JsonManager::GetDouble(JsonValue* handle, double* out_value) { if (!handle || !out_value) { return false; } if (handle->IsMutable()) { - if (!yyjson_mut_is_real(handle->m_pVal_mut)) { + if (!yyjson_mut_is_num(handle->m_pVal_mut)) { return false; } - *out_value = yyjson_mut_get_real(handle->m_pVal_mut); + *out_value = yyjson_mut_get_num(handle->m_pVal_mut); return true; } else { - if (!yyjson_is_real(handle->m_pVal)) { + if (!yyjson_is_num(handle->m_pVal)) { return false; } - *out_value = yyjson_get_real(handle->m_pVal); + *out_value = yyjson_get_num(handle->m_pVal); return true; } } @@ -3477,7 +3472,7 @@ bool JsonManager::PtrGetBool(JsonValue* handle, const char* path, bool* out_valu } } -bool JsonManager::PtrGetFloat(JsonValue* handle, const char* path, double* out_value, char* error, size_t error_size) +bool JsonManager::PtrGetDouble(JsonValue* handle, const char* path, double* out_value, char* error, size_t error_size) { if (!handle || !path || !out_value) { if (error && error_size > 0) { @@ -3499,14 +3494,14 @@ bool JsonManager::PtrGetFloat(JsonValue* handle, const char* path, double* out_v return false; } - if (!yyjson_mut_is_real(val)) { + if (!yyjson_mut_is_num(val)) { if (error && error_size > 0) { - SetErrorSafe(error, error_size, "Type mismatch at path '%s': expected float value, got %s", path, yyjson_mut_get_type_desc(val)); + SetErrorSafe(error, error_size, "Type mismatch at path '%s': expected number value, got %s", path, yyjson_mut_get_type_desc(val)); } return false; } - *out_value = yyjson_mut_get_real(val); + *out_value = yyjson_mut_get_num(val); return true; } else { yyjson_val* val = yyjson_doc_ptr_getx(handle->m_pDocument->get(), path, strlen(path), &ptrGetError); @@ -3519,14 +3514,14 @@ bool JsonManager::PtrGetFloat(JsonValue* handle, const char* path, double* out_v return false; } - if (!yyjson_is_real(val)) { + if (!yyjson_is_num(val)) { if (error && error_size > 0) { - SetErrorSafe(error, error_size, "Type mismatch at path '%s': expected float value, got %s", path, yyjson_get_type_desc(val)); + SetErrorSafe(error, error_size, "Type mismatch at path '%s': expected number value, got %s", path, yyjson_get_type_desc(val)); } return false; } - *out_value = yyjson_get_real(val); + *out_value = yyjson_get_num(val); return true; } } @@ -3849,7 +3844,7 @@ bool JsonManager::PtrSetBool(JsonValue* handle, const char* path, bool value, ch return success; } -bool JsonManager::PtrSetFloat(JsonValue* handle, const char* path, double value, char* error, size_t error_size) +bool JsonManager::PtrSetDouble(JsonValue* handle, const char* path, double value, char* error, size_t error_size) { if (!handle || !handle->IsMutable() || !path) { if (error && error_size > 0) { @@ -3914,14 +3909,12 @@ bool JsonManager::PtrSetInt64(JsonValue* handle, const char* path, std::variant< return false; } - yyjson_mut_val* val = std::visit([&](auto&& val_input) -> yyjson_mut_val* { - using T = std::decay_t; - if constexpr (std::is_same_v) { - return yyjson_mut_sint(handle->m_pDocument_mut->get(), val_input); - } else { - return yyjson_mut_uint(handle->m_pDocument_mut->get(), val_input); - } - }, value); + yyjson_mut_val* val; + if (std::holds_alternative(value)) { + val = yyjson_mut_sint(handle->m_pDocument_mut->get(), std::get(value)); + } else { + val = yyjson_mut_uint(handle->m_pDocument_mut->get(), std::get(value)); + } if (!val) { if (error && error_size > 0) { SetErrorSafe(error, error_size, "Failed to create JSON value"); @@ -4058,7 +4051,7 @@ bool JsonManager::PtrAddBool(JsonValue* handle, const char* path, bool value, ch return success; } -bool JsonManager::PtrAddFloat(JsonValue* handle, const char* path, double value, char* error, size_t error_size) +bool JsonManager::PtrAddDouble(JsonValue* handle, const char* path, double value, char* error, size_t error_size) { if (!handle || !handle->IsMutable() || !path) { if (error && error_size > 0) { @@ -4123,14 +4116,12 @@ bool JsonManager::PtrAddInt64(JsonValue* handle, const char* path, std::variant< return false; } - yyjson_mut_val* val = std::visit([&](auto&& val_input) -> yyjson_mut_val* { - using T = std::decay_t; - if constexpr (std::is_same_v) { - return yyjson_mut_sint(handle->m_pDocument_mut->get(), val_input); - } else { - return yyjson_mut_uint(handle->m_pDocument_mut->get(), val_input); - } - }, value); + yyjson_mut_val* val; + if (std::holds_alternative(value)) { + val = yyjson_mut_sint(handle->m_pDocument_mut->get(), std::get(value)); + } else { + val = yyjson_mut_uint(handle->m_pDocument_mut->get(), std::get(value)); + } if (!val) { if (error && error_size > 0) { SetErrorSafe(error, error_size, "Failed to create JSON value"); @@ -4300,7 +4291,7 @@ bool JsonManager::PtrTryGetBool(JsonValue* handle, const char* path, bool* out_v } } -bool JsonManager::PtrTryGetFloat(JsonValue* handle, const char* path, double* out_value) +bool JsonManager::PtrTryGetDouble(JsonValue* handle, const char* path, double* out_value) { if (!handle || !path || !out_value) { return false; @@ -4616,12 +4607,12 @@ void JsonManager::Release(JsonValue* value) } } -HandleType_t JsonManager::GetHandleType() +HandleType_t JsonManager::GetJsonHandleType() { return g_JsonType; } -JsonValue* JsonManager::GetFromHandle(IPluginContext* pContext, Handle_t handle) +JsonValue* JsonManager::GetValueFromHandle(IPluginContext* pContext, Handle_t handle) { HandleError err; HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity()); @@ -4945,6 +4936,27 @@ void* JsonManager::ObjIterRemove(JsonObjIter* iter) return yyjson_mut_obj_iter_remove(&iter->m_iterMut); } +bool JsonManager::ObjIterGetKeyString(JsonObjIter* iter, void* key, const char** out_str, size_t* out_len) +{ + if (!iter || !key || !out_str) { + return false; + } + + if (iter->m_isMutable) { + *out_str = yyjson_mut_get_str(reinterpret_cast(key)); + if (out_len) { + *out_len = yyjson_mut_get_len(reinterpret_cast(key)); + } + } else { + *out_str = yyjson_get_str(reinterpret_cast(key)); + if (out_len) { + *out_len = yyjson_get_len(reinterpret_cast(key)); + } + } + + return *out_str != nullptr; +} + void JsonManager::ReleaseArrIter(JsonArrIter* iter) { if (iter) { @@ -5143,26 +5155,22 @@ bool JsonManager::SetInt64(JsonValue* handle, std::variant va return false; } - return std::visit([&](auto&& val) -> bool { - using T = std::decay_t; - if (handle->IsMutable()) { - if constexpr (std::is_same_v) { - return yyjson_mut_set_sint(handle->m_pVal_mut, val); - } else if constexpr (std::is_same_v) { - return yyjson_mut_set_uint(handle->m_pVal_mut, val); - } + if (handle->IsMutable()) { + if (std::holds_alternative(value)) { + return yyjson_mut_set_sint(handle->m_pVal_mut, std::get(value)); } else { - if constexpr (std::is_same_v) { - return yyjson_set_sint(handle->m_pVal, val); - } else if constexpr (std::is_same_v) { - return yyjson_set_uint(handle->m_pVal, val); - } + return yyjson_mut_set_uint(handle->m_pVal_mut, std::get(value)); } - return false; - }, value); + } else { + if (std::holds_alternative(value)) { + return yyjson_set_sint(handle->m_pVal, std::get(value)); + } else { + return yyjson_set_uint(handle->m_pVal, std::get(value)); + } + } } -bool JsonManager::SetFloat(JsonValue* handle, double value) +bool JsonManager::SetDouble(JsonValue* handle, double value) { if (!handle) { return false; diff --git a/src/JsonManager.h b/src/JsonManager.h index 2fc6a14..c53b36f 100755 --- a/src/JsonManager.h +++ b/src/JsonManager.h @@ -346,6 +346,7 @@ class JsonManager : public IJsonManager virtual bool IsImmutable(JsonValue* handle) override; virtual size_t GetReadSize(JsonValue* handle) override; virtual size_t GetRefCount(JsonValue* handle) override; + virtual size_t GetValCount(JsonValue* handle) override; // ========== Object Operations ========== virtual JsonValue* ObjectInit() override; @@ -359,7 +360,7 @@ class JsonManager : public IJsonManager virtual JsonValue* ObjectGetValueAt(JsonValue* handle, size_t index) override; virtual JsonValue* ObjectGet(JsonValue* handle, const char* key) override; virtual bool ObjectGetBool(JsonValue* handle, const char* key, bool* out_value) override; - virtual bool ObjectGetFloat(JsonValue* handle, const char* key, double* out_value) override; + virtual bool ObjectGetDouble(JsonValue* handle, const char* key, double* out_value) override; virtual bool ObjectGetInt(JsonValue* handle, const char* key, int* out_value) override; virtual bool ObjectGetInt64(JsonValue* handle, const char* key, std::variant* out_value) override; virtual bool ObjectGetString(JsonValue* handle, const char* key, const char** out_str, size_t* out_len) override; @@ -368,7 +369,7 @@ class JsonManager : public IJsonManager virtual bool ObjectRenameKey(JsonValue* handle, const char* old_key, const char* new_key, bool allow_duplicate) override; virtual bool ObjectSet(JsonValue* handle, const char* key, JsonValue* value) override; virtual bool ObjectSetBool(JsonValue* handle, const char* key, bool value) override; - virtual bool ObjectSetFloat(JsonValue* handle, const char* key, double value) override; + virtual bool ObjectSetDouble(JsonValue* handle, const char* key, double value) override; virtual bool ObjectSetInt(JsonValue* handle, const char* key, int value) override; virtual bool ObjectSetInt64(JsonValue* handle, const char* key, std::variant value) override; virtual bool ObjectSetNull(JsonValue* handle, const char* key) override; @@ -376,6 +377,7 @@ class JsonManager : public IJsonManager virtual bool ObjectRemove(JsonValue* handle, const char* key) override; virtual bool ObjectClear(JsonValue* handle) override; virtual bool ObjectSort(JsonValue* handle, JSON_SORT_ORDER sort_mode) override; + virtual bool ObjectRotate(JsonValue* handle, size_t idx) override; // ========== Array Operations ========== virtual JsonValue* ArrayInit() override; @@ -384,7 +386,7 @@ class JsonManager : public IJsonManager virtual JsonValue* ArrayInitWithInt64(const char** values, size_t count, char* error, size_t error_size) override; virtual JsonValue* ArrayInitWithBool(const bool* values, size_t count) override; - virtual JsonValue* ArrayInitWithFloat(const double* values, size_t count) override; + virtual JsonValue* ArrayInitWithDouble(const double* values, size_t count) override; virtual JsonValue* ArrayParseString(const char* str, yyjson_read_flag read_flg, char* error, size_t error_size) override; virtual JsonValue* ArrayParseFile(const char* path, yyjson_read_flag read_flg, @@ -394,21 +396,21 @@ class JsonManager : public IJsonManager virtual JsonValue* ArrayGetFirst(JsonValue* handle) override; virtual JsonValue* ArrayGetLast(JsonValue* handle) override; virtual bool ArrayGetBool(JsonValue* handle, size_t index, bool* out_value) override; - virtual bool ArrayGetFloat(JsonValue* handle, size_t index, double* out_value) override; + virtual bool ArrayGetDouble(JsonValue* handle, size_t index, double* out_value) override; virtual bool ArrayGetInt(JsonValue* handle, size_t index, int* out_value) override; virtual bool ArrayGetInt64(JsonValue* handle, size_t index, std::variant* out_value) override; virtual bool ArrayGetString(JsonValue* handle, size_t index, const char** out_str, size_t* out_len) override; virtual bool ArrayIsNull(JsonValue* handle, size_t index) override; virtual bool ArrayReplace(JsonValue* handle, size_t index, JsonValue* value) override; virtual bool ArrayReplaceBool(JsonValue* handle, size_t index, bool value) override; - virtual bool ArrayReplaceFloat(JsonValue* handle, size_t index, double value) override; + virtual bool ArrayReplaceDouble(JsonValue* handle, size_t index, double value) override; virtual bool ArrayReplaceInt(JsonValue* handle, size_t index, int value) override; virtual bool ArrayReplaceInt64(JsonValue* handle, size_t index, std::variant value) override; virtual bool ArrayReplaceNull(JsonValue* handle, size_t index) override; virtual bool ArrayReplaceString(JsonValue* handle, size_t index, const char* value) override; virtual bool ArrayAppend(JsonValue* handle, JsonValue* value) override; virtual bool ArrayAppendBool(JsonValue* handle, bool value) override; - virtual bool ArrayAppendFloat(JsonValue* handle, double value) override; + virtual bool ArrayAppendDouble(JsonValue* handle, double value) override; virtual bool ArrayAppendInt(JsonValue* handle, int value) override; virtual bool ArrayAppendInt64(JsonValue* handle, std::variant value) override; virtual bool ArrayAppendNull(JsonValue* handle) override; @@ -417,14 +419,14 @@ class JsonManager : public IJsonManager virtual bool ArrayInsertBool(JsonValue* handle, size_t index, bool value) override; virtual bool ArrayInsertInt(JsonValue* handle, size_t index, int value) override; virtual bool ArrayInsertInt64(JsonValue* handle, size_t index, std::variant value) override; - virtual bool ArrayInsertFloat(JsonValue* handle, size_t index, double value) override; + virtual bool ArrayInsertDouble(JsonValue* handle, size_t index, double value) override; virtual bool ArrayInsertString(JsonValue* handle, size_t index, const char* value) override; virtual bool ArrayInsertNull(JsonValue* handle, size_t index) override; virtual bool ArrayPrepend(JsonValue* handle, JsonValue* value) override; virtual bool ArrayPrependBool(JsonValue* handle, bool value) override; virtual bool ArrayPrependInt(JsonValue* handle, int value) override; virtual bool ArrayPrependInt64(JsonValue* handle, std::variant value) override; - virtual bool ArrayPrependFloat(JsonValue* handle, double value) override; + virtual bool ArrayPrependDouble(JsonValue* handle, double value) override; virtual bool ArrayPrependString(JsonValue* handle, const char* value) override; virtual bool ArrayPrependNull(JsonValue* handle) override; virtual bool ArrayRemove(JsonValue* handle, size_t index) override; @@ -436,19 +438,20 @@ class JsonManager : public IJsonManager virtual int ArrayIndexOfString(JsonValue* handle, const char* search_value) override; virtual int ArrayIndexOfInt(JsonValue* handle, int search_value) override; virtual int ArrayIndexOfInt64(JsonValue* handle, std::variant search_value) override; - virtual int ArrayIndexOfFloat(JsonValue* handle, double search_value) override; + virtual int ArrayIndexOfDouble(JsonValue* handle, double search_value) override; virtual bool ArraySort(JsonValue* handle, JSON_SORT_ORDER sort_mode) override; + virtual bool ArrayRotate(JsonValue* handle, size_t idx) override; // ========== Value Operations ========== virtual JsonValue* Pack(const char* format, IPackParamProvider* param_provider, char* error, size_t error_size) override; virtual JsonValue* CreateBool(bool value) override; - virtual JsonValue* CreateFloat(double value) override; + virtual JsonValue* CreateDouble(double value) override; virtual JsonValue* CreateInt(int value) override; virtual JsonValue* CreateInt64(std::variant value) override; virtual JsonValue* CreateNull() override; virtual JsonValue* CreateString(const char* value) override; virtual bool GetBool(JsonValue* handle, bool* out_value) override; - virtual bool GetFloat(JsonValue* handle, double* out_value) override; + virtual bool GetDouble(JsonValue* handle, double* out_value) override; virtual bool GetInt(JsonValue* handle, int* out_value) override; virtual bool GetInt64(JsonValue* handle, std::variant* out_value) override; virtual bool GetString(JsonValue* handle, const char** out_str, size_t* out_len) override; @@ -456,7 +459,7 @@ class JsonManager : public IJsonManager // ========== Pointer Operations ========== virtual JsonValue* PtrGet(JsonValue* handle, const char* path, char* error, size_t error_size) override; virtual bool PtrGetBool(JsonValue* handle, const char* path, bool* out_value, char* error, size_t error_size) override; - virtual bool PtrGetFloat(JsonValue* handle, const char* path, double* out_value, char* error, size_t error_size) override; + virtual bool PtrGetDouble(JsonValue* handle, const char* path, double* out_value, char* error, size_t error_size) override; virtual bool PtrGetInt(JsonValue* handle, const char* path, int* out_value, char* error, size_t error_size) override; virtual bool PtrGetInt64(JsonValue* handle, const char* path, std::variant* out_value, char* error, size_t error_size) override; virtual bool PtrGetString(JsonValue* handle, const char* path, const char** out_str, size_t* out_len, char* error, size_t error_size) override; @@ -464,14 +467,14 @@ class JsonManager : public IJsonManager virtual bool PtrGetLength(JsonValue* handle, const char* path, size_t* out_len, char* error, size_t error_size) override; virtual bool PtrSet(JsonValue* handle, const char* path, JsonValue* value, char* error, size_t error_size) override; virtual bool PtrSetBool(JsonValue* handle, const char* path, bool value, char* error, size_t error_size) override; - virtual bool PtrSetFloat(JsonValue* handle, const char* path, double value, char* error, size_t error_size) override; + virtual bool PtrSetDouble(JsonValue* handle, const char* path, double value, char* error, size_t error_size) override; virtual bool PtrSetInt(JsonValue* handle, const char* path, int value, char* error, size_t error_size) override; virtual bool PtrSetInt64(JsonValue* handle, const char* path, std::variant value, char* error, size_t error_size) override; virtual bool PtrSetString(JsonValue* handle, const char* path, const char* value, char* error, size_t error_size) override; virtual bool PtrSetNull(JsonValue* handle, const char* path, char* error, size_t error_size) override; virtual bool PtrAdd(JsonValue* handle, const char* path, JsonValue* value, char* error, size_t error_size) override; virtual bool PtrAddBool(JsonValue* handle, const char* path, bool value, char* error, size_t error_size) override; - virtual bool PtrAddFloat(JsonValue* handle, const char* path, double value, char* error, size_t error_size) override; + virtual bool PtrAddDouble(JsonValue* handle, const char* path, double value, char* error, size_t error_size) override; virtual bool PtrAddInt(JsonValue* handle, const char* path, int value, char* error, size_t error_size) override; virtual bool PtrAddInt64(JsonValue* handle, const char* path, std::variant value, char* error, size_t error_size) override; virtual bool PtrAddString(JsonValue* handle, const char* path, const char* value, char* error, size_t error_size) override; @@ -479,7 +482,7 @@ class JsonManager : public IJsonManager virtual bool PtrRemove(JsonValue* handle, const char* path, char* error, size_t error_size) override; virtual JsonValue* PtrTryGet(JsonValue* handle, const char* path) override; virtual bool PtrTryGetBool(JsonValue* handle, const char* path, bool* out_value) override; - virtual bool PtrTryGetFloat(JsonValue* handle, const char* path, double* out_value) override; + virtual bool PtrTryGetDouble(JsonValue* handle, const char* path, double* out_value) override; virtual bool PtrTryGetInt(JsonValue* handle, const char* path, int* out_value) override; virtual bool PtrTryGetInt64(JsonValue* handle, const char* path, std::variant* out_value) override; virtual bool PtrTryGetString(JsonValue* handle, const char* path, const char** out_str, size_t* out_len) override; @@ -512,6 +515,7 @@ class JsonManager : public IJsonManager virtual JsonValue* ObjIterGet(JsonObjIter* iter, const char* key) override; virtual size_t ObjIterGetIndex(JsonObjIter* iter) override; virtual void* ObjIterRemove(JsonObjIter* iter) override; + virtual bool ObjIterGetKeyString(JsonObjIter* iter, void* key, const char** out_str, size_t* out_len = nullptr) override; // ========== Iterator Release Operations ========== virtual void ReleaseArrIter(JsonArrIter* iter) override; @@ -527,10 +531,10 @@ class JsonManager : public IJsonManager virtual void Release(JsonValue* value) override; // ========== Handle Type Operations ========== - virtual HandleType_t GetHandleType() override; + virtual HandleType_t GetJsonHandleType() override; // ========== Handle Operations ========== - virtual JsonValue* GetFromHandle(IPluginContext* pContext, Handle_t handle) override; + virtual JsonValue* GetValueFromHandle(IPluginContext* pContext, Handle_t handle) override; // ========== Number Read/Write Operations ========== virtual JsonValue* ReadNumber(const char* dat, uint32_t read_flg = 0, @@ -546,7 +550,7 @@ class JsonManager : public IJsonManager virtual bool SetBool(JsonValue* handle, bool value) override; virtual bool SetInt(JsonValue* handle, int value) override; virtual bool SetInt64(JsonValue* handle, std::variant value) override; - virtual bool SetFloat(JsonValue* handle, double value) override; + virtual bool SetDouble(JsonValue* handle, double value) override; virtual bool SetString(JsonValue* handle, const char* value) override; virtual bool SetNull(JsonValue* handle) override; diff --git a/src/JsonNatives.cpp b/src/JsonNatives.cpp index c964a43..63f432c 100755 --- a/src/JsonNatives.cpp +++ b/src/JsonNatives.cpp @@ -49,6 +49,35 @@ class SourceModPackParamProvider : public IPackParamProvider } }; +/** + * Helper function: Convert int64 variant to string + * @param value The variant containing int64_t or uint64_t + * @param buffer Output buffer + * @param buffer_size Size of the buffer + * @return true on success, false on error + */ +static inline bool Int64VariantToString(const std::variant& value, char* buffer, size_t buffer_size) +{ + if (!buffer || buffer_size < 2) { + return false; + } + + std::to_chars_result result; + + if (std::holds_alternative(value)) { + result = std::to_chars(buffer, buffer + buffer_size - 1, std::get(value)); + } else { + result = std::to_chars(buffer, buffer + buffer_size - 1, std::get(value)); + } + + if (result.ec == std::errc()) { + *result.ptr = '\0'; + return true; + } + + return false; +} + /** * Helper function: Create a SourceMod handle for JsonValue and return it directly * Used by functions that return Handle_t @@ -149,18 +178,12 @@ static cell_t CreateAndReturnObjIterHandle(IPluginContext* pContext, JsonObjIter static cell_t json_pack(IPluginContext* pContext, const cell_t* params) { - // SourcePawn has a limit of 32 parameters (defined SP_MAX_EXEC_PARAMS) - // including the format string, so we need to check if the number of parameters is less than the limit - if (params[0] > SP_MAX_EXEC_PARAMS - 1) { - return pContext->ThrowNativeError("Too many parameters (max %d)", SP_MAX_EXEC_PARAMS - 1); - } - char* fmt; pContext->LocalToString(params[1], &fmt); SourceModPackParamProvider provider(pContext, params, 2); - char error[JSON_PACK_ERROR_SIZE]; + char error[JSON_ERROR_BUFFER_SIZE]; JsonValue* pJSONValue = g_pJsonManager->Pack(fmt, &provider, error, sizeof(error)); if (!pJSONValue) { @@ -191,8 +214,8 @@ static cell_t json_doc_parse(IPluginContext* pContext, const cell_t* params) static cell_t json_doc_equals(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle1 = g_pJsonManager->GetFromHandle(pContext, params[1]); - JsonValue* handle2 = g_pJsonManager->GetFromHandle(pContext, params[2]); + JsonValue* handle1 = g_pJsonManager->GetValueFromHandle(pContext, params[1]); + JsonValue* handle2 = g_pJsonManager->GetValueFromHandle(pContext, params[2]); if (!handle1 || !handle2) return 0; @@ -201,8 +224,8 @@ static cell_t json_doc_equals(IPluginContext* pContext, const cell_t* params) static cell_t json_doc_copy_deep(IPluginContext* pContext, const cell_t* params) { - JsonValue* targetDoc = g_pJsonManager->GetFromHandle(pContext, params[1]); - JsonValue* sourceValue = g_pJsonManager->GetFromHandle(pContext, params[2]); + JsonValue* targetDoc = g_pJsonManager->GetValueFromHandle(pContext, params[1]); + JsonValue* sourceValue = g_pJsonManager->GetValueFromHandle(pContext, params[2]); if (!targetDoc || !sourceValue) return 0; @@ -217,7 +240,7 @@ static cell_t json_doc_copy_deep(IPluginContext* pContext, const cell_t* params) static cell_t json_get_type_desc(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -233,7 +256,7 @@ static cell_t json_obj_parse_str(IPluginContext* pContext, const cell_t* params) pContext->LocalToString(params[1], &str); uint32_t read_flg = static_cast(params[2]); - char error[JSON_PACK_ERROR_SIZE]; + char error[JSON_ERROR_BUFFER_SIZE]; JsonValue* pJSONValue = g_pJsonManager->ObjectParseString(str, read_flg, error, sizeof(error)); if (!pJSONValue) { @@ -249,7 +272,7 @@ static cell_t json_obj_parse_file(IPluginContext* pContext, const cell_t* params pContext->LocalToString(params[1], &path); uint32_t read_flg = static_cast(params[2]); - char error[JSON_PACK_ERROR_SIZE]; + char error[JSON_ERROR_BUFFER_SIZE]; JsonValue* pJSONValue = g_pJsonManager->ObjectParseFile(path, read_flg, error, sizeof(error)); if (!pJSONValue) { @@ -265,7 +288,7 @@ static cell_t json_arr_parse_str(IPluginContext* pContext, const cell_t* params) pContext->LocalToString(params[1], &str); uint32_t read_flg = static_cast(params[2]); - char error[JSON_PACK_ERROR_SIZE]; + char error[JSON_ERROR_BUFFER_SIZE]; JsonValue* pJSONValue = g_pJsonManager->ArrayParseString(str, read_flg, error, sizeof(error)); if (!pJSONValue) { @@ -281,7 +304,7 @@ static cell_t json_arr_parse_file(IPluginContext* pContext, const cell_t* params pContext->LocalToString(params[1], &path); uint32_t read_flg = static_cast(params[2]); - char error[JSON_PACK_ERROR_SIZE]; + char error[JSON_ERROR_BUFFER_SIZE]; JsonValue* pJSONValue = g_pJsonManager->ArrayParseFile(path, read_flg, error, sizeof(error)); if (!pJSONValue) { @@ -293,7 +316,7 @@ static cell_t json_arr_parse_file(IPluginContext* pContext, const cell_t* params static cell_t json_arr_index_of_bool(IPluginContext *pContext, const cell_t *params) { - JsonValue *handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue *handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -303,7 +326,7 @@ static cell_t json_arr_index_of_bool(IPluginContext *pContext, const cell_t *par static cell_t json_arr_index_of_str(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -315,7 +338,7 @@ static cell_t json_arr_index_of_str(IPluginContext* pContext, const cell_t* para static cell_t json_arr_index_of_int(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -325,7 +348,7 @@ static cell_t json_arr_index_of_int(IPluginContext* pContext, const cell_t* para static cell_t json_arr_index_of_integer64(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -344,17 +367,16 @@ static cell_t json_arr_index_of_integer64(IPluginContext* pContext, const cell_t static cell_t json_arr_index_of_float(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; - float searchValue = sp_ctof(params[2]); - return g_pJsonManager->ArrayIndexOfFloat(handle, searchValue); + return g_pJsonManager->ArrayIndexOfDouble(handle, sp_ctof(params[2])); } static cell_t json_get_type(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -363,7 +385,7 @@ static cell_t json_get_type(IPluginContext* pContext, const cell_t* params) static cell_t json_get_subtype(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -372,7 +394,7 @@ static cell_t json_get_subtype(IPluginContext* pContext, const cell_t* params) static cell_t json_is_array(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -381,7 +403,7 @@ static cell_t json_is_array(IPluginContext* pContext, const cell_t* params) static cell_t json_is_object(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -390,7 +412,7 @@ static cell_t json_is_object(IPluginContext* pContext, const cell_t* params) static cell_t json_is_int(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -399,7 +421,7 @@ static cell_t json_is_int(IPluginContext* pContext, const cell_t* params) static cell_t json_is_uint(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -408,7 +430,7 @@ static cell_t json_is_uint(IPluginContext* pContext, const cell_t* params) static cell_t json_is_sint(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -417,7 +439,7 @@ static cell_t json_is_sint(IPluginContext* pContext, const cell_t* params) static cell_t json_is_num(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -426,7 +448,7 @@ static cell_t json_is_num(IPluginContext* pContext, const cell_t* params) static cell_t json_is_bool(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -435,7 +457,7 @@ static cell_t json_is_bool(IPluginContext* pContext, const cell_t* params) static cell_t json_is_true(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -444,7 +466,7 @@ static cell_t json_is_true(IPluginContext* pContext, const cell_t* params) static cell_t json_is_false(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -453,7 +475,7 @@ static cell_t json_is_false(IPluginContext* pContext, const cell_t* params) static cell_t json_is_float(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -462,7 +484,7 @@ static cell_t json_is_float(IPluginContext* pContext, const cell_t* params) static cell_t json_is_str(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -471,7 +493,7 @@ static cell_t json_is_str(IPluginContext* pContext, const cell_t* params) static cell_t json_is_null(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -480,7 +502,7 @@ static cell_t json_is_null(IPluginContext* pContext, const cell_t* params) static cell_t json_is_ctn(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -489,7 +511,7 @@ static cell_t json_is_ctn(IPluginContext* pContext, const cell_t* params) static cell_t json_is_mutable(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -498,7 +520,7 @@ static cell_t json_is_mutable(IPluginContext* pContext, const cell_t* params) static cell_t json_is_immutable(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -566,7 +588,7 @@ static cell_t json_create_bool(IPluginContext* pContext, const cell_t* params) static cell_t json_create_float(IPluginContext* pContext, const cell_t* params) { - JsonValue* pJSONValue = g_pJsonManager->CreateFloat(sp_ctof(params[1])); + JsonValue* pJSONValue = g_pJsonManager->CreateDouble(sp_ctof(params[1])); return CreateAndReturnHandle(pContext, pJSONValue, "JSON float value"); } @@ -613,7 +635,7 @@ static cell_t json_create_str(IPluginContext* pContext, const cell_t* params) static cell_t json_get_bool(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -627,12 +649,12 @@ static cell_t json_get_bool(IPluginContext* pContext, const cell_t* params) static cell_t json_get_float(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; double value; - if (!g_pJsonManager->GetFloat(handle, &value)) { + if (!g_pJsonManager->GetDouble(handle, &value)) { return pContext->ThrowNativeError("Type mismatch: expected float value"); } @@ -641,7 +663,7 @@ static cell_t json_get_float(IPluginContext* pContext, const cell_t* params) static cell_t json_get_int(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -655,7 +677,7 @@ static cell_t json_get_int(IPluginContext* pContext, const cell_t* params) static cell_t json_get_integer64(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -665,10 +687,8 @@ static cell_t json_get_integer64(IPluginContext* pContext, const cell_t* params) } char result[JSON_INT64_BUFFER_SIZE]; - if (std::holds_alternative(value)) { - snprintf(result, sizeof(result), "%" PRIu64, std::get(value)); - } else { - snprintf(result, sizeof(result), "%" PRId64, std::get(value)); + if (!Int64VariantToString(value, result, sizeof(result))) { + return pContext->ThrowNativeError("Failed to convert integer64 to string"); } pContext->StringToLocalUTF8(params[2], params[3], result, nullptr); @@ -677,7 +697,7 @@ static cell_t json_get_integer64(IPluginContext* pContext, const cell_t* params) static cell_t json_get_str(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -701,7 +721,7 @@ static cell_t json_get_str(IPluginContext* pContext, const cell_t* params) static cell_t json_equals_str(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; char* str; @@ -712,7 +732,7 @@ static cell_t json_equals_str(IPluginContext* pContext, const cell_t* params) static cell_t json_get_serialized_size(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -724,7 +744,7 @@ static cell_t json_get_serialized_size(IPluginContext* pContext, const cell_t* p static cell_t json_get_read_size(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -736,13 +756,22 @@ static cell_t json_get_read_size(IPluginContext* pContext, const cell_t* params) static cell_t json_get_ref_count(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; return g_pJsonManager->GetRefCount(handle); } +static cell_t json_get_val_count(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + return g_pJsonManager->GetValCount(handle); +} + static cell_t json_create_null(IPluginContext* pContext, const cell_t* params) { JsonValue* pJSONValue = g_pJsonManager->CreateNull(); @@ -861,7 +890,7 @@ static cell_t json_arr_init_with_float(IPluginContext* pContext, const cell_t* p values.push_back(sp_ctof(addr[i])); } - JsonValue* pJSONValue = g_pJsonManager->ArrayInitWithFloat(values.data(), values.size()); + JsonValue* pJSONValue = g_pJsonManager->ArrayInitWithDouble(values.data(), values.size()); if (!pJSONValue) { return pContext->ThrowNativeError("Failed to create JSON array from float values"); @@ -872,7 +901,7 @@ static cell_t json_arr_init_with_float(IPluginContext* pContext, const cell_t* p static cell_t json_arr_get_size(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -882,7 +911,7 @@ static cell_t json_arr_get_size(IPluginContext* pContext, const cell_t* params) static cell_t json_arr_get_val(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -903,7 +932,7 @@ static cell_t json_arr_get_val(IPluginContext* pContext, const cell_t* params) static cell_t json_arr_get_first(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -918,7 +947,7 @@ static cell_t json_arr_get_first(IPluginContext* pContext, const cell_t* params) static cell_t json_arr_get_last(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -933,7 +962,7 @@ static cell_t json_arr_get_last(IPluginContext* pContext, const cell_t* params) static cell_t json_arr_get_bool(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -953,7 +982,7 @@ static cell_t json_arr_get_bool(IPluginContext* pContext, const cell_t* params) static cell_t json_arr_get_float(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -964,7 +993,7 @@ static cell_t json_arr_get_float(IPluginContext* pContext, const cell_t* params) size_t index = static_cast(index_param); double value; - if (!g_pJsonManager->ArrayGetFloat(handle, index, &value)) { + if (!g_pJsonManager->ArrayGetDouble(handle, index, &value)) { return pContext->ThrowNativeError("Failed to get float at index %d", index); } @@ -973,7 +1002,7 @@ static cell_t json_arr_get_float(IPluginContext* pContext, const cell_t* params) static cell_t json_arr_get_integer(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -993,7 +1022,7 @@ static cell_t json_arr_get_integer(IPluginContext* pContext, const cell_t* param static cell_t json_arr_get_integer64(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -1009,10 +1038,8 @@ static cell_t json_arr_get_integer64(IPluginContext* pContext, const cell_t* par } char result[JSON_INT64_BUFFER_SIZE]; - if (std::holds_alternative(value)) { - snprintf(result, sizeof(result), "%" PRIu64, std::get(value)); - } else { - snprintf(result, sizeof(result), "%" PRId64, std::get(value)); + if (!Int64VariantToString(value, result, sizeof(result))) { + return pContext->ThrowNativeError("Failed to convert integer64 to string"); } pContext->StringToLocalUTF8(params[3], params[4], result, nullptr); @@ -1021,7 +1048,7 @@ static cell_t json_arr_get_integer64(IPluginContext* pContext, const cell_t* par static cell_t json_arr_get_str(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -1049,7 +1076,7 @@ static cell_t json_arr_get_str(IPluginContext* pContext, const cell_t* params) static cell_t json_arr_is_null(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -1064,8 +1091,8 @@ static cell_t json_arr_is_null(IPluginContext* pContext, const cell_t* params) static cell_t json_arr_replace_val(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle1 = g_pJsonManager->GetFromHandle(pContext, params[1]); - JsonValue* handle2 = g_pJsonManager->GetFromHandle(pContext, params[3]); + JsonValue* handle1 = g_pJsonManager->GetValueFromHandle(pContext, params[1]); + JsonValue* handle2 = g_pJsonManager->GetValueFromHandle(pContext, params[3]); if (!handle1 || !handle2) return 0; @@ -1084,7 +1111,7 @@ static cell_t json_arr_replace_val(IPluginContext* pContext, const cell_t* param static cell_t json_arr_replace_bool(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -1103,7 +1130,7 @@ static cell_t json_arr_replace_bool(IPluginContext* pContext, const cell_t* para static cell_t json_arr_replace_float(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -1117,12 +1144,12 @@ static cell_t json_arr_replace_float(IPluginContext* pContext, const cell_t* par } size_t index = static_cast(index_param); - return g_pJsonManager->ArrayReplaceFloat(handle, index, sp_ctof(params[3])); + return g_pJsonManager->ArrayReplaceDouble(handle, index, sp_ctof(params[3])); } static cell_t json_arr_replace_integer(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -1141,7 +1168,7 @@ static cell_t json_arr_replace_integer(IPluginContext* pContext, const cell_t* p static cell_t json_arr_replace_integer64(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -1170,7 +1197,7 @@ static cell_t json_arr_replace_integer64(IPluginContext* pContext, const cell_t* static cell_t json_arr_replace_null(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -1189,7 +1216,7 @@ static cell_t json_arr_replace_null(IPluginContext* pContext, const cell_t* para static cell_t json_arr_replace_str(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -1211,8 +1238,8 @@ static cell_t json_arr_replace_str(IPluginContext* pContext, const cell_t* param static cell_t json_arr_append_val(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle1 = g_pJsonManager->GetFromHandle(pContext, params[1]); - JsonValue* handle2 = g_pJsonManager->GetFromHandle(pContext, params[2]); + JsonValue* handle1 = g_pJsonManager->GetValueFromHandle(pContext, params[1]); + JsonValue* handle2 = g_pJsonManager->GetValueFromHandle(pContext, params[2]); if (!handle1 || !handle2) return 0; @@ -1225,7 +1252,7 @@ static cell_t json_arr_append_val(IPluginContext* pContext, const cell_t* params static cell_t json_arr_append_bool(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -1238,7 +1265,7 @@ static cell_t json_arr_append_bool(IPluginContext* pContext, const cell_t* param static cell_t json_arr_append_float(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -1246,12 +1273,12 @@ static cell_t json_arr_append_float(IPluginContext* pContext, const cell_t* para return pContext->ThrowNativeError("Cannot append value to an immutable JSON array"); } - return g_pJsonManager->ArrayAppendFloat(handle, sp_ctof(params[2])); + return g_pJsonManager->ArrayAppendDouble(handle, sp_ctof(params[2])); } static cell_t json_arr_append_int(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -1264,7 +1291,7 @@ static cell_t json_arr_append_int(IPluginContext* pContext, const cell_t* params static cell_t json_arr_append_integer64(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -1287,7 +1314,7 @@ static cell_t json_arr_append_integer64(IPluginContext* pContext, const cell_t* static cell_t json_arr_append_null(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -1300,7 +1327,7 @@ static cell_t json_arr_append_null(IPluginContext* pContext, const cell_t* param static cell_t json_arr_append_str(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -1316,7 +1343,7 @@ static cell_t json_arr_append_str(IPluginContext* pContext, const cell_t* params static cell_t json_arr_insert(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; if (!handle->IsMutable()) { @@ -1334,7 +1361,7 @@ static cell_t json_arr_insert(IPluginContext* pContext, const cell_t* params) return pContext->ThrowNativeError("Index is out of bounds (got %d, max %zu)", index, arr_size); } - JsonValue* value = g_pJsonManager->GetFromHandle(pContext, params[3]); + JsonValue* value = g_pJsonManager->GetValueFromHandle(pContext, params[3]); if (!value) return 0; return g_pJsonManager->ArrayInsert(handle, index, value); @@ -1342,7 +1369,7 @@ static cell_t json_arr_insert(IPluginContext* pContext, const cell_t* params) static cell_t json_arr_insert_bool(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; if (!handle->IsMutable()) { @@ -1367,7 +1394,7 @@ static cell_t json_arr_insert_bool(IPluginContext* pContext, const cell_t* param static cell_t json_arr_insert_int(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; if (!handle->IsMutable()) { @@ -1392,7 +1419,7 @@ static cell_t json_arr_insert_int(IPluginContext* pContext, const cell_t* params static cell_t json_arr_insert_int64(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; if (!handle->IsMutable()) { @@ -1425,7 +1452,7 @@ static cell_t json_arr_insert_int64(IPluginContext* pContext, const cell_t* para static cell_t json_arr_insert_float(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; if (!handle->IsMutable()) { @@ -1443,14 +1470,12 @@ static cell_t json_arr_insert_float(IPluginContext* pContext, const cell_t* para return pContext->ThrowNativeError("Index is out of bounds (got %d, max %zu)", index, arr_size); } - float value = sp_ctof(params[3]); - - return g_pJsonManager->ArrayInsertFloat(handle, index, value); + return g_pJsonManager->ArrayInsertDouble(handle, index, sp_ctof(params[3])); } static cell_t json_arr_insert_str(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; if (!handle->IsMutable()) { @@ -1476,7 +1501,7 @@ static cell_t json_arr_insert_str(IPluginContext* pContext, const cell_t* params static cell_t json_arr_insert_null(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; if (!handle->IsMutable()) { @@ -1494,20 +1519,19 @@ static cell_t json_arr_insert_null(IPluginContext* pContext, const cell_t* param return pContext->ThrowNativeError("Index is out of bounds (got %d, max %zu)", index, arr_size); } - return g_pJsonManager->ArrayInsertNull(handle, index); } static cell_t json_arr_prepend(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; if (!handle->IsMutable()) { return pContext->ThrowNativeError("Cannot prepend value to an immutable JSON array"); } - JsonValue* value = g_pJsonManager->GetFromHandle(pContext, params[2]); + JsonValue* value = g_pJsonManager->GetValueFromHandle(pContext, params[2]); if (!value) return 0; return g_pJsonManager->ArrayPrepend(handle, value); @@ -1515,7 +1539,7 @@ static cell_t json_arr_prepend(IPluginContext* pContext, const cell_t* params) static cell_t json_arr_prepend_bool(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; if (!handle->IsMutable()) { @@ -1529,7 +1553,7 @@ static cell_t json_arr_prepend_bool(IPluginContext* pContext, const cell_t* para static cell_t json_arr_prepend_int(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; if (!handle->IsMutable()) { @@ -1543,7 +1567,7 @@ static cell_t json_arr_prepend_int(IPluginContext* pContext, const cell_t* param static cell_t json_arr_prepend_int64(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; if (!handle->IsMutable()) { @@ -1565,21 +1589,19 @@ static cell_t json_arr_prepend_int64(IPluginContext* pContext, const cell_t* par static cell_t json_arr_prepend_float(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; if (!handle->IsMutable()) { return pContext->ThrowNativeError("Cannot prepend value to an immutable JSON array"); } - float value = sp_ctof(params[2]); - - return g_pJsonManager->ArrayPrependFloat(handle, value); + return g_pJsonManager->ArrayPrependDouble(handle, sp_ctof(params[2])); } static cell_t json_arr_prepend_str(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; if (!handle->IsMutable()) { @@ -1594,7 +1616,7 @@ static cell_t json_arr_prepend_str(IPluginContext* pContext, const cell_t* param static cell_t json_arr_prepend_null(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; if (!handle->IsMutable()) { @@ -1606,7 +1628,7 @@ static cell_t json_arr_prepend_null(IPluginContext* pContext, const cell_t* para static cell_t json_arr_remove(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -1625,7 +1647,7 @@ static cell_t json_arr_remove(IPluginContext* pContext, const cell_t* params) static cell_t json_arr_remove_first(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -1638,7 +1660,7 @@ static cell_t json_arr_remove_first(IPluginContext* pContext, const cell_t* para static cell_t json_arr_remove_last(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -1651,7 +1673,7 @@ static cell_t json_arr_remove_last(IPluginContext* pContext, const cell_t* param static cell_t json_arr_remove_range(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -1675,7 +1697,7 @@ static cell_t json_arr_remove_range(IPluginContext* pContext, const cell_t* para static cell_t json_arr_clear(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -1688,7 +1710,7 @@ static cell_t json_arr_clear(IPluginContext* pContext, const cell_t* params) static cell_t json_doc_write_to_str(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -1715,7 +1737,7 @@ static cell_t json_doc_write_to_str(IPluginContext* pContext, const cell_t* para static cell_t json_doc_write_to_file(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -1723,7 +1745,7 @@ static cell_t json_doc_write_to_file(IPluginContext* pContext, const cell_t* par pContext->LocalToString(params[2], &path); uint32_t write_flg = static_cast(params[3]); - char error[JSON_PACK_ERROR_SIZE]; + char error[JSON_ERROR_BUFFER_SIZE]; if (!g_pJsonManager->WriteToFile(handle, path, write_flg, error, sizeof(error))) { return pContext->ThrowNativeError(error); } @@ -1733,7 +1755,7 @@ static cell_t json_doc_write_to_file(IPluginContext* pContext, const cell_t* par static cell_t json_obj_get_size(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -1743,7 +1765,7 @@ static cell_t json_obj_get_size(IPluginContext* pContext, const cell_t* params) static cell_t json_obj_get_key(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -1764,7 +1786,7 @@ static cell_t json_obj_get_key(IPluginContext* pContext, const cell_t* params) static cell_t json_obj_get_val_at(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -1785,7 +1807,7 @@ static cell_t json_obj_get_val_at(IPluginContext* pContext, const cell_t* params static cell_t json_obj_get_val(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -1803,7 +1825,7 @@ static cell_t json_obj_get_val(IPluginContext* pContext, const cell_t* params) static cell_t json_obj_get_bool(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -1820,7 +1842,7 @@ static cell_t json_obj_get_bool(IPluginContext* pContext, const cell_t* params) static cell_t json_obj_get_float(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -1828,7 +1850,7 @@ static cell_t json_obj_get_float(IPluginContext* pContext, const cell_t* params) pContext->LocalToString(params[2], &key); double value; - if (!g_pJsonManager->ObjectGetFloat(handle, key, &value)) { + if (!g_pJsonManager->ObjectGetDouble(handle, key, &value)) { return pContext->ThrowNativeError("Failed to get float for key '%s'", key); } @@ -1837,7 +1859,7 @@ static cell_t json_obj_get_float(IPluginContext* pContext, const cell_t* params) static cell_t json_obj_get_int(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -1854,7 +1876,7 @@ static cell_t json_obj_get_int(IPluginContext* pContext, const cell_t* params) static cell_t json_obj_get_integer64(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -1867,10 +1889,8 @@ static cell_t json_obj_get_integer64(IPluginContext* pContext, const cell_t* par } char result[JSON_INT64_BUFFER_SIZE]; - if (std::holds_alternative(value)) { - snprintf(result, sizeof(result), "%" PRIu64, std::get(value)); - } else { - snprintf(result, sizeof(result), "%" PRId64, std::get(value)); + if (!Int64VariantToString(value, result, sizeof(result))) { + return pContext->ThrowNativeError("Failed to convert integer64 to string"); } pContext->StringToLocalUTF8(params[3], params[4], result, nullptr); @@ -1879,7 +1899,7 @@ static cell_t json_obj_get_integer64(IPluginContext* pContext, const cell_t* par static cell_t json_obj_get_str(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -1904,7 +1924,7 @@ static cell_t json_obj_get_str(IPluginContext* pContext, const cell_t* params) static cell_t json_obj_clear(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -1917,7 +1937,7 @@ static cell_t json_obj_clear(IPluginContext* pContext, const cell_t* params) static cell_t json_obj_is_null(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -1934,7 +1954,7 @@ static cell_t json_obj_is_null(IPluginContext* pContext, const cell_t* params) static cell_t json_obj_has_key(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -1948,7 +1968,7 @@ static cell_t json_obj_has_key(IPluginContext* pContext, const cell_t* params) static cell_t json_obj_rename_key(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -1973,8 +1993,8 @@ static cell_t json_obj_rename_key(IPluginContext* pContext, const cell_t* params static cell_t json_obj_set_val(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle1 = g_pJsonManager->GetFromHandle(pContext, params[1]); - JsonValue* handle2 = g_pJsonManager->GetFromHandle(pContext, params[3]); + JsonValue* handle1 = g_pJsonManager->GetValueFromHandle(pContext, params[1]); + JsonValue* handle2 = g_pJsonManager->GetValueFromHandle(pContext, params[3]); if (!handle1 || !handle2) return 0; @@ -1990,7 +2010,7 @@ static cell_t json_obj_set_val(IPluginContext* pContext, const cell_t* params) static cell_t json_obj_set_bool(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -2006,7 +2026,7 @@ static cell_t json_obj_set_bool(IPluginContext* pContext, const cell_t* params) static cell_t json_obj_set_float(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -2017,12 +2037,12 @@ static cell_t json_obj_set_float(IPluginContext* pContext, const cell_t* params) char* key; pContext->LocalToString(params[2], &key); - return g_pJsonManager->ObjectSetFloat(handle, key, sp_ctof(params[3])); + return g_pJsonManager->ObjectSetDouble(handle, key, sp_ctof(params[3])); } static cell_t json_obj_set_int(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -2038,7 +2058,7 @@ static cell_t json_obj_set_int(IPluginContext* pContext, const cell_t* params) static cell_t json_obj_set_integer64(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -2062,7 +2082,7 @@ static cell_t json_obj_set_integer64(IPluginContext* pContext, const cell_t* par static cell_t json_obj_set_null(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -2078,7 +2098,7 @@ static cell_t json_obj_set_null(IPluginContext* pContext, const cell_t* params) static cell_t json_obj_set_str(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -2095,7 +2115,7 @@ static cell_t json_obj_set_str(IPluginContext* pContext, const cell_t* params) static cell_t json_obj_remove(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -2111,14 +2131,14 @@ static cell_t json_obj_remove(IPluginContext* pContext, const cell_t* params) static cell_t json_ptr_get_val(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; char* path; pContext->LocalToString(params[2], &path); - char error[JSON_PACK_ERROR_SIZE]; + char error[JSON_ERROR_BUFFER_SIZE]; JsonValue* pJSONValue = g_pJsonManager->PtrGet(handle, path, error, sizeof(error)); if (!pJSONValue) { @@ -2130,7 +2150,7 @@ static cell_t json_ptr_get_val(IPluginContext* pContext, const cell_t* params) static cell_t json_ptr_get_bool(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -2138,7 +2158,7 @@ static cell_t json_ptr_get_bool(IPluginContext* pContext, const cell_t* params) pContext->LocalToString(params[2], &path); bool value; - char error[JSON_PACK_ERROR_SIZE]; + char error[JSON_ERROR_BUFFER_SIZE]; if (!g_pJsonManager->PtrGetBool(handle, path, &value, error, sizeof(error))) { return pContext->ThrowNativeError("%s", error); } @@ -2148,7 +2168,7 @@ static cell_t json_ptr_get_bool(IPluginContext* pContext, const cell_t* params) static cell_t json_ptr_get_float(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -2156,8 +2176,8 @@ static cell_t json_ptr_get_float(IPluginContext* pContext, const cell_t* params) pContext->LocalToString(params[2], &path); double value; - char error[JSON_PACK_ERROR_SIZE]; - if (!g_pJsonManager->PtrGetFloat(handle, path, &value, error, sizeof(error))) { + char error[JSON_ERROR_BUFFER_SIZE]; + if (!g_pJsonManager->PtrGetDouble(handle, path, &value, error, sizeof(error))) { return pContext->ThrowNativeError("%s", error); } @@ -2166,7 +2186,7 @@ static cell_t json_ptr_get_float(IPluginContext* pContext, const cell_t* params) static cell_t json_ptr_get_int(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -2174,7 +2194,7 @@ static cell_t json_ptr_get_int(IPluginContext* pContext, const cell_t* params) pContext->LocalToString(params[2], &path); int value; - char error[JSON_PACK_ERROR_SIZE]; + char error[JSON_ERROR_BUFFER_SIZE]; if (!g_pJsonManager->PtrGetInt(handle, path, &value, error, sizeof(error))) { return pContext->ThrowNativeError("%s", error); } @@ -2184,7 +2204,7 @@ static cell_t json_ptr_get_int(IPluginContext* pContext, const cell_t* params) static cell_t json_ptr_get_integer64(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -2192,16 +2212,14 @@ static cell_t json_ptr_get_integer64(IPluginContext* pContext, const cell_t* par pContext->LocalToString(params[2], &path); std::variant value; - char error[JSON_PACK_ERROR_SIZE]; + char error[JSON_ERROR_BUFFER_SIZE]; if (!g_pJsonManager->PtrGetInt64(handle, path, &value, error, sizeof(error))) { return pContext->ThrowNativeError("%s", error); } char result[JSON_INT64_BUFFER_SIZE]; - if (std::holds_alternative(value)) { - snprintf(result, sizeof(result), "%" PRIu64, std::get(value)); - } else { - snprintf(result, sizeof(result), "%" PRId64, std::get(value)); + if (!Int64VariantToString(value, result, sizeof(result))) { + return pContext->ThrowNativeError("Failed to convert integer64 to string"); } pContext->StringToLocalUTF8(params[3], params[4], result, nullptr); @@ -2210,7 +2228,7 @@ static cell_t json_ptr_get_integer64(IPluginContext* pContext, const cell_t* par static cell_t json_ptr_get_str(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -2219,7 +2237,7 @@ static cell_t json_ptr_get_str(IPluginContext* pContext, const cell_t* params) const char* str; size_t len; - char error[JSON_PACK_ERROR_SIZE]; + char error[JSON_ERROR_BUFFER_SIZE]; if (!g_pJsonManager->PtrGetString(handle, path, &str, &len, error, sizeof(error))) { return pContext->ThrowNativeError("%s", error); } @@ -2236,7 +2254,7 @@ static cell_t json_ptr_get_str(IPluginContext* pContext, const cell_t* params) static cell_t json_ptr_get_is_null(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -2244,7 +2262,7 @@ static cell_t json_ptr_get_is_null(IPluginContext* pContext, const cell_t* param pContext->LocalToString(params[2], &path); bool is_null; - char error[JSON_PACK_ERROR_SIZE]; + char error[JSON_ERROR_BUFFER_SIZE]; if (!g_pJsonManager->PtrGetIsNull(handle, path, &is_null, error, sizeof(error))) { return pContext->ThrowNativeError("%s", error); } @@ -2254,7 +2272,7 @@ static cell_t json_ptr_get_is_null(IPluginContext* pContext, const cell_t* param static cell_t json_ptr_get_length(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -2262,7 +2280,7 @@ static cell_t json_ptr_get_length(IPluginContext* pContext, const cell_t* params pContext->LocalToString(params[2], &path); size_t len; - char error[JSON_PACK_ERROR_SIZE]; + char error[JSON_ERROR_BUFFER_SIZE]; if (!g_pJsonManager->PtrGetLength(handle, path, &len, error, sizeof(error))) { return pContext->ThrowNativeError("%s", error); } @@ -2272,8 +2290,8 @@ static cell_t json_ptr_get_length(IPluginContext* pContext, const cell_t* params static cell_t json_ptr_set_val(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle1 = g_pJsonManager->GetFromHandle(pContext, params[1]); - JsonValue* handle2 = g_pJsonManager->GetFromHandle(pContext, params[3]); + JsonValue* handle1 = g_pJsonManager->GetValueFromHandle(pContext, params[1]); + JsonValue* handle2 = g_pJsonManager->GetValueFromHandle(pContext, params[3]); if (!handle1 || !handle2) return 0; @@ -2284,7 +2302,7 @@ static cell_t json_ptr_set_val(IPluginContext* pContext, const cell_t* params) char* path; pContext->LocalToString(params[2], &path); - char error[JSON_PACK_ERROR_SIZE]; + char error[JSON_ERROR_BUFFER_SIZE]; if (!g_pJsonManager->PtrSet(handle1, path, handle2, error, sizeof(error))) { return pContext->ThrowNativeError("%s", error); } @@ -2294,7 +2312,7 @@ static cell_t json_ptr_set_val(IPluginContext* pContext, const cell_t* params) static cell_t json_ptr_set_bool(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -2305,7 +2323,7 @@ static cell_t json_ptr_set_bool(IPluginContext* pContext, const cell_t* params) char* path; pContext->LocalToString(params[2], &path); - char error[JSON_PACK_ERROR_SIZE]; + char error[JSON_ERROR_BUFFER_SIZE]; if (!g_pJsonManager->PtrSetBool(handle, path, params[3], error, sizeof(error))) { return pContext->ThrowNativeError("%s", error); } @@ -2315,7 +2333,7 @@ static cell_t json_ptr_set_bool(IPluginContext* pContext, const cell_t* params) static cell_t json_ptr_set_float(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -2326,8 +2344,8 @@ static cell_t json_ptr_set_float(IPluginContext* pContext, const cell_t* params) char* path; pContext->LocalToString(params[2], &path); - char error[JSON_PACK_ERROR_SIZE]; - if (!g_pJsonManager->PtrSetFloat(handle, path, sp_ctof(params[3]), error, sizeof(error))) { + char error[JSON_ERROR_BUFFER_SIZE]; + if (!g_pJsonManager->PtrSetDouble(handle, path, sp_ctof(params[3]), error, sizeof(error))) { return pContext->ThrowNativeError("%s", error); } @@ -2336,7 +2354,7 @@ static cell_t json_ptr_set_float(IPluginContext* pContext, const cell_t* params) static cell_t json_ptr_set_int(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -2347,7 +2365,7 @@ static cell_t json_ptr_set_int(IPluginContext* pContext, const cell_t* params) char* path; pContext->LocalToString(params[2], &path); - char error[JSON_PACK_ERROR_SIZE]; + char error[JSON_ERROR_BUFFER_SIZE]; if (!g_pJsonManager->PtrSetInt(handle, path, params[3], error, sizeof(error))) { return pContext->ThrowNativeError("%s", error); } @@ -2357,7 +2375,7 @@ static cell_t json_ptr_set_int(IPluginContext* pContext, const cell_t* params) static cell_t json_ptr_set_integer64(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -2370,7 +2388,7 @@ static cell_t json_ptr_set_integer64(IPluginContext* pContext, const cell_t* par pContext->LocalToString(params[3], &value); std::variant variant_value; - char error[JSON_PACK_ERROR_SIZE]; + char error[JSON_ERROR_BUFFER_SIZE]; if (!g_pJsonManager->ParseInt64Variant(value, &variant_value, error, sizeof(error))) { return pContext->ThrowNativeError("%s", error); @@ -2385,7 +2403,7 @@ static cell_t json_ptr_set_integer64(IPluginContext* pContext, const cell_t* par static cell_t json_ptr_set_str(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -2397,7 +2415,7 @@ static cell_t json_ptr_set_str(IPluginContext* pContext, const cell_t* params) pContext->LocalToString(params[2], &path); pContext->LocalToString(params[3], &str); - char error[JSON_PACK_ERROR_SIZE]; + char error[JSON_ERROR_BUFFER_SIZE]; if (!g_pJsonManager->PtrSetString(handle, path, str, error, sizeof(error))) { return pContext->ThrowNativeError("%s", error); } @@ -2407,7 +2425,7 @@ static cell_t json_ptr_set_str(IPluginContext* pContext, const cell_t* params) static cell_t json_ptr_set_null(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -2418,7 +2436,7 @@ static cell_t json_ptr_set_null(IPluginContext* pContext, const cell_t* params) char* path; pContext->LocalToString(params[2], &path); - char error[JSON_PACK_ERROR_SIZE]; + char error[JSON_ERROR_BUFFER_SIZE]; if (!g_pJsonManager->PtrSetNull(handle, path, error, sizeof(error))) { return pContext->ThrowNativeError("%s", error); } @@ -2428,8 +2446,8 @@ static cell_t json_ptr_set_null(IPluginContext* pContext, const cell_t* params) static cell_t json_ptr_add_val(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle1 = g_pJsonManager->GetFromHandle(pContext, params[1]); - JsonValue* handle2 = g_pJsonManager->GetFromHandle(pContext, params[3]); + JsonValue* handle1 = g_pJsonManager->GetValueFromHandle(pContext, params[1]); + JsonValue* handle2 = g_pJsonManager->GetValueFromHandle(pContext, params[3]); if (!handle1 || !handle2) return 0; @@ -2440,7 +2458,7 @@ static cell_t json_ptr_add_val(IPluginContext* pContext, const cell_t* params) char* path; pContext->LocalToString(params[2], &path); - char error[JSON_PACK_ERROR_SIZE]; + char error[JSON_ERROR_BUFFER_SIZE]; if (!g_pJsonManager->PtrAdd(handle1, path, handle2, error, sizeof(error))) { return pContext->ThrowNativeError("%s", error); } @@ -2450,7 +2468,7 @@ static cell_t json_ptr_add_val(IPluginContext* pContext, const cell_t* params) static cell_t json_ptr_add_bool(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -2461,7 +2479,7 @@ static cell_t json_ptr_add_bool(IPluginContext* pContext, const cell_t* params) char* path; pContext->LocalToString(params[2], &path); - char error[JSON_PACK_ERROR_SIZE]; + char error[JSON_ERROR_BUFFER_SIZE]; if (!g_pJsonManager->PtrAddBool(handle, path, params[3], error, sizeof(error))) { return pContext->ThrowNativeError("%s", error); } @@ -2471,7 +2489,7 @@ static cell_t json_ptr_add_bool(IPluginContext* pContext, const cell_t* params) static cell_t json_ptr_add_float(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -2482,8 +2500,8 @@ static cell_t json_ptr_add_float(IPluginContext* pContext, const cell_t* params) char* path; pContext->LocalToString(params[2], &path); - char error[JSON_PACK_ERROR_SIZE]; - if (!g_pJsonManager->PtrAddFloat(handle, path, sp_ctof(params[3]), error, sizeof(error))) { + char error[JSON_ERROR_BUFFER_SIZE]; + if (!g_pJsonManager->PtrAddDouble(handle, path, sp_ctof(params[3]), error, sizeof(error))) { return pContext->ThrowNativeError("%s", error); } @@ -2492,7 +2510,7 @@ static cell_t json_ptr_add_float(IPluginContext* pContext, const cell_t* params) static cell_t json_ptr_add_int(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -2503,7 +2521,7 @@ static cell_t json_ptr_add_int(IPluginContext* pContext, const cell_t* params) char* path; pContext->LocalToString(params[2], &path); - char error[JSON_PACK_ERROR_SIZE]; + char error[JSON_ERROR_BUFFER_SIZE]; if (!g_pJsonManager->PtrAddInt(handle, path, params[3], error, sizeof(error))) { return pContext->ThrowNativeError("%s", error); } @@ -2513,7 +2531,7 @@ static cell_t json_ptr_add_int(IPluginContext* pContext, const cell_t* params) static cell_t json_ptr_add_integer64(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -2526,7 +2544,7 @@ static cell_t json_ptr_add_integer64(IPluginContext* pContext, const cell_t* par pContext->LocalToString(params[3], &value); std::variant variant_value; - char error[JSON_PACK_ERROR_SIZE]; + char error[JSON_ERROR_BUFFER_SIZE]; if (!g_pJsonManager->ParseInt64Variant(value, &variant_value, error, sizeof(error))) { return pContext->ThrowNativeError("%s", error); @@ -2541,7 +2559,7 @@ static cell_t json_ptr_add_integer64(IPluginContext* pContext, const cell_t* par static cell_t json_ptr_add_str(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -2553,7 +2571,7 @@ static cell_t json_ptr_add_str(IPluginContext* pContext, const cell_t* params) pContext->LocalToString(params[2], &path); pContext->LocalToString(params[3], &str); - char error[JSON_PACK_ERROR_SIZE]; + char error[JSON_ERROR_BUFFER_SIZE]; if (!g_pJsonManager->PtrAddString(handle, path, str, error, sizeof(error))) { return pContext->ThrowNativeError("%s", error); } @@ -2563,7 +2581,7 @@ static cell_t json_ptr_add_str(IPluginContext* pContext, const cell_t* params) static cell_t json_ptr_add_null(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -2574,7 +2592,7 @@ static cell_t json_ptr_add_null(IPluginContext* pContext, const cell_t* params) char* path; pContext->LocalToString(params[2], &path); - char error[JSON_PACK_ERROR_SIZE]; + char error[JSON_ERROR_BUFFER_SIZE]; if (!g_pJsonManager->PtrAddNull(handle, path, error, sizeof(error))) { return pContext->ThrowNativeError("%s", error); } @@ -2584,7 +2602,7 @@ static cell_t json_ptr_add_null(IPluginContext* pContext, const cell_t* params) static cell_t json_ptr_remove_val(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -2595,7 +2613,7 @@ static cell_t json_ptr_remove_val(IPluginContext* pContext, const cell_t* params char* path; pContext->LocalToString(params[2], &path); - char error[JSON_PACK_ERROR_SIZE]; + char error[JSON_ERROR_BUFFER_SIZE]; if (!g_pJsonManager->PtrRemove(handle, path, error, sizeof(error))) { return pContext->ThrowNativeError("%s", error); } @@ -2605,7 +2623,7 @@ static cell_t json_ptr_remove_val(IPluginContext* pContext, const cell_t* params static cell_t json_ptr_try_get_val(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -2623,7 +2641,7 @@ static cell_t json_ptr_try_get_val(IPluginContext* pContext, const cell_t* param static cell_t json_ptr_try_get_bool(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; char* path; @@ -2643,14 +2661,14 @@ static cell_t json_ptr_try_get_bool(IPluginContext* pContext, const cell_t* para static cell_t json_ptr_try_get_float(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; char* path; pContext->LocalToString(params[2], &path); double value; - if (!g_pJsonManager->PtrTryGetFloat(handle, path, &value)) { + if (!g_pJsonManager->PtrTryGetDouble(handle, path, &value)) { return 0; } @@ -2663,7 +2681,7 @@ static cell_t json_ptr_try_get_float(IPluginContext* pContext, const cell_t* par static cell_t json_ptr_try_get_int(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; char* path; @@ -2683,7 +2701,7 @@ static cell_t json_ptr_try_get_int(IPluginContext* pContext, const cell_t* param static cell_t json_ptr_try_get_integer64(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; char* path; @@ -2696,10 +2714,8 @@ static cell_t json_ptr_try_get_integer64(IPluginContext* pContext, const cell_t* size_t maxlen = static_cast(params[4]); char result[JSON_INT64_BUFFER_SIZE]; - if (std::holds_alternative(value)) { - snprintf(result, sizeof(result), "%" PRIu64, std::get(value)); - } else { - snprintf(result, sizeof(result), "%" PRId64, std::get(value)); + if (!Int64VariantToString(value, result, sizeof(result))) { + return 0; } pContext->StringToLocalUTF8(params[3], maxlen, result, nullptr); return 1; @@ -2707,7 +2723,7 @@ static cell_t json_ptr_try_get_integer64(IPluginContext* pContext, const cell_t* static cell_t json_ptr_try_get_str(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; char* path; @@ -2732,7 +2748,7 @@ static cell_t json_ptr_try_get_str(IPluginContext* pContext, const cell_t* param static cell_t json_obj_foreach(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; const char* key; @@ -2749,7 +2765,7 @@ static cell_t json_obj_foreach(IPluginContext* pContext, const cell_t* params) static cell_t json_arr_foreach(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; size_t index; @@ -2768,7 +2784,7 @@ static cell_t json_arr_foreach(IPluginContext* pContext, const cell_t* params) static cell_t json_obj_foreach_key(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; const char* key; @@ -2784,7 +2800,7 @@ static cell_t json_obj_foreach_key(IPluginContext* pContext, const cell_t* param static cell_t json_arr_foreach_index(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; size_t index; @@ -2802,7 +2818,7 @@ static cell_t json_arr_foreach_index(IPluginContext* pContext, const cell_t* par static cell_t json_arr_sort(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -2818,9 +2834,23 @@ static cell_t json_arr_sort(IPluginContext* pContext, const cell_t* params) return g_pJsonManager->ArraySort(handle, sort_mode); } +static cell_t json_arr_rotate(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot rotate an immutable JSON array"); + } + + size_t idx = static_cast(params[2]); + return g_pJsonManager->ArrayRotate(handle, idx); +} + static cell_t json_obj_sort(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -2836,9 +2866,23 @@ static cell_t json_obj_sort(IPluginContext* pContext, const cell_t* params) return g_pJsonManager->ObjectSort(handle, sort_mode); } +static cell_t json_obj_rotate(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot rotate an immutable JSON object"); + } + + size_t idx = static_cast(params[2]); + return g_pJsonManager->ObjectRotate(handle, idx); +} + static cell_t json_doc_to_mutable(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -2857,7 +2901,7 @@ static cell_t json_doc_to_mutable(IPluginContext* pContext, const cell_t* params static cell_t json_doc_to_immutable(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; @@ -2876,8 +2920,8 @@ static cell_t json_doc_to_immutable(IPluginContext* pContext, const cell_t* para static cell_t json_apply_json_patch(IPluginContext* pContext, const cell_t* params) { - JsonValue* target = g_pJsonManager->GetFromHandle(pContext, params[1]); - JsonValue* patch = g_pJsonManager->GetFromHandle(pContext, params[2]); + JsonValue* target = g_pJsonManager->GetValueFromHandle(pContext, params[1]); + JsonValue* patch = g_pJsonManager->GetValueFromHandle(pContext, params[2]); if (!target || !patch) return 0; @@ -2897,8 +2941,8 @@ static cell_t json_apply_json_patch(IPluginContext* pContext, const cell_t* para static cell_t json_json_patch_in_place(IPluginContext* pContext, const cell_t* params) { - JsonValue* target = g_pJsonManager->GetFromHandle(pContext, params[1]); - JsonValue* patch = g_pJsonManager->GetFromHandle(pContext, params[2]); + JsonValue* target = g_pJsonManager->GetValueFromHandle(pContext, params[1]); + JsonValue* patch = g_pJsonManager->GetValueFromHandle(pContext, params[2]); if (!target || !patch) return 0; @@ -2915,8 +2959,8 @@ static cell_t json_json_patch_in_place(IPluginContext* pContext, const cell_t* p static cell_t json_apply_merge_patch(IPluginContext* pContext, const cell_t* params) { - JsonValue* target = g_pJsonManager->GetFromHandle(pContext, params[1]); - JsonValue* patch = g_pJsonManager->GetFromHandle(pContext, params[2]); + JsonValue* target = g_pJsonManager->GetValueFromHandle(pContext, params[1]); + JsonValue* patch = g_pJsonManager->GetValueFromHandle(pContext, params[2]); if (!target || !patch) return 0; @@ -2936,8 +2980,8 @@ static cell_t json_apply_merge_patch(IPluginContext* pContext, const cell_t* par static cell_t json_merge_patch_in_place(IPluginContext* pContext, const cell_t* params) { - JsonValue* target = g_pJsonManager->GetFromHandle(pContext, params[1]); - JsonValue* patch = g_pJsonManager->GetFromHandle(pContext, params[2]); + JsonValue* target = g_pJsonManager->GetValueFromHandle(pContext, params[1]); + JsonValue* patch = g_pJsonManager->GetValueFromHandle(pContext, params[2]); if (!target || !patch) return 0; @@ -2954,7 +2998,7 @@ static cell_t json_merge_patch_in_place(IPluginContext* pContext, const cell_t* static cell_t json_arr_iter_init(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; JsonArrIter* iter = g_pJsonManager->ArrIterWith(handle); @@ -3016,7 +3060,7 @@ static cell_t json_arr_iter_reset(IPluginContext* pContext, const cell_t* params static cell_t json_obj_iter_init(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; JsonObjIter* iter = g_pJsonManager->ObjIterWith(handle); @@ -3031,10 +3075,12 @@ static cell_t json_obj_iter_next(IPluginContext* pContext, const cell_t* params) void* key = g_pJsonManager->ObjIterNext(iter); if (!key) return 0; - const char* key_str; - size_t key_len; - g_pJsonManager->GetString(reinterpret_cast(key), &key_str, &key_len); - pContext->StringToLocalUTF8(params[2], params[3], key_str, nullptr); + const char* key_str = nullptr; + if (!g_pJsonManager->ObjIterGetKeyString(iter, key, &key_str)) { + return 0; + } + + pContext->StringToLocalUTF8(params[2], params[3], key_str ? key_str : "", nullptr); return 1; } @@ -3074,7 +3120,7 @@ static cell_t json_obj_iter_get(IPluginContext* pContext, const cell_t* params) JsonValue* val = g_pJsonManager->ObjIterGet(iter, key); if (!val) { - return 0; + return pContext->ThrowNativeError("Failed to get value from iterator"); } return CreateAndReturnHandle(pContext, val, "object iterator value"); @@ -3141,7 +3187,7 @@ static cell_t json_read_number(IPluginContext* pContext, const cell_t* params) static cell_t json_write_number(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; cell_t buffer_size_param = params[3]; @@ -3177,7 +3223,7 @@ static cell_t json_write_number(IPluginContext* pContext, const cell_t* params) static cell_t json_set_fp_to_float(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; bool flt = params[2] != 0; @@ -3190,7 +3236,7 @@ static cell_t json_set_fp_to_float(IPluginContext* pContext, const cell_t* param static cell_t json_set_fp_to_fixed(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; int prec = params[2]; @@ -3207,7 +3253,7 @@ static cell_t json_set_fp_to_fixed(IPluginContext* pContext, const cell_t* param static cell_t json_set_bool(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; bool value = params[2] != 0; @@ -3220,7 +3266,7 @@ static cell_t json_set_bool(IPluginContext* pContext, const cell_t* params) static cell_t json_set_int(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; int value = params[2]; @@ -3233,7 +3279,7 @@ static cell_t json_set_int(IPluginContext* pContext, const cell_t* params) static cell_t json_set_int64(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; char* str; @@ -3255,11 +3301,10 @@ static cell_t json_set_int64(IPluginContext* pContext, const cell_t* params) static cell_t json_set_float(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; - float value = sp_ctof(params[2]); - if (!g_pJsonManager->SetFloat(handle, value)) { + if (!g_pJsonManager->SetDouble(handle, sp_ctof(params[2]))) { return pContext->ThrowNativeError("Failed to set value to float (value is object or array)"); } @@ -3268,7 +3313,7 @@ static cell_t json_set_float(IPluginContext* pContext, const cell_t* params) static cell_t json_set_string(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; char* value; @@ -3283,7 +3328,7 @@ static cell_t json_set_string(IPluginContext* pContext, const cell_t* params) static cell_t json_set_null(IPluginContext* pContext, const cell_t* params) { - JsonValue* handle = g_pJsonManager->GetFromHandle(pContext, params[1]); + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); if (!handle) return 0; if (!g_pJsonManager->SetNull(handle)) { @@ -3322,6 +3367,7 @@ const sp_nativeinfo_t g_JsonNatives[] = {"JSONObject.FromString", json_obj_parse_str}, {"JSONObject.FromFile", json_obj_parse_file}, {"JSONObject.Sort", json_obj_sort}, + {"JSONObject.Rotate", json_obj_rotate}, // JSONArray {"JSONArray.JSONArray", json_arr_init}, @@ -3381,6 +3427,7 @@ const sp_nativeinfo_t g_JsonNatives[] = {"JSONArray.IndexOfInt64", json_arr_index_of_integer64}, {"JSONArray.IndexOfFloat", json_arr_index_of_float}, {"JSONArray.Sort", json_arr_sort}, + {"JSONArray.Rotate", json_arr_rotate}, // JSON UTILITY {"JSON.ToString", json_doc_write_to_str}, @@ -3393,6 +3440,7 @@ const sp_nativeinfo_t g_JsonNatives[] = {"JSON.GetSerializedSize", json_get_serialized_size}, {"JSON.ReadSize.get", json_get_read_size}, {"JSON.RefCount.get", json_get_ref_count}, + {"JSON.ValCount.get", json_get_val_count}, {"JSON.Type.get", json_get_type}, {"JSON.SubType.get", json_get_subtype}, {"JSON.IsArray.get", json_is_array}, diff --git a/src/extension.cpp b/src/extension.cpp index 2c624ac..4e2b15d 100755 --- a/src/extension.cpp +++ b/src/extension.cpp @@ -17,26 +17,30 @@ bool JsonExtension::SDK_OnLoad(char* error, size_t maxlen, bool late) sharesys->AddNatives(myself, g_JsonNatives); sharesys->RegisterLibrary(myself, "json"); - HandleAccess haJSON; - handlesys->InitAccessDefaults(nullptr, &haJSON); - haJSON.access[HandleAccess_Read] = 0; - haJSON.access[HandleAccess_Delete] = 0; + HandleAccess haDefault; + handlesys->InitAccessDefaults(nullptr, &haDefault); + haDefault.access[HandleAccess_Read] = 0; + haDefault.access[HandleAccess_Delete] = 0; + + TypeAccess taDefault; + handlesys->InitAccessDefaults(&taDefault, nullptr); + taDefault.access[HTypeAccess_Create] = true; HandleError err; - g_JsonType = handlesys->CreateType("JSON", &g_JsonHandler, 0, nullptr, &haJSON, myself->GetIdentity(), &err); + g_JsonType = handlesys->CreateType("JSON", &g_JsonHandler, 0, &taDefault, &haDefault, myself->GetIdentity(), &err); if (!g_JsonType) { snprintf(error, maxlen, "Failed to create JSON handle type (err: %d)", err); return false; } - g_ArrIterType = handlesys->CreateType("JSONArrIter", &g_ArrIterHandler, 0, nullptr, &haJSON, myself->GetIdentity(), &err); + g_ArrIterType = handlesys->CreateType("JSONArrIter", &g_ArrIterHandler, 0, &taDefault, &haDefault, myself->GetIdentity(), &err); if (!g_ArrIterType) { snprintf(error, maxlen, "Failed to create JSONArrIter handle type (err: %d)", err); return false; } - g_ObjIterType = handlesys->CreateType("JSONObjIter", &g_ObjIterHandler, 0, nullptr, &haJSON, myself->GetIdentity(), &err); + g_ObjIterType = handlesys->CreateType("JSONObjIter", &g_ObjIterHandler, 0, &taDefault, &haDefault, myself->GetIdentity(), &err); if (!g_ObjIterType) { snprintf(error, maxlen, "Failed to create JSONObjIter handle type (err: %d)", err); return false; @@ -47,7 +51,7 @@ bool JsonExtension::SDK_OnLoad(char* error, size_t maxlen, bool late) g_pJsonManager = nullptr; } - g_pJsonManager = new JsonManager(); + g_pJsonManager = new(std::nothrow) JsonManager(); if (!g_pJsonManager) { snprintf(error, maxlen, "Failed to create JSON manager instance"); return false;