From 5af62b03b8872984f66fbf15269f04f2cd0799cd Mon Sep 17 00:00:00 2001 From: ProjectSky Date: Sat, 8 Nov 2025 20:16:30 +0800 Subject: [PATCH] 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 - Added some new native functions --- AMBuildScript | 2 +- extensions/{yyjson => json}/AMBuilder | 8 +- .../IYYJSONManager.h => json/IJsonManager.h} | 849 +++-- .../JsonManager.cpp} | 1969 +++++++--- extensions/json/JsonManager.h | 402 ++ extensions/json/JsonNatives.cpp | 3244 +++++++++++++++++ extensions/json/extension.cpp | 85 + extensions/json/extension.h | 42 + extensions/{yyjson => json}/smsdk_config.h | 6 +- extensions/{yyjson => json}/version.rc | 8 +- extensions/{yyjson => json}/yyjson/LICENSE | 0 extensions/{yyjson => json}/yyjson/yyjson.c | 0 extensions/{yyjson => json}/yyjson/yyjson.h | 0 extensions/yyjson/YYJSONManager.h | 264 -- extensions/yyjson/extension.cpp | 57 - extensions/yyjson/extension.h | 28 - extensions/yyjson/json_natives.cpp | 2479 ------------- plugins/include/{yyjson.inc => json.inc} | 1199 ++++-- .../{test_yyjson.sp => test_json.sp} | 1658 +++++++-- 19 files changed, 8081 insertions(+), 4219 deletions(-) rename extensions/{yyjson => json}/AMBuilder (77%) rename extensions/{yyjson/IYYJSONManager.h => json/IJsonManager.h} (51%) rename extensions/{yyjson/YYJSONManager.cpp => json/JsonManager.cpp} (57%) create mode 100755 extensions/json/JsonManager.h create mode 100755 extensions/json/JsonNatives.cpp create mode 100755 extensions/json/extension.cpp create mode 100755 extensions/json/extension.h rename extensions/{yyjson => json}/smsdk_config.h (71%) rename extensions/{yyjson => json}/version.rc (85%) rename extensions/{yyjson => json}/yyjson/LICENSE (100%) rename extensions/{yyjson => json}/yyjson/yyjson.c (100%) rename extensions/{yyjson => json}/yyjson/yyjson.h (100%) delete mode 100755 extensions/yyjson/YYJSONManager.h delete mode 100755 extensions/yyjson/extension.cpp delete mode 100755 extensions/yyjson/extension.h delete mode 100755 extensions/yyjson/json_natives.cpp rename plugins/include/{yyjson.inc => json.inc} (50%) rename plugins/testsuite/{test_yyjson.sp => test_json.sp} (51%) diff --git a/AMBuildScript b/AMBuildScript index 953703918..5771402ab 100644 --- a/AMBuildScript +++ b/AMBuildScript @@ -678,7 +678,7 @@ else: 'extensions/tf2/AMBuilder', 'extensions/topmenus/AMBuilder', 'extensions/updater/AMBuilder', - 'extensions/yyjson/AMBuilder', + 'extensions/json/AMBuilder', ] if builder.backend == 'amb2': diff --git a/extensions/yyjson/AMBuilder b/extensions/json/AMBuilder similarity index 77% rename from extensions/yyjson/AMBuilder rename to extensions/json/AMBuilder index 4eef70229..718ae8d43 100755 --- a/extensions/yyjson/AMBuilder +++ b/extensions/json/AMBuilder @@ -2,7 +2,7 @@ import os for cxx in builder.targets: - binary = SM.ExtLibrary(builder, cxx, 'yyjson.ext') + binary = SM.ExtLibrary(builder, cxx, 'json.ext') if binary.compiler.family == 'gcc' or binary.compiler.family == 'clang': binary.compiler.cxxflags += ['-fno-rtti'] binary.compiler.cflags += ['-fPIC'] @@ -14,13 +14,13 @@ for cxx in builder.targets: ] binary.compiler.cxxincludes += [ - os.path.join(builder.sourcePath, 'extensions', 'yyjson', 'yyjson'), + os.path.join(builder.sourcePath, 'extensions', 'json', 'yyjson'), ] binary.sources += [ 'extension.cpp', - 'YYJSONManager.cpp', - 'json_natives.cpp', + 'JsonManager.cpp', + 'JsonNatives.cpp', 'yyjson/yyjson.c', '../../public/smsdk_ext.cpp', ] diff --git a/extensions/yyjson/IYYJSONManager.h b/extensions/json/IJsonManager.h similarity index 51% rename from extensions/yyjson/IYYJSONManager.h rename to extensions/json/IJsonManager.h index 2f2928cf1..d9c359a6e 100755 --- a/extensions/yyjson/IYYJSONManager.h +++ b/extensions/json/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,26 +10,29 @@ 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 }; /** * @brief Parameter provider interface for Pack operation - * + * * Allows Pack to retrieve parameters in a platform-independent way. */ class IPackParamProvider @@ -43,29 +47,29 @@ public: }; /** - * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 YYJSONValue pointer, or nullptr on error (error will be reported to context) + * @return JsonValue pointer, or nullptr on error (error will be reported to context) */ - virtual YYJSONValue* GetFromHandle(IPluginContext* pContext, Handle_t handle) = 0; + 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 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 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/extensions/yyjson/YYJSONManager.cpp b/extensions/json/JsonManager.cpp similarity index 57% rename from extensions/yyjson/YYJSONManager.cpp rename to extensions/json/JsonManager.cpp index f3b1fc97f..2d03a9efb 100755 --- a/extensions/yyjson/YYJSONManager.cpp +++ b/extensions/json/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); + } } -std::shared_ptr YYJSONManager::WrapDocument(yyjson_mut_doc* doc) { +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::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 (index >= obj_size) { + return nullptr; + } + + auto pJSONValue = CreateWrapper(); if (handle->IsMutable()) { - size_t obj_size = yyjson_mut_obj_size(handle->m_pVal_mut); - if (index >= obj_size) { - return nullptr; - } - 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 = nullptr; + for (size_t i = 0; i <= index; i++) { + key = yyjson_obj_iter_next(&iter); + if (!key) { + return nullptr; + } } - yyjson_val* 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 pYYJSONValue.release(); + return pJSONValue.release(); } -YYJSONValue* YYJSONManager::ArrayParseString(const char* str, yyjson_read_flag read_flg, +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 pJSONValue.release(); +} + +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 (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 == YYJSON_SORT_RANDOM) { + 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(); - if (!pYYJSONValue->m_pVal_mut) { + 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 (!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 result; + } + + 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); + 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; + } + } + + return result; +} + +JsonValue* JsonManager::PtrTryGet(JsonValue* handle, const char* path) { if (!handle || !path) { - return nullptr; - } - - 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); - - if (!val || ptrGetError.code) { 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 pYYJSONValue; + 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; + } + + 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/extensions/json/JsonManager.h b/extensions/json/JsonManager.h new file mode 100755 index 000000000..76c356ede --- /dev/null +++ b/extensions/json/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/extensions/json/JsonNatives.cpp b/extensions/json/JsonNatives.cpp new file mode 100755 index 000000000..e5c86d902 --- /dev/null +++ b/extensions/json/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/extensions/json/extension.cpp b/extensions/json/extension.cpp new file mode 100755 index 000000000..3a3ed0ccb --- /dev/null +++ b/extensions/json/extension.cpp @@ -0,0 +1,85 @@ +#include "extension.h" +#include "JsonManager.h" + +JsonExtension g_JsonExt; +SMEXT_LINK(&g_JsonExt); + +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, g_JsonNatives); + sharesys->RegisterLibrary(myself, "json"); + + HandleAccess haJSON; + handlesys->InitAccessDefaults(nullptr, &haJSON); + haJSON.access[HandleAccess_Read] = 0; + haJSON.access[HandleAccess_Delete] = 0; + + HandleError err; + g_JsonType = handlesys->CreateType("JSON", &g_JsonHandler, 0, nullptr, &haJSON, 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); + 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_pJsonManager) { + delete g_pJsonManager; + g_pJsonManager = nullptr; + } + + g_pJsonManager = new JsonManager(); + if (!g_pJsonManager) { + snprintf(error, maxlen, "Failed to create JSON manager instance"); + return false; + } + + return sharesys->AddInterface(myself, g_pJsonManager); +} + +void JsonExtension::SDK_OnUnload() +{ + handlesys->RemoveType(g_JsonType, myself->GetIdentity()); + handlesys->RemoveType(g_ArrIterType, myself->GetIdentity()); + handlesys->RemoveType(g_ObjIterType, myself->GetIdentity()); + + if (g_pJsonManager) { + delete g_pJsonManager; + g_pJsonManager = nullptr; + } +} + +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 (JsonObjIter*)object; +} \ No newline at end of file diff --git a/extensions/json/extension.h b/extensions/json/extension.h new file mode 100755 index 000000000..311490541 --- /dev/null +++ b/extensions/json/extension.h @@ -0,0 +1,42 @@ +#ifndef _INCLUDE_SOURCEMOD_EXTENSION_PROPER_H_ +#define _INCLUDE_SOURCEMOD_EXTENSION_PROPER_H_ + +#include "smsdk_ext.h" +#include "IJsonManager.h" + +class JsonExtension : public SDKExtension +{ +public: + virtual bool SDK_OnLoad(char *error, size_t maxlength, bool late); + virtual void SDK_OnUnload(); +}; + +class JsonHandler : public IHandleTypeDispatch +{ +public: + void OnHandleDestroy(HandleType_t type, void *object); +}; + +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/extensions/yyjson/smsdk_config.h b/extensions/json/smsdk_config.h similarity index 71% rename from extensions/yyjson/smsdk_config.h rename to extensions/json/smsdk_config.h index acee57d8e..73184d5f0 100755 --- a/extensions/yyjson/smsdk_config.h +++ b/extensions/json/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/extensions/yyjson/version.rc b/extensions/json/version.rc similarity index 85% rename from extensions/yyjson/version.rc rename to extensions/json/version.rc index c27ef431c..dd8d069a3 100755 --- a/extensions/yyjson/version.rc +++ b/extensions/json/version.rc @@ -45,13 +45,13 @@ BEGIN BEGIN BLOCK "000004b0" BEGIN - VALUE "Comments", "YYJSON Extension" - VALUE "FileDescription", "SourceMod YYJSON Extension" + VALUE "Comments", "JSON Extension" + VALUE "FileDescription", "SourceMod JSON Extension" VALUE "FileVersion", SM_VERSION_FILE - VALUE "InternalName", "SourceMod YYJSON Extension" + VALUE "InternalName", "SourceMod JSON Extension" VALUE "LegalCopyright", "Copyright (c) 2004-2009, AlliedModders LLC" VALUE "OriginalFilename", BINARY_NAME - VALUE "ProductName", "SourceMod YYJSON Extension" + VALUE "ProductName", "SourceMod JSON Extension" VALUE "ProductVersion", SM_VERSION_STRING END END diff --git a/extensions/yyjson/yyjson/LICENSE b/extensions/json/yyjson/LICENSE similarity index 100% rename from extensions/yyjson/yyjson/LICENSE rename to extensions/json/yyjson/LICENSE diff --git a/extensions/yyjson/yyjson/yyjson.c b/extensions/json/yyjson/yyjson.c similarity index 100% rename from extensions/yyjson/yyjson/yyjson.c rename to extensions/json/yyjson/yyjson.c diff --git a/extensions/yyjson/yyjson/yyjson.h b/extensions/json/yyjson/yyjson.h similarity index 100% rename from extensions/yyjson/yyjson/yyjson.h rename to extensions/json/yyjson/yyjson.h diff --git a/extensions/yyjson/YYJSONManager.h b/extensions/yyjson/YYJSONManager.h deleted file mode 100755 index 802e5c5eb..000000000 --- a/extensions/yyjson/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/extensions/yyjson/extension.cpp b/extensions/yyjson/extension.cpp deleted file mode 100755 index f927f2316..000000000 --- a/extensions/yyjson/extension.cpp +++ /dev/null @@ -1,57 +0,0 @@ -#include "extension.h" -#include "YYJSONManager.h" - -JsonExtension g_JsonExtension; -SMEXT_LINK(&g_JsonExtension); - -HandleType_t g_htJSON; -JSONHandler g_JSONHandler; -IYYJSONManager* g_pYYJSONManager; - -bool JsonExtension::SDK_OnLoad(char* error, size_t maxlen, bool late) -{ - sharesys->AddNatives(myself, json_natives); - sharesys->RegisterLibrary(myself, "yyjson"); - - HandleAccess haJSON; - handlesys->InitAccessDefaults(nullptr, &haJSON); - haJSON.access[HandleAccess_Read] = 0; - haJSON.access[HandleAccess_Delete] = 0; - - HandleError err; - g_htJSON = handlesys->CreateType("YYJSON", &g_JSONHandler, 0, nullptr, &haJSON, myself->GetIdentity(), &err); - - if (!g_htJSON) { - snprintf(error, maxlen, "Failed to create YYJSON handle type (err: %d)", err); - return false; - } - - // Delete the existing instance if it exists - if (g_pYYJSONManager) { - delete g_pYYJSONManager; - g_pYYJSONManager = nullptr; - } - - g_pYYJSONManager = new YYJSONManager(); - if (!g_pYYJSONManager) { - snprintf(error, maxlen, "Failed to create YYJSONManager instance"); - return false; - } - - return sharesys->AddInterface(myself, g_pYYJSONManager); -} - -void JsonExtension::SDK_OnUnload() -{ - handlesys->RemoveType(g_htJSON, myself->GetIdentity()); - - if (g_pYYJSONManager) { - delete g_pYYJSONManager; - g_pYYJSONManager = nullptr; - } -} - -void JSONHandler::OnHandleDestroy(HandleType_t type, void* object) -{ - delete (YYJSONValue*)object; -} \ No newline at end of file diff --git a/extensions/yyjson/extension.h b/extensions/yyjson/extension.h deleted file mode 100755 index 0d1d503cf..000000000 --- a/extensions/yyjson/extension.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef _INCLUDE_SOURCEMOD_EXTENSION_PROPER_H_ -#define _INCLUDE_SOURCEMOD_EXTENSION_PROPER_H_ - -#include "smsdk_ext.h" -#include -#include -#include "IYYJSONManager.h" - -class JsonExtension : public SDKExtension -{ -public: - virtual bool SDK_OnLoad(char *error, size_t maxlength, bool late); - virtual void SDK_OnUnload(); -}; - -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; - -#endif // _INCLUDE_SOURCEMOD_EXTENSION_PROPER_H_ \ No newline at end of file diff --git a/extensions/yyjson/json_natives.cpp b/extensions/yyjson/json_natives.cpp deleted file mode 100755 index b4774e670..000000000 --- a/extensions/yyjson/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/plugins/include/yyjson.inc b/plugins/include/json.inc similarity index 50% rename from plugins/include/yyjson.inc rename to plugins/include/json.inc index 1813e8297..6d8109618 100755 --- a/plugins/include/yyjson.inc +++ b/plugins/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); /** - * Creates a deep copy of a JSON value and returns it as a new JSON handle. + * 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 * * @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 + * 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 * - * @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 string 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 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. + * 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. + * @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 YYJSONArray FromStrings(const char[][] strings, int size); + public static native JSONArray FromStrings(const char[][] strings, int size); + + /** + * Creates a new JSON array from an array of integer values + * + * @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 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(); } }; -public Extension __ext_yyjson = { - name = "yyjson", - file = "yyjson.ext", +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_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/plugins/testsuite/test_yyjson.sp b/plugins/testsuite/test_json.sp similarity index 51% rename from plugins/testsuite/test_yyjson.sp rename to plugins/testsuite/test_json.sp index 8d3f7fb54..d732b26eb 100755 --- a/plugins/testsuite/test_yyjson.sp +++ b/plugins/testsuite/test_json.sp @@ -1,14 +1,14 @@ #include -#include +#include #pragma newdecls required #pragma semicolon 1 public Plugin myinfo = { - name = "YYJSON Test Suite", + name = "JSON Test Suite", author = "ProjectSky", - description = "test suite for YYJSON functions", + description = "test suite for JSON functions", version = "1.0.0", url = "https://github.com/ProjectSky/sm-ext-yyjson" }; @@ -22,7 +22,7 @@ bool g_bCurrentTestFailed = false; public void OnPluginStart() { - RegServerCmd("test_yyjson", Command_RunTests, "Run YYJSON test suite"); + RegServerCmd("test_json", Command_RunTests, "Run JSON test suite"); } // ============================================================================ @@ -78,11 +78,11 @@ void AssertEq(int a, int b, const char[] message = "") char buffer[256]; if (message[0] != '\0') { - Format(buffer, sizeof(buffer), "%s (expected %d, got %d)", message, b, a); + FormatEx(buffer, sizeof(buffer), "%s (expected %d, got %d)", message, b, a); } else { - Format(buffer, sizeof(buffer), "Expected %d, got %d", b, a); + FormatEx(buffer, sizeof(buffer), "Expected %d, got %d", b, a); } PrintToServer("[FAIL] %s - %s", g_sCurrentTest, buffer); } @@ -99,11 +99,11 @@ stock void AssertNeq(int a, int b, const char[] message = "") char buffer[256]; if (message[0] != '\0') { - Format(buffer, sizeof(buffer), "%s (values should not be equal: %d)", message, a); + FormatEx(buffer, sizeof(buffer), "%s (values should not be equal: %d)", message, a); } else { - Format(buffer, sizeof(buffer), "Values should not be equal: %d", a); + FormatEx(buffer, sizeof(buffer), "Values should not be equal: %d", a); } PrintToServer("[FAIL] %s - %s", g_sCurrentTest, buffer); } @@ -130,17 +130,17 @@ void AssertFalse(bool condition, const char[] message = "") */ void AssertStrEq(const char[] a, const char[] b, const char[] message = "") { - if (!StrEqual(a, b)) + if (strcmp(a, b) != 0) { g_bCurrentTestFailed = true; char buffer[512]; if (message[0] != '\0') { - Format(buffer, sizeof(buffer), "%s (expected \"%s\", got \"%s\")", message, b, a); + FormatEx(buffer, sizeof(buffer), "%s (expected \"%s\", got \"%s\")", message, b, a); } else { - Format(buffer, sizeof(buffer), "Expected \"%s\", got \"%s\"", b, a); + FormatEx(buffer, sizeof(buffer), "Expected \"%s\", got \"%s\"", b, a); } PrintToServer("[FAIL] %s - %s", g_sCurrentTest, buffer); } @@ -157,11 +157,11 @@ void AssertFloatEq(float a, float b, const char[] message = "", float epsilon = char buffer[256]; if (message[0] != '\0') { - Format(buffer, sizeof(buffer), "%s (expected %.4f, got %.4f)", message, b, a); + FormatEx(buffer, sizeof(buffer), "%s (expected %.4f, got %.4f)", message, b, a); } else { - Format(buffer, sizeof(buffer), "Expected %.4f, got %.4f", b, a); + FormatEx(buffer, sizeof(buffer), "Expected %.4f, got %.4f", b, a); } PrintToServer("[FAIL] %s - %s", g_sCurrentTest, buffer); } @@ -190,14 +190,14 @@ void AssertNullHandle(Handle handle, const char[] message = "") public Action Command_RunTests(int args) { PrintToServer("========================================"); - PrintToServer("YYJSON Test Suite"); + PrintToServer("JSON Test Suite"); PrintToServer("========================================"); - + // Reset statistics g_iTotalTests = 0; g_iPassedTests = 0; g_iFailedTests = 0; - + // Run all test categories Test_BasicValues(); Test_ObjectOperations(); @@ -206,24 +206,25 @@ public Action Command_RunTests(int args) Test_Iterators(); Test_JSONPointer(); Test_AdvancedFeatures(); + Test_Int64Operations(); Test_EdgeCases(); - + // Print results PrintToServer("========================================"); - PrintToServer("YYJSON Test Suite Results"); + 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; } @@ -234,189 +235,189 @@ public Action Command_RunTests(int args) void Test_BasicValues() { PrintToServer("\n[Category] Basic Value Tests"); - + // Test CreateBool & GetBool TestStart("BasicValues_CreateBool_True"); { - YYJSON val = YYJSON.CreateBool(true); + JSON val = JSON.CreateBool(true); AssertValidHandle(val); - AssertTrue(YYJSON.GetBool(val)); - AssertEq(val.Type, YYJSON_TYPE_BOOL); + 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"); { - YYJSON val = YYJSON.CreateBool(false); + JSON val = JSON.CreateBool(false); AssertValidHandle(val); - AssertFalse(YYJSON.GetBool(val)); + AssertFalse(val.GetBool()); AssertTrue(val.IsBool); AssertFalse(val.IsTrue); AssertTrue(val.IsFalse); delete val; } TestEnd(); - + // Test CreateInt & GetInt TestStart("BasicValues_CreateInt_Positive"); { - YYJSON val = YYJSON.CreateInt(42); + JSON val = JSON.CreateInt(42); AssertValidHandle(val); - AssertEq(YYJSON.GetInt(val), 42); - AssertEq(val.Type, YYJSON_TYPE_NUM); + AssertEq(val.GetInt(), 42); + AssertEq(val.Type, JSON_TYPE_NUM); AssertTrue(val.IsInt); AssertTrue(val.IsNum); delete val; } TestEnd(); - + TestStart("BasicValues_CreateInt_Negative"); { - YYJSON val = YYJSON.CreateInt(-123); + JSON val = JSON.CreateInt(-123); AssertValidHandle(val); - AssertEq(YYJSON.GetInt(val), -123); + AssertEq(val.GetInt(), -123); AssertTrue(val.IsSint); delete val; } TestEnd(); - + TestStart("BasicValues_CreateInt_Zero"); { - YYJSON val = YYJSON.CreateInt(0); + JSON val = JSON.CreateInt(0); AssertValidHandle(val); - AssertEq(YYJSON.GetInt(val), 0); + AssertEq(val.GetInt(), 0); delete val; } TestEnd(); - + // Test CreateFloat & GetFloat TestStart("BasicValues_CreateFloat_Positive"); { - YYJSON val = YYJSON.CreateFloat(3.14159); + JSON val = JSON.CreateFloat(3.14159); AssertValidHandle(val); - AssertFloatEq(YYJSON.GetFloat(val), 3.14159); + AssertFloatEq(val.GetFloat(), 3.14159); AssertTrue(val.IsFloat); AssertTrue(val.IsNum); delete val; } TestEnd(); - + TestStart("BasicValues_CreateFloat_Negative"); { - YYJSON val = YYJSON.CreateFloat(-2.71828); + JSON val = JSON.CreateFloat(-2.71828); AssertValidHandle(val); - AssertFloatEq(YYJSON.GetFloat(val), -2.71828); + AssertFloatEq(val.GetFloat(), -2.71828); delete val; } TestEnd(); - + TestStart("BasicValues_CreateFloat_Zero"); { - YYJSON val = YYJSON.CreateFloat(0.0); + JSON val = JSON.CreateFloat(0.0); AssertValidHandle(val); - AssertFloatEq(YYJSON.GetFloat(val), 0.0); + AssertFloatEq(val.GetFloat(), 0.0); delete val; } TestEnd(); - + // Test CreateString & GetString TestStart("BasicValues_CreateString_Regular"); { - YYJSON val = YYJSON.CreateString("Hello, World!"); + JSON val = JSON.CreateString("Hello, World!"); AssertValidHandle(val); char buffer[64]; - AssertTrue(YYJSON.GetString(val, buffer, sizeof(buffer))); + AssertTrue(val.GetString(buffer, sizeof(buffer))); AssertStrEq(buffer, "Hello, World!"); - AssertEq(val.Type, YYJSON_TYPE_STR); + AssertEq(val.Type, JSON_TYPE_STR); AssertTrue(val.IsStr); delete val; } TestEnd(); - + TestStart("BasicValues_CreateString_Empty"); { - YYJSON val = YYJSON.CreateString(""); + JSON val = JSON.CreateString(""); AssertValidHandle(val); char buffer[64]; - AssertTrue(YYJSON.GetString(val, buffer, sizeof(buffer))); + AssertTrue(val.GetString(buffer, sizeof(buffer))); AssertStrEq(buffer, ""); delete val; } TestEnd(); - + TestStart("BasicValues_CreateString_Unicode"); { - YYJSON val = YYJSON.CreateString("测试字符串"); + JSON val = JSON.CreateString("测试字符串"); AssertValidHandle(val); char buffer[64]; - AssertTrue(YYJSON.GetString(val, buffer, sizeof(buffer))); + AssertTrue(val.GetString(buffer, sizeof(buffer))); AssertStrEq(buffer, "测试字符串"); delete val; } TestEnd(); - + // Test CreateInt64 & GetInt64 TestStart("BasicValues_CreateInt64_Large"); { - YYJSON val = YYJSON.CreateInt64("9223372036854775807"); + JSON val = JSON.CreateInt64("9223372036854775807"); AssertValidHandle(val); char buffer[32]; - AssertTrue(YYJSON.GetInt64(val, buffer, sizeof(buffer))); + AssertTrue(val.GetInt64(buffer, sizeof(buffer))); AssertStrEq(buffer, "9223372036854775807"); delete val; } TestEnd(); - + TestStart("BasicValues_CreateInt64_Negative"); { - YYJSON val = YYJSON.CreateInt64("-9223372036854775808"); + JSON val = JSON.CreateInt64("-9223372036854775808"); AssertValidHandle(val); char buffer[32]; - AssertTrue(YYJSON.GetInt64(val, buffer, sizeof(buffer))); + AssertTrue(val.GetInt64(buffer, sizeof(buffer))); AssertStrEq(buffer, "-9223372036854775808"); delete val; } TestEnd(); - + // Test CreateNull TestStart("BasicValues_CreateNull"); { - YYJSON val = YYJSON.CreateNull(); + JSON val = JSON.CreateNull(); AssertValidHandle(val); - AssertEq(val.Type, YYJSON_TYPE_NULL); + AssertEq(val.Type, JSON_TYPE_NULL); AssertTrue(val.IsNull); delete val; } TestEnd(); - + // Test GetTypeDesc TestStart("BasicValues_GetTypeDesc"); { - 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(); + 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]; - YYJSON.GetTypeDesc(boolVal, buffer, sizeof(buffer)); + JSON.GetTypeDesc(boolVal, buffer, sizeof(buffer)); AssertStrEq(buffer, "true"); - YYJSON.GetTypeDesc(intVal, buffer, sizeof(buffer)); - AssertTrue(StrEqual(buffer, "uint") || StrEqual(buffer, "sint")); + JSON.GetTypeDesc(intVal, buffer, sizeof(buffer)); + AssertTrue(strcmp(buffer, "uint") == 0 || strcmp(buffer, "sint") == 0); - YYJSON.GetTypeDesc(floatVal, buffer, sizeof(buffer)); + JSON.GetTypeDesc(floatVal, buffer, sizeof(buffer)); AssertStrEq(buffer, "real"); - YYJSON.GetTypeDesc(strVal, buffer, sizeof(buffer)); + JSON.GetTypeDesc(strVal, buffer, sizeof(buffer)); AssertStrEq(buffer, "string"); - YYJSON.GetTypeDesc(nullVal, buffer, sizeof(buffer)); + JSON.GetTypeDesc(nullVal, buffer, sizeof(buffer)); AssertStrEq(buffer, "null"); delete boolVal; @@ -435,50 +436,50 @@ void Test_BasicValues() void Test_ObjectOperations() { PrintToServer("\n[Category] Object Operations Tests"); - + // Test object creation TestStart("Object_Constructor"); { - YYJSONObject obj = new YYJSONObject(); + JSONObject obj = new JSONObject(); AssertValidHandle(obj); AssertTrue(obj.IsObject); AssertEq(obj.Size, 0); delete obj; } TestEnd(); - + // Test Set/Get methods TestStart("Object_SetGetInt"); { - YYJSONObject obj = new YYJSONObject(); + JSONObject obj = new JSONObject(); AssertTrue(obj.SetInt("number", 42)); AssertEq(obj.GetInt("number"), 42); AssertEq(obj.Size, 1); delete obj; } TestEnd(); - + TestStart("Object_SetGetFloat"); { - YYJSONObject obj = new YYJSONObject(); + JSONObject obj = new JSONObject(); AssertTrue(obj.SetFloat("pi", 3.14159)); AssertFloatEq(obj.GetFloat("pi"), 3.14159); delete obj; } TestEnd(); - + TestStart("Object_SetGetBool"); { - YYJSONObject obj = new YYJSONObject(); + JSONObject obj = new JSONObject(); AssertTrue(obj.SetBool("flag", true)); AssertTrue(obj.GetBool("flag")); delete obj; } TestEnd(); - + TestStart("Object_SetGetString"); { - YYJSONObject obj = new YYJSONObject(); + JSONObject obj = new JSONObject(); AssertTrue(obj.SetString("name", "test")); char buffer[64]; AssertTrue(obj.GetString("name", buffer, sizeof(buffer))); @@ -486,10 +487,10 @@ void Test_ObjectOperations() delete obj; } TestEnd(); - + TestStart("Object_SetGetInt64"); { - YYJSONObject obj = new YYJSONObject(); + JSONObject obj = new JSONObject(); AssertTrue(obj.SetInt64("bignum", "9223372036854775807")); char buffer[32]; AssertTrue(obj.GetInt64("bignum", buffer, sizeof(buffer))); @@ -497,31 +498,31 @@ void Test_ObjectOperations() delete obj; } TestEnd(); - + TestStart("Object_SetGetNull"); { - YYJSONObject obj = new YYJSONObject(); + JSONObject obj = new JSONObject(); AssertTrue(obj.SetNull("nullable")); AssertTrue(obj.IsNull("nullable")); delete obj; } TestEnd(); - + // Test HasKey TestStart("Object_HasKey"); { - YYJSONObject obj = new YYJSONObject(); + 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"); { - YYJSONObject obj = new YYJSONObject(); + JSONObject obj = new JSONObject(); obj.SetInt("first", 1); obj.SetInt("second", 2); @@ -529,19 +530,19 @@ void Test_ObjectOperations() AssertTrue(obj.GetKey(0, key, sizeof(key))); AssertStrEq(key, "first"); - YYJSON val = obj.GetValueAt(0); + JSON val = obj.GetValueAt(0); AssertValidHandle(val); - AssertEq(YYJSON.GetInt(val), 1); + AssertEq(val.GetInt(), 1); delete val; delete obj; } TestEnd(); - + // Test Remove TestStart("Object_Remove"); { - YYJSONObject obj = new YYJSONObject(); + JSONObject obj = new JSONObject(); obj.SetInt("remove_me", 1); obj.SetInt("keep_me", 2); AssertEq(obj.Size, 2); @@ -554,11 +555,11 @@ void Test_ObjectOperations() delete obj; } TestEnd(); - + // Test Clear TestStart("Object_Clear"); { - YYJSONObject obj = new YYJSONObject(); + JSONObject obj = new JSONObject(); obj.SetInt("a", 1); obj.SetInt("b", 2); obj.SetInt("c", 3); @@ -570,11 +571,11 @@ void Test_ObjectOperations() delete obj; } TestEnd(); - + // Test RenameKey TestStart("Object_RenameKey"); { - YYJSONObject obj = new YYJSONObject(); + JSONObject obj = new JSONObject(); obj.SetInt("oldname", 42); AssertTrue(obj.RenameKey("oldname", "newname")); @@ -585,11 +586,11 @@ void Test_ObjectOperations() delete obj; } TestEnd(); - + // Test FromString TestStart("Object_FromString"); { - YYJSONObject obj = YYJSONObject.FromString("{\"key\":\"value\",\"num\":123}"); + JSONObject obj = JSONObject.FromString("{\"key\":\"value\",\"num\":123}"); AssertValidHandle(obj); char buffer[64]; @@ -600,12 +601,12 @@ void Test_ObjectOperations() delete obj; } TestEnd(); - + // Test FromStrings TestStart("Object_FromStrings"); { char pairs[][] = {"name", "test", "type", "demo", "version", "1.0"}; - YYJSONObject obj = YYJSONObject.FromStrings(pairs, sizeof(pairs)); + JSONObject obj = JSONObject.FromStrings(pairs, sizeof(pairs)); AssertValidHandle(obj); AssertEq(obj.Size, 3); @@ -616,16 +617,16 @@ void Test_ObjectOperations() delete obj; } TestEnd(); - + // Test Sort TestStart("Object_Sort_Ascending"); { - YYJSONObject obj = new YYJSONObject(); + JSONObject obj = new JSONObject(); obj.SetInt("zebra", 1); obj.SetInt("alpha", 2); obj.SetInt("beta", 3); - AssertTrue(obj.Sort(YYJSON_SORT_ASC)); + AssertTrue(obj.Sort(JSON_SORT_ASC)); char key[32]; obj.GetKey(0, key, sizeof(key)); @@ -634,15 +635,15 @@ void Test_ObjectOperations() delete obj; } TestEnd(); - + TestStart("Object_Sort_Descending"); { - YYJSONObject obj = new YYJSONObject(); + JSONObject obj = new JSONObject(); obj.SetInt("alpha", 1); obj.SetInt("beta", 2); obj.SetInt("gamma", 3); - AssertTrue(obj.Sort(YYJSON_SORT_DESC)); + AssertTrue(obj.Sort(JSON_SORT_DESC)); char key[32]; obj.GetKey(0, key, sizeof(key)); @@ -651,12 +652,12 @@ void Test_ObjectOperations() delete obj; } TestEnd(); - + // Test Set with handle TestStart("Object_SetWithHandle"); { - YYJSONObject obj = new YYJSONObject(); - YYJSON val = YYJSON.CreateInt(999); + JSONObject obj = new JSONObject(); + JSON val = JSON.CreateInt(999); AssertTrue(obj.Set("nested", val)); AssertEq(obj.GetInt("nested"), 999); @@ -665,16 +666,16 @@ void Test_ObjectOperations() delete obj; } TestEnd(); - + // Test Get with handle TestStart("Object_GetHandle"); { - YYJSONObject obj = new YYJSONObject(); + JSONObject obj = new JSONObject(); obj.SetInt("value", 42); - YYJSON val = obj.Get("value"); + JSON val = obj.Get("value"); AssertValidHandle(val); - AssertEq(YYJSON.GetInt(val), 42); + AssertEq(val.GetInt(), 42); delete val; delete obj; @@ -689,50 +690,50 @@ void Test_ObjectOperations() void Test_ArrayOperations() { PrintToServer("\n[Category] Array Operations Tests"); - + // Test array creation TestStart("Array_Constructor"); { - YYJSONArray arr = new YYJSONArray(); + JSONArray arr = new JSONArray(); AssertValidHandle(arr); AssertTrue(arr.IsArray); AssertEq(arr.Length, 0); delete arr; } TestEnd(); - + // Test Push methods TestStart("Array_PushInt"); { - YYJSONArray arr = new YYJSONArray(); + JSONArray arr = new JSONArray(); AssertTrue(arr.PushInt(42)); AssertEq(arr.Length, 1); AssertEq(arr.GetInt(0), 42); delete arr; } TestEnd(); - + TestStart("Array_PushFloat"); { - YYJSONArray arr = new YYJSONArray(); + JSONArray arr = new JSONArray(); AssertTrue(arr.PushFloat(3.14)); AssertFloatEq(arr.GetFloat(0), 3.14); delete arr; } TestEnd(); - + TestStart("Array_PushBool"); { - YYJSONArray arr = new YYJSONArray(); + JSONArray arr = new JSONArray(); AssertTrue(arr.PushBool(true)); AssertTrue(arr.GetBool(0)); delete arr; } TestEnd(); - + TestStart("Array_PushString"); { - YYJSONArray arr = new YYJSONArray(); + JSONArray arr = new JSONArray(); AssertTrue(arr.PushString("test")); char buffer[64]; AssertTrue(arr.GetString(0, buffer, sizeof(buffer))); @@ -740,10 +741,10 @@ void Test_ArrayOperations() delete arr; } TestEnd(); - + TestStart("Array_PushInt64"); { - YYJSONArray arr = new YYJSONArray(); + JSONArray arr = new JSONArray(); AssertTrue(arr.PushInt64("9223372036854775807")); char buffer[32]; arr.GetInt64(0, buffer, sizeof(buffer)); @@ -751,41 +752,41 @@ void Test_ArrayOperations() delete arr; } TestEnd(); - + TestStart("Array_PushNull"); { - YYJSONArray arr = new YYJSONArray(); + JSONArray arr = new JSONArray(); AssertTrue(arr.PushNull()); AssertTrue(arr.IsNull(0)); delete arr; } TestEnd(); - + // Test Set/Get methods TestStart("Array_SetGetInt"); { - YYJSONArray arr = new YYJSONArray(); + JSONArray arr = new JSONArray(); arr.PushInt(0); AssertTrue(arr.SetInt(0, 100)); AssertEq(arr.GetInt(0), 100); delete arr; } TestEnd(); - + TestStart("Array_SetGetFloat"); { - YYJSONArray arr = new YYJSONArray(); + 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"); { - YYJSONArray arr = new YYJSONArray(); + JSONArray arr = new JSONArray(); arr.PushInt(1); arr.PushInt(2); arr.PushInt(3); @@ -799,10 +800,10 @@ void Test_ArrayOperations() delete arr; } TestEnd(); - + TestStart("Array_RemoveFirst"); { - YYJSONArray arr = new YYJSONArray(); + JSONArray arr = new JSONArray(); arr.PushInt(1); arr.PushInt(2); arr.PushInt(3); @@ -814,10 +815,10 @@ void Test_ArrayOperations() delete arr; } TestEnd(); - + TestStart("Array_RemoveLast"); { - YYJSONArray arr = new YYJSONArray(); + JSONArray arr = new JSONArray(); arr.PushInt(1); arr.PushInt(2); arr.PushInt(3); @@ -829,10 +830,10 @@ void Test_ArrayOperations() delete arr; } TestEnd(); - + TestStart("Array_RemoveRange"); { - YYJSONArray arr = new YYJSONArray(); + JSONArray arr = new JSONArray(); for (int i = 0; i < 10; i++) { arr.PushInt(i); @@ -844,10 +845,10 @@ void Test_ArrayOperations() delete arr; } TestEnd(); - + TestStart("Array_Clear"); { - YYJSONArray arr = new YYJSONArray(); + JSONArray arr = new JSONArray(); arr.PushInt(1); arr.PushInt(2); arr.PushInt(3); @@ -858,33 +859,33 @@ void Test_ArrayOperations() delete arr; } TestEnd(); - + // Test First/Last properties TestStart("Array_FirstLast"); { - YYJSONArray arr = new YYJSONArray(); + JSONArray arr = new JSONArray(); arr.PushInt(10); arr.PushInt(20); arr.PushInt(30); - YYJSON first = arr.First; - YYJSON last = arr.Last; + JSON first = arr.First; + JSON last = arr.Last; AssertValidHandle(first); AssertValidHandle(last); - AssertEq(YYJSON.GetInt(first), 10); - AssertEq(YYJSON.GetInt(last), 30); + AssertEq(first.GetInt(), 10); + AssertEq(last.GetInt(), 30); delete first; delete last; delete arr; } TestEnd(); - + // Test IndexOf methods TestStart("Array_IndexOfInt"); { - YYJSONArray arr = new YYJSONArray(); + JSONArray arr = new JSONArray(); arr.PushInt(10); arr.PushInt(20); arr.PushInt(30); @@ -895,10 +896,10 @@ void Test_ArrayOperations() delete arr; } TestEnd(); - + TestStart("Array_IndexOfBool"); { - YYJSONArray arr = new YYJSONArray(); + JSONArray arr = new JSONArray(); arr.PushBool(false); arr.PushBool(true); arr.PushBool(false); @@ -908,10 +909,10 @@ void Test_ArrayOperations() delete arr; } TestEnd(); - + TestStart("Array_IndexOfString"); { - YYJSONArray arr = new YYJSONArray(); + JSONArray arr = new JSONArray(); arr.PushString("apple"); arr.PushString("banana"); arr.PushString("cherry"); @@ -922,10 +923,10 @@ void Test_ArrayOperations() delete arr; } TestEnd(); - + TestStart("Array_IndexOfFloat"); { - YYJSONArray arr = new YYJSONArray(); + JSONArray arr = new JSONArray(); arr.PushFloat(1.1); arr.PushFloat(2.2); arr.PushFloat(3.3); @@ -935,10 +936,10 @@ void Test_ArrayOperations() delete arr; } TestEnd(); - + TestStart("Array_IndexOfInt64"); { - YYJSONArray arr = new YYJSONArray(); + JSONArray arr = new JSONArray(); arr.PushInt64("1000000000000"); arr.PushInt64("2000000000000"); @@ -948,11 +949,11 @@ void Test_ArrayOperations() delete arr; } TestEnd(); - + // Test FromString TestStart("Array_FromString"); { - YYJSONArray arr = YYJSONArray.FromString("[1,2,3,4,5]"); + JSONArray arr = JSONArray.FromString("[1,2,3,4,5]"); AssertValidHandle(arr); AssertEq(arr.Length, 5); AssertEq(arr.GetInt(0), 1); @@ -960,12 +961,12 @@ void Test_ArrayOperations() delete arr; } TestEnd(); - + // Test FromStrings TestStart("Array_FromStrings"); { char strings[][] = {"apple", "banana", "cherry"}; - YYJSONArray arr = YYJSONArray.FromStrings(strings, sizeof(strings)); + JSONArray arr = JSONArray.FromStrings(strings, sizeof(strings)); AssertValidHandle(arr); AssertEq(arr.Length, 3); @@ -976,47 +977,47 @@ void Test_ArrayOperations() delete arr; } TestEnd(); - + // Test Sort TestStart("Array_Sort_Ascending"); { - YYJSONArray arr = new YYJSONArray(); + JSONArray arr = new JSONArray(); arr.PushInt(3); arr.PushInt(1); arr.PushInt(4); arr.PushInt(1); arr.PushInt(5); - AssertTrue(arr.Sort(YYJSON_SORT_ASC)); + AssertTrue(arr.Sort(JSON_SORT_ASC)); AssertEq(arr.GetInt(0), 1); AssertEq(arr.GetInt(4), 5); delete arr; } TestEnd(); - + TestStart("Array_Sort_Descending"); { - YYJSONArray arr = new YYJSONArray(); + JSONArray arr = new JSONArray(); arr.PushInt(1); arr.PushInt(2); arr.PushInt(3); arr.PushInt(4); arr.PushInt(5); - AssertTrue(arr.Sort(YYJSON_SORT_DESC)); + 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"); { - YYJSONArray arr = new YYJSONArray(); - YYJSON val = YYJSON.CreateInt(999); + JSONArray arr = new JSONArray(); + JSON val = JSON.CreateInt(999); AssertTrue(arr.Push(val)); AssertEq(arr.GetInt(0), 999); @@ -1025,21 +1026,477 @@ void Test_ArrayOperations() delete arr; } TestEnd(); - + // Test Get with handle TestStart("Array_GetHandle"); { - YYJSONArray arr = new YYJSONArray(); + JSONArray arr = new JSONArray(); arr.PushInt(42); - YYJSON val = arr.Get(0); + JSON val = arr.Get(0); AssertValidHandle(val); - AssertEq(YYJSON.GetInt(val), 42); + 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(); } // ============================================================================ @@ -1049,59 +1506,59 @@ void Test_ArrayOperations() void Test_ParseAndSerialize() { PrintToServer("\n[Category] Parse & Serialize Tests"); - + // Test Parse string TestStart("Parse_SimpleObject"); { - YYJSON json = YYJSON.Parse("{\"key\":\"value\",\"num\":42}"); + JSON json = JSON.Parse("{\"key\":\"value\",\"num\":42}"); AssertValidHandle(json); AssertTrue(json.IsObject); delete json; } TestEnd(); - + TestStart("Parse_SimpleArray"); { - YYJSON json = YYJSON.Parse("[1,2,3,4,5]"); + JSON json = JSON.Parse("[1,2,3,4,5]"); AssertValidHandle(json); AssertTrue(json.IsArray); delete json; } TestEnd(); - + TestStart("Parse_NestedStructure"); { - YYJSON json = YYJSON.Parse("{\"user\":{\"name\":\"test\",\"age\":25},\"items\":[1,2,3]}"); + JSON json = JSON.Parse("{\"user\":{\"name\":\"test\",\"age\":25},\"items\":[1,2,3]}"); AssertValidHandle(json); delete json; } TestEnd(); - + // Test mutable/immutable TestStart("Parse_ImmutableDocument"); { - YYJSON json = YYJSON.Parse("{\"key\":\"value\"}"); + JSON json = JSON.Parse("{\"key\":\"value\"}"); AssertValidHandle(json); AssertTrue(json.IsImmutable); AssertFalse(json.IsMutable); delete json; } TestEnd(); - + TestStart("Parse_MutableDocument"); { - YYJSON json = YYJSON.Parse("{\"key\":\"value\"}", false, true); + JSON json = JSON.Parse("{\"key\":\"value\"}", false, true); AssertValidHandle(json); AssertTrue(json.IsMutable); AssertFalse(json.IsImmutable); delete json; } TestEnd(); - + // Test ToString TestStart("Serialize_ToString"); { - YYJSONObject obj = new YYJSONObject(); + JSONObject obj = new JSONObject(); obj.SetInt("num", 42); obj.SetString("str", "test"); @@ -1114,26 +1571,26 @@ void Test_ParseAndSerialize() delete obj; } TestEnd(); - + TestStart("Serialize_ToString_Pretty"); { - YYJSONObject obj = new YYJSONObject(); + JSONObject obj = new JSONObject(); obj.SetInt("a", 1); obj.SetInt("b", 2); char buffer[256]; - int len = obj.ToString(buffer, sizeof(buffer), YYJSON_WRITE_PRETTY); + 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"); { - YYJSONObject obj = new YYJSONObject(); + JSONObject obj = new JSONObject(); obj.SetInt("test", 123); int size = obj.GetSerializedSize(); @@ -1146,40 +1603,40 @@ void Test_ParseAndSerialize() delete obj; } TestEnd(); - + // Test read flags TestStart("Parse_WithTrailingCommas"); { - YYJSON json = YYJSON.Parse("[1,2,3,]", false, false, YYJSON_READ_ALLOW_TRAILING_COMMAS); + JSON json = JSON.Parse("[1,2,3,]", false, false, JSON_READ_ALLOW_TRAILING_COMMAS); AssertValidHandle(json); delete json; } TestEnd(); - + TestStart("Parse_WithComments"); { - YYJSON json = YYJSON.Parse("/* comment */ {\"key\":\"value\"}", false, false, YYJSON_READ_ALLOW_COMMENTS); + 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"); { - YYJSONObject obj = new YYJSONObject(); + JSONObject obj = new JSONObject(); obj.SetInt("filetest", 999); obj.SetString("name", "testfile"); // Write to file - AssertTrue(obj.ToFile("yyjson_test_temp.json")); + AssertTrue(obj.ToFile("json_test_temp.json")); // Read from file - YYJSONObject loaded = YYJSON.Parse("yyjson_test_temp.json", true); + JSONObject loaded = JSON.Parse("json_test_temp.json", true); AssertValidHandle(loaded); // Verify content - YYJSONObject loadedObj = loaded; + JSONObject loadedObj = loaded; AssertEq(loadedObj.GetInt("filetest"), 999); char buffer[64]; @@ -1190,21 +1647,21 @@ void Test_ParseAndSerialize() delete loaded; // Cleanup - DeleteFile("yyjson_test_temp.json"); + 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}"; - YYJSON json1 = YYJSON.Parse(original); + JSON json1 = JSON.Parse(original); char buffer[256]; json1.ToString(buffer, sizeof(buffer)); - YYJSON json2 = YYJSON.Parse(buffer); - AssertTrue(YYJSON.Equals(json1, json2)); + JSON json2 = JSON.Parse(buffer); + AssertTrue(JSON.Equals(json1, json2)); delete json1; delete json2; @@ -1219,18 +1676,18 @@ void Test_ParseAndSerialize() void Test_Iterators() { PrintToServer("\n[Category] Iterator Tests"); - + // Test ForeachObject TestStart("Iterator_ForeachObject"); { - YYJSONObject obj = new YYJSONObject(); + JSONObject obj = new JSONObject(); obj.SetInt("a", 1); obj.SetInt("b", 2); obj.SetInt("c", 3); int count = 0; char key[32]; - YYJSON value; + JSON value; while (obj.ForeachObject(key, sizeof(key), value)) { @@ -1243,18 +1700,18 @@ void Test_Iterators() delete obj; } TestEnd(); - + // Test ForeachArray TestStart("Iterator_ForeachArray"); { - YYJSONArray arr = new YYJSONArray(); + JSONArray arr = new JSONArray(); arr.PushInt(10); arr.PushInt(20); arr.PushInt(30); int count = 0; int index; - YYJSON value; + JSON value; while (arr.ForeachArray(index, value)) { @@ -1268,11 +1725,11 @@ void Test_Iterators() delete arr; } TestEnd(); - + // Test ForeachKey TestStart("Iterator_ForeachKey"); { - YYJSONObject obj = new YYJSONObject(); + JSONObject obj = new JSONObject(); obj.SetInt("key1", 1); obj.SetInt("key2", 2); obj.SetInt("key3", 3); @@ -1290,11 +1747,11 @@ void Test_Iterators() delete obj; } TestEnd(); - + // Test ForeachIndex TestStart("Iterator_ForeachIndex"); { - YYJSONArray arr = new YYJSONArray(); + JSONArray arr = new JSONArray(); arr.PushInt(1); arr.PushInt(2); arr.PushInt(3); @@ -1312,26 +1769,26 @@ void Test_Iterators() delete arr; } TestEnd(); - + // Test empty iterations TestStart("Iterator_EmptyObject"); { - YYJSONObject obj = new YYJSONObject(); + JSONObject obj = new JSONObject(); char key[32]; - YYJSON value; + JSON value; AssertFalse(obj.ForeachObject(key, sizeof(key), value)); delete obj; } TestEnd(); - + TestStart("Iterator_EmptyArray"); { - YYJSONArray arr = new YYJSONArray(); + JSONArray arr = new JSONArray(); int index; - YYJSON value; + JSON value; AssertFalse(arr.ForeachArray(index, value)); delete arr; @@ -1346,38 +1803,38 @@ void Test_Iterators() void Test_JSONPointer() { PrintToServer("\n[Category] JSON Pointer Tests"); - + // Test PtrSet methods TestStart("Pointer_PtrSetInt"); { - YYJSONObject obj = new YYJSONObject(); + JSONObject obj = new JSONObject(); AssertTrue(obj.PtrSetInt("/number", 42)); AssertEq(obj.PtrGetInt("/number"), 42); delete obj; } TestEnd(); - + TestStart("Pointer_PtrSetFloat"); { - YYJSONObject obj = new YYJSONObject(); + JSONObject obj = new JSONObject(); AssertTrue(obj.PtrSetFloat("/pi", 3.14159)); AssertFloatEq(obj.PtrGetFloat("/pi"), 3.14159); delete obj; } TestEnd(); - + TestStart("Pointer_PtrSetBool"); { - YYJSONObject obj = new YYJSONObject(); + JSONObject obj = new JSONObject(); AssertTrue(obj.PtrSetBool("/flag", true)); AssertTrue(obj.PtrGetBool("/flag")); delete obj; } TestEnd(); - + TestStart("Pointer_PtrSetString"); { - YYJSONObject obj = new YYJSONObject(); + JSONObject obj = new JSONObject(); AssertTrue(obj.PtrSetString("/name", "test")); char buffer[64]; @@ -1387,10 +1844,10 @@ void Test_JSONPointer() delete obj; } TestEnd(); - + TestStart("Pointer_PtrSetInt64"); { - YYJSONObject obj = new YYJSONObject(); + JSONObject obj = new JSONObject(); AssertTrue(obj.PtrSetInt64("/bignum", "9223372036854775807")); char buffer[32]; @@ -1400,55 +1857,55 @@ void Test_JSONPointer() delete obj; } TestEnd(); - + TestStart("Pointer_PtrSetNull"); { - YYJSONObject obj = new YYJSONObject(); + JSONObject obj = new JSONObject(); AssertTrue(obj.PtrSetNull("/nullable")); AssertTrue(obj.PtrGetIsNull("/nullable")); delete obj; } TestEnd(); - + // Test nested path creation TestStart("Pointer_NestedPathCreation"); { - YYJSONObject obj = new YYJSONObject(); + 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"); { - YYJSONObject obj = new YYJSONObject(); + JSONObject obj = new JSONObject(); obj.PtrSetInt("/test", 999); - YYJSON val = obj.PtrGet("/test"); + JSON val = obj.PtrGet("/test"); AssertValidHandle(val); - AssertEq(YYJSON.GetInt(val), 999); + AssertEq(val.GetInt(), 999); delete val; delete obj; } TestEnd(); - + // Test PtrAdd methods TestStart("Pointer_PtrAddInt"); { - YYJSONObject obj = new YYJSONObject(); + 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"); { - YYJSONObject obj = new YYJSONObject(); + JSONObject obj = new JSONObject(); obj.PtrSetString("/items/0", "first"); AssertTrue(obj.PtrAddString("/items/1", "second")); @@ -1459,28 +1916,28 @@ void Test_JSONPointer() delete obj; } TestEnd(); - + // Test PtrRemove TestStart("Pointer_PtrRemove"); { - YYJSONObject obj = new YYJSONObject(); + JSONObject obj = new JSONObject(); obj.PtrSetInt("/remove_me", 1); obj.PtrSetInt("/keep_me", 2); AssertTrue(obj.PtrRemove("/remove_me")); - YYJSON val; + JSON val; obj.PtrTryGetVal("/remove_me", val); AssertNullHandle(val); delete obj; } TestEnd(); - + // Test PtrGetLength TestStart("Pointer_PtrGetLength"); { - YYJSONObject obj = new YYJSONObject(); + JSONObject obj = new JSONObject(); obj.PtrSetString("/text", "hello"); int len = obj.PtrGetLength("/text"); @@ -1489,11 +1946,11 @@ void Test_JSONPointer() delete obj; } TestEnd(); - + // Test PtrTryGet methods TestStart("Pointer_PtrTryGetInt"); { - YYJSONObject obj = new YYJSONObject(); + JSONObject obj = new JSONObject(); obj.PtrSetInt("/value", 42); int value; @@ -1505,10 +1962,10 @@ void Test_JSONPointer() delete obj; } TestEnd(); - + TestStart("Pointer_PtrTryGetBool"); { - YYJSONObject obj = new YYJSONObject(); + JSONObject obj = new JSONObject(); obj.PtrSetBool("/flag", true); bool value; @@ -1518,10 +1975,10 @@ void Test_JSONPointer() delete obj; } TestEnd(); - + TestStart("Pointer_PtrTryGetFloat"); { - YYJSONObject obj = new YYJSONObject(); + JSONObject obj = new JSONObject(); obj.PtrSetFloat("/pi", 3.14); float value; @@ -1531,10 +1988,10 @@ void Test_JSONPointer() delete obj; } TestEnd(); - + TestStart("Pointer_PtrTryGetString"); { - YYJSONObject obj = new YYJSONObject(); + JSONObject obj = new JSONObject(); obj.PtrSetString("/name", "test"); char buffer[64]; @@ -1544,10 +2001,10 @@ void Test_JSONPointer() delete obj; } TestEnd(); - + TestStart("Pointer_PtrTryGetInt64"); { - YYJSONObject obj = new YYJSONObject(); + JSONObject obj = new JSONObject(); obj.PtrSetInt64("/bignum", "123456789012345"); char buffer[32]; @@ -1557,16 +2014,16 @@ void Test_JSONPointer() delete obj; } TestEnd(); - + TestStart("Pointer_PtrTryGetVal"); { - YYJSONObject obj = new YYJSONObject(); + JSONObject obj = new JSONObject(); obj.PtrSetInt("/test", 42); - YYJSON value; + JSON value; AssertTrue(obj.PtrTryGetVal("/test", value)); AssertValidHandle(value); - AssertEq(YYJSON.GetInt(value), 42); + AssertEq(value.GetInt(), 42); delete value; delete obj; @@ -1581,16 +2038,16 @@ void Test_JSONPointer() void Test_AdvancedFeatures() { PrintToServer("\n[Category] Advanced Features Tests"); - + // Test DeepCopy TestStart("Advanced_DeepCopy_Object"); { - YYJSONObject original = new YYJSONObject(); + JSONObject original = new JSONObject(); original.SetInt("num", 42); original.SetString("str", "test"); - YYJSONObject target = new YYJSONObject(); - YYJSONObject copy = YYJSON.DeepCopy(target, original); + JSONObject target = new JSONObject(); + JSONObject copy = JSON.DeepCopy(target, original); AssertValidHandle(copy); AssertEq(copy.GetInt("num"), 42); @@ -1604,16 +2061,16 @@ void Test_AdvancedFeatures() delete copy; } TestEnd(); - + TestStart("Advanced_DeepCopy_Array"); { - YYJSONArray original = new YYJSONArray(); + JSONArray original = new JSONArray(); original.PushInt(1); original.PushInt(2); original.PushInt(3); - YYJSONArray target = new YYJSONArray(); - YYJSONArray copy = YYJSON.DeepCopy(target, original); + JSONArray target = new JSONArray(); + JSONArray copy = JSON.DeepCopy(target, original); AssertValidHandle(copy); AssertEq(copy.Length, 3); @@ -1625,47 +2082,47 @@ void Test_AdvancedFeatures() delete copy; } TestEnd(); - + // Test Equals TestStart("Advanced_Equals_True"); { - YYJSONObject obj1 = new YYJSONObject(); + JSONObject obj1 = new JSONObject(); obj1.SetInt("a", 1); obj1.SetString("b", "test"); - YYJSONObject obj2 = new YYJSONObject(); + JSONObject obj2 = new JSONObject(); obj2.SetInt("a", 1); obj2.SetString("b", "test"); - AssertTrue(YYJSON.Equals(obj1, obj2)); + AssertTrue(JSON.Equals(obj1, obj2)); delete obj1; delete obj2; } TestEnd(); - + TestStart("Advanced_Equals_False"); { - YYJSONObject obj1 = new YYJSONObject(); + JSONObject obj1 = new JSONObject(); obj1.SetInt("a", 1); - YYJSONObject obj2 = new YYJSONObject(); + JSONObject obj2 = new JSONObject(); obj2.SetInt("a", 2); - AssertFalse(YYJSON.Equals(obj1, obj2)); + AssertFalse(JSON.Equals(obj1, obj2)); delete obj1; delete obj2; } TestEnd(); - + // Test ToMutable/ToImmutable TestStart("Advanced_ToMutable"); { - YYJSON immutable = YYJSON.Parse("{\"key\":\"value\"}"); + JSON immutable = JSON.Parse("{\"key\":\"value\"}"); AssertTrue(immutable.IsImmutable); - YYJSON mutable = immutable.ToMutable(); + JSON mutable = immutable.ToMutable(); AssertValidHandle(mutable); AssertTrue(mutable.IsMutable); @@ -1673,14 +2130,14 @@ void Test_AdvancedFeatures() delete mutable; } TestEnd(); - + TestStart("Advanced_ToImmutable"); { - YYJSONObject mutable = new YYJSONObject(); + JSONObject mutable = new JSONObject(); mutable.SetInt("key", 42); AssertTrue(mutable.IsMutable); - YYJSON immutable = mutable.ToImmutable(); + JSON immutable = mutable.ToImmutable(); AssertValidHandle(immutable); AssertTrue(immutable.IsImmutable); @@ -1688,11 +2145,11 @@ void Test_AdvancedFeatures() delete immutable; } TestEnd(); - + // Test Pack TestStart("Advanced_Pack_SimpleObject"); { - YYJSONObject json = YYJSON.Pack("{s:i,s:s,s:b}", + JSONObject json = JSON.Pack("{s:i,s:s,s:b}", "num", 42, "str", "test", "bool", true @@ -1701,7 +2158,7 @@ void Test_AdvancedFeatures() AssertValidHandle(json); AssertTrue(json.IsObject); - YYJSONObject obj = json; + JSONObject obj = json; AssertEq(obj.GetInt("num"), 42); char buffer[64]; @@ -1713,15 +2170,15 @@ void Test_AdvancedFeatures() delete json; } TestEnd(); - + TestStart("Advanced_Pack_Array"); { - YYJSONArray json = YYJSON.Pack("[i,i,i]", 1, 2, 3); + JSONArray json = JSON.Pack("[i,i,i]", 1, 2, 3); AssertValidHandle(json); AssertTrue(json.IsArray); - YYJSONArray arr = json; + JSONArray arr = json; AssertEq(arr.Length, 3); AssertEq(arr.GetInt(0), 1); AssertEq(arr.GetInt(2), 3); @@ -1729,10 +2186,10 @@ void Test_AdvancedFeatures() delete json; } TestEnd(); - + TestStart("Advanced_Pack_Nested"); { - YYJSONObject json = YYJSON.Pack("{s:{s:s,s:i}}", + JSONObject json = JSON.Pack("{s:{s:s,s:i}}", "user", "name", "test", "age", 25 @@ -1741,7 +2198,7 @@ void Test_AdvancedFeatures() AssertValidHandle(json); char buffer[64]; - YYJSONObject obj = json; + JSONObject obj = json; obj.PtrGetString("/user/name", buffer, sizeof(buffer)); AssertStrEq(buffer, "test"); AssertEq(obj.PtrGetInt("/user/age"), 25); @@ -1749,14 +2206,14 @@ void Test_AdvancedFeatures() delete json; } TestEnd(); - + // Test mixed type array sorting TestStart("Advanced_MixedTypeSort"); { - YYJSONArray json = YYJSON.Parse("[true, 42, \"hello\", 1.5, false]", false, true); - YYJSONArray arr = json; + JSONArray json = JSON.Parse("[true, 42, \"hello\", 1.5, false]", false, true); + JSONArray arr = json; - AssertTrue(arr.Sort(YYJSON_SORT_ASC)); + AssertTrue(arr.Sort(JSON_SORT_ASC)); AssertEq(arr.Length, 5); delete json; @@ -1765,17 +2222,506 @@ void Test_AdvancedFeatures() } // ============================================================================ -// 2.8 Edge Cases & Error Handling Tests +// 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"); { - YYJSONObject obj = new YYJSONObject(); + JSONObject obj = new JSONObject(); AssertEq(obj.Size, 0); AssertFalse(obj.HasKey("anything")); @@ -1786,19 +2732,19 @@ void Test_EdgeCases() delete obj; } TestEnd(); - + TestStart("EdgeCase_EmptyArray"); { - YYJSONArray arr = new YYJSONArray(); + JSONArray arr = new JSONArray(); AssertEq(arr.Length, 0); delete arr; } TestEnd(); - + // Test nonexistent keys/indices TestStart("EdgeCase_NonexistentKey"); { - YYJSONObject obj = new YYJSONObject(); + JSONObject obj = new JSONObject(); obj.SetInt("exists", 1); AssertTrue(obj.HasKey("exists")); @@ -1807,10 +2753,10 @@ void Test_EdgeCases() delete obj; } TestEnd(); - + TestStart("EdgeCase_ArrayOutOfBounds"); { - YYJSONArray arr = new YYJSONArray(); + JSONArray arr = new JSONArray(); arr.PushInt(1); // Verify array bounds @@ -1820,7 +2766,7 @@ void Test_EdgeCases() delete arr; } TestEnd(); - + // Test very long strings TestStart("EdgeCase_LongString"); { @@ -1831,21 +2777,21 @@ void Test_EdgeCases() } longStr[sizeof(longStr) - 1] = '\0'; - YYJSON val = YYJSON.CreateString(longStr); + JSON val = JSON.CreateString(longStr); AssertValidHandle(val); char retrieved[1024]; - AssertTrue(YYJSON.GetString(val, retrieved, sizeof(retrieved))); + AssertTrue(val.GetString(retrieved, sizeof(retrieved))); AssertStrEq(retrieved, longStr); delete val; } TestEnd(); - + // Test deep nesting TestStart("EdgeCase_DeepNesting"); { - YYJSONObject obj = new YYJSONObject(); + JSONObject obj = new JSONObject(); // Create deeply nested structure obj.PtrSetInt("/a/b/c/d/e/f/g/h/i/j", 42); @@ -1854,11 +2800,11 @@ void Test_EdgeCases() delete obj; } TestEnd(); - + // Test large arrays TestStart("EdgeCase_LargeArray"); { - YYJSONArray arr = new YYJSONArray(); + JSONArray arr = new JSONArray(); for (int i = 0; i < 1000; i++) { @@ -1872,16 +2818,16 @@ void Test_EdgeCases() delete arr; } TestEnd(); - + // Test large objects TestStart("EdgeCase_LargeObject"); { - YYJSONObject obj = new YYJSONObject(); + JSONObject obj = new JSONObject(); char key[32]; for (int i = 0; i < 100; i++) { - Format(key, sizeof(key), "key_%d", i); + FormatEx(key, sizeof(key), "key_%d", i); obj.SetInt(key, i); } @@ -1892,85 +2838,85 @@ void Test_EdgeCases() delete obj; } TestEnd(); - + // Test int64 boundaries TestStart("EdgeCase_Int64_MaxValue"); { - YYJSON val = YYJSON.CreateInt64("9223372036854775807"); + JSON val = JSON.CreateInt64("9223372036854775807"); AssertValidHandle(val); char buffer[32]; - YYJSON.GetInt64(val, buffer, sizeof(buffer)); + val.GetInt64(buffer, sizeof(buffer)); AssertStrEq(buffer, "9223372036854775807"); delete val; } TestEnd(); - + TestStart("EdgeCase_Int64_MinValue"); { - YYJSON val = YYJSON.CreateInt64("-9223372036854775808"); + JSON val = JSON.CreateInt64("-9223372036854775808"); AssertValidHandle(val); char buffer[32]; - YYJSON.GetInt64(val, buffer, sizeof(buffer)); + val.GetInt64(buffer, sizeof(buffer)); AssertStrEq(buffer, "-9223372036854775808"); delete val; } TestEnd(); - + // Test special float values TestStart("EdgeCase_Float_VerySmall"); { - YYJSON val = YYJSON.CreateFloat(0.000001); + JSON val = JSON.CreateFloat(0.000001); AssertValidHandle(val); - AssertFloatEq(YYJSON.GetFloat(val), 0.000001); + AssertFloatEq(val.GetFloat(), 0.000001); delete val; } TestEnd(); - + TestStart("EdgeCase_Float_VeryLarge"); { - YYJSON val = YYJSON.CreateFloat(999999.999999); + JSON val = JSON.CreateFloat(999999.999999); AssertValidHandle(val); - AssertFloatEq(YYJSON.GetFloat(val), 999999.999999, "", 0.001); + AssertFloatEq(val.GetFloat(), 999999.999999, "", 0.001); delete val; } TestEnd(); - + // Test special characters in strings TestStart("EdgeCase_SpecialCharacters"); { - YYJSON val = YYJSON.CreateString("Line1\nLine2\tTabbed\"Quoted\""); + JSON val = JSON.CreateString("Line1\nLine2\tTabbed\"Quoted\""); AssertValidHandle(val); char buffer[128]; - YYJSON.GetString(val, buffer, sizeof(buffer)); + val.GetString(buffer, sizeof(buffer)); Assert(StrContains(buffer, "Line1") != -1); delete val; } TestEnd(); - + // Test removing from empty array TestStart("EdgeCase_RemoveFromEmptyArray"); { - YYJSONArray arr = new YYJSONArray(); + JSONArray arr = new JSONArray(); AssertFalse(arr.RemoveFirst()); AssertFalse(arr.RemoveLast()); delete arr; } TestEnd(); - + // Test clearing already empty containers TestStart("EdgeCase_ClearEmpty"); { - YYJSONObject obj = new YYJSONObject(); + JSONObject obj = new JSONObject(); AssertTrue(obj.Clear()); AssertEq(obj.Size, 0); - YYJSONArray arr = new YYJSONArray(); + JSONArray arr = new JSONArray(); AssertTrue(arr.Clear()); AssertEq(arr.Length, 0); @@ -1978,22 +2924,22 @@ void Test_EdgeCases() delete arr; } TestEnd(); - + // Test IndexOf on empty array TestStart("EdgeCase_IndexOfEmpty"); { - YYJSONArray arr = new YYJSONArray(); + 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"); { - YYJSONObject obj = new YYJSONObject(); + JSONObject obj = new JSONObject(); // Note: PtrGet throws exception for nonexistent paths (expected behavior) // Use PtrTryGet methods for safe access @@ -2003,25 +2949,25 @@ void Test_EdgeCases() delete obj; } TestEnd(); - + // Test sorting empty containers TestStart("EdgeCase_SortEmpty"); { - YYJSONObject obj = new YYJSONObject(); + JSONObject obj = new JSONObject(); AssertTrue(obj.Sort()); - YYJSONArray arr = new YYJSONArray(); + JSONArray arr = new JSONArray(); AssertTrue(arr.Sort()); delete obj; delete arr; } TestEnd(); - + // Test single element operations TestStart("EdgeCase_SingleElement"); { - YYJSONArray arr = new YYJSONArray(); + JSONArray arr = new JSONArray(); arr.PushInt(42); AssertTrue(arr.Sort()); @@ -2029,9 +2975,9 @@ void Test_EdgeCases() AssertEq(arr.IndexOfInt(42), 0); - YYJSON first = arr.First; - YYJSON last = arr.Last; - AssertEq(YYJSON.GetInt(first), YYJSON.GetInt(last)); + JSON first = arr.First; + JSON last = arr.Last; + AssertEq(first.GetInt(), last.GetInt()); delete first; delete last;