diff --git a/AMBuildScript b/AMBuildScript index 9e63058..ad0cead 100755 --- a/AMBuildScript +++ b/AMBuildScript @@ -1,295 +1,212 @@ # vim: set sts=2 ts=8 sw=2 tw=99 et ft=python: import os -def ResolveEnvPath(env, folder): - if env in os.environ: - path = os.environ[env] - if os.path.isdir(path): - return path - return None - - head = os.getcwd() - oldhead = None - while head != None and head != oldhead: - path = os.path.join(head, folder) - if os.path.isdir(path): - return path - oldhead = head - head, tail = os.path.split(head) - - return None - -def Normalize(path): - return os.path.abspath(os.path.normpath(path)) - def SetArchFlags(compiler): - if compiler.behavior == 'gcc': - if compiler.target.arch == 'x86_64': - compiler.cflags += ['-fPIC'] - else: - compiler.cflags += ['-fPIC'] - elif compiler.like('msvc'): + if compiler.like('msvc'): if compiler.target.arch == 'x86_64': compiler.defines += ['WIN64'] else: compiler.defines += ['WIN32'] class ExtensionConfig(object): - def __init__(self): - self.extensions = [] - self.sm_root = None - self.all_targets = [] - self.target_archs = set() - - if builder.options.targets: - target_archs = builder.options.targets.split(',') - else: - target_archs = ['x86'] - - for arch in target_archs: - try: - cxx = builder.DetectCxx(target_arch = arch) - self.target_archs.add(cxx.target.arch) - except Exception as e: - # Error if archs were manually overridden. - if builder.options.targets: - raise - print('Skipping target {}: {}'.format(arch, e)) - continue - self.all_targets.append(cxx) - - if not self.all_targets: - raise Exception('No suitable C/C++ compiler was found.') - - @property - def tag(self): - if builder.options.debug == '1': - return 'Debug' - return 'Release' - - def detectSourceMod(self): - if builder.options.sm_path: - self.sm_root = builder.options.sm_path - else: - self.sm_root = ResolveEnvPath('SOURCEMOD18', 'sourcemod-1.8') - if not self.sm_root: - self.sm_root = ResolveEnvPath('SOURCEMOD110', 'sourcemod-1.10') - if not self.sm_root: - self.sm_root = ResolveEnvPath('SOURCEMOD', 'sourcemod') - if not self.sm_root: - self.sm_root = ResolveEnvPath('SOURCEMOD_DEV', 'sourcemod-central') - - if not self.sm_root or not os.path.isdir(self.sm_root): - raise Exception('Could not find a source copy of SourceMod') - - self.sm_root = Normalize(self.sm_root) - - def configure(self): - for cxx in self.all_targets: - self.configure_cxx(cxx) - - def configure_cxx(self, cxx): - if cxx.family == 'msvc': - if cxx.version < 1900: - raise Exception('MSVC 2015 or later is required.') - elif cxx.family == 'gcc': - if cxx.version < 'gcc-4.9': - raise Exception('GCC 4.9 or later is required.') - elif cxx.family == 'clang': - if cxx.version < 'clang-3.4': - raise Exception('Clang 3.4 or later is required.') - - if cxx.like('gcc'): - self.configure_gcc(cxx) - elif cxx.family == 'msvc': - self.configure_msvc(cxx) - - # Optimization - if builder.options.opt == '1': - cxx.defines += ['NDEBUG'] - - # Debugging - if builder.options.debug == '1': - cxx.defines += ['DEBUG', '_DEBUG'] - - # Platform-specifics - if cxx.target.platform == 'linux': - self.configure_linux(cxx) - elif cxx.target.platform == 'mac': - self.configure_mac(cxx) - elif cxx.target.platform == 'windows': - self.configure_windows(cxx) - - # Finish up. - cxx.includes += [ - os.path.join(self.sm_root, 'public'), - ] - - def configure_gcc(self, cxx): - cxx.defines += [ - 'stricmp=strcasecmp', - '_stricmp=strcasecmp', - '_snprintf=snprintf', - '_vsnprintf=vsnprintf', - 'HAVE_STDINT_H', - 'GNUC', - ] - cxx.cflags += [ - '-pipe', - '-fno-strict-aliasing', - '-Wall', - '-Werror', - '-Wno-unused', - '-Wno-switch', - '-Wno-array-bounds', - '-msse', - '-fvisibility=hidden', - '-fexceptions', - ] - - if cxx.version == 'apple-clang-6.0' or cxx.version == 'clang-3.4': - cxx.cxxflags += ['-std=c++1y'] - else: - cxx.cxxflags += ['-std=c++17'] - - cxx.cxxflags += [ - '-fno-threadsafe-statics', - '-Wno-non-virtual-dtor', - '-Wno-overloaded-virtual', - '-fvisibility-inlines-hidden', - '-fexceptions', - ] - - have_gcc = cxx.family == 'gcc' - have_clang = cxx.family == 'clang' - if cxx.version >= 'clang-3.9' or cxx.version == 'clang-3.4' or cxx.version > 'apple-clang-6.0': - cxx.cxxflags += ['-Wno-expansion-to-defined'] - if cxx.version == 'clang-3.9' or cxx.version == 'apple-clang-8.0': - cxx.cflags += ['-Wno-varargs'] - if cxx.version >= 'clang-3.4' or cxx.version >= 'apple-clang-7.0': - cxx.cxxflags += ['-Wno-inconsistent-missing-override'] - if cxx.version >= 'clang-2.9' or cxx.version >= 'apple-clang-3.0': - cxx.cxxflags += ['-Wno-null-dereference'] - if have_clang or (cxx.version >= 'gcc-4.6'): - cxx.cflags += ['-Wno-narrowing'] - if have_clang or (cxx.version >= 'gcc-4.7'): - cxx.cxxflags += ['-Wno-delete-non-virtual-dtor'] - if cxx.version >= 'gcc-4.8': - cxx.cflags += ['-Wno-unused-result'] - if cxx.version >= 'gcc-9.0': - cxx.cxxflags += ['-Wno-class-memaccess', '-Wno-packed-not-aligned'] - - if have_clang: - cxx.cxxflags += ['-Wno-implicit-exception-spec-mismatch'] - if cxx.version >= 'apple-clang-5.1' or cxx.version >= 'clang-3.4': - cxx.cxxflags += ['-Wno-deprecated-register'] - else: - cxx.cxxflags += ['-Wno-deprecated'] - cxx.cflags += ['-Wno-sometimes-uninitialized'] - - if have_gcc: - cxx.cflags += ['-mfpmath=sse'] - cxx.cflags += ['-Wno-maybe-uninitialized'] - - if builder.options.opt == '1': - cxx.cflags += ['-O3'] - - # This needs to be after our optimization flags which could otherwise disable it. - # Don't omit the frame pointer. - cxx.cflags += ['-fno-omit-frame-pointer'] - - def configure_msvc(self, cxx): - if builder.options.debug == '1': - cxx.cflags += ['/MTd'] - cxx.linkflags += ['/NODEFAULTLIB:libcmt'] - else: - cxx.cflags += ['/MT'] - cxx.defines += [ - '_CRT_SECURE_NO_DEPRECATE', - '_CRT_SECURE_NO_WARNINGS', - '_CRT_NONSTDC_NO_DEPRECATE', - '_ITERATOR_DEBUG_LEVEL=0', - ] - cxx.cflags += [ - '/W3', - ] - cxx.cxxflags += [ - '/EHsc', - '/GR-', - '/TP', - ] - cxx.linkflags += [ - 'kernel32.lib', - 'user32.lib', - 'gdi32.lib', - 'winspool.lib', - 'comdlg32.lib', - 'advapi32.lib', - 'shell32.lib', - 'ole32.lib', - 'oleaut32.lib', - 'uuid.lib', - 'odbc32.lib', - 'odbccp32.lib', - ] - - if builder.options.opt == '1': - cxx.cflags += ['/Ox', '/Zo'] - cxx.linkflags += ['/OPT:ICF', '/OPT:REF'] - - if builder.options.debug == '1': - cxx.cflags += ['/Od', '/RTC1'] - - # This needs to be after our optimization flags which could otherwise disable it. - # Don't omit the frame pointer. - cxx.cflags += ['/Oy-'] - - def configure_linux(self, cxx): - cxx.defines += ['_LINUX', 'POSIX'] - cxx.linkflags += ['-Wl,--exclude-libs,ALL', '-lm', '-s'] - if cxx.family == 'gcc': - cxx.linkflags += ['-static-libgcc'] - elif cxx.family == 'clang': - cxx.linkflags += ['-lgcc_eh'] - cxx.linkflags += ['-static-libstdc++'] - - def configure_mac(self, cxx): - cxx.defines += ['OSX', '_OSX', 'POSIX'] - cxx.cflags += ['-mmacosx-version-min=10.7'] - cxx.linkflags += [ - '-mmacosx-version-min=10.7', - '-lc++', - '-stdlib=libc++', - ] - cxx.cxxflags += ['-stdlib=libc++'] - - def configure_windows(self, cxx): - cxx.defines += [ - '_WINDOWS', - '_WIN32_WINNT=0x0601', - ] - - def ConfigureForExtension(self, context, compiler): - compiler.cxxincludes += [ - os.path.join(context.currentSourcePath), - os.path.join(self.sm_root, 'public'), - os.path.join(self.sm_root, 'public', 'extensions'), - os.path.join(self.sm_root, 'sourcepawn', 'include'), - os.path.join(self.sm_root, 'public', 'amtl', 'amtl'), - os.path.join(self.sm_root, 'public', 'amtl'), - ] - return compiler - - def Library(self, context, compiler, name): - compiler = compiler.clone() - SetArchFlags(compiler) - self.ConfigureForExtension(context, compiler) - return compiler.Library(name) - - def StaticLibrary(self, context, compiler, name): - compiler = compiler.clone() - SetArchFlags(compiler) - return compiler.StaticLibrary(name) + def __init__(self): + self.extensions = [] + self.sm_root = None + self.all_targets = [] + self.target_archs = set() + + if builder.options.targets: + target_archs = builder.options.targets.split(',') + else: + target_archs = ['x86'] + + for arch in target_archs: + try: + cxx = builder.DetectCxx(target_arch=arch) + self.target_archs.add(cxx.target.arch) + except Exception as e: + if builder.options.targets: + raise + print('Skipping target {}: {}'.format(arch, e)) + continue + self.all_targets.append(cxx) + + if not self.all_targets: + raise Exception('No suitable C/C++ compiler was found.') + + def detectSourceMod(self): + if builder.options.sm_path: + self.sm_root = builder.options.sm_path + else: + raise Exception('SourceMod path not specified') + + if not self.sm_root or not os.path.isdir(self.sm_root): + raise Exception('Could not find a source copy of SourceMod') + + self.sm_root = os.path.abspath(os.path.normpath(self.sm_root)) + + def configure(self): + for cxx in self.all_targets: + self.configure_cxx(cxx) + + def configure_cxx(self, cxx): + if cxx.family == 'msvc': + if cxx.version < 1914 and builder.options.generator != 'vs': + raise Exception('Only MSVC 2017 15.7 and later are supported, full C++17 support is required.') + elif cxx.family == 'gcc': + if cxx.version < 'gcc-9': + raise Exception('Only GCC versions 9 or later are supported, full C++17 support is required.') + elif cxx.family == 'clang': + if cxx.version < 'clang-5': + raise Exception('Only clang versions 5 or later are supported, full C++17 support is required.') + + if cxx.like('gcc'): + self.configure_gcc(cxx) + elif cxx.family == 'msvc': + self.configure_msvc(cxx) + + # Optimization + if builder.options.opt == '1': + cxx.defines += ['NDEBUG'] + + # Debugging + if builder.options.debug == '1': + cxx.defines += ['DEBUG', '_DEBUG'] + + # Platform-specifics + if cxx.target.platform == 'linux': + self.configure_linux(cxx) + elif cxx.target.platform == 'windows': + self.configure_windows(cxx) + + # Finish up. + cxx.includes += [ + os.path.join(self.sm_root, 'public'), + ] + + def configure_gcc(self, cxx): + cxx.defines += [ + 'stricmp=strcasecmp', + '_stricmp=strcasecmp', + '_snprintf=snprintf', + '_vsnprintf=vsnprintf', + 'HAVE_STDINT_H', + 'GNUC', + ] + + cxx.cflags += [ + '-pipe', + '-fno-strict-aliasing', + '-Wall', + '-fvisibility=hidden', + '-fPIC', + ] + + # Clang-specific warnings + if cxx.family == 'clang': + cxx.cflags += ['-Wno-sometimes-uninitialized'] + cxx.cxxflags += ['-Wno-implicit-exception-spec-mismatch'] + + cxx.cxxflags += ['-std=c++17'] + + cxx.cxxflags += [ + '-fno-threadsafe-statics', + '-fvisibility-inlines-hidden', + ] + + # Clang version-specific warnings + if cxx.family == 'clang': + if cxx.version >= 'clang-3.4' or cxx.version >= 'apple-clang-7.0': + cxx.cxxflags += ['-Wno-inconsistent-missing-override'] + if cxx.version >= 'apple-clang-5.1' or cxx.version >= 'clang-3.4': + cxx.cxxflags += ['-Wno-deprecated-register'] + + # GCC version-specific warnings + if cxx.family == 'gcc' and cxx.version >= 'gcc-9.0': + cxx.cxxflags += ['-Wno-class-memaccess'] + + if builder.options.opt == '1': + cxx.cflags += ['-O2'] + + # Keep frame pointer for debugging + cxx.cflags += ['-fno-omit-frame-pointer'] + + def configure_msvc(self, cxx): + if builder.options.debug == '1': + cxx.cflags += ['/MTd'] + else: + cxx.cflags += ['/MT'] + + cxx.defines += [ + '_CRT_SECURE_NO_WARNINGS', + '_CRT_NONSTDC_NO_DEPRECATE', + ] + + cxx.cflags += ['/W3'] + + cxx.cxxflags += [ + '/std:c++17', + '/EHsc', + '/GR-', + '/TP', + ] + + cxx.linkflags += [ + 'kernel32.lib', + 'user32.lib', + 'gdi32.lib', + ] + + if builder.options.opt == '1': + cxx.cflags += ['/Ox', '/Zo'] + cxx.linkflags += ['/OPT:ICF', '/OPT:REF'] + + if builder.options.debug == '1': + cxx.cflags += ['/Od', '/RTC1'] + + # Keep frame pointer + cxx.cflags += ['/Oy-'] + + def configure_linux(self, cxx): + cxx.defines += ['_LINUX', 'POSIX'] + cxx.linkflags += ['-Wl,--exclude-libs,ALL', '-lm'] + + if builder.options.opt == '1': + cxx.linkflags += ['-s'] + + if cxx.family == 'gcc': + cxx.linkflags += ['-static-libgcc'] + elif cxx.family == 'clang': + cxx.linkflags += ['-lgcc_eh'] + + cxx.linkflags += ['-static-libstdc++'] + + def configure_windows(self, cxx): + cxx.defines += [ + '_WINDOWS', + '_WIN32_WINNT=0x0601', + ] + + def Library(self, context, compiler, name): + compiler = compiler.clone() + SetArchFlags(compiler) + self.ConfigureForExtension(context, compiler) + return compiler.Library(name) + + def StaticLibrary(self, context, compiler, name): + compiler = compiler.clone() + return compiler.StaticLibrary(name) + + def ConfigureForExtension(self, context, compiler): + compiler.cxxincludes += [ + os.path.join(context.currentSourcePath), + os.path.join(self.sm_root, 'public'), + os.path.join(self.sm_root, 'public', 'extensions'), + os.path.join(self.sm_root, 'sourcepawn', 'include'), + os.path.join(self.sm_root, 'public', 'amtl', 'amtl'), + os.path.join(self.sm_root, 'public', 'amtl'), + ] + return compiler Extension = ExtensionConfig() Extension.detectSourceMod() @@ -301,12 +218,12 @@ builder.targets = builder.CloneableList(Extension.all_targets) # Add additional buildscripts here BuildScripts = [ - 'AMBuilder', + 'AMBuilder', ] if builder.backend == 'amb2': - BuildScripts += [ - 'PackageScript', - ] + BuildScripts += [ + 'PackageScript', + ] -builder.Build(BuildScripts, {'Extension': Extension}) +builder.Build(BuildScripts, {'Extension': Extension}) \ No newline at end of file diff --git a/AMBuilder b/AMBuilder index 3d10770..c4eeb7e 100755 --- a/AMBuilder +++ b/AMBuilder @@ -2,7 +2,7 @@ import os for cxx in builder.targets: - binary = Extension.Library(builder, cxx, 'yyjson.ext') + binary = Extension.Library(builder, cxx, 'json.ext') arch = binary.compiler.target.arch binary.compiler.defines += [ @@ -12,8 +12,8 @@ for cxx in builder.targets: binary.sources += [ 'third_party/yyjson/yyjson.c', 'src/extension.cpp', - 'src/YYJSONManager.cpp', - 'src/json_natives.cpp', + 'src/JsonManager.cpp', + 'src/JsonNatives.cpp', os.path.join(Extension.sm_root, 'public', 'smsdk_ext.cpp'), ] diff --git a/PackageScript b/PackageScript index c5f89c6..8b94de4 100755 --- a/PackageScript +++ b/PackageScript @@ -40,5 +40,5 @@ for cxx_task in Extension.extensions: # Copy include files CopyFiles('scripting/include', 'addons/sourcemod/scripting/include', - [ 'yyjson.inc' ] + [ 'json.inc' ] ) \ No newline at end of file diff --git a/README.md b/README.md index 33ed5ae..2717459 100755 --- a/README.md +++ b/README.md @@ -6,6 +6,8 @@ A [SourceMod](http://www.sourcemod.net/) extension that provides comprehensive J ## Key Features * High-performance JSON parsing and serialization using YYJSON * Support for [JSON Pointer](https://datatracker.ietf.org/doc/html/rfc6901) operations +* Support for [JSON Patch](https://datatracker.ietf.org/doc/html/rfc6902) operations +* Support for [JSON Merge Patch](https://datatracker.ietf.org/doc/html/rfc7386) operations * x64 support * Easy-to-use API for both objects and arrays * Pretty printing and writing support @@ -18,7 +20,7 @@ A [SourceMod](http://www.sourcemod.net/) extension that provides comprehensive J Performance test results using [twitter.json](https://github.com/ibireme/yyjson_benchmark/blob/master/data/json/twitter.json) (0.60 MB): ``` -=== YYJSON Performance Benchmark === +=== JSON Performance Benchmark === Test iterations: 100 Data size: 0.60 MB Parse time: 0.025 seconds @@ -27,7 +29,7 @@ Parse operations per second: 3937.93 ops/sec Parse speed: 2371.66 MB/s (2.31 GB/s) Stringify speed: 4505.23 MB/s (4.39 GB/s) Stringify operations per second: 7480.55 ops/sec -=== YYJSON Performance Benchmark End === +=== JSON Performance Benchmark End === ``` Test environment: @@ -37,7 +39,7 @@ Test environment: - Test iterations: 100 - SourceMod Version: 1.13.0.6966 - YYJSON Version: Latest version -- Test script: [yyjson_perf_test.sp](scripting/yyjson_perf_test.sp) +- Test script: [json_perf_test.sp](scripting/json_perf_test.sp) Note: Performance may vary depending on server hardware and load conditions. @@ -58,33 +60,33 @@ ambuild ``` ## Documentation -* [API Reference](https://github.com/ProjectSky/sm-ext-yyjson/blob/main/scripting/include/yyjson.inc) +* [API Reference](https://github.com/ProjectSky/sm-ext-yyjson/blob/main/scripting/include/json.inc) * [Latest Release](https://github.com/ProjectSky/sm-ext-yyjson/releases) ### SourceMod Extension API ```cpp -#include +#include -IYYJSONManager* g_pYYJSONManager = nullptr; +IJsonManager* g_pJsonManager = nullptr; void Ext::SDK_OnAllLoaded() { - SM_GET_LATE_IFACE(YYJSONMANAGER, g_pYYJSONManager); + SM_GET_LATE_IFACE(JSONMANAGER, g_pJsonManager); char error[256]; - YYJSONValue* val = g_pYYJSONManager->ParseJSON("{\"name\":\"John\", \"age\":30}", false, false, 0, error, sizeof(error)); + JsonValue* val = g_pJsonManager->ParseJSON("{\"name\":\"John\", \"age\":30}", false, false, 0, error, sizeof(error)); if (!val) { PrintToServer("Failed to parse JSON: %s", error); return; } - size_t size = g_pYYJSONManager->GetSerializedSize(val); + size_t size = g_pJsonManager->GetSerializedSize(val); char buffer[size]; - g_pYYJSONManager->WriteToString(val, buffer, size); + g_pJsonManager->WriteToString(val, buffer, size); // must release the value after using - g_pYYJSONManager->Release(val); + g_pJsonManager->Release(val); printf("JSON: %s\n", buffer); } @@ -95,7 +97,7 @@ void Ext::SDK_OnAllLoaded() #### Working with Objects ```cpp // Create a JSON object -YYJSONObject obj = new YYJSONObject(); +JSONObject obj = new JSONObject(); obj.SetInt("int", 1); obj.SetInt64("int64", "9223372036854775800"); obj.SetFloat("float", 2.0); @@ -120,7 +122,7 @@ delete obj; #### Working with Arrays ```cpp // Create a JSON array -YYJSONArray arr = new YYJSONArray(); +JSONArray arr = new JSONArray(); arr.PushInt(1); arr.PushInt64("9223372036854775800"); arr.PushFloat(2.0); @@ -146,7 +148,7 @@ delete arr; #### Using JSON Pointer ```cpp // Create nested structures -YYJSONObject obj = new YYJSONObject(); +JSONObject obj = new JSONObject(); obj.PtrSetInt("/a/b/c", 1); delete obj; @@ -172,7 +174,7 @@ delete obj; } */ -YYJSONObject data = YYJSON.Parse("example.json", true); +JSONObject data = JSON.Parse("example.json", true); // Access values using JSON Pointer int value = data.PtrGetInt("/int"); // Returns: 1234 float fValue = data.PtrGetFloat("/arr/1"); // Returns: 1.2344 @@ -183,46 +185,38 @@ delete data; #### Array and Object Iteration ```cpp // Object iteration -YYJSONObject obj = YYJSON.Parse("{\"a\": 1, \"b\": 2, \"c\": 3}"); +JSONObject obj = JSON.Parse("{\"a\": 1, \"b\": 2, \"c\": 3}"); char key[64]; -YYJSON value; -// Method 1: Using Foreach (Recommended) -while (obj.ForeachObject(key, sizeof(key), value)) { - PrintToServer("Key: %s", key); - delete value; -} - -// Method 2: Using ForeachKey (only used for keys) -while (obj.ForeachKey(key, sizeof(key))) { +// Method 1: Using Object Iterator (Recommended) +JSONObjIter iter = new JSONObjIter(obj); +while (iter.Next(key, sizeof(key))) { PrintToServer("Key: %s", key); } +delete iter; -// Method 3: Classic iteration +// Method 2: Classic iteration for (int i = 0; i < obj.Size; i++) { obj.GetKey(i, key, sizeof(key)); - value = obj.GetValueAt(i); + JSON value = obj.GetValueAt(i); delete value; } delete obj; // Array iteration -YYJSONArray arr = YYJSON.Parse("[1, 2, 3, 4, 5]"); -int index; +JSONArray arr = JSON.Parse("[1, 2, 3, 4, 5]"); +JSON value; -// Method 1: Using Foreach (Recommended) -while (arr.ForeachArray(index, value)) { - PrintToServer("Index: %d", index); +// Method 1: Using Array Iterator (Recommended) +JSONArrIter iter = new JSONArrIter(arr); +while ((value = iter.Next) != null) { + PrintToServer("Index: %d", iter.Index); delete value; } +delete iter; -// Method 2: Using ForeachIndex (only used for index) -while (arr.ForeachIndex(index)) { - PrintToServer("Index: %d", index); -} - -// Method 3: Classic iteration +// Method 2: Classic iteration for (int i = 0; i < arr.Length; i++) { value = arr.Get(i); delete value; @@ -234,7 +228,7 @@ delete arr; #### Array Search Operations ```cpp // Create a test array -YYJSONArray arr = YYJSON.Parse( +JSONArray arr = JSON.Parse( "[42, true, \"hello\", 3.14, \"world\", false, 42]" ); @@ -258,7 +252,7 @@ delete arr; #### Sorting Arrays and Objects ```cpp // Array sorting -YYJSONArray arr = YYJSON.Parse( +JSONArray arr = JSON.Parse( "[3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]", .is_mutable_doc = true ); @@ -272,14 +266,14 @@ arr.Sort(YYJSON_SORT_RANDOM); // Random // [5, 2, 9, 1, 6, 3, 4, 5, 1, 3, 5] (example output) // Mixed type array sorting -YYJSONArray mixed = YYJSON.Parse( +JSONArray mixed = JSON.Parse( "[true, 42, \"hello\", 1.23, false, \"world\"]", .is_mutable_doc = true ); mixed.Sort(); // [false, true, 1.23, 42, "hello", "world"] // Object sorting by keys -YYJSONObject obj = YYJSON.Parse( +JSONObject obj = JSON.Parse( "{\"zebra\": 1, \"alpha\": 2, \"beta\": 3, \"gamma\": 4}", .is_mutable_doc = true ); @@ -302,7 +296,7 @@ delete obj; // Create object from key-value string arrays char pairs[][] = {"name", "test", "type", "demo", "version", "1.0.0"}; -YYJSONObject obj = YYJSONObject.FromStrings(pairs, sizeof(pairs)); +JSONObject obj = JSONObject.FromStrings(pairs, sizeof(pairs)); /* Output: { @@ -314,7 +308,7 @@ YYJSONObject obj = YYJSONObject.FromStrings(pairs, sizeof(pairs)); // Create array from string array char items[][] = {"apple", "banana", "orange"}; -YYJSONArray arr = YYJSONArray.FromStrings(items, sizeof(items)); +JSONArray arr = JSONArray.FromStrings(items, sizeof(items)); delete obj; delete arr; @@ -330,7 +324,7 @@ delete arr; #### Using JSON Pack ```cpp // Create object with mixed types -YYJSON packed = YYJSON.Pack("{s:s,s:i,s:f,s:b,s:n}", +JSON packed = JSON.Pack("{s:s,s:i,s:f,s:b,s:n}", "name", "John", "age", 25, "height", 1.75, @@ -350,7 +344,7 @@ delete packed; */ // Create nested structures -YYJSON nested = YYJSON.Pack("{s:{s:s,s:[iii]}}", +JSON nested = JSON.Pack("{s:{s:s,s:[iii]}}", "user", "name", "John", "scores", 85, 90, 95 @@ -367,7 +361,7 @@ delete nested; */ // Create array with mixed types -YYJSON array = YYJSON.Pack("[sifbn]", +JSON array = JSON.Pack("[sifbn]", "test", 42, 3.14, true ); delete array; @@ -388,10 +382,10 @@ When parsing JSON documents, you can choose whether to create a mutable or immut ```cpp // Create an immutable document (read-only) -YYJSONObject obj = YYJSON.Parse("example.json", true); +JSONObject obj = JSON.Parse("example.json", true); // Create a mutable document (read-write) -YYJSONObject obj = YYJSON.Parse("example.json", true, true); +JSONObject obj = JSON.Parse("example.json", true, true); ``` Immutable documents: @@ -410,7 +404,7 @@ Immutable documents support a variety of read operations: Example of operations with immutable documents: ```cpp // Create an immutable document -YYJSONObject obj = YYJSON.Parse("example.json", true); +JSONObject obj = JSON.Parse("example.json", true); // Reading is allowed int value = obj.GetInt("key"); // Works fine @@ -429,10 +423,10 @@ You can convert between mutable and immutable documents using deep copy: ```cpp // Create an immutable document -YYJSONObject immutable = YYJSON.Parse("example.json", true); +JSONObject immutable = JSON.Parse("example.json", true); // Create a mutable copy -YYJSONObject mutable = immutable.ToMutable(); +JSONObject mutable = immutable.ToMutable(); // Now you can modify the mutable copy mutable.SetInt("key", 123); diff --git a/public/IJsonManager.h b/public/IJsonManager.h new file mode 100755 index 0000000..f43fdbe --- /dev/null +++ b/public/IJsonManager.h @@ -0,0 +1,1945 @@ +#ifndef _INCLUDE_IJSONMANAGER_H_ +#define _INCLUDE_IJSONMANAGER_H_ + +#include +#include + +using SourceMod::Handle_t; +using SourceMod::HandleType_t; +using SourceMod::SMInterface; +using SourcePawn::IPluginContext; + +// Forward declaration +class JsonValue; +class JsonArrIter; +class JsonObjIter; + +#define SMINTERFACE_JSONMANAGER_NAME "IJsonManager" +#define SMINTERFACE_JSONMANAGER_VERSION 3 +#define JSON_ERROR_BUFFER_SIZE 256 +#define JSON_INT64_BUFFER_SIZE 32 + +/** + * @brief JSON sorting order + */ +enum JSON_SORT_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 +{ +public: + virtual ~IPackParamProvider() = default; + + virtual bool GetNextString(const char** out_str) = 0; + virtual bool GetNextInt(int* out_value) = 0; + virtual bool GetNextFloat(float* out_value) = 0; + virtual bool GetNextBool(bool* out_value) = 0; +}; + +/** + * @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 + * IJsonManager* g_pJsonManager = nullptr; + * + * bool YourExtension::SDK_OnAllLoaded() + * { + * SM_GET_LATE_IFACE(JSONMANAGER, g_pJsonManager); + * } + */ +class IJsonManager : public SMInterface +{ +public: + virtual ~IJsonManager() = default; + + virtual const char *GetInterfaceName() override { + return SMINTERFACE_JSONMANAGER_NAME; + } + + virtual unsigned int GetInterfaceVersion() override { + return SMINTERFACE_JSONMANAGER_VERSION; + } + +public: + /** + * Parse JSON from string or file + * @param json_str JSON string or file path + * @param is_file true if json_str is a file path + * @param is_mutable true to create mutable document (default: false) + * @param read_flg Read flags (YYJSON_READ_FLAG values, default: 0) + * @param error Error buffer (optional) + * @param error_size Error buffer size + * @return JSON value pointer or nullptr on error + */ + 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; + + /** + * Write JSON to string + * @param handle JSON value + * @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 + * @return true on success, false if buffer is too small or on error + * + * @note The out_size parameter returns the size including null terminator + * + * @warning Buffer Size Requirements: + * This function does not allocate memory, but the buffer must be larger than + * the final JSON size to allow temporary space. + * + * The extra space is needed temporarily for each value while it is written, + * and is reused for later values: + * - Number: 40 bytes + * - String: 16 + (str_len * 6) bytes + * - Other values: 16 bytes + * - Nesting depth: 16 * max_json_depth bytes + * + * @note GetSerializedSize() only returns the final JSON size, NOT including the temporary space + * You must manually add extra buffer space based on your JSON content + * @note For most use cases, prefer WriteToStringPtr() for simplicity + * Use this method only when you need to avoid heap allocation (e.g., stack buffers, pre-allocated pools) + */ + virtual bool WriteToString(JsonValue* handle, char* buffer, size_t buffer_size, + uint32_t write_flg = 0, size_t* out_size = nullptr) = 0; + + /** + * Write JSON to string and return allocated string + * @param handle JSON value + * @param write_flg Write flags (YYJSON_WRITE_FLAG values, default: 0) + * @param out_size Pointer to receive actual size written (including null terminator) optional + * @return Allocated string pointer on success, nullptr on error. Caller must free() the returned pointer + * + * @note This function handles memory allocation internally, + * avoiding the complexity of manual buffer size calculation required by WriteToString() + * @note This is the recommended method for most use cases due to its simplicity and safety + */ + virtual char* WriteToStringPtr(JsonValue* handle, uint32_t write_flg = 0, size_t* out_size = nullptr) = 0; + + /** + * Apply JSON Patch (RFC 6902) and return a new JSON value + * @param target Target JSON value + * @param patch JSON Patch document + * @param result_mutable true to return mutable result, false for immutable + * @param error Error buffer (optional) + * @param error_size Error buffer size + * @return Patched JSON value on success, nullptr on failure + */ + virtual JsonValue* ApplyJsonPatch(JsonValue* target, JsonValue* patch, bool result_mutable, + char* error = nullptr, size_t error_size = 0) = 0; + + /** + * Apply JSON Patch in place (target must be mutable) + * @param target Target JSON value (mutable document) + * @param patch JSON Patch document + * @param error Error buffer (optional) + * @param error_size Error buffer size + * @return true on success, false on failure + */ + virtual bool JsonPatchInPlace(JsonValue* target, JsonValue* patch, + char* error = nullptr, size_t error_size = 0) = 0; + + /** + * Apply JSON Merge Patch (RFC 7396) and return a new JSON value + * @param target Target JSON value + * @param patch JSON Merge Patch document + * @param result_mutable true to return mutable result, false for immutable + * @param error Error buffer (optional) + * @param error_size Error buffer size + * @return Patched JSON value on success, nullptr on failure + */ + virtual JsonValue* ApplyMergePatch(JsonValue* target, JsonValue* patch, bool result_mutable, + char* error = nullptr, size_t error_size = 0) = 0; + + /** + * Apply JSON Merge Patch in place (target must be mutable) + * @param target Target JSON value (mutable document) + * @param patch JSON Merge Patch document + * @param error Error buffer (optional) + * @param error_size Error buffer size + * @return true on success, false on failure + */ + virtual bool MergePatchInPlace(JsonValue* target, JsonValue* patch, + char* error = nullptr, size_t error_size = 0) = 0; + + /** + * Write JSON to file + * @param handle JSON value + * @param path File path + * @param write_flg Write flags (YYJSON_WRITE_FLAG values, default: 0) + * @param error Error buffer (optional) + * @param error_size Error buffer size + * @return true on success + */ + virtual bool WriteToFile(JsonValue* handle, const char* path, uint32_t write_flg = 0, + char* error = nullptr, size_t error_size = 0) = 0; + + /** + * Compare two JSON values for equality + * @param handle1 First JSON value to compare + * @param handle2 Second JSON value to compare + * @return true if values are equal, false otherwise + * @note Compares structure and content recursively + */ + 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 + * @param targetDoc Target document that will own the copied value + * @param sourceValue Source value to copy + * @return New JSON value (deep copy) or nullptr on failure + * @note The returned value is owned by targetDoc's document context + */ + 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(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 + * different sizes and may cause buffer overflow. + * + * @example + * // Correct usage: + * auto flags = YYJSON_WRITE_PRETTY; + * size_t size = g_pJsonManager->GetSerializedSize(handle, flags); + * char* buffer = new char[size]; + * g_pJsonManager->WriteToString(handle, buffer, size, flags); // Use same flags + */ + virtual size_t GetSerializedSize(JsonValue* handle, uint32_t write_flg = 0) = 0; + + /** + * Convert immutable document to mutable + * @param handle Immutable JSON value + * @return New mutable JSON value or nullptr if already mutable or on error + * @note Creates a deep copy as a mutable document + */ + virtual JsonValue* ToMutable(JsonValue* handle) = 0; + + /** + * Convert mutable document to immutable + * @param handle Mutable JSON value + * @return New immutable JSON value or nullptr if already immutable or on error + * @note Creates a deep copy as an immutable document + */ + virtual JsonValue* ToImmutable(JsonValue* handle) = 0; + + /** + * Get JSON type + * @param handle JSON value + * @return YYJSON_TYPE value + */ + virtual uint8_t GetType(JsonValue* handle) = 0; + + /** + * Get JSON subtype + * @param handle JSON value + * @return YYJSON_SUBTYPE value + */ + 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(JsonValue* handle) = 0; + + /** + * Check if value is an object + * @param handle JSON value + * @return true if value is an object + */ + 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(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(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(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(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(JsonValue* handle) = 0; + + /** + * Check if value is boolean true + * @param handle JSON value + * @return true if value is boolean true + */ + 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(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(JsonValue* handle) = 0; + + /** + * Check if value is a string + * @param handle JSON value + * @return true if value is a string + */ + virtual bool IsStr(JsonValue* handle) = 0; + + /** + * Check if value is null + * @param handle JSON value + * @return true if value is null + */ + 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(JsonValue* handle) = 0; + + /** + * Check if document is mutable + * @param handle JSON value + * @return true if document is mutable + */ + virtual bool IsMutable(JsonValue* handle) = 0; + + /** + * Check if document is immutable + * @param handle JSON value + * @return true if document is immutable + */ + 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 (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 includes the null terminator + */ + virtual size_t GetReadSize(JsonValue* handle) = 0; + + /** + * Get the reference count of the document + * @param handle JSON value + * @return Reference count of the document (number of JsonValue objects sharing this document) + * @note Returns 0 if handle is null or has no document + */ + virtual size_t GetRefCount(JsonValue* handle) = 0; + + /** + * Get the total number of values in a document + * @param handle JSON value + * @return Total number of values in the document tree, 0 if handle is null or mutable + * @note Only works on immutable documents (parsed from JSON) + * @note Useful for performance planning and memory estimation + * @note Counts ALL values including containers, keys, and leaf values + * Example: {"a":1,"b":2,"c":3} returns 7 (1 object + 3 keys + 3 values) + */ + virtual size_t GetValCount(JsonValue* handle) = 0; + + /** + * Create an empty mutable JSON object + * @return New mutable JSON object or nullptr on failure + */ + virtual JsonValue* ObjectInit() = 0; + + /** + * Create a JSON object from key-value string pairs + * @param pairs Array of strings [key1, val1, key2, val2, ...] + * @param count Number of key-value pairs + * @return New JSON object or nullptr on failure + */ + virtual JsonValue* ObjectInitWithStrings(const char** pairs, size_t count) = 0; + + /** + * Parse a JSON object from string + * @param str JSON string to parse + * @param read_flg Read flags (YYJSON_READ_FLAG values, default: 0) + * @param error Error buffer (optional) + * @param error_size Error buffer size + * @return Parsed JSON object or nullptr on error + * @note Returns error if root is not an object + */ + virtual JsonValue* ObjectParseString(const char* str, uint32_t read_flg = 0, + char* error = nullptr, size_t error_size = 0) = 0; + + /** + * Parse a JSON object from file + * @param path File path + * @param read_flg Read flags (YYJSON_READ_FLAG values, default: 0) + * @param error Error buffer (optional) + * @param error_size Error buffer size + * @return Parsed JSON object or nullptr on error + * @note Returns error if root is not an object + */ + virtual JsonValue* ObjectParseFile(const char* path, uint32_t read_flg = 0, + char* error = nullptr, size_t error_size = 0) = 0; + + /** + * Get number of key-value pairs in object + * @param handle JSON object + * @return Number of key-value pairs + */ + virtual size_t ObjectGetSize(JsonValue* handle) = 0; + + /** + * Get key name at specific index + * @param handle JSON object + * @param index Index of key-value pair + * @param out_key Pointer to receive key string + * @return true on success, false if index out of bounds + */ + virtual bool ObjectGetKey(JsonValue* handle, size_t index, const char** out_key) = 0; + + /** + * Get value at specific index + * @param handle JSON object + * @param index Index of key-value pair + * @return JSON value or nullptr if index out of bounds + */ + virtual JsonValue* ObjectGetValueAt(JsonValue* handle, size_t index) = 0; + + /** + * Get value by key name + * @param handle JSON object + * @param key Key name + * @return JSON value or nullptr if key not found + */ + virtual JsonValue* ObjectGet(JsonValue* handle, const char* key) = 0; + + /** + * Get boolean value by key + * @param handle JSON object + * @param key Key name + * @param out_value Pointer to receive boolean value + * @return true on success, false if key not found or type mismatch + */ + virtual bool ObjectGetBool(JsonValue* handle, const char* key, bool* out_value) = 0; + + /** + * Get float value by key + * @param handle JSON object + * @param key Key name + * @param out_value Pointer to receive double value + * @return true on success, false if key not found or type mismatch + * @note Integers values are auto converted to double + */ + virtual bool ObjectGetDouble(JsonValue* handle, const char* key, double* out_value) = 0; + + /** + * Get integer value by key + * @param handle JSON object + * @param key Key name + * @param out_value Pointer to receive integer value + * @return true on success, false if key not found or type mismatch + */ + virtual bool ObjectGetInt(JsonValue* handle, const char* key, int* out_value) = 0; + + /** + * 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 (std::variant) + * @return true on success, false if key not found or type mismatch + */ + virtual bool ObjectGetInt64(JsonValue* handle, const char* key, std::variant* out_value) = 0; + + /** + * Get string value by key + * @param handle JSON object + * @param key Key name + * @param out_str Pointer to receive string pointer + * @param out_len Pointer to receive string length + * @return true on success, false if key not found or type mismatch + */ + virtual bool ObjectGetString(JsonValue* handle, const char* key, const char** out_str, size_t* out_len) = 0; + + /** + * Check if value at key is null + * @param handle JSON object + * @param key Key name + * @param out_is_null Pointer to receive result + * @return true if key exists, false if key not found + */ + virtual bool ObjectIsNull(JsonValue* handle, const char* key, bool* out_is_null) = 0; + + /** + * Check if object has a specific key + * @param handle JSON object + * @param key Key name (or JSON pointer if use_pointer is true) + * @param use_pointer If true, treat key as JSON pointer + * @return true if key exists + */ + virtual bool ObjectHasKey(JsonValue* handle, const char* key, bool use_pointer) = 0; + + /** + * Rename a key in the object + * @param handle Mutable JSON object + * @param old_key Current key name + * @param new_key New key name + * @param allow_duplicate Allow duplicate key names + * @return true on success + * @note Only works on mutable objects + */ + virtual bool ObjectRenameKey(JsonValue* handle, const char* old_key, const char* new_key, bool allow_duplicate) = 0; + + /** + * Set value by key (mutable only) + * @param handle Mutable JSON object + * @param key Key name + * @param value JSON value to set + * @return true on success + */ + virtual bool ObjectSet(JsonValue* handle, const char* key, JsonValue* value) = 0; + + /** + * Set boolean value by key (mutable only) + * @param handle Mutable JSON object + * @param key Key name + * @param value Boolean value + * @return true on success + */ + virtual bool ObjectSetBool(JsonValue* handle, const char* key, bool value) = 0; + + /** + * Set double value by key (mutable only) + * @param handle Mutable JSON object + * @param key Key name + * @param value Double value + * @return true on success + */ + virtual bool ObjectSetDouble(JsonValue* handle, const char* key, double value) = 0; + + /** + * Set integer value by key (mutable only) + * @param handle Mutable JSON object + * @param key Key name + * @param value Integer value + * @return true on success + */ + virtual bool ObjectSetInt(JsonValue* handle, const char* key, int value) = 0; + + /** + * 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 (std::variant) + * @return true on success + */ + virtual bool ObjectSetInt64(JsonValue* handle, const char* key, std::variant value) = 0; + + /** + * Set null value by key (mutable only) + * @param handle Mutable JSON object + * @param key Key name + * @return true on success + */ + virtual bool ObjectSetNull(JsonValue* handle, const char* key) = 0; + + /** + * Set string value by key (mutable only) + * @param handle Mutable JSON object + * @param key Key name + * @param value String value + * @return true on success + */ + virtual bool ObjectSetString(JsonValue* handle, const char* key, const char* value) = 0; + + /** + * Remove key-value pair by key (mutable only) + * @param handle Mutable JSON object + * @param key Key name + * @return true on success + */ + 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(JsonValue* handle) = 0; + + /** + * Sort object keys + * @param handle Mutable JSON object + * @param sort_mode Sort order (see JSON_SORT_ORDER enum) + * @return true on success + * @note Only works on mutable objects + */ + virtual bool ObjectSort(JsonValue* handle, JSON_SORT_ORDER sort_mode) = 0; + + /** + * Rotate key-value pairs in the object + * @param handle Mutable JSON object + * @param idx Number of positions to rotate (must be less than object size) + * @return true on success, false if idx >= object size or object is immutable + * @note Only works on mutable objects + * @note Example: {"a":1,"b":2,"c":3,"d":4} rotate 1 becomes {"b":2,"c":3,"d":4,"a":1} + * @note Valid range: 0 <= idx < object size + * @warning This function takes linear time proportional to the rotation amount + */ + virtual bool ObjectRotate(JsonValue* handle, size_t idx) = 0; + + /** + * Create an empty mutable JSON array + * @return New mutable JSON array or nullptr on failure + */ + virtual JsonValue* ArrayInit() = 0; + + /** + * Create a JSON array from string values + * @param strings Array of string values + * @param count Number of strings + * @return New JSON array or nullptr on failure + */ + 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 double values + * @param values Array of double values + * @param count Number of values + * @return New JSON array or nullptr on failure + */ + virtual JsonValue* ArrayInitWithDouble(const double* values, size_t count) = 0; + + /** + * Parse a JSON array from string + * @param str JSON string to parse + * @param read_flg Read flags (YYJSON_READ_FLAG values, default: 0) + * @param error Error buffer (optional) + * @param error_size Error buffer size + * @return Parsed JSON array or nullptr on error + * @note Returns error if root is not an array + */ + virtual JsonValue* ArrayParseString(const char* str, uint32_t read_flg = 0, + char* error = nullptr, size_t error_size = 0) = 0; + + /** + * Parse a JSON array from file + * @param path File path + * @param read_flg Read flags (YYJSON_READ_FLAG values, default: 0) + * @param error Error buffer (optional) + * @param error_size Error buffer size + * @return Parsed JSON array or nullptr on error + * @note Returns error if root is not an array + */ + virtual JsonValue* ArrayParseFile(const char* path, uint32_t read_flg = 0, + char* error = nullptr, size_t error_size = 0) = 0; + + /** + * Get number of elements in array + * @param handle JSON array + * @return Number of elements + */ + virtual size_t ArrayGetSize(JsonValue* handle) = 0; + + /** + * Get element at specific index + * @param handle JSON array + * @param index Element index + * @return JSON value or nullptr if index out of bounds + */ + 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 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 JsonValue* ArrayGetLast(JsonValue* handle) = 0; + + /** + * Get boolean value at index + * @param handle JSON array + * @param index Element index + * @param out_value Pointer to receive boolean value + * @return true on success, false if index out of bounds or type mismatch + */ + virtual bool ArrayGetBool(JsonValue* handle, size_t index, bool* out_value) = 0; + + /** + * Get double value at index + * @param handle JSON array + * @param index Element index + * @param out_value Pointer to receive double value + * @return true on success, false if index out of bounds or type mismatch + * @note Integers values are auto converted to double + */ + virtual bool ArrayGetDouble(JsonValue* handle, size_t index, double* out_value) = 0; + + /** + * Get integer value at index + * @param handle JSON array + * @param index Element index + * @param out_value Pointer to receive integer value + * @return true on success, false if index out of bounds or type mismatch + */ + virtual bool ArrayGetInt(JsonValue* handle, size_t index, int* out_value) = 0; + + /** + * 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 (std::variant) + * @return true on success, false if index out of bounds or type mismatch + */ + virtual bool ArrayGetInt64(JsonValue* handle, size_t index, std::variant* out_value) = 0; + + /** + * Get string value at index + * @param handle JSON array + * @param index Element index + * @param out_str Pointer to receive string pointer + * @param out_len Pointer to receive string length + * @return true on success, false if index out of bounds or type mismatch + */ + virtual bool ArrayGetString(JsonValue* handle, size_t index, const char** out_str, size_t* out_len) = 0; + + /** + * Check if element at index is null + * @param handle JSON array + * @param index Element index + * @return true if element is null + */ + virtual bool ArrayIsNull(JsonValue* handle, size_t index) = 0; + + /** + * Replace element at index with JSON value (mutable only) + * @param handle Mutable JSON array + * @param index Element index + * @param value JSON value to set + * @return true on success + */ + virtual bool ArrayReplace(JsonValue* handle, size_t index, JsonValue* value) = 0; + + /** + * Replace element at index with boolean (mutable only) + * @param handle Mutable JSON array + * @param index Element index + * @param value Boolean value + * @return true on success + */ + virtual bool ArrayReplaceBool(JsonValue* handle, size_t index, bool value) = 0; + + /** + * Replace element at index with double (mutable only) + * @param handle Mutable JSON array + * @param index Element index + * @param value Double value + * @return true on success + */ + virtual bool ArrayReplaceDouble(JsonValue* handle, size_t index, double value) = 0; + + /** + * Replace element at index with integer (mutable only) + * @param handle Mutable JSON array + * @param index Element index + * @param value Integer value + * @return true on success + */ + virtual bool ArrayReplaceInt(JsonValue* handle, size_t index, int value) = 0; + + /** + * 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 (std::variant) + * @return true on success + */ + virtual bool ArrayReplaceInt64(JsonValue* handle, size_t index, std::variant value) = 0; + + /** + * Replace element at index with null (mutable only) + * @param handle Mutable JSON array + * @param index Element index + * @return true on success + */ + virtual bool ArrayReplaceNull(JsonValue* handle, size_t index) = 0; + + /** + * Replace element at index with string (mutable only) + * @param handle Mutable JSON array + * @param index Element index + * @param value String value + * @return true on success + */ + virtual bool ArrayReplaceString(JsonValue* handle, size_t index, const char* value) = 0; + + /** + * Append JSON value to end of array (mutable only) + * @param handle Mutable JSON array + * @param value JSON value to append + * @return true on success + */ + virtual bool ArrayAppend(JsonValue* handle, JsonValue* value) = 0; + + /** + * Append boolean to end of array (mutable only) + * @param handle Mutable JSON array + * @param value Boolean value + * @return true on success + */ + virtual bool ArrayAppendBool(JsonValue* handle, bool value) = 0; + + /** + * Append double to end of array (mutable only) + * @param handle Mutable JSON array + * @param value Double value + * @return true on success + */ + virtual bool ArrayAppendDouble(JsonValue* handle, double value) = 0; + + /** + * Append integer to end of array (mutable only) + * @param handle Mutable JSON array + * @param value Integer value + * @return true on success + */ + virtual bool ArrayAppendInt(JsonValue* handle, int value) = 0; + + /** + * 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 (std::variant) + * @return true on success + */ + 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(JsonValue* handle) = 0; + + /** + * Append string to end of array (mutable only) + * @param handle Mutable JSON array + * @param value String value + * @return true on success + */ + 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 double at specific index (mutable only) + * @param handle Mutable JSON array + * @param index Element index + * @param value Double value + * @return true on success + */ + virtual bool ArrayInsertDouble(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 double to beginning of array (mutable only) + * @param handle Mutable JSON array + * @param value Double value + * @return true on success + */ + virtual bool ArrayPrependDouble(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) + * @param handle Mutable JSON array + * @param index Element index + * @return true on success + */ + 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(JsonValue* handle) = 0; + + /** + * Remove last element (mutable only) + * @param handle Mutable JSON array + * @return true on success + */ + virtual bool ArrayRemoveLast(JsonValue* handle) = 0; + + /** + * Remove range of elements (mutable only) + * @param handle JSON array + * @param start_index Start index (inclusive) + * @param count Number of elements to remove + * @return true on success + */ + virtual bool ArrayRemoveRange(JsonValue* handle, size_t start_index, size_t count) = 0; + + /** + * Remove all elements (mutable only) + * @param handle Mutable JSON array + * @return true on success + */ + virtual bool ArrayClear(JsonValue* handle) = 0; + + /** + * Find index of boolean value + * @param handle JSON array + * @param search_value Boolean value to search for + * @return Index of first match, or -1 if not found + */ + virtual int ArrayIndexOfBool(JsonValue* handle, bool search_value) = 0; + + /** + * Find index of string value + * @param handle JSON array + * @param search_value String value to search for + * @return Index of first match, or -1 if not found + */ + virtual int ArrayIndexOfString(JsonValue* handle, const char* search_value) = 0; + + /** + * Find index of integer value + * @param handle JSON array + * @param search_value Integer value to search for + * @return Index of first match, or -1 if not found + */ + virtual int ArrayIndexOfInt(JsonValue* handle, int search_value) = 0; + + /** + * Find index of 64-bit integer value (auto-detects signed/unsigned) + * @param handle JSON array + * @param search_value 64-bit integer value to search for (std::variant) + * @return Index of first match, or -1 if not found + */ + virtual int ArrayIndexOfInt64(JsonValue* handle, std::variant search_value) = 0; + + /** + * Find index of double value + * @param handle JSON array + * @param search_value Double value to search for + * @return Index of first match, or -1 if not found + */ + virtual int ArrayIndexOfDouble(JsonValue* handle, double search_value) = 0; + + /** + * Sort array elements + * @param handle Mutable JSON array + * @param sort_mode Sort order (see JSON_SORT_ORDER enum) + * @return true on success + * @note Only works on mutable arrays + */ + virtual bool ArraySort(JsonValue* handle, JSON_SORT_ORDER sort_mode) = 0; + + /** + * Rotate array elements + * @param handle Mutable JSON array + * @param idx Number of positions to rotate (must be less than array length) + * @return true on success, false if idx >= array length or array is immutable + * @note Only works on mutable arrays + * @note Example: [1,2,3,4,5] rotate 2 becomes [3,4,5,1,2] + * @note Valid range: 0 <= idx < array length + * @warning This function takes linear time proportional to the rotation amount + */ + virtual bool ArrayRotate(JsonValue* handle, size_t idx) = 0; + + /** + * Create JSON value from format string and parameters + * @param format Format string (e.g., "{s:i,s:s}", "[i,s,b]") + * Format specifiers: + * - 's': string + * - 'i': integer + * - 'f': float + * - 'b': boolean + * - 'n': null + * - '{': object start, '}': object end + * - '[': array start, ']': array end + * @param param_provider Parameter provider implementation + * @param error Error buffer (optional) + * @param error_size Error buffer size + * @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 JsonValue* Pack(const char* format, IPackParamProvider* param_provider, + char* error = nullptr, size_t error_size = 0) = 0; + + /** + * Create a JSON boolean value + * @param value Boolean value + * @return New JSON boolean or nullptr on failure + */ + virtual JsonValue* CreateBool(bool value) = 0; + + /** + * Create a JSON double value + * @param value Double value + * @return New JSON double or nullptr on failure + */ + virtual JsonValue* CreateDouble(double value) = 0; + + /** + * Create a JSON integer value + * @param value Integer value + * @return New JSON integer or nullptr on failure + */ + virtual JsonValue* CreateInt(int value) = 0; + + /** + * 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 JsonValue* CreateInt64(std::variant value) = 0; + + /** + * Create a JSON null value + * @return New JSON null or nullptr on failure + */ + virtual JsonValue* CreateNull() = 0; + + /** + * Create a JSON string value + * @param value String value + * @return New JSON string or nullptr on failure + */ + virtual JsonValue* CreateString(const char* value) = 0; + + /** + * Get boolean value from JSON + * @param handle JSON value + * @param out_value Pointer to receive boolean value + * @return true on success, false on type mismatch + */ + virtual bool GetBool(JsonValue* handle, bool* out_value) = 0; + + /** + * Get double value from JSON + * @param handle JSON value + * @param out_value Pointer to receive double value + * @return true on success, false on type mismatch + * @note Integers values are auto converted to double + */ + virtual bool GetDouble(JsonValue* handle, double* out_value) = 0; + + /** + * Get integer value from JSON + * @param handle JSON value + * @param out_value Pointer to receive integer value + * @return true on success, false on type mismatch + */ + virtual bool GetInt(JsonValue* handle, int* out_value) = 0; + + /** + * 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 (std::variant) + * @return true on success, false on type mismatch + */ + virtual bool GetInt64(JsonValue* handle, std::variant* out_value) = 0; + + /** + * Get string value from JSON + * @param handle JSON value + * @param out_str Pointer to receive string pointer + * @param out_len Pointer to receive string length + * @return true on success, false on type mismatch + */ + virtual bool GetString(JsonValue* handle, const char** out_str, size_t* out_len) = 0; + + /** + * Get value using JSON Pointer + * @param handle JSON value + * @param path JSON Pointer path (e.g., "/users/0/name") + * @param error Error buffer (optional) + * @param error_size Error buffer size + * @return JSON value or nullptr on error + */ + virtual JsonValue* PtrGet(JsonValue* handle, const char* path, + char* error = nullptr, size_t error_size = 0) = 0; + + /** + * Get boolean value using JSON Pointer + * @param handle JSON value + * @param path JSON Pointer path + * @param out_value Pointer to receive boolean value + * @param error Error buffer (optional) + * @param error_size Error buffer size + * @return true on success, false on error + */ + virtual bool PtrGetBool(JsonValue* handle, const char* path, bool* out_value, + char* error = nullptr, size_t error_size = 0) = 0; + + /** + * Get double value using JSON Pointer + * @param handle JSON value + * @param path JSON Pointer path + * @param out_value Pointer to receive double value + * @param error Error buffer (optional) + * @param error_size Error buffer size + * @return true on success, false on error + * @note Integers values are auto converted to double + */ + virtual bool PtrGetDouble(JsonValue* handle, const char* path, double* out_value, + char* error = nullptr, size_t error_size = 0) = 0; + + /** + * Get integer value using JSON Pointer + * @param handle JSON value + * @param path JSON Pointer path + * @param out_value Pointer to receive integer value + * @param error Error buffer (optional) + * @param error_size Error buffer size + * @return true on success, false on error + */ + 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 (auto-detects signed/unsigned) + * @param handle JSON value + * @param path JSON Pointer path + * @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(JsonValue* handle, const char* path, std::variant* out_value, + char* error = nullptr, size_t error_size = 0) = 0; + + /** + * Get string value using JSON Pointer + * @param handle JSON value + * @param path JSON Pointer path + * @param out_str Pointer to receive string pointer + * @param out_len Pointer to receive string length + * @param error Error buffer (optional) + * @param error_size Error buffer size + * @return true on success, false on error + */ + 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; + + /** + * Check if value is null using JSON Pointer + * @param handle JSON value + * @param path JSON Pointer path + * @param out_is_null Pointer to receive result + * @param error Error buffer (optional) + * @param error_size Error buffer size + * @return true on success, false on error + */ + virtual bool PtrGetIsNull(JsonValue* handle, const char* path, bool* out_is_null, + char* error = nullptr, size_t error_size = 0) = 0; + + /** + * Get length of container (array/object) using JSON Pointer + * @param handle JSON value + * @param path JSON Pointer path + * @param out_len Pointer to receive length + * @param error Error buffer (optional) + * @param error_size Error buffer size + * @return true on success, false on error + */ + virtual bool PtrGetLength(JsonValue* handle, const char* path, size_t* out_len, + char* error = nullptr, size_t error_size = 0) = 0; + + /** + * Set value using JSON Pointer (mutable only) + * @param handle Mutable JSON value + * @param path JSON Pointer path + * @param value JSON value to set + * @param error Error buffer (optional) + * @param error_size Error buffer size + * @return true on success, false on error + */ + virtual bool PtrSet(JsonValue* handle, const char* path, JsonValue* value, + char* error = nullptr, size_t error_size = 0) = 0; + + /** + * Set boolean value using JSON Pointer (mutable only) + * @param handle Mutable JSON value + * @param path JSON Pointer path + * @param value Boolean value + * @param error Error buffer (optional) + * @param error_size Error buffer size + * @return true on success, false on error + */ + virtual bool PtrSetBool(JsonValue* handle, const char* path, bool value, + char* error = nullptr, size_t error_size = 0) = 0; + + /** + * Set double value using JSON Pointer (mutable only) + * @param handle Mutable JSON value + * @param path JSON Pointer path + * @param value Double value + * @param error Error buffer (optional) + * @param error_size Error buffer size + * @return true on success, false on error + */ + virtual bool PtrSetDouble(JsonValue* handle, const char* path, double value, + char* error = nullptr, size_t error_size = 0) = 0; + + /** + * Set integer value using JSON Pointer (mutable only) + * @param handle Mutable JSON value + * @param path JSON Pointer path + * @param value Integer value + * @param error Error buffer (optional) + * @param error_size Error buffer size + * @return true on success, false on error + */ + 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, auto-detects signed/unsigned) + * @param handle Mutable JSON value + * @param path JSON Pointer path + * @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(JsonValue* handle, const char* path, std::variant value, + char* error = nullptr, size_t error_size = 0) = 0; + + /** + * Set string value using JSON Pointer (mutable only) + * @param handle Mutable JSON value + * @param path JSON Pointer path + * @param value String value + * @param error Error buffer (optional) + * @param error_size Error buffer size + * @return true on success, false on error + */ + virtual bool PtrSetString(JsonValue* handle, const char* path, const char* value, + char* error = nullptr, size_t error_size = 0) = 0; + + /** + * Set null value using JSON Pointer (mutable only) + * @param handle Mutable JSON value + * @param path JSON Pointer path + * @param error Error buffer (optional) + * @param error_size Error buffer size + * @return true on success, false on error + */ + virtual bool PtrSetNull(JsonValue* handle, const char* path, + char* error = nullptr, size_t error_size = 0) = 0; + + /** + * Add value to array using JSON Pointer (mutable only) + * @param handle Mutable JSON value + * @param path JSON Pointer path to array + * @param value JSON value to add + * @param error Error buffer (optional) + * @param error_size Error buffer size + * @return true on success, false on error + */ + virtual bool PtrAdd(JsonValue* handle, const char* path, JsonValue* value, + char* error = nullptr, size_t error_size = 0) = 0; + + /** + * Add boolean to array using JSON Pointer (mutable only) + * @param handle Mutable JSON value + * @param path JSON Pointer path to array + * @param value Boolean value + * @param error Error buffer (optional) + * @param error_size Error buffer size + * @return true on success, false on error + */ + virtual bool PtrAddBool(JsonValue* handle, const char* path, bool value, + char* error = nullptr, size_t error_size = 0) = 0; + + /** + * Add double to array using JSON Pointer (mutable only) + * @param handle Mutable JSON value + * @param path JSON Pointer path to array + * @param value Double value + * @param error Error buffer (optional) + * @param error_size Error buffer size + * @return true on success, false on error + */ + virtual bool PtrAddDouble(JsonValue* handle, const char* path, double value, + char* error = nullptr, size_t error_size = 0) = 0; + + /** + * Add integer to array using JSON Pointer (mutable only) + * @param handle Mutable JSON value + * @param path JSON Pointer path to array + * @param value Integer value + * @param error Error buffer (optional) + * @param error_size Error buffer size + * @return true on success, false on error + */ + 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, auto-detects signed/unsigned) + * @param handle Mutable JSON value + * @param path JSON Pointer path to array + * @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(JsonValue* handle, const char* path, std::variant value, + char* error = nullptr, size_t error_size = 0) = 0; + + /** + * Add string to array using JSON Pointer (mutable only) + * @param handle Mutable JSON value + * @param path JSON Pointer path to array + * @param value String value + * @param error Error buffer (optional) + * @param error_size Error buffer size + * @return true on success, false on error + */ + virtual bool PtrAddString(JsonValue* handle, const char* path, const char* value, + char* error = nullptr, size_t error_size = 0) = 0; + + /** + * Add null to array using JSON Pointer (mutable only) + * @param handle Mutable JSON value + * @param path JSON Pointer path to array + * @param error Error buffer (optional) + * @param error_size Error buffer size + * @return true on success, false on error + */ + virtual bool PtrAddNull(JsonValue* handle, const char* path, + char* error = nullptr, size_t error_size = 0) = 0; + + /** + * Remove value using JSON Pointer (mutable only) + * @param handle Mutable JSON value + * @param path JSON Pointer path + * @param error Error buffer (optional) + * @param error_size Error buffer size + * @return true on success, false on error + */ + virtual bool PtrRemove(JsonValue* handle, const char* path, + char* error = nullptr, size_t error_size = 0) = 0; + + /** + * Try to get value using JSON Pointer (no error on failure) + * @param handle JSON value + * @param path JSON Pointer path + * @return JSON value or nullptr if not found + */ + virtual JsonValue* PtrTryGet(JsonValue* handle, const char* path) = 0; + + /** + * Try to get boolean value using JSON Pointer (returns false on failure) + * @param handle JSON value + * @param path JSON Pointer path + * @param out_value Pointer to receive boolean value + * @return true on success, false if not found or type mismatch + */ + virtual bool PtrTryGetBool(JsonValue* handle, const char* path, bool* out_value) = 0; + + /** + * Try to get double value using JSON Pointer (returns false on failure) + * @param handle JSON value + * @param path JSON Pointer path + * @param out_value Pointer to receive double value + * @return true on success, false if not found or type mismatch + * @note Integers values are auto converted to double + */ + virtual bool PtrTryGetDouble(JsonValue* handle, const char* path, double* out_value) = 0; + + /** + * Try to get integer value using JSON Pointer (returns false on failure) + * @param handle JSON value + * @param path JSON Pointer path + * @param out_value Pointer to receive integer value + * @return true on success, false if not found or type mismatch + */ + virtual bool PtrTryGetInt(JsonValue* handle, const char* path, int* out_value) = 0; + + /** + * 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 (std::variant) + * @return true on success, false if not found or type mismatch + */ + 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) + * @param handle JSON value + * @param path JSON Pointer path + * @param out_str Pointer to receive string pointer + * @param out_len Pointer to receive string length + * @return true on success, false if not found or type mismatch + */ + 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 JsonValue object + // Call these functions in a loop until they return false + + /** + * Get next key-value pair from object iterator + * @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 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(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 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(JsonValue* handle, size_t* out_index, + JsonValue** out_value) = 0; + + /** + * Get next key from object iterator (key only, no value) + * @param handle JSON object + * @param out_key Pointer to receive key string + * @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(JsonValue* handle, const char** out_key, + size_t* out_key_len) = 0; + + /** + * Get next index from array iterator (index only, no value) + * @param handle JSON array + * @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(JsonValue* handle, size_t* out_index) = 0; + + /** + * Release a JsonValue object + * External extensions should use this instead of deleting directly + * @param value The JsonValue to release + */ + virtual void Release(JsonValue* value) = 0; + + /** + * Get the HandleType_t for JSON handles + * External extensions MUST use this method to obtain the handle type + * @return The HandleType_t for JSON handles + */ + virtual HandleType_t GetJsonHandleType() = 0; + + /** + * Read JsonValue from a SourceMod handle + * @param pContext Plugin context + * @param handle Handle to read from + * @return JsonValue pointer, or nullptr on error (error will be reported to context) + */ + virtual JsonValue* GetValueFromHandle(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 + * @note Caller must release the iterator using ReleaseArrIter() once finished + * @note Iterators are single-pass; once ArrIterNext() returns nullptr, create a new iterator or call ArrIterReset() to iterate again + */ + virtual JsonArrIter* ArrIterInit(JsonValue* handle) = 0; + + /** + * Create an array iterator with an array + * @param handle JSON array value + * @return New array iterator or nullptr on error + * @note Caller must release the iterator using ReleaseArrIter() once finished + * @note Iterators are single-pass; once ArrIterNext() returns nullptr, create a new iterator or call ArrIterReset() to iterate again + */ + virtual JsonArrIter* ArrIterWith(JsonValue* handle) = 0; + + /** + * Reset an array iterator to the beginning + * @param iter Array iterator + * @return true on success, false if iterator is invalid or reset failed + */ + virtual bool ArrIterReset(JsonArrIter* iter) = 0; + + /** + * Get next element from array iterator + * @param iter Array iterator + * @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 + * @note Caller must release the iterator using ReleaseObjIter() once finished + * @note Iterators are single-pass; once ObjIterNext() returns nullptr, create a new iterator or call ObjIterReset() to iterate again + */ + virtual JsonObjIter* ObjIterInit(JsonValue* handle) = 0; + + /** + * Create an object iterator with an object + * @param handle JSON object value + * @return New object iterator or nullptr on error + * @note Caller must release the iterator using ReleaseObjIter() once finished + * @note Iterators are single-pass; once ObjIterNext() returns nullptr, create a new iterator or call ObjIterReset() to iterate again + */ + virtual JsonObjIter* ObjIterWith(JsonValue* handle) = 0; + + /** + * Reset an object iterator to the beginning + * @param iter Object iterator + * @return true on success, false if iterator is invalid or reset failed + */ + virtual bool ObjIterReset(JsonObjIter* iter) = 0; + + /** + * Get next key from object iterator + * @param iter Object iterator + * @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; + + /** + * Get key string from object iterator key pointer + * @param iter Object iterator + * @param key Key pointer (returned from ObjIterNext) + * @param out_str Pointer to receive key string + * @param out_len Pointer to receive key length (optional) + * @return true on success, false on error + * @note Do not free the returned string - it is owned by the JSON document + */ + virtual bool ObjIterGetKeyString(JsonObjIter* iter, void* key, const char** out_str, size_t* out_len = nullptr) = 0; + + /** + * Release an array iterator + * @param iter Iterator to release + */ + 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) + * @note This affects how the number is serialized in all write operations + */ + virtual bool SetFpToFloat(JsonValue* handle, bool flt) = 0; + + /** + * Set floating-point number's output format to fixed-point notation + * @param handle JSON floating-point number value + * @param prec Precision (1-15), similar to ECMAScript `Number.prototype.toFixed(prec)` but with trailing zeros removed + * @return true on success, false if handle is not a floating-point number or prec is out of range + * @note Only works on floating-point numbers (not integers) + * @note This will produce shorter output but may lose some precision + * @note This affects how the number is serialized in all write operations + */ + virtual bool SetFpToFixed(JsonValue* handle, int prec) = 0; + + /** + * 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 Double value + * @return true on success, false if handle is object or array + * @warning For immutable documents, this breaks immutability. Use with caution. + * @note This modifies the value in-place without creating a new value + */ + virtual bool SetDouble(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_IJSONMANAGER_H_ \ No newline at end of file diff --git a/public/IYYJSONManager.h b/public/IYYJSONManager.h deleted file mode 100644 index ff5172e..0000000 --- a/public/IYYJSONManager.h +++ /dev/null @@ -1,1360 +0,0 @@ -#ifndef _INCLUDE_SM_YYJSON_IYYJSONMANAGER_H_ -#define _INCLUDE_SM_YYJSON_IYYJSONMANAGER_H_ - -#include - -using SourceMod::Handle_t; -using SourceMod::HandleType_t; -using SourceMod::SMInterface; -using SourcePawn::IPluginContext; - -// Forward declaration -class YYJSONValue; - -#define SMINTERFACE_YYJSONMANAGER_NAME "IYYJSONManager" -#define SMINTERFACE_YYJSONMANAGER_VERSION 1 -#define YYJSON_PACK_ERROR_SIZE 256 -#define YYJSON_ERROR_BUFFER_SIZE 256 - -/** - * @brief JSON sorting order - */ -enum YYJSON_SORT_ORDER -{ - YYJSON_SORT_ASC = 0, // Ascending order - YYJSON_SORT_DESC = 1, // Descending order - YYJSON_SORT_RANDOM = 2 // Random order -}; - -/** - * @brief Parameter provider interface for Pack operation - * - * Allows Pack to retrieve parameters in a platform-independent way. - */ -class IPackParamProvider -{ -public: - virtual ~IPackParamProvider() = default; - - virtual bool GetNextString(const char** out_str) = 0; - virtual bool GetNextInt(int* out_value) = 0; - virtual bool GetNextFloat(float* out_value) = 0; - virtual bool GetNextBool(bool* out_value) = 0; -}; - -/** - * @brief YYJSON 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; - * - * bool YourExtension::SDK_OnAllLoaded() - * { - * SM_GET_LATE_IFACE(YYJSONMANAGER, g_pYYJSONManager); - * } - */ -class IYYJSONManager : public SMInterface -{ -public: - virtual const char *GetInterfaceName() override { - return SMINTERFACE_YYJSONMANAGER_NAME; - } - - virtual unsigned int GetInterfaceVersion() override { - return SMINTERFACE_YYJSONMANAGER_VERSION; - } - -public: - /** - * Parse JSON from string or file - * @param json_str JSON string or file path - * @param is_file true if json_str is a file path - * @param is_mutable true to create mutable document (default: false) - * @param read_flg Read flags (YYJSON_READ_FLAG values, default: 0) - * @param error Error buffer (optional) - * @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, - uint32_t read_flg = 0, char* error = nullptr, size_t error_size = 0) = 0; - - /** - * Write JSON to string - * @param handle JSON value - * @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 - * @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, - uint32_t write_flg = 0, size_t* out_size = nullptr) = 0; - - /** - * Write JSON to file - * @param handle JSON value - * @param path File path - * @param write_flg Write flags (YYJSON_WRITE_FLAG values, default: 0) - * @param error Error buffer (optional) - * @param error_size Error buffer size - * @return true on success - */ - virtual bool WriteToFile(YYJSONValue* handle, const char* path, uint32_t write_flg = 0, - char* error = nullptr, size_t error_size = 0) = 0; - - /** - * Compare two JSON values for equality - * @param handle1 First JSON value to compare - * @param handle2 Second JSON value to compare - * @return true if values are equal, false otherwise - * @note Compares structure and content recursively - */ - virtual bool Equals(YYJSONValue* handle1, YYJSONValue* handle2) = 0; - - /** - * Deep copy a JSON value into a target document - * @param targetDoc Target document that will own the copied value - * @param sourceValue Source value to copy - * @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; - - /** - * 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; - - /** - * 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 - * different sizes and may cause buffer overflow. - * - * @example - * // Correct usage: - * auto flags = YYJSON_WRITE_PRETTY; - * size_t size = g_pYYJSONManager->GetSerializedSize(handle, flags); - * char* buffer = new char[size]; - * g_pYYJSONManager->WriteToString(handle, buffer, size, flags); // Use same flags - */ - virtual size_t GetSerializedSize(YYJSONValue* handle, uint32_t write_flg = 0) = 0; - - /** - * Convert immutable document to mutable - * @param handle Immutable JSON value - * @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; - - /** - * Convert mutable document to immutable - * @param handle Mutable JSON value - * @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; - - /** - * Get JSON type - * @param handle JSON value - * @return YYJSON_TYPE value - */ - virtual uint8_t GetType(YYJSONValue* handle) = 0; - - /** - * Get JSON subtype - * @param handle JSON value - * @return YYJSON_SUBTYPE value - */ - virtual uint8_t GetSubtype(YYJSONValue* handle) = 0; - - // Type checking methods - - /** - * Check if value is an array - * @param handle JSON value - * @return true if value is an array - */ - virtual bool IsArray(YYJSONValue* 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; - - /** - * 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; - - /** - * 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; - - /** - * 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; - - /** - * 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; - - /** - * 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; - - /** - * Check if value is boolean true - * @param handle JSON value - * @return true if value is boolean true - */ - virtual bool IsTrue(YYJSONValue* 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; - - /** - * 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; - - /** - * Check if value is a string - * @param handle JSON value - * @return true if value is a string - */ - virtual bool IsStr(YYJSONValue* handle) = 0; - - /** - * Check if value is null - * @param handle JSON value - * @return true if value is null - */ - virtual bool IsNull(YYJSONValue* 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; - - /** - * Check if document is mutable - * @param handle JSON value - * @return true if document is mutable - */ - virtual bool IsMutable(YYJSONValue* handle) = 0; - - /** - * Check if document is immutable - * @param handle JSON value - * @return true if document is immutable - */ - virtual bool IsImmutable(YYJSONValue* 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 - * - * @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 - */ - virtual size_t GetReadSize(YYJSONValue* handle) = 0; - - /** - * Create an empty mutable JSON object - * @return New mutable JSON object or nullptr on failure - */ - virtual YYJSONValue* ObjectInit() = 0; - - /** - * Create a JSON object from key-value string pairs - * @param pairs Array of strings [key1, val1, key2, val2, ...] - * @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; - - /** - * Parse a JSON object from string - * @param str JSON string to parse - * @param read_flg Read flags (YYJSON_READ_FLAG values, default: 0) - * @param error Error buffer (optional) - * @param error_size Error buffer size - * @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, - char* error = nullptr, size_t error_size = 0) = 0; - - /** - * Parse a JSON object from file - * @param path File path - * @param read_flg Read flags (YYJSON_READ_FLAG values, default: 0) - * @param error Error buffer (optional) - * @param error_size Error buffer size - * @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, - char* error = nullptr, size_t error_size = 0) = 0; - - /** - * Get number of key-value pairs in object - * @param handle JSON object - * @return Number of key-value pairs - */ - virtual size_t ObjectGetSize(YYJSONValue* handle) = 0; - - /** - * Get key name at specific index - * @param handle JSON object - * @param index Index of key-value pair - * @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; - - /** - * Get value at specific index - * @param handle JSON object - * @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; - - /** - * Get value by key name - * @param handle JSON object - * @param key Key name - * @return JSON value or nullptr if key not found - */ - virtual YYJSONValue* ObjectGet(YYJSONValue* handle, const char* key) = 0; - - /** - * Get boolean value by key - * @param handle JSON object - * @param key Key name - * @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; - - /** - * Get float value by key - * @param handle JSON object - * @param key Key name - * @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; - - /** - * Get integer value by key - * @param handle JSON object - * @param key Key name - * @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; - - /** - * Get 64-bit integer value by key - * @param handle JSON object - * @param key Key name - * @param out_value Pointer to receive 64-bit integer value - * @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; - - /** - * Get string value by key - * @param handle JSON object - * @param key Key name - * @param out_str Pointer to receive string pointer - * @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; - - /** - * Check if value at key is null - * @param handle JSON object - * @param key Key name - * @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; - - /** - * Check if object has a specific key - * @param handle JSON object - * @param key Key name (or JSON pointer if use_pointer is true) - * @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; - - /** - * Rename a key in the object - * @param handle Mutable JSON object - * @param old_key Current key name - * @param new_key New key name - * @param allow_duplicate Allow duplicate key names - * @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; - - /** - * Set value by key (mutable only) - * @param handle Mutable JSON object - * @param key Key name - * @param value JSON value to set - * @return true on success - */ - virtual bool ObjectSet(YYJSONValue* handle, const char* key, YYJSONValue* value) = 0; - - /** - * Set boolean value by key (mutable only) - * @param handle Mutable JSON object - * @param key Key name - * @param value Boolean value - * @return true on success - */ - virtual bool ObjectSetBool(YYJSONValue* handle, const char* key, bool value) = 0; - - /** - * Set float value by key (mutable only) - * @param handle Mutable JSON object - * @param key Key name - * @param value Float value - * @return true on success - */ - virtual bool ObjectSetFloat(YYJSONValue* handle, const char* key, double value) = 0; - - /** - * Set integer value by key (mutable only) - * @param handle Mutable JSON object - * @param key Key name - * @param value Integer value - * @return true on success - */ - virtual bool ObjectSetInt(YYJSONValue* handle, const char* key, int value) = 0; - - /** - * Set 64-bit integer value by key (mutable only) - * @param handle Mutable JSON object - * @param key Key name - * @param value 64-bit integer value - * @return true on success - */ - virtual bool ObjectSetInt64(YYJSONValue* handle, const char* key, int64_t value) = 0; - - /** - * Set null value by key (mutable only) - * @param handle Mutable JSON object - * @param key Key name - * @return true on success - */ - virtual bool ObjectSetNull(YYJSONValue* handle, const char* key) = 0; - - /** - * Set string value by key (mutable only) - * @param handle Mutable JSON object - * @param key Key name - * @param value String value - * @return true on success - */ - virtual bool ObjectSetString(YYJSONValue* handle, const char* key, const char* value) = 0; - - /** - * Remove key-value pair by key (mutable only) - * @param handle Mutable JSON object - * @param key Key name - * @return true on success - */ - virtual bool ObjectRemove(YYJSONValue* 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; - - /** - * Sort object keys - * @param handle Mutable JSON object - * @param sort_mode Sort order (YYJSON_SORT_ASC, YYJSON_SORT_DESC, or YYJSON_SORT_RANDOM) - * @return true on success - * @note Only works on mutable objects - */ - virtual bool ObjectSort(YYJSONValue* handle, YYJSON_SORT_ORDER sort_mode) = 0; - - /** - * Create an empty mutable JSON array - * @return New mutable JSON array or nullptr on failure - */ - virtual YYJSONValue* ArrayInit() = 0; - - /** - * Create a JSON array from string values - * @param strings Array of string values - * @param count Number of strings - * @return New JSON array or nullptr on failure - */ - virtual YYJSONValue* ArrayInitWithStrings(const char** strings, size_t count) = 0; - - /** - * Parse a JSON array from string - * @param str JSON string to parse - * @param read_flg Read flags (YYJSON_READ_FLAG values, default: 0) - * @param error Error buffer (optional) - * @param error_size Error buffer size - * @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, - char* error = nullptr, size_t error_size = 0) = 0; - - /** - * Parse a JSON array from file - * @param path File path - * @param read_flg Read flags (YYJSON_READ_FLAG values, default: 0) - * @param error Error buffer (optional) - * @param error_size Error buffer size - * @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, - char* error = nullptr, size_t error_size = 0) = 0; - - /** - * Get number of elements in array - * @param handle JSON array - * @return Number of elements - */ - virtual size_t ArrayGetSize(YYJSONValue* handle) = 0; - - /** - * Get element at specific index - * @param handle JSON array - * @param index Element index - * @return JSON value or nullptr if index out of bounds - */ - virtual YYJSONValue* ArrayGet(YYJSONValue* 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; - - /** - * 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; - - /** - * Get boolean value at index - * @param handle JSON array - * @param index Element index - * @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; - - /** - * Get float value at index - * @param handle JSON array - * @param index Element index - * @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; - - /** - * Get integer value at index - * @param handle JSON array - * @param index Element index - * @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; - - /** - * Get 64-bit integer value at index - * @param handle JSON array - * @param index Element index - * @param out_value Pointer to receive 64-bit integer value - * @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; - - /** - * Get string value at index - * @param handle JSON array - * @param index Element index - * @param out_str Pointer to receive string pointer - * @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; - - /** - * Check if element at index is null - * @param handle JSON array - * @param index Element index - * @return true if element is null - */ - virtual bool ArrayIsNull(YYJSONValue* handle, size_t index) = 0; - - /** - * Replace element at index with JSON value (mutable only) - * @param handle Mutable JSON array - * @param index Element index - * @param value JSON value to set - * @return true on success - */ - virtual bool ArrayReplace(YYJSONValue* handle, size_t index, YYJSONValue* value) = 0; - - /** - * Replace element at index with boolean (mutable only) - * @param handle Mutable JSON array - * @param index Element index - * @param value Boolean value - * @return true on success - */ - virtual bool ArrayReplaceBool(YYJSONValue* handle, size_t index, bool value) = 0; - - /** - * Replace element at index with float (mutable only) - * @param handle Mutable JSON array - * @param index Element index - * @param value Float value - * @return true on success - */ - virtual bool ArrayReplaceFloat(YYJSONValue* handle, size_t index, double value) = 0; - - /** - * Replace element at index with integer (mutable only) - * @param handle Mutable JSON array - * @param index Element index - * @param value Integer value - * @return true on success - */ - virtual bool ArrayReplaceInt(YYJSONValue* handle, size_t index, int value) = 0; - - /** - * Replace element at index with 64-bit integer (mutable only) - * @param handle Mutable JSON array - * @param index Element index - * @param value 64-bit integer value - * @return true on success - */ - virtual bool ArrayReplaceInt64(YYJSONValue* handle, size_t index, int64_t value) = 0; - - /** - * Replace element at index with null (mutable only) - * @param handle Mutable JSON array - * @param index Element index - * @return true on success - */ - virtual bool ArrayReplaceNull(YYJSONValue* handle, size_t index) = 0; - - /** - * Replace element at index with string (mutable only) - * @param handle Mutable JSON array - * @param index Element index - * @param value String value - * @return true on success - */ - virtual bool ArrayReplaceString(YYJSONValue* handle, size_t index, const char* value) = 0; - - /** - * Append JSON value to end of array (mutable only) - * @param handle Mutable JSON array - * @param value JSON value to append - * @return true on success - */ - virtual bool ArrayAppend(YYJSONValue* handle, YYJSONValue* value) = 0; - - /** - * Append boolean to end of array (mutable only) - * @param handle Mutable JSON array - * @param value Boolean value - * @return true on success - */ - virtual bool ArrayAppendBool(YYJSONValue* handle, bool value) = 0; - - /** - * Append float to end of array (mutable only) - * @param handle Mutable JSON array - * @param value Float value - * @return true on success - */ - virtual bool ArrayAppendFloat(YYJSONValue* handle, double value) = 0; - - /** - * Append integer to end of array (mutable only) - * @param handle Mutable JSON array - * @param value Integer value - * @return true on success - */ - virtual bool ArrayAppendInt(YYJSONValue* handle, int value) = 0; - - /** - * Append 64-bit integer to end of array (mutable only) - * @param handle Mutable JSON array - * @param value 64-bit integer value - * @return true on success - */ - virtual bool ArrayAppendInt64(YYJSONValue* handle, int64_t 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; - - /** - * Append string to end of array (mutable only) - * @param handle Mutable JSON array - * @param value String value - * @return true on success - */ - virtual bool ArrayAppendString(YYJSONValue* handle, const char* value) = 0; - - /** - * Remove element at specific index (mutable only) - * @param handle Mutable JSON array - * @param index Element index - * @return true on success - */ - virtual bool ArrayRemove(YYJSONValue* 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; - - /** - * Remove last element (mutable only) - * @param handle Mutable JSON array - * @return true on success - */ - virtual bool ArrayRemoveLast(YYJSONValue* handle) = 0; - - /** - * Remove range of elements (mutable only) - * @param handle JSON array - * @param start_index Start index (inclusive) - * @param end_index End index (exclusive) - * @return true on success - */ - virtual bool ArrayRemoveRange(YYJSONValue* 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; - - /** - * Find index of boolean value - * @param handle JSON array - * @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; - - /** - * Find index of string value - * @param handle JSON array - * @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; - - /** - * Find index of integer value - * @param handle JSON array - * @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; - - /** - * Find index of 64-bit integer value - * @param handle JSON array - * @param search_value 64-bit integer value to search for - * @return Index of first match, or -1 if not found - */ - virtual int ArrayIndexOfInt64(YYJSONValue* handle, int64_t search_value) = 0; - - /** - * Find index of float value - * @param handle JSON array - * @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; - - /** - * Sort array elements - * @param handle Mutable JSON array - * @param sort_mode Sort order (YYJSON_SORT_ASC, YYJSON_SORT_DESC, or YYJSON_SORT_RANDOM) - * @return true on success - * @note Only works on mutable arrays - */ - virtual bool ArraySort(YYJSONValue* handle, YYJSON_SORT_ORDER sort_mode) = 0; - - /** - * Create JSON value from format string and parameters - * @param format Format string (e.g., "{s:i,s:s}", "[i,s,b]") - * Format specifiers: - * - 's': string - * - 'i': integer - * - 'f': float - * - 'b': boolean - * - 'n': null - * - '{': object start, '}': object end - * - '[': array start, ']': array end - * @param param_provider Parameter provider implementation - * @param error Error buffer (optional) - * @param error_size Error buffer size - * @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, - char* error = nullptr, size_t error_size = 0) = 0; - - /** - * Create a JSON boolean value - * @param value Boolean value - * @return New JSON boolean or nullptr on failure - */ - virtual YYJSONValue* 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; - - /** - * Create a JSON integer value - * @param value Integer value - * @return New JSON integer or nullptr on failure - */ - virtual YYJSONValue* CreateInt(int value) = 0; - - /** - * Create a JSON 64-bit integer value - * @param value 64-bit integer value - * @return New JSON integer64 or nullptr on failure - */ - virtual YYJSONValue* CreateInt64(int64_t value) = 0; - - /** - * Create a JSON null value - * @return New JSON null or nullptr on failure - */ - virtual YYJSONValue* 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; - - /** - * Get boolean value from JSON - * @param handle JSON value - * @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; - - /** - * Get float value from JSON - * @param handle JSON value - * @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; - - /** - * Get integer value from JSON - * @param handle JSON value - * @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; - - /** - * Get 64-bit integer value from JSON - * @param handle JSON value - * @param out_value Pointer to receive 64-bit integer value - * @return true on success, false on type mismatch - */ - virtual bool GetInt64(YYJSONValue* handle, int64_t* out_value) = 0; - - /** - * Get string value from JSON - * @param handle JSON value - * @param out_str Pointer to receive string pointer - * @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; - - /** - * Get value using JSON Pointer - * @param handle JSON value - * @param path JSON Pointer path (e.g., "/users/0/name") - * @param error Error buffer (optional) - * @param error_size Error buffer size - * @return JSON value or nullptr on error - */ - virtual YYJSONValue* PtrGet(YYJSONValue* handle, const char* path, - char* error = nullptr, size_t error_size = 0) = 0; - - /** - * Get boolean value using JSON Pointer - * @param handle JSON value - * @param path JSON Pointer path - * @param out_value Pointer to receive boolean value - * @param error Error buffer (optional) - * @param error_size Error buffer size - * @return true on success, false on error - */ - virtual bool PtrGetBool(YYJSONValue* handle, const char* path, bool* out_value, - char* error = nullptr, size_t error_size = 0) = 0; - - /** - * Get float value using JSON Pointer - * @param handle JSON value - * @param path JSON Pointer path - * @param out_value Pointer to receive float value - * @param error Error buffer (optional) - * @param error_size Error buffer size - * @return true on success, false on error - */ - virtual bool PtrGetFloat(YYJSONValue* handle, const char* path, double* out_value, - char* error = nullptr, size_t error_size = 0) = 0; - - /** - * Get integer value using JSON Pointer - * @param handle JSON value - * @param path JSON Pointer path - * @param out_value Pointer to receive integer value - * @param error Error buffer (optional) - * @param error_size Error buffer size - * @return true on success, false on error - */ - virtual bool PtrGetInt(YYJSONValue* handle, const char* path, int* out_value, - char* error = nullptr, size_t error_size = 0) = 0; - - /** - * Get 64-bit integer value using JSON Pointer - * @param handle JSON value - * @param path JSON Pointer path - * @param out_value Pointer to receive 64-bit integer value - * @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, - char* error = nullptr, size_t error_size = 0) = 0; - - /** - * Get string value using JSON Pointer - * @param handle JSON value - * @param path JSON Pointer path - * @param out_str Pointer to receive string pointer - * @param out_len Pointer to receive string length - * @param error Error buffer (optional) - * @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, - size_t* out_len, char* error = nullptr, size_t error_size = 0) = 0; - - /** - * Check if value is null using JSON Pointer - * @param handle JSON value - * @param path JSON Pointer path - * @param out_is_null Pointer to receive result - * @param error Error buffer (optional) - * @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, - char* error = nullptr, size_t error_size = 0) = 0; - - /** - * Get length of container (array/object) using JSON Pointer - * @param handle JSON value - * @param path JSON Pointer path - * @param out_len Pointer to receive length - * @param error Error buffer (optional) - * @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, - char* error = nullptr, size_t error_size = 0) = 0; - - /** - * Set value using JSON Pointer (mutable only) - * @param handle Mutable JSON value - * @param path JSON Pointer path - * @param value JSON value to set - * @param error Error buffer (optional) - * @param error_size Error buffer size - * @return true on success, false on error - */ - virtual bool PtrSet(YYJSONValue* handle, const char* path, YYJSONValue* value, - char* error = nullptr, size_t error_size = 0) = 0; - - /** - * Set boolean value using JSON Pointer (mutable only) - * @param handle Mutable JSON value - * @param path JSON Pointer path - * @param value Boolean value - * @param error Error buffer (optional) - * @param error_size Error buffer size - * @return true on success, false on error - */ - virtual bool PtrSetBool(YYJSONValue* handle, const char* path, bool value, - char* error = nullptr, size_t error_size = 0) = 0; - - /** - * Set float value using JSON Pointer (mutable only) - * @param handle Mutable JSON value - * @param path JSON Pointer path - * @param value Float value - * @param error Error buffer (optional) - * @param error_size Error buffer size - * @return true on success, false on error - */ - virtual bool PtrSetFloat(YYJSONValue* handle, const char* path, double value, - char* error = nullptr, size_t error_size = 0) = 0; - - /** - * Set integer value using JSON Pointer (mutable only) - * @param handle Mutable JSON value - * @param path JSON Pointer path - * @param value Integer value - * @param error Error buffer (optional) - * @param error_size Error buffer size - * @return true on success, false on error - */ - virtual bool PtrSetInt(YYJSONValue* 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) - * @param handle Mutable JSON value - * @param path JSON Pointer path - * @param value 64-bit integer value - * @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, - char* error = nullptr, size_t error_size = 0) = 0; - - /** - * Set string value using JSON Pointer (mutable only) - * @param handle Mutable JSON value - * @param path JSON Pointer path - * @param value String value - * @param error Error buffer (optional) - * @param error_size Error buffer size - * @return true on success, false on error - */ - virtual bool PtrSetString(YYJSONValue* handle, const char* path, const char* value, - char* error = nullptr, size_t error_size = 0) = 0; - - /** - * Set null value using JSON Pointer (mutable only) - * @param handle Mutable JSON value - * @param path JSON Pointer path - * @param error Error buffer (optional) - * @param error_size Error buffer size - * @return true on success, false on error - */ - virtual bool PtrSetNull(YYJSONValue* handle, const char* path, - char* error = nullptr, size_t error_size = 0) = 0; - - /** - * Add value to array using JSON Pointer (mutable only) - * @param handle Mutable JSON value - * @param path JSON Pointer path to array - * @param value JSON value to add - * @param error Error buffer (optional) - * @param error_size Error buffer size - * @return true on success, false on error - */ - virtual bool PtrAdd(YYJSONValue* handle, const char* path, YYJSONValue* value, - char* error = nullptr, size_t error_size = 0) = 0; - - /** - * Add boolean to array using JSON Pointer (mutable only) - * @param handle Mutable JSON value - * @param path JSON Pointer path to array - * @param value Boolean value - * @param error Error buffer (optional) - * @param error_size Error buffer size - * @return true on success, false on error - */ - virtual bool PtrAddBool(YYJSONValue* handle, const char* path, bool value, - char* error = nullptr, size_t error_size = 0) = 0; - - /** - * Add float to array using JSON Pointer (mutable only) - * @param handle Mutable JSON value - * @param path JSON Pointer path to array - * @param value Float value - * @param error Error buffer (optional) - * @param error_size Error buffer size - * @return true on success, false on error - */ - virtual bool PtrAddFloat(YYJSONValue* handle, const char* path, double value, - char* error = nullptr, size_t error_size = 0) = 0; - - /** - * Add integer to array using JSON Pointer (mutable only) - * @param handle Mutable JSON value - * @param path JSON Pointer path to array - * @param value Integer value - * @param error Error buffer (optional) - * @param error_size Error buffer size - * @return true on success, false on error - */ - virtual bool PtrAddInt(YYJSONValue* 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) - * @param handle Mutable JSON value - * @param path JSON Pointer path to array - * @param value 64-bit integer value - * @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, - char* error = nullptr, size_t error_size = 0) = 0; - - /** - * Add string to array using JSON Pointer (mutable only) - * @param handle Mutable JSON value - * @param path JSON Pointer path to array - * @param value String value - * @param error Error buffer (optional) - * @param error_size Error buffer size - * @return true on success, false on error - */ - virtual bool PtrAddString(YYJSONValue* handle, const char* path, const char* value, - char* error = nullptr, size_t error_size = 0) = 0; - - /** - * Add null to array using JSON Pointer (mutable only) - * @param handle Mutable JSON value - * @param path JSON Pointer path to array - * @param error Error buffer (optional) - * @param error_size Error buffer size - * @return true on success, false on error - */ - virtual bool PtrAddNull(YYJSONValue* handle, const char* path, - char* error = nullptr, size_t error_size = 0) = 0; - - /** - * Remove value using JSON Pointer (mutable only) - * @param handle Mutable JSON value - * @param path JSON Pointer path - * @param error Error buffer (optional) - * @param error_size Error buffer size - * @return true on success, false on error - */ - virtual bool PtrRemove(YYJSONValue* handle, const char* path, - char* error = nullptr, size_t error_size = 0) = 0; - - /** - * Try to get value using JSON Pointer (no error on failure) - * @param handle JSON value - * @param path JSON Pointer path - * @return JSON value or nullptr if not found - */ - virtual YYJSONValue* PtrTryGet(YYJSONValue* handle, const char* path) = 0; - - /** - * Try to get boolean value using JSON Pointer (returns false on failure) - * @param handle JSON value - * @param path JSON Pointer path - * @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; - - /** - * Try to get float value using JSON Pointer (returns false on failure) - * @param handle JSON value - * @param path JSON Pointer path - * @param out_value Pointer to receive float value - * @return true on success, false if not found or type mismatch - */ - virtual bool PtrTryGetFloat(YYJSONValue* handle, const char* path, double* out_value) = 0; - - /** - * Try to get integer value using JSON Pointer (returns false on failure) - * @param handle JSON value - * @param path JSON Pointer path - * @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; - - /** - * Try to get 64-bit integer value using JSON Pointer (returns false on failure) - * @param handle JSON value - * @param path JSON Pointer path - * @param out_value Pointer to receive 64-bit integer value - * @return true on success, false if not found or type mismatch - */ - virtual bool PtrTryGetInt64(YYJSONValue* handle, const char* path, int64_t* out_value) = 0; - - /** - * Try to get string value using JSON Pointer (returns false on failure) - * @param handle JSON value - * @param path JSON Pointer path - * @param out_str Pointer to receive string pointer - * @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; - - // Note: Iterators are stateful and stored in the YYJSONValue object - // Call these functions in a loop until they return false - - /** - * Get next key-value pair from object iterator - * @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) - * @return true if iteration continues, false if iteration complete - * @note Iterator state is maintained in handle. Returns false when iteration completes. - */ - virtual bool ObjectForeachNext(YYJSONValue* handle, const char** out_key, - size_t* out_key_len, YYJSONValue** 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) - * @return true if iteration continues, false if iteration complete - * @note Iterator state is maintained in handle. Returns false when iteration completes. - */ - virtual bool ArrayForeachNext(YYJSONValue* handle, size_t* out_index, - YYJSONValue** out_value) = 0; - - /** - * Get next key from object iterator (key only, no value) - * @param handle JSON object - * @param out_key Pointer to receive key string - * @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. - */ - virtual bool ObjectForeachKeyNext(YYJSONValue* handle, const char** out_key, - size_t* out_key_len) = 0; - - /** - * Get next index from array iterator (index only, no value) - * @param handle JSON array - * @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. - */ - virtual bool ArrayForeachIndexNext(YYJSONValue* handle, size_t* out_index) = 0; - - /** - * Release a YYJSONValue object - * External extensions should use this instead of deleting directly - * @param value The YYJSONValue to release - */ - virtual void Release(YYJSONValue* value) = 0; - - /** - * Get the HandleType_t for YYJSON handles - * External extensions MUST use this method to obtain the handle type - * @return The HandleType_t for YYJSON handles - */ - virtual HandleType_t GetHandleType() = 0; - - /** - * Read YYJSONValue 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) - */ - virtual YYJSONValue* GetFromHandle(IPluginContext* pContext, Handle_t handle) = 0; -}; - -#endif // _INCLUDE_SM_YYJSON_IYYJSONMANAGER_H_ \ No newline at end of file diff --git a/pushbuild.txt b/pushbuild.txt deleted file mode 100755 index 68709e7..0000000 --- a/pushbuild.txt +++ /dev/null @@ -1,7 +0,0 @@ -some meaningless str -8c68c9448936fdfc437994f46753cdda63318509 -f30621f6c6bb134dcba0812438f47571f7bb7aee -597c28947ff356399c998661f25e44b2 -a1b8e64c0495b82ec41f8f020d3e3cd1 -e33332b9577da63d2ba281a660101493 -560bcf4dc0dd6e3debddad339bf013e6a7d41584 \ No newline at end of file diff --git a/scripting/include/json.inc b/scripting/include/json.inc new file mode 100755 index 0000000..bbf2bc8 --- /dev/null +++ b/scripting/include/json.inc @@ -0,0 +1,2389 @@ +#if defined _json_included + #endinput +#endif +#define _json_included + +// JSON value types +enum JSON_TYPE +{ + 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 JSON_SUBTYPE +{ + 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 JSON_READ_FLAG +{ + 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 JSON_WRITE_FLAG +{ + 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 + * + * @param n Precision digits (1-7) + * @return JSON write flag with precision setting + * @note This will produce shorter output but may lose some precision + */ +stock JSON_WRITE_FLAG JSON_WRITE_FP_TO_FIXED(int n) +{ + n = (n < 1) ? 1 : (n > 7 ? 7 : n); + return view_as(n << 28); +} + +// Sort order for arrays and objects +enum JSON_SORT_ORDER +{ + JSON_SORT_ASC = 0, // Ascending order (default) + JSON_SORT_DESC = 1, // Descending order + JSON_SORT_RANDOM = 2 // Random order +} + +methodmap JSON < Handle +{ + /** + * Creates a JSON value using a format string and arguments + * Format specifiers: + * - s: string + * - i: integer + * - f: float + * - b: boolean + * - n: null + * - {: start object + * - }: end object + * - [: start array + * - ]: end array + * + * @note Needs to be freed using delete or CloseHandle() + * @note SourcePawn imposes a limit of 32 parameters per function (defined by SP_MAX_EXEC_PARAMS), + * which precludes the construction of complex objects with this method. + * + * @param format Format string + * @param ... Arguments based on format string + * + * @return JSON handle + * @error If format string is invalid or arguments don't match + */ + public static native any Pack(const char[] format, any ...); + + /** + * 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 + * @param maxlength Maximum length of the string buffer + * @param value JSON handle to store the current value + * + * @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 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) + * @param value JSON handle to store the current value + * + * @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 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 + * @param maxlength Maximum length of the string buffer + * + * @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) + * + * @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); + + /** + * Converts an immutable JSON document to a mutable one + * + * @note Needs to be freed using delete or CloseHandle() + * + * @return Handle to the new mutable JSON document, INVALID_HANDLE on failure + * @error If the document is already mutable + */ + public native any ToMutable(); + + /** + * Converts a mutable JSON document to an immutable one + * + * @note Needs to be freed using delete or CloseHandle() + * + * @return Handle to the new immutable JSON document, INVALID_HANDLE on failure + * @error If the document is already immutable + */ + public native any ToImmutable(); + + /** + * Apply a JSON Patch (RFC 6902) to this value and return a new JSON handle + * + * @note Needs to be freed using delete or CloseHandle() + * + * @param patch JSON Patch document + * @param resultMutable True to return a mutable result, false for immutable + * + * @return New JSON handle on success + * @error Throws if the patch cannot be applied + */ + public native any ApplyJsonPatch(const JSON patch, bool resultMutable = false); + + /** + * Apply a JSON Patch (RFC 6902) to this value in place + * + * @param patch JSON Patch document + * + * @return True on success, false otherwise + * @error Throws if this value is immutable or patch failed + */ + public native bool JsonPatchInPlace(const JSON patch); + + /** + * Apply a JSON Merge Patch (RFC 7396) to this value and return a new JSON handle + * + * @note Needs to be freed using delete or CloseHandle() + * + * @param patch JSON Merge Patch document + * @param resultMutable True to return a mutable result, false for immutable + * + * @return New JSON handle on success + * @error Throws if the merge patch cannot be applied + */ + public native any ApplyMergePatch(const JSON patch, bool resultMutable = false); + + /** + * Apply a JSON Merge Patch (RFC 7396) to this value in place + * + * @param patch JSON Merge Patch document + * + * @return True on success, false otherwise + * @error Throws if this value is immutable or merge patch failed + */ + public native bool MergePatchInPlace(const JSON patch); + + /** + * Write a document to JSON file with options + * + * @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 flag The JSON write options + * + * @return True on success, false on failure + */ + public native bool ToFile(const char[] file, JSON_WRITE_FLAG flag = JSON_WRITE_NOFLAG); + + /** + * Write a value to JSON string + * + * @param buffer String buffer to write to + * @param maxlength Maximum length of the string buffer + * @param flag The JSON write options + * + * @return Number of characters written to the buffer (including null terminator) or 0 on failure + */ + 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 in all write operations + * + * @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 in all write operations + * + * @param prec Precision (1-7) + * + * @return True on success, false if handle is not a floating-point number or prec is out of range + * @error Invalid handle, handle is not a floating-point number, or precision out of range (1-7) + */ + 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 + * + * @note Needs to be freed using delete or CloseHandle() + * + * @param string String or file to parse + * @param is_file True to treat string param as file, false otherwise + * @param is_mutable_doc True to create a mutable document, false to create an immutable one + * @param flag The JSON read options + * + * @return JSON handle, false on failure + */ + public static native any Parse(const char[] string, bool is_file = false, bool is_mutable_doc = false, JSON_READ_FLAG flag = JSON_READ_NOFLAG); + + /** + * Read a JSON number from string + * + * @note Needs to be freed using delete or CloseHandle() + * @note The input string must be null-terminated UTF-8 without BOM + * @note The returned value is a mutable number value + * + * @param dat The JSON data (UTF-8 without BOM), null-terminator is required + * @param flag Read flags (JSON_READ_FLAG values, default: JSON_READ_NOFLAG) + * @param consumed Variable to store number of characters consumed (optional) + * + * @return New JSON number value or null on error + * @error If input data is invalid or number parsing fails + */ + public static native JSON ReadNumber(const char[] dat, JSON_READ_FLAG flag = JSON_READ_NOFLAG, int &consumed = 0); + + /** + * Creates a deep copy of a JSON value and returns it as a new JSON handle + * + * @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 + * @note The mutability of the returned copy depends on the targetDoc parameter + * + * @param targetDoc The target document that determines whether the copy will be mutable or immutable + * @param sourceValue The source JSON value to be copied + * + * @return New JSON handle on success, false on failure + */ + public static native any DeepCopy(const JSON targetDoc, const JSON sourceValue); + + /** + * Returns the JSON value's type description + * + * @param buffer String buffer to write to + * @param maxlength Maximum length of the string buffer + * + * @return The return value should be one of these strings: "raw", "null", "string", + * "array", "object", "true", "false", "uint", "sint", "real", "unknown" + */ + public native void GetTypeDesc(char[] buffer, int maxlength); + + /** + * Returns whether two JSON values are equal (deep compare) + * + * @note This function is recursive and may cause a stack overflow if the object level is too deep + * @note the result may be inaccurate if object has duplicate keys + * + * @param value1 First JSON value to compare + * @param value2 Second JSON value to compare + * + * @return True if they are the same, false otherwise + */ + 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 + * + * @note Needs to be freed using delete or CloseHandle() + * + * @param value The boolean value to be set + * + * @return JSON handle, null on error + */ + public static native JSON CreateBool(bool value); + + /** + * Creates and returns a float value + * + * @note Needs to be freed using delete or CloseHandle() + * + * @param value The float value to be set + * + * @return JSON handle, null on error + */ + public static native JSON CreateFloat(float value); + + /** + * Creates and returns an int value + * + * @note Needs to be freed using delete or CloseHandle() + * + * @param value The int value to be set + * + * @return JSON handle, null on error + */ + 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 (can be signed or unsigned) + * + * @return JSON handle, null on error + */ + public static native JSON CreateInt64(const char[] value); + + /** + * Creates and returns a string value + * + * @note Needs to be freed using delete or CloseHandle() + * + * @param value The string value to be set + * + * @return JSON handle, null on error + */ + 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 + */ + public static native JSON CreateNull(); + + /** + * Get boolean value + * + * @return Boolean value + */ + public native bool GetBool(); + + /** + * Get float value + * + * @note Integers values are auto converted to float + * + * @return float value + */ + public native float GetFloat(); + + /** + * Get int value + * + * @return int value + */ + public native int GetInt(); + + /** + * 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 buffer Buffer to copy to + * @param maxlength Maximum size of the buffer + * + * @return True on success, false on failure + */ + public native bool GetString(char[] buffer, int maxlength); + + /** + * Get JSON Handle serialized size in bytes (including null-terminator) + * + * @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 + */ + public native int GetSerializedSize(JSON_WRITE_FLAG flag = JSON_WRITE_NOFLAG); + + /** + * Get value by a JSON Pointer + * + * @note Needs to be freed using delete or CloseHandle() + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * + * @param path The JSON pointer string + * + * @return The value referenced by the JSON pointer + */ + public native any PtrGet(const char[] path); + + /** + * Get boolean value by a JSON Pointer + * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * + * @param path The JSON pointer string + * + * @return boolean value referenced by the JSON pointer + */ + public native bool PtrGetBool(const char[] path); + + /** + * Get float value by a JSON Pointer + * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * @note Integers values are auto converted to float + * + * @param path The JSON pointer string + * + * @return float value referenced by the JSON pointer + */ + public native float PtrGetFloat(const char[] path); + + /** + * Get integer value by a JSON Pointer + * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * + * @param path The JSON pointer string + * + * @return integer value referenced by the JSON pointer + */ + public native int PtrGetInt(const char[] path); + + /** + * Get integer64 value by a JSON Pointer (auto-detects signed/unsigned) + * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * + * @param path The JSON pointer string + * @param buffer Buffer to copy to + * @param maxlength Maximum size of the buffer + * + * @return True on success, false on failure + */ + public native bool PtrGetInt64(const char[] path, char[] buffer, int maxlength); + + /** + * Get string value by a JSON Pointer + * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * + * @param path The JSON pointer string + * @param buffer Buffer to copy to + * @param maxlength Maximum size of the buffer + * + * @return True on success, false on failure + */ + public native bool PtrGetString(const char[] path, char[] buffer, int maxlength); + + /** + * Get value is null by a JSON Pointer + * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * + * @param path The JSON pointer string + * + * @return True if the value is null, false otherwise + */ + public native bool PtrGetIsNull(const char[] path); + + /** + * Get JSON content length (string length, array size, object size) + * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * @note For strings: returns string length including null-terminator + * @note For arrays/objects: returns number of elements + * @note Returns 0 if value is null or type is not string/array/object + * + * @param path The JSON pointer string + * + * @return JSON content length + */ + public native int PtrGetLength(const char[] path); + + /** + * Set value by a JSON Pointer + * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * @note The parent nodes will be created if they do not exist. + * If the target value already exists, it will be replaced by the new value + * + * @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, JSON value); + + /** + * Set boolean value by a JSON Pointer + * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * @note The parent nodes will be created if they do not exist. + * If the target value already exists, it will be replaced by the new value + * + * @param path The JSON pointer string + * @param value The boolean value to be set + * + * @return true if JSON pointer is valid and new value is set, false otherwise + */ + public native bool PtrSetBool(const char[] path, bool value); + + /** + * Set float value by a JSON Pointer + * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * @note The parent nodes will be created if they do not exist. + * If the target value already exists, it will be replaced by the new value + * + * @param path The JSON pointer string + * @param value The float value to be set + * + * @return true if JSON pointer is valid and new value is set, false otherwise + */ + public native bool PtrSetFloat(const char[] path, float value); + + /** + * Set integer value by a JSON Pointer + * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * @note The parent nodes will be created if they do not exist. + * If the target value already exists, it will be replaced by the new value + * + * @param path The JSON pointer string + * @param value The integer value to be set + * + * @return true if JSON pointer is valid and new value is set, false otherwise + */ + public native bool PtrSetInt(const char[] path, int value); + + /** + * Set integer64 value by a JSON Pointer + * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * @note The parent nodes will be created if they do not exist. + * If the target value already exists, it will be replaced by the new value + * @note This function auto-detects whether the value is signed or unsigned + * + * @param path The JSON pointer string + * @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 + */ + public native bool PtrSetInt64(const char[] path, const char[] value); + + /** + * Set string value by a JSON Pointer + * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * @note The parent nodes will be created if they do not exist. + * If the target value already exists, it will be replaced by the new value + * + * @param path The JSON pointer string + * @param value The string value to be set + * + * @return true if JSON pointer is valid and new value is set, false otherwise + */ + public native bool PtrSetString(const char[] path, const char[] value); + + /** + * Set null value by a JSON Pointer + * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * @note The parent nodes will be created if they do not exist. + * If the target value already exists, it will be replaced by the new value + * + * @param path The JSON pointer string + * + * @return true if JSON pointer is valid and new value is set, false otherwise + */ + public native bool PtrSetNull(const char[] path); + + /** + * Add (insert) value by a JSON pointer + * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * + * @param path The JSON pointer string + * @param value The value to be added + * + * @return true if JSON pointer is valid and new value is set, false otherwise + */ + public native bool PtrAdd(const char[] path, JSON value); + + /** + * Add (insert) boolean value by a JSON pointer + * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * + * @param path The JSON pointer string + * @param value The boolean value to be added + * + * @return true if JSON pointer is valid and new value is set, false otherwise + */ + public native bool PtrAddBool(const char[] path, bool value); + + /** + * Add (insert) float value by a JSON pointer + * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * + * @param path The JSON pointer string + * @param value The float value to be added + * + * @return true if JSON pointer is valid and new value is set, false otherwise + */ + public native bool PtrAddFloat(const char[] path, float value); + + /** + * Add (insert) integer value by a JSON pointer + * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * + * @param path The JSON pointer string + * @param value The int value to be added + * + * @return true if JSON pointer is valid and new value is set, false otherwise + */ + public native bool PtrAddInt(const char[] path, int value); + + /** + * Add (insert) integer64 value by a JSON pointer + * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * + * @param path The JSON pointer string + * @param value The integer64 value to be added + * + * @return true if JSON pointer is valid and new value is set, false otherwise + */ + public native bool PtrAddInt64(const char[] path, const char[] value); + + /** + * Add (insert) string value by a JSON pointer + * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * + * @param path The JSON pointer string + * @param value The str value to be added + * + * @return true if JSON pointer is valid and new value is set, false otherwise + */ + public native bool PtrAddString(const char[] path, const char[] value); + + /** + * Add (insert) null value by a JSON pointer + * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * + * @param path The JSON pointer string + * + * @return true if JSON pointer is valid and new value is set, false otherwise + */ + public native bool PtrAddNull(const char[] path); + + /** + * Remove value by a JSON pointer + * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * + * @param path The JSON pointer string + * + * @return true if removed value, false otherwise + */ + public native bool PtrRemove(const char[] path); + + /** + * Try to get value by a JSON Pointer + * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * + * @param path The JSON pointer string + * @param value Handle to store value + * + * @return true on success, false otherwise + */ + public native bool PtrTryGetVal(const char[] path, JSON &value); + + /** + * Try to get boolean value by a JSON Pointer + * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * + * @param path The JSON pointer string + * @param value Store the boolean value + * + * @return true on success, false otherwise + */ + public native bool PtrTryGetBool(const char[] path, bool &value); + + /** + * Try to get float value by a JSON Pointer + * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * @note Integers values are auto converted to float + * + * @param path The JSON pointer string + * @param value Store the float value + * + * @return true on success, false otherwise + */ + public native bool PtrTryGetFloat(const char[] path, float &value); + + /** + * Try to get integer value by a JSON Pointer + * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * + * @param path The JSON pointer string + * @param value Store the integer value + * + * @return true on success, false otherwise + */ + public native bool PtrTryGetInt(const char[] path, int &value); + + /** + * Try to get integer64 value by a JSON Pointer (automatically detects signed/unsigned) + * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * + * @param path The JSON pointer string + * @param buffer Buffer to store the integer64 value + * @param maxlength Maximum length of the buffer + * + * @return true on success, false otherwise + */ + public native bool PtrTryGetInt64(const char[] path, char[] buffer, int maxlength); + + /** + * Try to get string value by a JSON Pointer + * + * @note JSON Pointer paths are always resolved from the document root, not from the current value + * + * @param path The JSON pointer string + * @param buffer Buffer to store the string value + * @param maxlength Maximum length of the buffer + * + * @return true on success, false otherwise + */ + public native bool PtrTryGetString(const char[] path, char[] buffer, int maxlength); + + /** + * Retrieves json type + */ + property JSON_TYPE Type { + public native get(); + } + + /** + * Retrieves json subtype + */ + property JSON_SUBTYPE SubType { + public native get(); + } + + /** + * Retrieves json is array + */ + property bool IsArray { + public native get(); + } + + /** + * Retrieves json is object + */ + property bool IsObject { + public native get(); + } + + /** + * Retrieves json is integer (uint64_t/int64_t) + */ + property bool IsInt { + public native get(); + } + + /** + * Retrieves json is unsigned integer (uint64_t) + */ + property bool IsUint { + public native get(); + } + + /** + * Retrieves json is signed integer (int64_t) + */ + property bool IsSint { + public native get(); + } + + /** + * Retrieves json is number (uint64_t/int64_t/double) + */ + property bool IsNum { + public native get(); + } + + /** + * Retrieves json is boolean + */ + property bool IsBool { + public native get(); + } + + /** + * Retrieves json is true + */ + property bool IsTrue { + public native get(); + } + + /** + * Retrieves json is false + */ + property bool IsFalse { + public native get(); + } + + /** + * Retrieves json is float + */ + property bool IsFloat { + public native get(); + } + + /** + * Retrieves json is string + */ + property bool IsStr { + public native get(); + } + + /** + * Retrieves json is null + */ + property bool IsNull { + public native get(); + } + + /** + * Retrieves json is container (array/object) + */ + property bool IsCtn { + public native get(); + } + + /** + * Retrieves json is mutable doc + */ + property bool IsMutable { + public native get(); + } + + /** + * Retrieves json is immutable doc + */ + property bool IsImmutable { + public native get(); + } + + /** + * Retrieves the size of the JSON data as it was originally read from parsing + * + * @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 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 + */ + property int ReadSize { + public native get(); + } + + /** + * Get the reference count of the document (debugging purpose) + * + * @return Reference count (number of JSON objects sharing this document) + * + * @note Returns 0 if the handle is invalid + */ + property int RefCount { + public native get(); + } + + /** + * Get the total number of values in the document + * + * @note Only works on immutable documents (parsed from JSON) + * @note Returns 0 for mutable documents or null handles + * @note Useful for performance planning and memory estimation + */ + property int ValCount { + public native get(); + } +}; + +methodmap JSONObject < JSON +{ + /** + * Creates a JSON object A JSON object maps strings (called "keys") to values Keys in a + * JSON object are unique That is, there is at most one entry in the map for a given key + * + * @note Needs to be freed using delete or CloseHandle() + */ + 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 + * + * @note Needs to be freed using delete or CloseHandle() + * + * @param pairs Array of strings containing alternating keys and values + * @param size Total size of the array (must be even) + * + * @return New JSON object handle + * @error If array size is invalid, any key is empty, or if creation fails + * + */ + public static native JSONObject FromStrings(const char[][] pairs, int size); + + /** + * Loads a JSON object from a file + * + * @note Needs to be freed using delete or CloseHandle() + * + * @param file File to read from + * @param flag The JSON read options + * + * @return Object handle, or null on failure + */ + public static native JSONObject FromFile(const char[] file, JSON_READ_FLAG flag = JSON_READ_NOFLAG); + + /** + * Loads a JSON object from a string + * + * @note Needs to be freed using delete or CloseHandle() + * + * @param buffer String buffer to load into the JSON object + * @param flag The JSON read options + * + * @return Object handle, or null on failure + */ + public static native JSONObject FromString(const char[] buffer, JSON_READ_FLAG flag = JSON_READ_NOFLAG); + + /** + * Gets a value from the object + * + * @note Needs to be freed using delete or CloseHandle() + * @note This function takes a linear search time + * + * @param key Key name + * + * @return Returns the value to which the specified key is mapped, or null if this object contains no mapping for the key + */ + public native any Get(const char[] key); + + /** + * Sets a value in the object + * + * @param key Key name + * @param value JSON handle to set + * + * @return True if succeed, false otherwise + */ + public native bool Set(const char[] key, const JSON value); + + /** + * Gets a boolean value from the object + * + * @param key Key name + * + * @return Boolean value + */ + public native bool GetBool(const char[] key); + + /** + * Gets a float value from the object + * + * @note Integers values are auto converted to float + * + * @param key Key name + * + * @return Float value + */ + public native float GetFloat(const char[] key); + + /** + * Gets a integer value from the object + * + * @param key Key name + * + * @return Integer value + */ + public native int GetInt(const char[] key); + + /** + * 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); + + /** + * Gets name of the object's key + * + * @param index Position from which get key name + * @param buffer Buffer to copy string to + * @param maxlength Maximum size of the buffer + * + * @return True on success, false on failure + */ + public native bool GetKey(int index, char[] buffer, int maxlength); + + /** + * Gets a value at the specified position from the object + * + * @note Needs to be freed using delete or CloseHandle() + * + * @param index Position from which get key name + * + * @return Returns the value to which index + */ + public native any GetValueAt(int index); + + /** + * Returns whether or not a key exists in the object + * + * @param key Key string + * @param ptr_use Use JSON Pointer + * + * @return True if the key exists, false otherwise + */ + public native bool HasKey(const char[] key, bool ptr_use = false); + + /** + * Replaces all matching keys with the new key + * The old_key and new_key should be a null-terminated UTF-8 string + * The new_key is copied and held by doc + * + * @note This function takes a linear search time + * + * @param old_key The key to rename + * @param new_key The new key name + * @param allow_duplicate Whether to allow renaming even if new key exists + * + * @return True if at least one key was renamed, false otherwise + */ + public native bool RenameKey(const char[] old_key, const char[] new_key, bool allow_duplicate = false); + + /** + * Gets string data from the object + * + * @param key Key name + * @param buffer Buffer to copy string to + * @param maxlength Maximum size of the buffer + * + * @return True on success, false on failure + */ + public native bool GetString(const char[] key, char[] buffer, int maxlength); + + /** + * Returns whether or not a value in the object is null + * + * @param key Key string + * + * @return True if the value is null, false otherwise + */ + public native bool IsNull(const char[] key); + + /** + * Sets a boolean value in the object + * + * @param key Key name + * @param value Boolean value to set + * + * @return True if succeed, false otherwise + */ + public native bool SetBool(const char[] key, bool value); + + /** + * Sets a float value in the object + * + * @param key Key name + * @param value float to set + * + * @return True if succeed, false otherwise + */ + public native bool SetFloat(const char[] key, float value); + + /** + * Sets a integer value in the object + * + * @param key Key name + * @param value integer to set + * + * @return True if succeed, false otherwise + */ + public native bool SetInt(const char[] key, int value); + + /** + * 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 (can be signed or unsigned) + * + * @return True on success, false on failure + */ + public native bool SetInt64(const char[] key, const char[] value); + + /** + * Sets a null in the object + * + * @param key Key name + * + * @return True if succeed, false otherwise + */ + public native bool SetNull(const char[] key); + + /** + * Sets string data in the object + * + * @param key Key name + * @param value String to copy + * + * @return True if succeed, false otherwise + */ + public native bool SetString(const char[] key, const char[] value); + + /** + * Removes a key and its value in the object + * + * @note This function takes a linear search time + * + * @param key Key name + * + * @return True if succeed, false otherwise + */ + public native bool Remove(const char[] key); + + /** + * Removes all keys and their values in the object + * + * @return True if succeed, false otherwise + */ + public native bool Clear(); + + /** + * Sorts the object's keys + * + * @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 JSON_SORT_ORDER enums + * + * @return True on success, false on failure + * @error Invalid handle or handle is not an object + */ + public native bool Sort(JSON_SORT_ORDER order = JSON_SORT_ASC); + + /** + * Rotates key-value pairs in the object + * + * @note This function takes a linear search time + * @note Only works on mutable objects + * @note Example: {"a":1,"b":2,"c":3,"d":4} rotate 1 becomes {"b":2,"c":3,"d":4,"a":1} + * + * @param idx Number of positions to rotate + * + * @return True on success, false if object is immutable or operation failed + * @error Invalid handle or attempting to rotate an immutable object + */ + public native bool Rotate(int idx); + + /** + * Retrieves the size of the object + */ + property int Size { + public native get(); + } +}; + +methodmap JSONArray < JSON +{ + /** + * Creates a JSON array + * + * @note Needs to be freed using delete or CloseHandle() + */ + public native JSONArray(); + + /** + * Creates a new JSON array from an array of strings + * + * @note Needs to be freed using delete or CloseHandle() + * + * @param strings Array of strings to create array from + * @param size Size of the array + * @return New JSON array handle, or null if creation failed + */ + public static native JSONArray FromStrings(const char[][] strings, int size); + + /** + * Creates a new JSON array from an array of integer values + * + * @note Needs to be freed using delete or CloseHandle() + * + * @param values Array of int values to create array from + * @param size Size of the array + * @return New JSON array handle, or null if creation failed + */ + 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) + * + * @note Needs to be freed using delete or CloseHandle() + * + * @param values Array of int64 string values (can be signed or unsigned) + * @param size Size of the array + * @return New JSON array handle, or null if creation failed + * @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 + * + * @note Needs to be freed using delete or CloseHandle() + * + * @param values Array of bool values to create array from + * @param size Size of the array + * @return New JSON array handle, or null if creation failed + */ + public static native JSONArray FromBool(const bool[] values, int size); + + /** + * Creates a new JSON array from an array of float values + * + * @note Needs to be freed using delete or CloseHandle() + * + * @param values Array of float values to create array from + * @param size Size of the array + * @return New JSON array handle, or null if creation failed + */ + public static native JSONArray FromFloat(const float[] values, int size); + + /** + * Loads a JSON array from a file + * + * @note Needs to be freed using delete or CloseHandle() + * + * @param file File to read from + * @param flag Read flag + * @return Array handle, or null on failure + */ + public static native JSONArray FromFile(const char[] file, JSON_READ_FLAG flag = JSON_READ_NOFLAG); + + /** + * Loads a JSON array from a string + * + * @note Needs to be freed using delete or CloseHandle() + * + * @param buffer String buffer to load into the JSON array + * @param flag Read flag + * @return Array handle, or null on failure + */ + public static native JSONArray FromString(const char[] buffer, JSON_READ_FLAG flag = JSON_READ_NOFLAG); + + /** + * Gets a value from the array + * + * @note Needs to be freed using delete or CloseHandle() + * @note This function takes a linear search time + * + * @param index Position in the array (starting from 0) + * + * @return Value handle on success, null if array is empty or index out of bounds + */ + public native any Get(int index); + + /** + * Gets a boolean value from the array + * + * @param index Position in the array (starting from 0) + * + * @return Boolean value + */ + public native bool GetBool(int index); + + /** + * Gets a float value from the array + * + * @note Integers values are auto converted to float + * + * @param index Position in the array (starting from 0) + * + * @return The number as float + */ + public native float GetFloat(int index); + + /** + * Gets a integer value from the array + * + * @param index Position in the array (starting from 0) + * + * @return integer value + */ + public native int GetInt(int index); + + /** + * 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 True on success, false on failure + */ + public native bool GetInt64(int index, char[] buffer, int maxlength); + + /** + * Gets string data from the array + * + * @param index Position in the array (starting from 0) + * @param buffer Buffer to copy string to + * @param maxlength Maximum size of the buffer + * + * @return True on success, false on failure + */ + public native bool GetString(int index, char[] buffer, int maxlength); + + /** + * Returns whether or not a value in the array is null + * + * @param index Position in the array (starting from 0) + * + * @return True if the value is null, false otherwise + */ + public native bool IsNull(int index); + + /** + * Replaces a value at index + * + * @note This function takes a linear search time + * + * @param index The index to which to replace the value + * @param value The new value to replace + * + * @return True if succeed, false otherwise + */ + public native bool Set(int index, const JSON value); + + /** + * Replaces a boolean value at index + * + * @param index The index to which to replace the value + * @param value The new boolean value to replace + * + * @return True if succeed, false otherwise + */ + public native bool SetBool(int index, bool value); + + /** + * Replaces a float value at index + * + * @param index The index to which to replace the value + * @param value The new float value to replace + * + * @return True if succeed, false otherwise + */ + public native bool SetFloat(int index, float value); + + /** + * Replaces an integer value at index + * + * @param index The index to which to replace the value + * @param value The new int value to replace + * + * @return True if succeed, false otherwise + */ + public native bool SetInt(int index, int value); + + /** + * 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 (can be signed or unsigned) + * + * @return True if succeed, false otherwise + */ + public native bool SetInt64(int index, const char[] value); + + /** + * Replaces a string value at index + * + * @param index The index to which to replace the value + * @param value The new string value to replace + * + * @return True if succeed, false otherwise + */ + public native bool SetString(int index, const char[] value); + + /** + * Replaces a null value at index + * + * @param index The index to which to replace the value + * + * @return True if succeed, false otherwise + */ + public native bool SetNull(int index); + + /** + * Inserts a value at the end of the array + * + * @param value JSON handle to set + * + * @return True if succeed, false otherwise + */ + public native bool Push(const JSON value); + + /** + * Inserts a boolean value at the end of the array + * + * @param value Boolean value to set + * + * @return True if succeed, false otherwise + */ + public native bool PushBool(bool value); + + /** + * Inserts a float value at the end of the array + * + * @param value float to set + * + * @return True if succeed, false otherwise + */ + public native bool PushFloat(float value); + + /** + * Inserts an integer value at the end of the array + * + * @param value integer to set + * + * @return True if succeed, false otherwise + */ + public native bool PushInt(int value); + + /** + * Inserts an integer64 value at the end of the array + * + * @param value integer64 value + * + * @return True if succeed, false otherwise + */ + public native bool PushInt64(const char[] value); + + /** + * Inserts a string value at the end of the array + * + * @param value String to copy + * + * @return True if succeed, false otherwise + */ + public native bool PushString(const char[] value); + + /** + * Inserts a null value at the end of the array + * + * @return True if succeed, false otherwise + */ + public native bool PushNull(); + + /** + * Inserts a JSON value at specific index + * + * @note This function takes a linear search time + * + * @param index Position to insert (0 to size, size means append) + * @param value JSON handle to insert + * + * @return True if succeed, false otherwise + */ + public native bool Insert(int index, const JSON value); + + /** + * Inserts a boolean value at specific index + * + * @note This function takes a linear search time + * + * @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 + * + * @note This function takes a linear search time + * + * @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 + * + * @note This function takes a linear search time + * + * @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 + * + * @note This function takes a linear search time + * + * @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 + * + * @note This function takes a linear search time + * + * @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 + * + * @note This function takes a linear search time + * + * @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 + * + * @note This function takes a linear search time + * + * @param index Position in the array (starting from 0) + * + * @return True if succeed, false otherwise + */ + public native bool Remove(int index); + + /** + * Removes the first value in this array + * + * @return True if succeed, false otherwise + */ + public native bool RemoveFirst(); + + /** + * Removes the last value in this array + * + * @return True if succeed, false otherwise + */ + public native bool RemoveLast(); + + /** + * Removes all values within a specified range in the array + * + * @note This function takes a linear search time + * + * @param start_index The start index of the range (0 is the first) + * @param count Number of items to remove (can be 0, in which case nothing happens) + * + * @return True if succeed, false otherwise + */ + public native bool RemoveRange(int start_index, int count); + + /** + * Searches for a boolean value in the array and returns its index + * + * @param value The boolean value to search for + * + * @return The index of the first matching element, or -1 if not found + * @error Invalid handle or handle is not an array + */ + public native int IndexOfBool(bool value); + + /** + * Searches for a string value in the array and returns its index + * + * @param value The string value to search for + * + * @return The index of the first matching element, or -1 if not found + * @error Invalid handle or handle is not an array + */ + public native int IndexOfString(const char[] value); + + /** + * Searches for an integer value in the array and returns its index + * + * @param value The integer value to search for + * + * @return The index of the first matching element, or -1 if not found + * @error Invalid handle or handle is not an array + */ + public native int IndexOfInt(int value); + + /** + * Searches for an integer64 value in the array and returns its index + * + * @param value The integer64 value to search for as string + * + * @return The index of the first matching element, or -1 if not found + * @error Invalid handle, handle is not an array, or invalid integer64 string format + */ + public native int IndexOfInt64(const char[] value); + + /** + * Searches for a float value in the array and returns its index + * + * @param value The float value to search for + * + * @return The index of the first matching element, or -1 if not found + * @error Invalid handle or handle is not an array + */ + public native int IndexOfFloat(float value); + + /** + * Removes all elements from the array + * + * @return True if succeed, false otherwise + */ + public native bool Clear(); + + /** + * Sorts the array elements + * + * @note Sorting rules: + * - Different types are sorted by their type ID + * - Strings are sorted lexicographically + * - Numbers are sorted by their numeric value + * - Booleans are sorted with false before true + * - Other types (null, object, array) are sorted by type only + * + * @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(JSON_SORT_ORDER order = JSON_SORT_ASC); + + /** + * Rotates array elements + * + * @note This function takes a linear search time + * @note Only works on mutable arrays + * @note Example: [1,2,3,4,5] rotate 2 becomes [3,4,5,1,2] + * + * @param idx Number of positions to rotate + * + * @return True on success, false if array is immutable or operation failed + * @error Invalid handle or attempting to rotate an immutable array + */ + public native bool Rotate(int idx); + + /** + * Retrieves the size of the array + */ + property int Length { + public native get(); + } + + /** + * @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 + * Needs to be freed using delete or CloseHandle() + */ + 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 + * Needs to be freed using delete or CloseHandle() + */ + property JSON Last { + public native get(); + } +}; + +methodmap JSONArrIter < Handle +{ + /** + * Creates an array iterator from a JSON array value + * + * @note Needs to be freed using delete or CloseHandle() + * @note Iterators are single-pass. Once Next returns null, call Reset() or create + * a new iterator to traverse the array again + * + * @param array JSON array value to iterate + * + * @return Array iterator handle, or null on failure + * @error Invalid handle or handle is not an array + */ + public native JSONArrIter(JSON array); + + /** + * Resets the iterator to the beginning of the array + * + * @note Only resets the iterator, does not free the iterator handle + * + * @return True on success, false on failure + * @error Invalid iterator handle + */ + public native bool Reset(); + + /** + * Moves the iterator to the next element + * + * @note Needs to be freed using delete or CloseHandle() + * + * @return JSON value handle for the next element, or null if iteration is complete + * @error Invalid iterator handle + */ + 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() + * @note Iterators are single-pass. Once Next returns null, call Reset() or create + * a new iterator to traverse the object again + * + * @param obj JSON object value to iterate + * + * @return Object iterator handle, or null on failure + * @error Invalid handle or handle is not an object + */ + public native JSONObjIter(JSON obj); + + /** + * Resets the iterator to the beginning of the object + * + * @note Only resets the iterator, does not free the iterator handle + * + * @return True on success, false on failure + * @error Invalid iterator handle + */ + public native bool Reset(); + + /** + * Moves the iterator to the next key + * + * @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 + autoload = 0, +#endif +#if defined REQUIRE_EXTENSIONS + required = 1, +#else + required = 0, +#endif +}; + +#if !defined REQUIRE_EXTENSIONS +public void __pl_json_SetNTVOptional() +{ + // JSONObject + 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"); + MarkNativeAsOptional("JSONObject.Rotate"); + + // JSONArray + 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"); + MarkNativeAsOptional("JSONArray.Rotate"); + + // JSON + 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.RefCount.get"); + MarkNativeAsOptional("JSON.ValCount.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"); + MarkNativeAsOptional("JSON.ApplyJsonPatch"); + MarkNativeAsOptional("JSON.JsonPatchInPlace"); + MarkNativeAsOptional("JSON.ApplyMergePatch"); + MarkNativeAsOptional("JSON.MergePatchInPlace"); + + // JSON CREATE & GET + 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("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"); + MarkNativeAsOptional("JSONArrIter.Reset"); + + // JSONObjIter + MarkNativeAsOptional("JSONObjIter.JSONObjIter"); + MarkNativeAsOptional("JSONObjIter.Next"); + MarkNativeAsOptional("JSONObjIter.HasNext.get"); + MarkNativeAsOptional("JSONObjIter.Value.get"); + MarkNativeAsOptional("JSONObjIter.Get"); + MarkNativeAsOptional("JSONObjIter.Index.get"); + MarkNativeAsOptional("JSONObjIter.Remove"); + MarkNativeAsOptional("JSONObjIter.Reset"); +} +#endif \ No newline at end of file diff --git a/scripting/include/yyjson.inc b/scripting/include/yyjson.inc deleted file mode 100644 index 1813e82..0000000 --- a/scripting/include/yyjson.inc +++ /dev/null @@ -1,1634 +0,0 @@ -#if defined _yyjson_included - #endinput -#endif -#define _yyjson_included - -// JSON value types -enum YYJSON_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 value subtypes -enum YYJSON_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 reader flags for parsing behavior -enum YYJSON_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 writer flags for serialization behavior -enum YYJSON_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) -} - -/** 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) -{ - return view_as(n << 28); -} - -// Sort order for arrays and objects -enum YYJSON_SORT_ORDER -{ - YYJSON_SORT_ASC = 0, // Ascending order (default) - YYJSON_SORT_DESC = 1, // Descending order - YYJSON_SORT_RANDOM = 2 // Random order -} - -methodmap YYJSON < Handle -{ - /** - * Creates a JSON value using a format string and arguments - * Format specifiers: - * - s: string - * - i: integer - * - f: float - * - b: boolean - * - n: null - * - {: start object - * - }: end object - * - [: start array - * - ]: end array - * - * @param format Format string - * @param ... Arguments based on format string - * - * @return JSON handle - * @error If format string is invalid or arguments don't match - */ - public static native any Pack(const char[] format, any ...); - - /** - * Iterates over the object's key-value pairs - * - * @note Needs to be freed using delete or CloseHandle() - * - * @param buffer Buffer to copy key name to - * @param maxlength Maximum length of the string buffer - * @param value JSON handle to store the current value - * - * @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); - - /** - * Iterates over the array's values - * - * @note Needs to be freed using delete or CloseHandle() - * - * @param index Variable to store current array index (starting from 0) - * @param value JSON handle to store the current value - * - * @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); - - /** - * Same as ForeachObject, but only iterates over the object's keys - * - * @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 - * @param maxlength Maximum length of the string buffer - * - * @return True if there are more elements, false when iteration is complete - * @error Invalid handle or handle is not an object - */ - public native bool ForeachKey(char[] buffer, int maxlength); - - /** - * Same as ForeachArray, but only iterates over the array's indexes - * - * @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) - * - * @return True if there are more elements, false when iteration is complete - * @error Invalid handle or handle is not an array - */ - public native bool ForeachIndex(int &index); - - /** - * Converts an immutable JSON document to a mutable one - * - * @return Handle to the new mutable JSON document, INVALID_HANDLE on failure - * @error If the document is already mutable - */ - public native any ToMutable(); - - /** - * Converts a mutable JSON document to an immutable one - * - * @return Handle to the new immutable JSON document, INVALID_HANDLE on failure - * @error If the document is already immutable - */ - public native any ToImmutable(); - - /** - * Write a document to JSON file with options - * - * @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 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); - - /** - * Write a value to JSON string - * - * @param buffer String buffer to write to - * @param maxlength Maximum length of the string buffer - * @param flag The JSON write options - * - * @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); - - /** - * Parses JSON string or a file that contains JSON - * - * @note Needs to be freed using delete or CloseHandle() - * - * @param string String or file to parse - * @param is_file True to treat string param as file, false otherwise - * @param is_mutable_doc True to create a mutable document, false to create an immutable one - * @param flag The JSON read options - * - * @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); - - /** - * 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 - * @note The mutability of the returned copy depends on the targetDoc parameter - * - * @param targetDoc The target document that determines whether the copy will be mutable or immutable - * @param sourceValue The source JSON value to be copied - * - * @return New JSON handle on success, false on failure - */ - public static native any DeepCopy(const YYJSON targetDoc, const YYJSON sourceValue); - - /** - * Returns the JSON value's type description - * - * @param value JSON handle - * @param buffer String buffer to write to - * @param maxlength Maximum length of the string buffer - * - * @return The return value should be one of these strings: "raw", "null", "string", - * "array", "object", "true", "false", "uint", "sint", "real", "unknown". - */ - public static native void GetTypeDesc(const YYJSON value, char[] buffer, int maxlength); - - /** - * Returns whether two JSON values are equal (deep compare) - * - * @note This function is recursive and may cause a stack overflow if the object level is too deep - * @note the result may be inaccurate if object has duplicate keys - * - * @param value1 JSON handle - * @param value2 JSON handle - * - * @return True if they are the same, false otherwise - */ - public static native bool Equals(const YYJSON value1, const YYJSON value2); - - /** - * Creates and returns a boolean value - * - * @note Needs to be freed using delete or CloseHandle() - * - * @param value The boolean value to be set - * - * @return JSON handle, NULL on error - */ - public static native YYJSON CreateBool(bool value); - - /** - * Creates and returns a float value - * - * @note Needs to be freed using delete or CloseHandle() - * - * @param value The float value to be set - * - * @return JSON handle, NULL on error - */ - public static native YYJSON CreateFloat(float value); - - /** - * Creates and returns an int value - * - * @note Needs to be freed using delete or CloseHandle() - * - * @param value The int value to be set - * - * @return JSON handle, NULL on error - */ - public static native YYJSON CreateInt(int value); - - /** - * Creates and returns an integer64 value - * - * @note Needs to be freed using delete or CloseHandle() - * - * @param value The integer64 value to be set - * - * @return JSON handle, NULL on error - */ - public static native YYJSON CreateInt64(const char[] value); - - /** - * Creates and returns a string value - * - * @note Needs to be freed using delete or CloseHandle() - * - * @param value The string value to be set - * - * @return JSON handle, NULL on error - */ - public static native YYJSON 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 - */ - public static native YYJSON CreateNull(); - - /** - * Get boolean value by a JSON Handle - * - * @param value JSON handle - * - * @return Boolean value - */ - public static native bool GetBool(const YYJSON value); - - /** - * Get float value by a JSON Handle - * - * @param value JSON handle - * - * @return float value - */ - public static native float GetFloat(const YYJSON value); - - /** - * Get int value by a JSON Handle - * - * @param value JSON handle - * - * @return int value - */ - public static native int GetInt(const YYJSON value); - - /** - * Get integer64 value by a JSON Handle - * - * @param value JSON handle - * @param buffer Buffer to copy to - * @param maxlength Maximum size of the buffer - * - * @return True on success, false on failure - */ - public static native bool GetInt64(const YYJSON value, char[] buffer, int maxlength); - - /** - * Get 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); - - /** - * Get JSON Handle serialized size in bytes (including null-terminator) - * - * @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. - */ - public native int GetSerializedSize(YYJSON_WRITE_FLAG flag = YYJSON_WRITE_NOFLAG); - - /** - * Get value by a JSON Pointer - * - * @note Needs to be freed using delete or CloseHandle() - * - * @param path The JSON pointer string - * - * @return The value referenced by the JSON pointer - */ - public native any PtrGet(const char[] path); - - /** - * Get boolean value by a JSON Pointer - * - * @param path The JSON pointer string - * - * @return boolean value referenced by the JSON pointer - */ - public native bool PtrGetBool(const char[] path); - - /** - * Get float value by a JSON Pointer - * - * @param path The JSON pointer string - * - * @return float value referenced by the JSON pointer - */ - public native float PtrGetFloat(const char[] path); - - /** - * Get integer value by a JSON Pointer - * - * @param path The JSON pointer string - * - * @return integer value referenced by the JSON pointer - */ - 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 - */ - public native bool PtrGetInt64(const char[] path, char[] buffer, int maxlength); - - /** - * Get string 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 True on success, false on failure - */ - public native bool PtrGetString(const char[] path, char[] buffer, int maxlength); - - /** - * Get value is null by a JSON Pointer - * - * @param path The JSON pointer string - * - * @return True if the value is null, false otherwise - */ - public native bool PtrGetIsNull(const char[] path); - - /** - * Get JSON content length (string length, array size, object size) - * - * @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 - * - * @param path The JSON pointer string - * - * @return JSON content length - */ - public native int PtrGetLength(const char[] path); - - /** - * 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 - * - * @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); - - /** - * 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 - * - * @param path The JSON pointer string - * @param value The boolean value to be set - * - * @return true if JSON pointer is valid and new value is set, false otherwise - */ - public native bool PtrSetBool(const char[] path, bool value); - - /** - * 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 - * - * @param path The JSON pointer string - * @param value The float value to be set - * - * @return true if JSON pointer is valid and new value is set, false otherwise - */ - public native bool PtrSetFloat(const char[] path, float value); - - /** - * 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 - * - * @param path The JSON pointer string - * @param value The integer value to be set - * - * @return true if JSON pointer is valid and new value is set, false otherwise - */ - public native bool PtrSetInt(const char[] path, int value); - - /** - * 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 - * - * @param path The JSON pointer string - * @param value The integer64 value to be set - * - * @return true if JSON pointer is valid and new value is set, false otherwise - */ - public native bool PtrSetInt64(const char[] path, const char[] value); - - /** - * 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 - * - * @param path The JSON pointer string - * @param value The string value to be set - * - * @return true if JSON pointer is valid and new value is set, false otherwise - */ - public native bool PtrSetString(const char[] path, const char[] value); - - /** - * 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 - * - * @param path The JSON pointer string - * - * @return true if JSON pointer is valid and new value is set, false otherwise - */ - public native bool PtrSetNull(const char[] path); - - /** - * Add (insert) value by a JSON pointer - * - * @param path The JSON pointer string - * @param value The value to be added - * - * @return true if JSON pointer is valid and new value is set, false otherwise - */ - public native bool PtrAdd(const char[] path, YYJSON value); - - /** - * Add (insert) boolean value by a JSON pointer - * - * @param path The JSON pointer string - * @param value The boolean value to be added - * - * @return true if JSON pointer is valid and new value is set, false otherwise - */ - public native bool PtrAddBool(const char[] path, bool value); - - /** - * Add (insert) float value by a JSON pointer - * - * @param path The JSON pointer string - * @param value The float value to be added - * - * @return true if JSON pointer is valid and new value is set, false otherwise - */ - public native bool PtrAddFloat(const char[] path, float value); - - /** - * Add (insert) integer value by a JSON pointer - * - * @param path The JSON pointer string - * @param value The int value to be added - * - * @return true if JSON pointer is valid and new value is set, false otherwise - */ - public native bool PtrAddInt(const char[] path, int value); - - /** - * Add (insert) integer64 value by a JSON pointer - * - * @param path The JSON pointer string - * @param value The integer64 value to be added - * - * @return true if JSON pointer is valid and new value is set, false otherwise - */ - public native bool PtrAddInt64(const char[] path, const char[] value); - - /** - * Add (insert) string value by a JSON pointer - * - * @param path The JSON pointer string - * @param value The str value to be added - * - * @return true if JSON pointer is valid and new value is set, false otherwise - */ - public native bool PtrAddString(const char[] path, const char[] value); - - /** - * Add (insert) null value by a JSON pointer - * - * @param path The JSON pointer string - * - * @return true if JSON pointer is valid and new value is set, false otherwise - */ - public native bool PtrAddNull(const char[] path); - - /** - * Remove value by a JSON pointer - * - * @param path The JSON pointer string - * - * @return true if removed value, false otherwise - */ - public native bool PtrRemove(const char[] path); - - /** - * Try to get value by a JSON Pointer - * - * @param path The JSON pointer string - * @param value Handle to store value - * - * @return true on success, false otherwise - */ - public native bool PtrTryGetVal(const char[] path, YYJSON &value); - - /** - * Try to get boolean value by a JSON Pointer - * - * @param path The JSON pointer string - * @param value Store the boolean value - * - * @return true on success, false otherwise - */ - public native bool PtrTryGetBool(const char[] path, bool &value); - - /** - * Try to get float value by a JSON Pointer - * - * @param path The JSON pointer string - * @param value Store the float value - * - * @return true on success, false otherwise - */ - public native bool PtrTryGetFloat(const char[] path, float &value); - - /** - * Try to get integer value by a JSON Pointer - * - * @param path The JSON pointer string - * @param value Store the integer value - * - * @return true on success, false otherwise - */ - 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 - */ - public native bool PtrTryGetInt64(const char[] path, char[] buffer, int maxlength); - - /** - * Try to get string value by a JSON Pointer - * - * @param path The JSON pointer string - * @param buffer Buffer to store the string value - * @param maxlength Maximum length of the buffer - * - * @return true on success, false otherwise - */ - public native bool PtrTryGetString(const char[] path, char[] buffer, int maxlength); - - /** - * Retrieves json type - */ - property YYJSON_TYPE Type { - public native get(); - } - - /** - * Retrieves json subtype - */ - property YYJSON_SUBTYPE SubType { - public native get(); - } - - /** - * Retrieves json is array - */ - property bool IsArray { - public native get(); - } - - /** - * Retrieves json is object - */ - property bool IsObject { - public native get(); - } - - /** - * Retrieves json is integer (uint64_t/int64_t) - */ - property bool IsInt { - public native get(); - } - - /** - * Retrieves json is unsigned integer (uint64_t) - */ - property bool IsUint { - public native get(); - } - - /** - * Retrieves json is signed integer (int64_t) - */ - property bool IsSint { - public native get(); - } - - /** - * Retrieves json is number (uint64_t/int64_t/double) - */ - property bool IsNum { - public native get(); - } - - /** - * Retrieves json is boolean - */ - property bool IsBool { - public native get(); - } - - /** - * Retrieves json is true - */ - property bool IsTrue { - public native get(); - } - - /** - * Retrieves json is false - */ - property bool IsFalse { - public native get(); - } - - /** - * Retrieves json is float - */ - property bool IsFloat { - public native get(); - } - - /** - * Retrieves json is string - */ - property bool IsStr { - public native get(); - } - - /** - * Retrieves json is null - */ - property bool IsNull { - public native get(); - } - - /** - * Retrieves json is container (array/object) - */ - property bool IsCtn { - public native get(); - } - - /** - * Retrieves json is mutable doc - */ - property bool IsMutable { - public native get(); - } - - /** - * Retrieves json is immutable doc - */ - property bool IsImmutable { - public native get(); - } - - /** - * Retrieves the size of the JSON data as it was originally read from parsing - * - * @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 This value does not auto-update if the document is modified - * @note For modified documents, use GetSerializedSize() to obtain the current size - */ - property int ReadSize { - public native get(); - } -}; - -methodmap YYJSONObject < YYJSON -{ - /** - * Creates a JSON object A JSON object maps strings (called "keys") to values Keys in a - * JSON object are unique That is, there is at most one entry in the map for a given key - * - * @note Needs to be freed using delete or CloseHandle() - */ - public native YYJSONObject(); - - /** - * 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. - * - * @param pairs Array of strings containing alternating keys and values - * @param size Total size of the array (must be even) - * - * @return New JSON object handle - * @error If array size is invalid, any key is empty, or if creation fails - * - */ - public static native YYJSONObject FromStrings(const char[][] pairs, int size); - - /** - * Loads a JSON object from a file - * - * @param file File to read from - * @param flag The JSON read options - * - * @return Object handle, or null on failure - */ - public static native YYJSONObject FromFile(const char[] file, YYJSON_READ_FLAG flag = YYJSON_READ_NOFLAG); - - /** - * Loads a JSON object from a string - * - * @param buffer String buffer to load into the JSON object - * @param flag The JSON read options - * - * @return Object handle, or null on failure - */ - public static native YYJSONObject FromString(const char[] buffer, YYJSON_READ_FLAG flag = YYJSON_READ_NOFLAG); - - /** - * Gets a value from the object - * - * @note This function takes a linear search time - * - * @param key Key name - * - * @return Returns the value to which the specified key is mapped, or null if this object contains no mapping for the key - */ - public native any Get(const char[] key); - - /** - * Sets a value in the object - * - * @param key Key name - * @param value JSON handle to set - * - * @return True if succeed, false otherwise - */ - public native bool Set(const char[] key, const YYJSON value); - - /** - * Gets a boolean value from the object - * - * @param key Key name - * - * @return Boolean value - */ - public native bool GetBool(const char[] key); - - /** - * Gets a float value from the object - * - * @param key Key name - * - * @return Float value - */ - public native float GetFloat(const char[] key); - - /** - * Gets a integer value from the object - * - * @param key Key name - * - * @return Integer value - */ - 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 - */ - public native bool GetInt64(const char[] key, char[] buffer, int maxlength); - - /** - * Gets name of the object's key - * - * @param index Position from which get key name - * @param buffer Buffer to copy string to - * @param maxlength Maximum size of the buffer - * - * @return True on success, false on failure - */ - public native bool GetKey(int index, char[] buffer, int maxlength); - - /** - * Gets a value at the specified position from the object - * - * @note Needs to be freed using delete or CloseHandle() - * - * @param index Position from which get key name - * - * @return Returns the value to which index - */ - public native any GetValueAt(int index); - - /** - * Returns whether or not a key exists in the object - * - * @param key Key string - * @param ptr_use Use JSON Pointer - * - * @return True if the key exists, false otherwise - */ - public native bool HasKey(const char[] key, bool ptr_use = false); - - /** - * Replaces all matching keys with the new key - * The old_key and new_key should be a null-terminated UTF-8 string - * The new_key is copied and held by doc - * - * @note This function takes a linear search time - * - * @param old_key The key to rename - * @param new_key The new key name - * @param allow_duplicate Whether to allow renaming even if new key exists - * - * @return True if at least one key was renamed, false otherwise - */ - public native bool RenameKey(const char[] old_key, const char[] new_key, bool allow_duplicate = false); - - /** - * Gets string data from the object - * - * @param key Key name - * @param buffer Buffer to copy string to - * @param maxlength Maximum size of the buffer - * - * @return True on success, false on failure - */ - public native bool GetString(const char[] key, char[] buffer, int maxlength); - - /** - * Returns whether or not a value in the object is null - * - * @param key Key string - * - * @return True if the value is null, false otherwise - */ - public native bool IsNull(const char[] key); - - /** - * Sets a boolean value in the object - * - * @param key Key name - * @param value Boolean value to set - * - * @return True if succeed, false otherwise - */ - public native bool SetBool(const char[] key, bool value); - - /** - * Sets a float value in the object - * - * @param key Key name - * @param value float to set - * - * @return True if succeed, false otherwise - */ - public native bool SetFloat(const char[] key, float value); - - /** - * Sets a integer value in the object - * - * @param key Key name - * @param value integer to set - * - * @return True if succeed, false otherwise - */ - public native bool SetInt(const char[] key, int value); - - /** - * Sets a 64-bit integer value in the object, either inserting a new entry or replacing an old one - * - * @param key Key string - * @param value Value to store at this key - * - * @return True on success, false on failure - */ - public native bool SetInt64(const char[] key, const char[] value); - - /** - * Sets a null in the object - * - * @param key Key name - * - * @return True if succeed, false otherwise - */ - public native bool SetNull(const char[] key); - - /** - * Sets string data in the object - * - * @param key Key name - * @param value String to copy - * - * @return True if succeed, false otherwise - */ - public native bool SetString(const char[] key, const char[] value); - - /** - * Removes a key and its value in the object - * - * @note This function takes a linear search time - * - * @param key Key name - * - * @return True if succeed, false otherwise - */ - public native bool Remove(const char[] key); - - /** - * Removes all keys and their values in the object - * - * @return True if succeed, false otherwise - */ - public native bool Clear(); - - /** - * Sorts the object's keys - * - * @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 - * - * @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); - - /** - * Retrieves the size of the object - */ - property int Size { - public native get(); - } -}; - -methodmap YYJSONArray < YYJSON -{ - /** - * Creates a JSON array - * - * @note Needs to be freed using delete or CloseHandle() - */ - public native YYJSONArray(); - - /** - * Creates a new JSON array from an array of strings. - * - * @param strings Array of strings to create array from. - * @param size Size of the array. - * @return New JSON array handle, or null if creation failed. - */ - public static native YYJSONArray FromStrings(const char[][] strings, int size); - - /** - * Loads a JSON array from a file - * - * @param file File to read from - * @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); - - /** - * Loads a JSON array from a string - * - * @param buffer String buffer to load into the JSON array - * @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); - - /** - * Gets a value from the array - * - * @note This function takes a linear search time - * - * @param index Position in the array (starting from 0) - * - * @return Value handle on success, null if array is empty or index out of bounds - */ - public native any Get(int index); - - /** - * Gets a boolean value from the array - * - * @param index Position in the array (starting from 0) - * - * @return Boolean value - */ - public native bool GetBool(int index); - - /** - * Gets a float value from the array - * - * @param index Position in the array (starting from 0) - * - * @return The number as float - */ - public native float GetFloat(int index); - - /** - * Gets a integer value from the array - * - * @param index Position in the array (starting from 0) - * - * @return integer value - */ - 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 - */ - public native void GetInt64(int index, char[] buffer, int maxlength); - - /** - * Gets string data from the array - * - * @param index Position in the array (starting from 0) - * @param buffer Buffer to copy string to - * @param maxlength Maximum size of the buffer - * - * @return True on success, false on failure - */ - public native bool GetString(int index, char[] buffer, int maxlength); - - /** - * Returns whether or not a value in the array is null - * - * @param index Position in the array (starting from 0) - * - * @return True if the value is null, false otherwise - */ - public native bool IsNull(int index); - - /** - * Replaces a value at index - * - * @note This function takes a linear search time - * - * @param index The index to which to replace the value - * @param value The new value to replace - * - * @return True if succeed, false otherwise - */ - public native bool Set(int index, const YYJSON value); - - /** - * Replaces a boolean value at index - * - * @param index The index to which to replace the value - * @param value The new boolean value to replace - * - * @return True if succeed, false otherwise - */ - public native bool SetBool(int index, bool value); - - /** - * Replaces a float value at index - * - * @param index The index to which to replace the value - * @param value The new float value to replace - * - * @return True if succeed, false otherwise - */ - public native bool SetFloat(int index, float value); - - /** - * Replaces an integer value at index - * - * @param index The index to which to replace the value - * @param value The new int value to replace - * - * @return True if succeed, false otherwise - */ - public native bool SetInt(int index, int value); - - /** - * Replaces an integer64 value at index - * - * @param index The index to which to replace the value - * @param value The new integer64 value to replace - * - * @return True if succeed, false otherwise - */ - public native bool SetInt64(int index, const char[] value); - - /** - * Replaces a string value at index - * - * @param index The index to which to replace the value - * @param value The new string value to replace - * - * @return True if succeed, false otherwise - */ - public native bool SetString(int index, const char[] value); - - /** - * Replaces a null value at index - * - * @param index The index to which to replace the value - * - * @return True if succeed, false otherwise - */ - public native bool SetNull(int index); - - /** - * Inserts a value at the end of the array - * - * @param value JSON handle to set - * - * @return The value to be inserted. Returns false if it is NULL - */ - public native bool Push(const YYJSON 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 - */ - public native bool PushBool(bool value); - - /** - * Inserts a float value at the end of the array - * - * @param value float to set - * - * @return The value to be inserted. Returns false if it is NULL - */ - public native bool PushFloat(float value); - - /** - * Inserts an integer value at the end of the array - * - * @param value integer to set - * - * @return The value to be inserted. Returns false if it is NULL - */ - public native bool PushInt(int value); - - /** - * Inserts an integer64 value at the end of the array - * - * @param value integer64 value - * - * @return The value to be inserted. Returns false if it is NULL - */ - public native bool PushInt64(const char[] value); - - /** - * Inserts a string value at the end of the array - * - * @param value String to copy - * - * @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 - */ - public native bool PushNull(); - - /** - * Removes an element from the array - * - * @note This function takes a linear search time - * - * @param index Position in the array (starting from 0) - * - * @return True if succeed, false otherwise - */ - public native bool Remove(int index); - - /** - * Removes the first value in this array - * - * @return True if succeed, false otherwise - */ - public native bool RemoveFirst(); - - /** - * Removes and returns the last value in this array - * - * @return True if succeed, false otherwise - */ - public native bool RemoveLast(); - - /** - * Removes all values within a specified range in the array - * - * @note This function takes a linear search time - * - * @param start_index The start index of the range (0 is the first) - * @param end_index The number of items in the range (can be 0, but do nothing) - * - * - * @return True if succeed, false otherwise - */ - public native bool RemoveRange(int start_index, int end_index); - - /** - * Searches for a boolean value in the array and returns its index - * - * @param value The boolean value to search for - * - * @return The index of the first matching element, or -1 if not found - * @error Invalid handle or handle is not an array - */ - public native int IndexOfBool(bool value); - - /** - * Searches for a string value in the array and returns its index - * - * @param value The string value to search for - * - * @return The index of the first matching element, or -1 if not found - * @error Invalid handle or handle is not an array - */ - public native int IndexOfString(const char[] value); - - /** - * Searches for an integer value in the array and returns its index - * - * @param value The integer value to search for - * - * @return The index of the first matching element, or -1 if not found - * @error Invalid handle or handle is not an array - */ - public native int IndexOfInt(int value); - - /** - * Searches for an integer64 value in the array and returns its index - * - * @param value The integer64 value to search for as string - * - * @return The index of the first matching element, or -1 if not found - * @error Invalid handle, handle is not an array, or invalid integer64 string format - */ - public native int IndexOfInt64(const char[] value); - - /** - * Searches for a float value in the array and returns its index - * - * @param value The float value to search for - * - * @return The index of the first matching element, or -1 if not found - * @error Invalid handle or handle is not an array - */ - public native int IndexOfFloat(float value); - - /** - * Removes all elements from the array - * - * @return True if succeed, false otherwise - */ - public native bool Clear(); - - /** - * Sorts the array elements - * - * @note Sorting rules: - * - Different types are sorted by their type ID - * - Strings are sorted lexicographically - * - Numbers are sorted by their numeric value - * - 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 - * - * @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); - - /** - * Retrieves the size of the array - */ - property int Length { - public native get(); - } - - /** - * @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 - * Needs to be freed using delete or CloseHandle() - */ - property YYJSON 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 - * Needs to be freed using delete or CloseHandle() - */ - property YYJSON Last { - public native get(); - } -}; - -public Extension __ext_yyjson = { - name = "yyjson", - file = "yyjson.ext", -#if defined AUTOLOAD_EXTENSIONS - autoload = 1, -#else - autoload = 0, -#endif -#if defined REQUIRE_EXTENSIONS - required = 1, -#else - required = 0, -#endif -}; - -#if !defined REQUIRE_EXTENSIONS -public void __pl_yyjson_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"); - - // 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"); - - // 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"); - - // 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"); - - // 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"); -} -#endif \ No newline at end of file diff --git a/scripting/yyjson_perf_test.sp b/scripting/json_perf_test.sp old mode 100644 new mode 100755 similarity index 82% rename from scripting/yyjson_perf_test.sp rename to scripting/json_perf_test.sp index 9a10f39..ac701a3 --- a/scripting/yyjson_perf_test.sp +++ b/scripting/json_perf_test.sp @@ -1,6 +1,6 @@ #include #include -#include +#include #pragma dynamic 531072 #define TEST_ITERATIONS 100 @@ -8,23 +8,23 @@ Profiler g_hProfiler; public Plugin myinfo = { - name = "YYJSON Benchmark", + name = "JSON Benchmark", author = "ProjectSky", - description = "Performance benchmark for YYJSON", + description = "Performance benchmark for JSON", version = "1.0.0", url = "https://github.com/ProjectSky/sm-ext-yyjson" }; public void OnPluginStart() { - RegServerCmd("sm_yyjson_benchmark", Command_Benchmark, "Run YYJSON performance benchmark"); + RegServerCmd("sm_json_benchmark", Command_Benchmark, "Run JSON performance benchmark"); g_hProfiler = new Profiler(); } Action Command_Benchmark(int args) { - YYJSON json = YYJSON.Parse("twitter.json", true); + JSON json = JSON.Parse("twitter.json", true); int dataLength = json.ReadSize; @@ -36,7 +36,7 @@ Action Command_Benchmark(int args) g_hProfiler.Start(); for (int i = 0; i < TEST_ITERATIONS; i++) { - YYJSON testJson = YYJSON.Parse(jsonStr); + JSON testJson = JSON.Parse(jsonStr); delete testJson; } g_hProfiler.Stop(); @@ -53,7 +53,7 @@ Action Command_Benchmark(int args) float parseTimePerOp = parseTime * 1000.0 / TEST_ITERATIONS; float stringifyTimePerOp = stringifyTime * 1000.0 / TEST_ITERATIONS; - PrintToServer("=== YYJSON Performance Benchmark ==="); + PrintToServer("=== JSON Performance Benchmark ==="); PrintToServer("Test iterations: %d", TEST_ITERATIONS); PrintToServer("Data size: %.2f MB", float(dataLength) / (1024.0 * 1024.0)); PrintToServer("Parse time: %.3f seconds", parseTime); @@ -66,7 +66,7 @@ Action Command_Benchmark(int args) PrintToServer("Parse speed: %.2f MB/s (%.2f GB/s)", parseSpeed, parseSpeed / 1024.0); PrintToServer("Stringify speed: %.2f MB/s (%.2f GB/s)", stringifySpeed, stringifySpeed / 1024.0); PrintToServer("Stringify operations per second: %.2f ops/sec", 1000.0 / stringifyTimePerOp); - PrintToServer("=== YYJSON Performance Benchmark End ==="); + PrintToServer("=== JSON Performance Benchmark End ==="); delete json; return Plugin_Handled; diff --git a/scripting/test_json.sp b/scripting/test_json.sp new file mode 100755 index 0000000..761f21b --- /dev/null +++ b/scripting/test_json.sp @@ -0,0 +1,3380 @@ +#include +#include + +#pragma newdecls required +#pragma semicolon 1 + +public Plugin myinfo = +{ + name = "JSON Test Suite", + author = "ProjectSky", + description = "test suite for JSON functions", + version = "1.0.0", + url = "https://github.com/ProjectSky/sm-ext-yyjson" +}; + +// Test statistics +int g_iTotalTests = 0; +int g_iPassedTests = 0; +int g_iFailedTests = 0; +char g_sCurrentTest[128]; +bool g_bCurrentTestFailed = false; + +public void OnPluginStart() +{ + RegServerCmd("test_json", Command_RunTests, "Run JSON test suite"); +} + +// ============================================================================ +// Test Framework - Assertion Functions +// ============================================================================ + +/** + * Start a new test case + */ +void TestStart(const char[] name) +{ + g_iTotalTests++; + strcopy(g_sCurrentTest, sizeof(g_sCurrentTest), name); + g_bCurrentTestFailed = false; +} + +/** + * End current test case + */ +void TestEnd() +{ + if (!g_bCurrentTestFailed) + { + g_iPassedTests++; + PrintToServer("[PASS] %s", g_sCurrentTest); + } + else + { + g_iFailedTests++; + } +} + +/** + * Assert a condition is true + */ +void Assert(bool condition, const char[] message = "") +{ + if (!condition) + { + g_bCurrentTestFailed = true; + PrintToServer("[FAIL] %s - %s", g_sCurrentTest, message); + } +} + +/** + * Assert two integers are equal + */ +void AssertEq(int a, int b, const char[] message = "") +{ + if (a != b) + { + g_bCurrentTestFailed = true; + char buffer[256]; + if (message[0] != '\0') + { + FormatEx(buffer, sizeof(buffer), "%s (expected %d, got %d)", message, b, a); + } + else + { + FormatEx(buffer, sizeof(buffer), "Expected %d, got %d", b, a); + } + PrintToServer("[FAIL] %s - %s", g_sCurrentTest, buffer); + } +} + +/** + * Assert two integers are not equal + */ +stock void AssertNeq(int a, int b, const char[] message = "") +{ + if (a == b) + { + g_bCurrentTestFailed = true; + char buffer[256]; + if (message[0] != '\0') + { + FormatEx(buffer, sizeof(buffer), "%s (values should not be equal: %d)", message, a); + } + else + { + FormatEx(buffer, sizeof(buffer), "Values should not be equal: %d", a); + } + PrintToServer("[FAIL] %s - %s", g_sCurrentTest, buffer); + } +} + +/** + * Assert condition is true + */ +void AssertTrue(bool condition, const char[] message = "") +{ + Assert(condition, message[0] != '\0' ? message : "Expected true, got false"); +} + +/** + * Assert condition is false + */ +void AssertFalse(bool condition, const char[] message = "") +{ + Assert(!condition, message[0] != '\0' ? message : "Expected false, got true"); +} + +/** + * Assert two strings are equal + */ +void AssertStrEq(const char[] a, const char[] b, const char[] message = "") +{ + if (strcmp(a, b) != 0) + { + g_bCurrentTestFailed = true; + char buffer[512]; + if (message[0] != '\0') + { + FormatEx(buffer, sizeof(buffer), "%s (expected \"%s\", got \"%s\")", message, b, a); + } + else + { + FormatEx(buffer, sizeof(buffer), "Expected \"%s\", got \"%s\"", b, a); + } + PrintToServer("[FAIL] %s - %s", g_sCurrentTest, buffer); + } +} + +/** + * Assert two floats are equal within epsilon + */ +void AssertFloatEq(float a, float b, const char[] message = "", float epsilon = 0.0001) +{ + if (FloatAbs(a - b) > epsilon) + { + g_bCurrentTestFailed = true; + char buffer[256]; + if (message[0] != '\0') + { + FormatEx(buffer, sizeof(buffer), "%s (expected %.4f, got %.4f)", message, b, a); + } + else + { + FormatEx(buffer, sizeof(buffer), "Expected %.4f, got %.4f", b, a); + } + PrintToServer("[FAIL] %s - %s", g_sCurrentTest, buffer); + } +} + +/** + * Assert handle is valid + */ +void AssertValidHandle(Handle handle, const char[] message = "") +{ + Assert(handle != null, message[0] != '\0' ? message : "Handle should not be null"); +} + +/** + * Assert handle is null + */ +void AssertNullHandle(Handle handle, const char[] message = "") +{ + Assert(handle == null, message[0] != '\0' ? message : "Handle should be null"); +} + +// ============================================================================ +// Test Command +// ============================================================================ + +public Action Command_RunTests(int args) +{ + PrintToServer("========================================"); + PrintToServer("JSON Test Suite"); + PrintToServer("========================================"); + + // Reset statistics + g_iTotalTests = 0; + g_iPassedTests = 0; + g_iFailedTests = 0; + + // Run all test categories + Test_BasicValues(); + Test_ObjectOperations(); + Test_ArrayOperations(); + Test_ParseAndSerialize(); + Test_Iterators(); + Test_JSONPointer(); + Test_AdvancedFeatures(); + Test_Int64Operations(); + Test_EdgeCases(); + + // Print results + PrintToServer("========================================"); + PrintToServer("JSON Test Suite Results"); + PrintToServer("========================================"); + PrintToServer("Total Tests: %d", g_iTotalTests); + PrintToServer("Passed: %d", g_iPassedTests); + PrintToServer("Failed: %d", g_iFailedTests); + + if (g_iTotalTests > 0) + { + float successRate = (float(g_iPassedTests) / float(g_iTotalTests)) * 100.0; + PrintToServer("Success Rate: %.2f%%", successRate); + } + + PrintToServer("========================================"); + + return Plugin_Handled; +} + +// ============================================================================ +// 2.1 Basic Value Tests +// ============================================================================ + +void Test_BasicValues() +{ + PrintToServer("\n[Category] Basic Value Tests"); + + // Test CreateBool & GetBool + TestStart("BasicValues_CreateBool_True"); + { + JSON val = JSON.CreateBool(true); + AssertValidHandle(val); + AssertTrue(val.GetBool()); + AssertEq(val.Type, JSON_TYPE_BOOL); + AssertTrue(val.IsBool); + AssertTrue(val.IsTrue); + AssertFalse(val.IsFalse); + delete val; + } + TestEnd(); + + TestStart("BasicValues_CreateBool_False"); + { + JSON val = JSON.CreateBool(false); + AssertValidHandle(val); + AssertFalse(val.GetBool()); + AssertTrue(val.IsBool); + AssertFalse(val.IsTrue); + AssertTrue(val.IsFalse); + delete val; + } + TestEnd(); + + // Test CreateInt & GetInt + TestStart("BasicValues_CreateInt_Positive"); + { + JSON val = JSON.CreateInt(42); + AssertValidHandle(val); + AssertEq(val.GetInt(), 42); + AssertEq(val.Type, JSON_TYPE_NUM); + AssertTrue(val.IsInt); + AssertTrue(val.IsNum); + delete val; + } + TestEnd(); + + TestStart("BasicValues_CreateInt_Negative"); + { + JSON val = JSON.CreateInt(-123); + AssertValidHandle(val); + AssertEq(val.GetInt(), -123); + AssertTrue(val.IsSint); + delete val; + } + TestEnd(); + + TestStart("BasicValues_CreateInt_Zero"); + { + JSON val = JSON.CreateInt(0); + AssertValidHandle(val); + AssertEq(val.GetInt(), 0); + delete val; + } + TestEnd(); + + // Test CreateFloat & GetFloat + TestStart("BasicValues_CreateFloat_Positive"); + { + JSON val = JSON.CreateFloat(3.14159); + AssertValidHandle(val); + AssertFloatEq(val.GetFloat(), 3.14159); + AssertTrue(val.IsFloat); + AssertTrue(val.IsNum); + delete val; + } + TestEnd(); + + TestStart("BasicValues_CreateFloat_Negative"); + { + JSON val = JSON.CreateFloat(-2.71828); + AssertValidHandle(val); + AssertFloatEq(val.GetFloat(), -2.71828); + delete val; + } + TestEnd(); + + TestStart("BasicValues_CreateFloat_Zero"); + { + JSON val = JSON.CreateFloat(0.0); + AssertValidHandle(val); + AssertFloatEq(val.GetFloat(), 0.0); + delete val; + } + TestEnd(); + + // Test CreateString & GetString + TestStart("BasicValues_CreateString_Regular"); + { + JSON val = JSON.CreateString("Hello, World!"); + AssertValidHandle(val); + char buffer[64]; + AssertTrue(val.GetString(buffer, sizeof(buffer))); + AssertStrEq(buffer, "Hello, World!"); + AssertEq(val.Type, JSON_TYPE_STR); + AssertTrue(val.IsStr); + delete val; + } + TestEnd(); + + TestStart("BasicValues_CreateString_Empty"); + { + JSON val = JSON.CreateString(""); + AssertValidHandle(val); + char buffer[64]; + AssertTrue(val.GetString(buffer, sizeof(buffer))); + AssertStrEq(buffer, ""); + delete val; + } + TestEnd(); + + TestStart("BasicValues_CreateString_Unicode"); + { + JSON val = JSON.CreateString("测试字符串"); + AssertValidHandle(val); + char buffer[64]; + AssertTrue(val.GetString(buffer, sizeof(buffer))); + AssertStrEq(buffer, "测试字符串"); + delete val; + } + TestEnd(); + + // Test CreateInt64 & GetInt64 + TestStart("BasicValues_CreateInt64_Large"); + { + JSON val = JSON.CreateInt64("9223372036854775807"); + AssertValidHandle(val); + char buffer[32]; + AssertTrue(val.GetInt64(buffer, sizeof(buffer))); + AssertStrEq(buffer, "9223372036854775807"); + delete val; + } + TestEnd(); + + TestStart("BasicValues_CreateInt64_Negative"); + { + JSON val = JSON.CreateInt64("-9223372036854775808"); + AssertValidHandle(val); + char buffer[32]; + AssertTrue(val.GetInt64(buffer, sizeof(buffer))); + AssertStrEq(buffer, "-9223372036854775808"); + delete val; + } + TestEnd(); + + // Test CreateNull + TestStart("BasicValues_CreateNull"); + { + JSON val = JSON.CreateNull(); + AssertValidHandle(val); + AssertEq(val.Type, JSON_TYPE_NULL); + AssertTrue(val.IsNull); + delete val; + } + TestEnd(); + + // Test GetTypeDesc + TestStart("BasicValues_GetTypeDesc"); + { + JSON boolVal = JSON.CreateBool(true); + JSON intVal = JSON.CreateInt(42); + JSON floatVal = JSON.CreateFloat(3.14); + JSON strVal = JSON.CreateString("test"); + JSON nullVal = JSON.CreateNull(); + + char buffer[32]; + + boolVal.GetTypeDesc(buffer, sizeof(buffer)); + AssertStrEq(buffer, "true"); + + intVal.GetTypeDesc(buffer, sizeof(buffer)); + AssertTrue(strcmp(buffer, "uint") == 0 || strcmp(buffer, "sint") == 0); + + floatVal.GetTypeDesc(buffer, sizeof(buffer)); + AssertStrEq(buffer, "real"); + + strVal.GetTypeDesc(buffer, sizeof(buffer)); + AssertStrEq(buffer, "string"); + + nullVal.GetTypeDesc(buffer, sizeof(buffer)); + AssertStrEq(buffer, "null"); + + delete boolVal; + delete intVal; + delete floatVal; + delete strVal; + delete nullVal; + } + TestEnd(); +} + +// ============================================================================ +// 2.2 Object Operations Tests +// ============================================================================ + +void Test_ObjectOperations() +{ + PrintToServer("\n[Category] Object Operations Tests"); + + // Test object creation + TestStart("Object_Constructor"); + { + JSONObject obj = new JSONObject(); + AssertValidHandle(obj); + AssertTrue(obj.IsObject); + AssertEq(obj.Size, 0); + delete obj; + } + TestEnd(); + + // Test Set/Get methods + TestStart("Object_SetGetInt"); + { + JSONObject obj = new JSONObject(); + AssertTrue(obj.SetInt("number", 42)); + AssertEq(obj.GetInt("number"), 42); + AssertEq(obj.Size, 1); + delete obj; + } + TestEnd(); + + TestStart("Object_SetGetFloat"); + { + JSONObject obj = new JSONObject(); + AssertTrue(obj.SetFloat("pi", 3.14159)); + AssertFloatEq(obj.GetFloat("pi"), 3.14159); + delete obj; + } + TestEnd(); + + TestStart("Object_SetGetBool"); + { + JSONObject obj = new JSONObject(); + AssertTrue(obj.SetBool("flag", true)); + AssertTrue(obj.GetBool("flag")); + delete obj; + } + TestEnd(); + + TestStart("Object_SetGetString"); + { + JSONObject obj = new JSONObject(); + AssertTrue(obj.SetString("name", "test")); + char buffer[64]; + AssertTrue(obj.GetString("name", buffer, sizeof(buffer))); + AssertStrEq(buffer, "test"); + delete obj; + } + TestEnd(); + + TestStart("Object_SetGetInt64"); + { + JSONObject obj = new JSONObject(); + AssertTrue(obj.SetInt64("bignum", "9223372036854775807")); + char buffer[32]; + AssertTrue(obj.GetInt64("bignum", buffer, sizeof(buffer))); + AssertStrEq(buffer, "9223372036854775807"); + delete obj; + } + TestEnd(); + + TestStart("Object_SetGetNull"); + { + JSONObject obj = new JSONObject(); + AssertTrue(obj.SetNull("nullable")); + AssertTrue(obj.IsNull("nullable")); + delete obj; + } + TestEnd(); + + // Test HasKey + TestStart("Object_HasKey"); + { + JSONObject obj = new JSONObject(); + obj.SetInt("exists", 1); + AssertTrue(obj.HasKey("exists")); + AssertFalse(obj.HasKey("notexists")); + delete obj; + } + TestEnd(); + + // Test GetKey and GetValueAt + TestStart("Object_GetKeyAndValueAt"); + { + JSONObject obj = new JSONObject(); + obj.SetInt("first", 1); + obj.SetInt("second", 2); + + char key[32]; + AssertTrue(obj.GetKey(0, key, sizeof(key))); + AssertStrEq(key, "first"); + + JSON val = obj.GetValueAt(0); + AssertValidHandle(val); + AssertEq(val.GetInt(), 1); + + delete val; + delete obj; + } + TestEnd(); + + // Test Remove + TestStart("Object_Remove"); + { + JSONObject obj = new JSONObject(); + obj.SetInt("remove_me", 1); + obj.SetInt("keep_me", 2); + AssertEq(obj.Size, 2); + + AssertTrue(obj.Remove("remove_me")); + AssertEq(obj.Size, 1); + AssertFalse(obj.HasKey("remove_me")); + AssertTrue(obj.HasKey("keep_me")); + + delete obj; + } + TestEnd(); + + // Test Clear + TestStart("Object_Clear"); + { + JSONObject obj = new JSONObject(); + obj.SetInt("a", 1); + obj.SetInt("b", 2); + obj.SetInt("c", 3); + AssertEq(obj.Size, 3); + + AssertTrue(obj.Clear()); + AssertEq(obj.Size, 0); + + delete obj; + } + TestEnd(); + + // Test RenameKey + TestStart("Object_RenameKey"); + { + JSONObject obj = new JSONObject(); + obj.SetInt("oldname", 42); + + AssertTrue(obj.RenameKey("oldname", "newname")); + AssertFalse(obj.HasKey("oldname")); + AssertTrue(obj.HasKey("newname")); + AssertEq(obj.GetInt("newname"), 42); + + delete obj; + } + TestEnd(); + + // Test FromString + TestStart("Object_FromString"); + { + JSONObject obj = JSONObject.FromString("{\"key\":\"value\",\"num\":123}"); + AssertValidHandle(obj); + + char buffer[64]; + AssertTrue(obj.GetString("key", buffer, sizeof(buffer))); + AssertStrEq(buffer, "value"); + AssertEq(obj.GetInt("num"), 123); + + delete obj; + } + TestEnd(); + + // Test FromStrings + TestStart("Object_FromStrings"); + { + char pairs[][] = {"name", "test", "type", "demo", "version", "1.0"}; + JSONObject obj = JSONObject.FromStrings(pairs, sizeof(pairs)); + AssertValidHandle(obj); + AssertEq(obj.Size, 3); + + char buffer[64]; + AssertTrue(obj.GetString("name", buffer, sizeof(buffer))); + AssertStrEq(buffer, "test"); + + delete obj; + } + TestEnd(); + + // Test Sort + TestStart("Object_Sort_Ascending"); + { + JSONObject obj = new JSONObject(); + obj.SetInt("zebra", 1); + obj.SetInt("alpha", 2); + obj.SetInt("beta", 3); + + AssertTrue(obj.Sort(JSON_SORT_ASC)); + + char key[32]; + obj.GetKey(0, key, sizeof(key)); + AssertStrEq(key, "alpha"); + + delete obj; + } + TestEnd(); + + TestStart("Object_Sort_Descending"); + { + JSONObject obj = new JSONObject(); + obj.SetInt("alpha", 1); + obj.SetInt("beta", 2); + obj.SetInt("gamma", 3); + + AssertTrue(obj.Sort(JSON_SORT_DESC)); + + char key[32]; + obj.GetKey(0, key, sizeof(key)); + AssertStrEq(key, "gamma"); + + delete obj; + } + TestEnd(); + + // Test Rotate + TestStart("Object_Rotate_Forward"); + { + JSONObject obj = new JSONObject(); + obj.SetInt("a", 1); + obj.SetInt("b", 2); + obj.SetInt("c", 3); + obj.SetInt("d", 4); + + AssertTrue(obj.Rotate(1)); + + char key[32]; + obj.GetKey(0, key, sizeof(key)); + AssertStrEq(key, "b", "First key should be 'b' after rotate 1"); + obj.GetKey(3, key, sizeof(key)); + AssertStrEq(key, "a", "Last key should be 'a' after rotate 1"); + + delete obj; + } + TestEnd(); + + TestStart("Object_Rotate_Multiple"); + { + JSONObject obj = new JSONObject(); + obj.SetInt("first", 1); + obj.SetInt("second", 2); + obj.SetInt("third", 3); + obj.SetInt("fourth", 4); + obj.SetInt("fifth", 5); + + AssertTrue(obj.Rotate(2)); + + char key[32]; + obj.GetKey(0, key, sizeof(key)); + AssertStrEq(key, "third", "First key should be 'third' after rotate 2"); + obj.GetKey(4, key, sizeof(key)); + AssertStrEq(key, "second", "Last key should be 'second' after rotate 2"); + + delete obj; + } + TestEnd(); + + TestStart("Object_Rotate_Zero"); + { + JSONObject obj = new JSONObject(); + obj.SetInt("x", 1); + obj.SetInt("y", 2); + + AssertTrue(obj.Rotate(0)); + + char key[32]; + obj.GetKey(0, key, sizeof(key)); + AssertStrEq(key, "x", "Order should not change after rotate 0"); + + delete obj; + } + TestEnd(); + + // Test Set with handle + TestStart("Object_SetWithHandle"); + { + JSONObject obj = new JSONObject(); + JSON val = JSON.CreateInt(999); + + AssertTrue(obj.Set("nested", val)); + AssertEq(obj.GetInt("nested"), 999); + + delete val; + delete obj; + } + TestEnd(); + + // Test Get with handle + TestStart("Object_GetHandle"); + { + JSONObject obj = new JSONObject(); + obj.SetInt("value", 42); + + JSON val = obj.Get("value"); + AssertValidHandle(val); + AssertEq(val.GetInt(), 42); + + delete val; + delete obj; + } + TestEnd(); +} + +// ============================================================================ +// 2.3 Array Operations Tests +// ============================================================================ + +void Test_ArrayOperations() +{ + PrintToServer("\n[Category] Array Operations Tests"); + + // Test array creation + TestStart("Array_Constructor"); + { + JSONArray arr = new JSONArray(); + AssertValidHandle(arr); + AssertTrue(arr.IsArray); + AssertEq(arr.Length, 0); + delete arr; + } + TestEnd(); + + // Test Push methods + TestStart("Array_PushInt"); + { + JSONArray arr = new JSONArray(); + AssertTrue(arr.PushInt(42)); + AssertEq(arr.Length, 1); + AssertEq(arr.GetInt(0), 42); + delete arr; + } + TestEnd(); + + TestStart("Array_PushFloat"); + { + JSONArray arr = new JSONArray(); + AssertTrue(arr.PushFloat(3.14)); + AssertFloatEq(arr.GetFloat(0), 3.14); + delete arr; + } + TestEnd(); + + TestStart("Array_PushBool"); + { + JSONArray arr = new JSONArray(); + AssertTrue(arr.PushBool(true)); + AssertTrue(arr.GetBool(0)); + delete arr; + } + TestEnd(); + + TestStart("Array_PushString"); + { + JSONArray arr = new JSONArray(); + AssertTrue(arr.PushString("test")); + char buffer[64]; + AssertTrue(arr.GetString(0, buffer, sizeof(buffer))); + AssertStrEq(buffer, "test"); + delete arr; + } + TestEnd(); + + TestStart("Array_PushInt64"); + { + JSONArray arr = new JSONArray(); + AssertTrue(arr.PushInt64("9223372036854775807")); + char buffer[32]; + arr.GetInt64(0, buffer, sizeof(buffer)); + AssertStrEq(buffer, "9223372036854775807"); + delete arr; + } + TestEnd(); + + TestStart("Array_PushNull"); + { + JSONArray arr = new JSONArray(); + AssertTrue(arr.PushNull()); + AssertTrue(arr.IsNull(0)); + delete arr; + } + TestEnd(); + + // Test Set/Get methods + TestStart("Array_SetGetInt"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(0); + AssertTrue(arr.SetInt(0, 100)); + AssertEq(arr.GetInt(0), 100); + delete arr; + } + TestEnd(); + + TestStart("Array_SetGetFloat"); + { + JSONArray arr = new JSONArray(); + arr.PushFloat(0.0); + AssertTrue(arr.SetFloat(0, 2.718)); + AssertFloatEq(arr.GetFloat(0), 2.718); + delete arr; + } + TestEnd(); + + // Test Remove methods + TestStart("Array_Remove"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(1); + arr.PushInt(2); + arr.PushInt(3); + AssertEq(arr.Length, 3); + + AssertTrue(arr.Remove(1)); + AssertEq(arr.Length, 2); + AssertEq(arr.GetInt(0), 1); + AssertEq(arr.GetInt(1), 3); + + delete arr; + } + TestEnd(); + + TestStart("Array_RemoveFirst"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(1); + arr.PushInt(2); + arr.PushInt(3); + + AssertTrue(arr.RemoveFirst()); + AssertEq(arr.Length, 2); + AssertEq(arr.GetInt(0), 2); + + delete arr; + } + TestEnd(); + + TestStart("Array_RemoveLast"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(1); + arr.PushInt(2); + arr.PushInt(3); + + AssertTrue(arr.RemoveLast()); + AssertEq(arr.Length, 2); + AssertEq(arr.GetInt(1), 2); + + delete arr; + } + TestEnd(); + + TestStart("Array_RemoveRange"); + { + JSONArray arr = new JSONArray(); + for (int i = 0; i < 10; i++) + { + arr.PushInt(i); + } + + AssertTrue(arr.RemoveRange(2, 5)); + AssertEq(arr.Length, 5); + + delete arr; + } + TestEnd(); + + TestStart("Array_Clear"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(1); + arr.PushInt(2); + arr.PushInt(3); + + AssertTrue(arr.Clear()); + AssertEq(arr.Length, 0); + + delete arr; + } + TestEnd(); + + // Test First/Last properties + TestStart("Array_FirstLast"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(10); + arr.PushInt(20); + arr.PushInt(30); + + JSON first = arr.First; + JSON last = arr.Last; + + AssertValidHandle(first); + AssertValidHandle(last); + AssertEq(first.GetInt(), 10); + AssertEq(last.GetInt(), 30); + + delete first; + delete last; + delete arr; + } + TestEnd(); + + // Test IndexOf methods + TestStart("Array_IndexOfInt"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(10); + arr.PushInt(20); + arr.PushInt(30); + + AssertEq(arr.IndexOfInt(20), 1); + AssertEq(arr.IndexOfInt(999), -1); + + delete arr; + } + TestEnd(); + + TestStart("Array_IndexOfBool"); + { + JSONArray arr = new JSONArray(); + arr.PushBool(false); + arr.PushBool(true); + arr.PushBool(false); + + AssertEq(arr.IndexOfBool(true), 1); + + delete arr; + } + TestEnd(); + + TestStart("Array_IndexOfString"); + { + JSONArray arr = new JSONArray(); + arr.PushString("apple"); + arr.PushString("banana"); + arr.PushString("cherry"); + + AssertEq(arr.IndexOfString("banana"), 1); + AssertEq(arr.IndexOfString("orange"), -1); + + delete arr; + } + TestEnd(); + + TestStart("Array_IndexOfFloat"); + { + JSONArray arr = new JSONArray(); + arr.PushFloat(1.1); + arr.PushFloat(2.2); + arr.PushFloat(3.3); + + AssertEq(arr.IndexOfFloat(2.2), 1); + + delete arr; + } + TestEnd(); + + TestStart("Array_IndexOfInt64"); + { + JSONArray arr = new JSONArray(); + arr.PushInt64("1000000000000"); + arr.PushInt64("2000000000000"); + + AssertEq(arr.IndexOfInt64("2000000000000"), 1); + AssertEq(arr.IndexOfInt64("3000000000000"), -1); + + delete arr; + } + TestEnd(); + + // Test FromString + TestStart("Array_FromString"); + { + JSONArray arr = JSONArray.FromString("[1,2,3,4,5]"); + AssertValidHandle(arr); + AssertEq(arr.Length, 5); + AssertEq(arr.GetInt(0), 1); + AssertEq(arr.GetInt(4), 5); + delete arr; + } + TestEnd(); + + // Test FromStrings + TestStart("Array_FromStrings"); + { + char strings[][] = {"apple", "banana", "cherry"}; + JSONArray arr = JSONArray.FromStrings(strings, sizeof(strings)); + AssertValidHandle(arr); + AssertEq(arr.Length, 3); + + char buffer[64]; + arr.GetString(1, buffer, sizeof(buffer)); + AssertStrEq(buffer, "banana"); + + delete arr; + } + TestEnd(); + + // Test Sort + TestStart("Array_Sort_Ascending"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(3); + arr.PushInt(1); + arr.PushInt(4); + arr.PushInt(1); + arr.PushInt(5); + + AssertTrue(arr.Sort(JSON_SORT_ASC)); + AssertEq(arr.GetInt(0), 1); + AssertEq(arr.GetInt(4), 5); + + delete arr; + } + TestEnd(); + + TestStart("Array_Sort_Descending"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(1); + arr.PushInt(2); + arr.PushInt(3); + arr.PushInt(4); + arr.PushInt(5); + + AssertTrue(arr.Sort(JSON_SORT_DESC)); + AssertEq(arr.GetInt(0), 5); + AssertEq(arr.GetInt(4), 1); + + delete arr; + } + TestEnd(); + + // Test Rotate + TestStart("Array_Rotate_Forward"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(1); + arr.PushInt(2); + arr.PushInt(3); + arr.PushInt(4); + arr.PushInt(5); + + AssertTrue(arr.Rotate(2)); + AssertEq(arr.GetInt(0), 3, "First element should be 3 after rotate 2"); + AssertEq(arr.GetInt(1), 4, "Second element should be 4"); + AssertEq(arr.GetInt(2), 5, "Third element should be 5"); + AssertEq(arr.GetInt(3), 1, "Fourth element should be 1"); + AssertEq(arr.GetInt(4), 2, "Fifth element should be 2"); + + delete arr; + } + TestEnd(); + + TestStart("Array_Rotate_Single"); + { + JSONArray arr = new JSONArray(); + arr.PushString("a"); + arr.PushString("b"); + arr.PushString("c"); + arr.PushString("d"); + + AssertTrue(arr.Rotate(1)); + + char buffer[32]; + arr.GetString(0, buffer, sizeof(buffer)); + AssertStrEq(buffer, "b", "First element should be 'b' after rotate 1"); + arr.GetString(3, buffer, sizeof(buffer)); + AssertStrEq(buffer, "a", "Last element should be 'a' after rotate 1"); + + delete arr; + } + TestEnd(); + + TestStart("Array_Rotate_Zero"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(10); + arr.PushInt(20); + arr.PushInt(30); + + AssertTrue(arr.Rotate(0)); + AssertEq(arr.GetInt(0), 10, "Order should not change after rotate 0"); + AssertEq(arr.GetInt(1), 20); + AssertEq(arr.GetInt(2), 30); + + delete arr; + } + TestEnd(); + + TestStart("Array_Rotate_LargeIndex"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(1); + arr.PushInt(2); + arr.PushInt(3); + + // Rotate by array length or greater should fail (idx must be < length) + AssertFalse(arr.Rotate(3), "Rotate by array length should fail"); + AssertFalse(arr.Rotate(10), "Rotate by value > length should fail"); + + // Values should remain unchanged + AssertEq(arr.GetInt(0), 1); + AssertEq(arr.GetInt(1), 2); + AssertEq(arr.GetInt(2), 3); + + delete arr; + } + TestEnd(); + + // Test Push with handle + TestStart("Array_PushHandle"); + { + JSONArray arr = new JSONArray(); + JSON val = JSON.CreateInt(999); + + AssertTrue(arr.Push(val)); + AssertEq(arr.GetInt(0), 999); + + delete val; + delete arr; + } + TestEnd(); + + // Test Get with handle + TestStart("Array_GetHandle"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(42); + + JSON val = arr.Get(0); + AssertValidHandle(val); + AssertEq(val.GetInt(), 42); + + delete val; + delete arr; + } + TestEnd(); + + // Test FromInt + TestStart("Array_FromInt"); + { + int values[] = {10, 20, 30, 40, 50}; + JSONArray arr = JSONArray.FromInt(values, sizeof(values)); + + AssertValidHandle(arr); + AssertEq(arr.Length, 5); + AssertEq(arr.GetInt(0), 10); + AssertEq(arr.GetInt(2), 30); + AssertEq(arr.GetInt(4), 50); + + delete arr; + } + TestEnd(); + + TestStart("Array_FromInt_Negative"); + { + int values[] = {-100, -50, 0, 50, 100}; + JSONArray arr = JSONArray.FromInt(values, sizeof(values)); + + AssertValidHandle(arr); + AssertEq(arr.Length, 5); + AssertEq(arr.GetInt(0), -100); + AssertEq(arr.GetInt(2), 0); + AssertEq(arr.GetInt(4), 100); + + delete arr; + } + TestEnd(); + + // Test FromInt64 + TestStart("Array_FromInt64_Mixed"); + { + char values[][] = {"9223372036854775807", "-9223372036854775808", "0", "18446744073709551615"}; + JSONArray arr = JSONArray.FromInt64(values, sizeof(values)); + + AssertValidHandle(arr); + AssertEq(arr.Length, 4); + + char buffer[32]; + arr.GetInt64(0, buffer, sizeof(buffer)); + AssertStrEq(buffer, "9223372036854775807"); + + arr.GetInt64(1, buffer, sizeof(buffer)); + AssertStrEq(buffer, "-9223372036854775808"); + + arr.GetInt64(2, buffer, sizeof(buffer)); + AssertStrEq(buffer, "0"); + + arr.GetInt64(3, buffer, sizeof(buffer)); + AssertStrEq(buffer, "18446744073709551615"); + + delete arr; + } + TestEnd(); + + TestStart("Array_FromInt64_LargeValues"); + { + char values[][] = {"1234567890123456", "9876543210987654", "5555555555555555"}; + JSONArray arr = JSONArray.FromInt64(values, sizeof(values)); + + AssertValidHandle(arr); + AssertEq(arr.Length, 3); + + char buffer[32]; + arr.GetInt64(1, buffer, sizeof(buffer)); + AssertStrEq(buffer, "9876543210987654"); + + delete arr; + } + TestEnd(); + + // Test FromBool + TestStart("Array_FromBool"); + { + bool values[] = {true, false, true, true, false}; + JSONArray arr = JSONArray.FromBool(values, sizeof(values)); + + AssertValidHandle(arr); + AssertEq(arr.Length, 5); + AssertTrue(arr.GetBool(0)); + AssertFalse(arr.GetBool(1)); + AssertTrue(arr.GetBool(2)); + AssertTrue(arr.GetBool(3)); + AssertFalse(arr.GetBool(4)); + + delete arr; + } + TestEnd(); + + TestStart("Array_FromBool_AllTrue"); + { + bool values[] = {true, true, true}; + JSONArray arr = JSONArray.FromBool(values, sizeof(values)); + + AssertValidHandle(arr); + AssertEq(arr.Length, 3); + AssertTrue(arr.GetBool(0)); + AssertTrue(arr.GetBool(1)); + AssertTrue(arr.GetBool(2)); + + delete arr; + } + TestEnd(); + + // Test FromFloat + TestStart("Array_FromFloat"); + { + float values[] = {1.1, 2.2, 3.3, 4.4, 5.5}; + JSONArray arr = JSONArray.FromFloat(values, sizeof(values)); + + AssertValidHandle(arr); + AssertEq(arr.Length, 5); + AssertFloatEq(arr.GetFloat(0), 1.1); + AssertFloatEq(arr.GetFloat(2), 3.3, "", 0.01); + AssertFloatEq(arr.GetFloat(4), 5.5); + + delete arr; + } + TestEnd(); + + TestStart("Array_FromFloat_Negative"); + { + float values[] = {-3.14, 0.0, 2.718, -1.414}; + JSONArray arr = JSONArray.FromFloat(values, sizeof(values)); + + AssertValidHandle(arr); + AssertEq(arr.Length, 4); + AssertFloatEq(arr.GetFloat(0), -3.14); + AssertFloatEq(arr.GetFloat(1), 0.0); + AssertFloatEq(arr.GetFloat(2), 2.718); + + delete arr; + } + TestEnd(); + + // Test Insert methods + TestStart("Array_InsertInt"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(1); + arr.PushInt(3); + arr.PushInt(4); + + AssertTrue(arr.InsertInt(1, 2)); + AssertEq(arr.Length, 4); + AssertEq(arr.GetInt(0), 1); + AssertEq(arr.GetInt(1), 2); + AssertEq(arr.GetInt(2), 3); + AssertEq(arr.GetInt(3), 4); + + delete arr; + } + TestEnd(); + + TestStart("Array_InsertBool"); + { + JSONArray arr = new JSONArray(); + arr.PushBool(true); + arr.PushBool(true); + + AssertTrue(arr.InsertBool(1, false)); + AssertEq(arr.Length, 3); + AssertTrue(arr.GetBool(0)); + AssertFalse(arr.GetBool(1)); + AssertTrue(arr.GetBool(2)); + + delete arr; + } + TestEnd(); + + TestStart("Array_InsertFloat"); + { + JSONArray arr = new JSONArray(); + arr.PushFloat(1.1); + arr.PushFloat(3.3); + + AssertTrue(arr.InsertFloat(1, 2.2)); + AssertEq(arr.Length, 3); + AssertFloatEq(arr.GetFloat(0), 1.1); + AssertFloatEq(arr.GetFloat(1), 2.2); + AssertFloatEq(arr.GetFloat(2), 3.3, "", 0.01); + + delete arr; + } + TestEnd(); + + TestStart("Array_InsertString"); + { + JSONArray arr = new JSONArray(); + arr.PushString("first"); + arr.PushString("third"); + + AssertTrue(arr.InsertString(1, "second")); + AssertEq(arr.Length, 3); + + char buffer[64]; + arr.GetString(0, buffer, sizeof(buffer)); + AssertStrEq(buffer, "first"); + + arr.GetString(1, buffer, sizeof(buffer)); + AssertStrEq(buffer, "second"); + + arr.GetString(2, buffer, sizeof(buffer)); + AssertStrEq(buffer, "third"); + + delete arr; + } + TestEnd(); + + TestStart("Array_InsertInt64"); + { + JSONArray arr = new JSONArray(); + arr.PushInt64("1111111111111111"); + arr.PushInt64("3333333333333333"); + + AssertTrue(arr.InsertInt64(1, "2222222222222222")); + AssertEq(arr.Length, 3); + + char buffer[32]; + arr.GetInt64(0, buffer, sizeof(buffer)); + AssertStrEq(buffer, "1111111111111111"); + + arr.GetInt64(1, buffer, sizeof(buffer)); + AssertStrEq(buffer, "2222222222222222"); + + arr.GetInt64(2, buffer, sizeof(buffer)); + AssertStrEq(buffer, "3333333333333333"); + + delete arr; + } + TestEnd(); + + TestStart("Array_InsertNull"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(1); + arr.PushInt(2); + + AssertTrue(arr.InsertNull(1)); + AssertEq(arr.Length, 3); + AssertEq(arr.GetInt(0), 1); + AssertTrue(arr.IsNull(1)); + AssertEq(arr.GetInt(2), 2); + + delete arr; + } + TestEnd(); + + TestStart("Array_InsertAtBeginning"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(2); + arr.PushInt(3); + + AssertTrue(arr.InsertInt(0, 1)); + AssertEq(arr.Length, 3); + AssertEq(arr.GetInt(0), 1); + AssertEq(arr.GetInt(1), 2); + AssertEq(arr.GetInt(2), 3); + + delete arr; + } + TestEnd(); + + TestStart("Array_InsertAtEnd"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(1); + arr.PushInt(2); + + AssertTrue(arr.InsertInt(2, 3)); + AssertEq(arr.Length, 3); + AssertEq(arr.GetInt(0), 1); + AssertEq(arr.GetInt(1), 2); + AssertEq(arr.GetInt(2), 3); + + delete arr; + } + TestEnd(); + + TestStart("Array_InsertHandle"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(1); + arr.PushInt(3); + + JSON val = JSON.CreateInt(2); + AssertTrue(arr.Insert(1, val)); + + AssertEq(arr.Length, 3); + AssertEq(arr.GetInt(0), 1); + AssertEq(arr.GetInt(1), 2); + AssertEq(arr.GetInt(2), 3); + + delete val; + delete arr; + } + TestEnd(); + + // Test Prepend methods + TestStart("Array_PrependInt"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(2); + arr.PushInt(3); + + AssertTrue(arr.PrependInt(1)); + AssertEq(arr.Length, 3); + AssertEq(arr.GetInt(0), 1); + AssertEq(arr.GetInt(1), 2); + AssertEq(arr.GetInt(2), 3); + + delete arr; + } + TestEnd(); + + TestStart("Array_PrependBool"); + { + JSONArray arr = new JSONArray(); + arr.PushBool(false); + arr.PushBool(false); + + AssertTrue(arr.PrependBool(true)); + AssertEq(arr.Length, 3); + AssertTrue(arr.GetBool(0)); + AssertFalse(arr.GetBool(1)); + AssertFalse(arr.GetBool(2)); + + delete arr; + } + TestEnd(); + + TestStart("Array_PrependFloat"); + { + JSONArray arr = new JSONArray(); + arr.PushFloat(2.2); + arr.PushFloat(3.3); + + AssertTrue(arr.PrependFloat(1.1)); + AssertEq(arr.Length, 3); + AssertFloatEq(arr.GetFloat(0), 1.1); + AssertFloatEq(arr.GetFloat(1), 2.2); + + delete arr; + } + TestEnd(); + + TestStart("Array_PrependString"); + { + JSONArray arr = new JSONArray(); + arr.PushString("second"); + arr.PushString("third"); + + AssertTrue(arr.PrependString("first")); + AssertEq(arr.Length, 3); + + char buffer[64]; + arr.GetString(0, buffer, sizeof(buffer)); + AssertStrEq(buffer, "first"); + + arr.GetString(1, buffer, sizeof(buffer)); + AssertStrEq(buffer, "second"); + + delete arr; + } + TestEnd(); + + TestStart("Array_PrependInt64"); + { + JSONArray arr = new JSONArray(); + arr.PushInt64("2222222222222222"); + arr.PushInt64("3333333333333333"); + + AssertTrue(arr.PrependInt64("1111111111111111")); + AssertEq(arr.Length, 3); + + char buffer[32]; + arr.GetInt64(0, buffer, sizeof(buffer)); + AssertStrEq(buffer, "1111111111111111"); + + arr.GetInt64(1, buffer, sizeof(buffer)); + AssertStrEq(buffer, "2222222222222222"); + + delete arr; + } + TestEnd(); + + TestStart("Array_PrependNull"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(1); + arr.PushInt(2); + + AssertTrue(arr.PrependNull()); + AssertEq(arr.Length, 3); + AssertTrue(arr.IsNull(0)); + AssertEq(arr.GetInt(1), 1); + AssertEq(arr.GetInt(2), 2); + + delete arr; + } + TestEnd(); + + TestStart("Array_PrependHandle"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(2); + arr.PushInt(3); + + JSON val = JSON.CreateInt(1); + AssertTrue(arr.Prepend(val)); + + AssertEq(arr.Length, 3); + AssertEq(arr.GetInt(0), 1); + AssertEq(arr.GetInt(1), 2); + + delete val; + delete arr; + } + TestEnd(); + + TestStart("Array_PrependToEmpty"); + { + JSONArray arr = new JSONArray(); + + AssertTrue(arr.PrependInt(1)); + AssertEq(arr.Length, 1); + AssertEq(arr.GetInt(0), 1); + + delete arr; + } + TestEnd(); + + // Test combined Insert and Prepend + TestStart("Array_CombinedInsertPrepend"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(5); + arr.PrependInt(1); + arr.InsertInt(1, 3); + arr.InsertInt(1, 2); + arr.InsertInt(3, 4); + + AssertEq(arr.Length, 5); + AssertEq(arr.GetInt(0), 1); + AssertEq(arr.GetInt(1), 2); + AssertEq(arr.GetInt(2), 3); + AssertEq(arr.GetInt(3), 4); + AssertEq(arr.GetInt(4), 5); + + delete arr; + } + TestEnd(); +} + +// ============================================================================ +// 2.4 Parse & Serialize Tests +// ============================================================================ + +void Test_ParseAndSerialize() +{ + PrintToServer("\n[Category] Parse & Serialize Tests"); + + // Test Parse string + TestStart("Parse_SimpleObject"); + { + JSON json = JSON.Parse("{\"key\":\"value\",\"num\":42}"); + AssertValidHandle(json); + AssertTrue(json.IsObject); + delete json; + } + TestEnd(); + + TestStart("Parse_SimpleArray"); + { + JSON json = JSON.Parse("[1,2,3,4,5]"); + AssertValidHandle(json); + AssertTrue(json.IsArray); + delete json; + } + TestEnd(); + + TestStart("Parse_NestedStructure"); + { + JSON json = JSON.Parse("{\"user\":{\"name\":\"test\",\"age\":25},\"items\":[1,2,3]}"); + AssertValidHandle(json); + delete json; + } + TestEnd(); + + // Test mutable/immutable + TestStart("Parse_ImmutableDocument"); + { + JSON json = JSON.Parse("{\"key\":\"value\"}"); + AssertValidHandle(json); + AssertTrue(json.IsImmutable); + AssertFalse(json.IsMutable); + delete json; + } + TestEnd(); + + TestStart("Parse_MutableDocument"); + { + JSON json = JSON.Parse("{\"key\":\"value\"}", .is_mutable_doc = true); + AssertValidHandle(json); + AssertTrue(json.IsMutable); + AssertFalse(json.IsImmutable); + delete json; + } + TestEnd(); + + // Test ValCount (only works on immutable documents) + TestStart("Parse_ValCount_SimpleObject"); + { + JSON json = JSON.Parse("{\"a\":1,\"b\":2,\"c\":3}"); + AssertValidHandle(json); + int valCount = json.ValCount; + // Object has 7 values: 1 object + 3 keys ("a","b","c") + 3 integer values (1,2,3) + AssertEq(valCount, 7, "Simple object should have 7 values (object + keys + values)"); + delete json; + } + TestEnd(); + + TestStart("Parse_ValCount_NestedStructure"); + { + // {"user":{"name":"John","age":30},"items":[1,2,3]} + // Root object: 1 + // "user" key + nested object: 2 + // "name" key + "John" value: 2 + // "age" key + 30 value: 2 + // "items" key + array: 2 + // Three integers in array: 3 + // Total: 1 + 2 + 2 + 2 + 2 + 3 = 12 + JSON json = JSON.Parse("{\"user\":{\"name\":\"John\",\"age\":30},\"items\":[1,2,3]}"); + AssertValidHandle(json); + int valCount = json.ValCount; + AssertTrue(valCount > 0, "Nested structure should have multiple values"); + delete json; + } + TestEnd(); + + TestStart("Parse_ValCount_Array"); + { + JSON json = JSON.Parse("[1,2,3,4,5]"); + AssertValidHandle(json); + int valCount = json.ValCount; + // Array itself + 5 integers = 6 + AssertEq(valCount, 6, "Array with 5 elements should have 6 values"); + delete json; + } + TestEnd(); + + TestStart("Parse_ValCount_MutableReturnsZero"); + { + JSON json = JSON.Parse("{\"key\":\"value\"}", .is_mutable_doc = true); + AssertValidHandle(json); + int valCount = json.ValCount; + AssertEq(valCount, 0, "ValCount should return 0 for mutable documents"); + delete json; + } + TestEnd(); + + TestStart("Parse_ValCount_CreatedObjectReturnsZero"); + { + JSONObject obj = new JSONObject(); + obj.SetInt("a", 1); + obj.SetInt("b", 2); + int valCount = obj.ValCount; + AssertEq(valCount, 0, "ValCount should return 0 for created (mutable) objects"); + delete obj; + } + TestEnd(); + + // Test ToString + TestStart("Serialize_ToString"); + { + JSONObject obj = new JSONObject(); + obj.SetInt("num", 42); + obj.SetString("str", "test"); + + char buffer[256]; + int len = obj.ToString(buffer, sizeof(buffer)); + AssertTrue(len > 0); + Assert(StrContains(buffer, "num") != -1); + Assert(StrContains(buffer, "test") != -1); + + delete obj; + } + TestEnd(); + + TestStart("Serialize_ToString_Pretty"); + { + JSONObject obj = new JSONObject(); + obj.SetInt("a", 1); + obj.SetInt("b", 2); + + char buffer[256]; + int len = obj.ToString(buffer, sizeof(buffer), JSON_WRITE_PRETTY); + AssertTrue(len > 0); + Assert(StrContains(buffer, "\n") != -1, "Pretty output should contain newlines"); + + delete obj; + } + TestEnd(); + + // Test GetSerializedSize + TestStart("Serialize_GetSerializedSize"); + { + JSONObject obj = new JSONObject(); + obj.SetInt("test", 123); + + int size = obj.GetSerializedSize(); + AssertTrue(size > 0); + + char[] buffer = new char[size]; + int written = obj.ToString(buffer, size); + AssertEq(written, size); + + delete obj; + } + TestEnd(); + + // Test read flags + TestStart("Parse_WithTrailingCommas"); + { + JSON json = JSON.Parse("[1,2,3,]", .flag = JSON_READ_ALLOW_TRAILING_COMMAS); + AssertValidHandle(json); + delete json; + } + TestEnd(); + + TestStart("Parse_WithComments"); + { + JSON json = JSON.Parse("/* comment */ {\"key\":\"value\"}", .flag = JSON_READ_ALLOW_COMMENTS); + AssertValidHandle(json); + delete json; + } + TestEnd(); + + // Test file operations (create temporary test file) + TestStart("Parse_ToFile_FromFile"); + { + JSONObject obj = new JSONObject(); + obj.SetInt("filetest", 999); + obj.SetString("name", "testfile"); + + // Write to file + AssertTrue(obj.ToFile("json_test_temp.json")); + + // Read from file + JSONObject loaded = JSON.Parse("json_test_temp.json", true); + AssertValidHandle(loaded); + + // Verify content + JSONObject loadedObj = loaded; + AssertEq(loadedObj.GetInt("filetest"), 999); + + char buffer[64]; + loadedObj.GetString("name", buffer, sizeof(buffer)); + AssertStrEq(buffer, "testfile"); + + delete obj; + delete loaded; + + // Cleanup + DeleteFile("json_test_temp.json"); + } + TestEnd(); + + // Test round-trip serialization + TestStart("Parse_RoundTrip"); + { + char original[] = "{\"int\":42,\"float\":3.14,\"bool\":true,\"str\":\"test\",\"null\":null}"; + JSON json1 = JSON.Parse(original); + + char buffer[256]; + json1.ToString(buffer, sizeof(buffer)); + + JSON json2 = JSON.Parse(buffer); + AssertTrue(JSON.Equals(json1, json2)); + + delete json1; + delete json2; + } + TestEnd(); +} + +// ============================================================================ +// 2.5 Iterator Tests +// ============================================================================ + +void Test_Iterators() +{ + PrintToServer("\n[Category] Iterator Tests"); + + // Test ForeachObject + TestStart("Iterator_ForeachObject"); + { + JSONObject obj = new JSONObject(); + obj.SetInt("a", 1); + obj.SetInt("b", 2); + obj.SetInt("c", 3); + + int count = 0; + char key[32]; + JSONObjIter iter = new JSONObjIter(obj); + + while (iter.Next(key, sizeof(key))) + { + count++; + JSON value = iter.Value; + AssertValidHandle(value); + delete value; + } + AssertFalse(iter.HasNext); + AssertEq(count, 3); + + AssertTrue(iter.Reset()); + + count = 0; + while (iter.Next(key, sizeof(key))) + { + count++; + JSON value = iter.Value; + AssertValidHandle(value); + delete value; + } + AssertEq(count, 3); + delete iter; + delete obj; + } + TestEnd(); + + // Test ForeachArray + TestStart("Iterator_ForeachArray"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(10); + arr.PushInt(20); + arr.PushInt(30); + + int count = 0; + JSONArrIter iter = new JSONArrIter(arr); + + while (iter.HasNext) + { + JSON value = iter.Next; + AssertValidHandle(value); + AssertEq(iter.Index, count); + delete value; + count++; + } + AssertFalse(iter.HasNext); + AssertEq(count, 3); + + AssertTrue(iter.Reset()); + + count = 0; + while (iter.HasNext) + { + JSON value = iter.Next; + AssertValidHandle(value); + AssertEq(iter.Index, count); + delete value; + count++; + } + AssertEq(count, 3); + delete iter; + delete arr; + } + TestEnd(); + + // Test ForeachKey + TestStart("Iterator_ForeachKey"); + { + JSONObject obj = new JSONObject(); + obj.SetInt("key1", 1); + obj.SetInt("key2", 2); + obj.SetInt("key3", 3); + + int count = 0; + char key[32]; + JSONObjIter iter = new JSONObjIter(obj); + + while (iter.Next(key, sizeof(key))) + { + AssertTrue(strlen(key) > 0); + count++; + } + AssertFalse(iter.HasNext); + AssertEq(count, 3); + + AssertTrue(iter.Reset()); + + count = 0; + while (iter.Next(key, sizeof(key))) + { + AssertTrue(strlen(key) > 0); + count++; + } + AssertEq(count, 3); + delete iter; + delete obj; + } + TestEnd(); + + // Test ForeachIndex + TestStart("Iterator_ForeachIndex"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(1); + arr.PushInt(2); + arr.PushInt(3); + + int count = 0; + JSONArrIter iter = new JSONArrIter(arr); + + while (iter.HasNext) + { + JSON value = iter.Next; + AssertEq(iter.Index, count); + delete value; + count++; + } + AssertFalse(iter.HasNext); + AssertEq(count, 3); + + AssertTrue(iter.Reset()); + + count = 0; + while (iter.HasNext) + { + JSON value = iter.Next; + AssertEq(iter.Index, count); + delete value; + count++; + } + AssertEq(count, 3); + delete iter; + delete arr; + } + TestEnd(); + + // Test empty iterations + TestStart("Iterator_EmptyObject"); + { + JSONObject obj = new JSONObject(); + char key[32]; + JSONObjIter iter = new JSONObjIter(obj); + AssertFalse(iter.Next(key, sizeof(key))); + AssertTrue(iter.Reset()); + AssertFalse(iter.Next(key, sizeof(key))); + delete iter; + + delete obj; + } + TestEnd(); + + TestStart("Iterator_EmptyArray"); + { + JSONArray arr = new JSONArray(); + JSONArrIter iter = new JSONArrIter(arr); + AssertFalse(iter.HasNext); + AssertTrue(iter.Reset()); + AssertFalse(iter.HasNext); + delete iter; + + delete arr; + } + TestEnd(); +} + +// ============================================================================ +// 2.6 JSON Pointer Tests +// ============================================================================ + +void Test_JSONPointer() +{ + PrintToServer("\n[Category] JSON Pointer Tests"); + + // Test PtrSet methods + TestStart("Pointer_PtrSetInt"); + { + JSONObject obj = new JSONObject(); + AssertTrue(obj.PtrSetInt("/number", 42)); + AssertEq(obj.PtrGetInt("/number"), 42); + delete obj; + } + TestEnd(); + + TestStart("Pointer_PtrSetFloat"); + { + JSONObject obj = new JSONObject(); + AssertTrue(obj.PtrSetFloat("/pi", 3.14159)); + AssertFloatEq(obj.PtrGetFloat("/pi"), 3.14159); + delete obj; + } + TestEnd(); + + TestStart("Pointer_PtrSetBool"); + { + JSONObject obj = new JSONObject(); + AssertTrue(obj.PtrSetBool("/flag", true)); + AssertTrue(obj.PtrGetBool("/flag")); + delete obj; + } + TestEnd(); + + TestStart("Pointer_PtrSetString"); + { + JSONObject obj = new JSONObject(); + AssertTrue(obj.PtrSetString("/name", "test")); + + char buffer[64]; + AssertTrue(obj.PtrGetString("/name", buffer, sizeof(buffer))); + AssertStrEq(buffer, "test"); + + delete obj; + } + TestEnd(); + + TestStart("Pointer_PtrSetInt64"); + { + JSONObject obj = new JSONObject(); + AssertTrue(obj.PtrSetInt64("/bignum", "9223372036854775807")); + + char buffer[32]; + AssertTrue(obj.PtrGetInt64("/bignum", buffer, sizeof(buffer))); + AssertStrEq(buffer, "9223372036854775807"); + + delete obj; + } + TestEnd(); + + TestStart("Pointer_PtrSetNull"); + { + JSONObject obj = new JSONObject(); + AssertTrue(obj.PtrSetNull("/nullable")); + AssertTrue(obj.PtrGetIsNull("/nullable")); + delete obj; + } + TestEnd(); + + // Test nested path creation + TestStart("Pointer_NestedPathCreation"); + { + JSONObject obj = new JSONObject(); + AssertTrue(obj.PtrSetInt("/a/b/c/d", 123)); + AssertEq(obj.PtrGetInt("/a/b/c/d"), 123); + delete obj; + } + TestEnd(); + + // Test PtrGet with handle + TestStart("Pointer_PtrGet"); + { + JSONObject obj = new JSONObject(); + obj.PtrSetInt("/test", 999); + + JSON val = obj.PtrGet("/test"); + AssertValidHandle(val); + AssertEq(val.GetInt(), 999); + + delete val; + delete obj; + } + TestEnd(); + + // Test PtrAdd methods + TestStart("Pointer_PtrAddInt"); + { + JSONObject obj = new JSONObject(); + obj.PtrSetInt("/arr/0", 1); + AssertTrue(obj.PtrAddInt("/arr/1", 2)); + AssertEq(obj.PtrGetInt("/arr/1"), 2); + delete obj; + } + TestEnd(); + + TestStart("Pointer_PtrAddString"); + { + JSONObject obj = new JSONObject(); + obj.PtrSetString("/items/0", "first"); + AssertTrue(obj.PtrAddString("/items/1", "second")); + + char buffer[64]; + obj.PtrGetString("/items/1", buffer, sizeof(buffer)); + AssertStrEq(buffer, "second"); + + delete obj; + } + TestEnd(); + + // Test PtrRemove + TestStart("Pointer_PtrRemove"); + { + JSONObject obj = new JSONObject(); + obj.PtrSetInt("/remove_me", 1); + obj.PtrSetInt("/keep_me", 2); + + AssertTrue(obj.PtrRemove("/remove_me")); + + JSON val; + obj.PtrTryGetVal("/remove_me", val); + AssertNullHandle(val); + + delete obj; + } + TestEnd(); + + // Test PtrGetLength + TestStart("Pointer_PtrGetLength"); + { + JSONObject obj = new JSONObject(); + obj.PtrSetString("/text", "hello"); + + int len = obj.PtrGetLength("/text"); + AssertEq(len, 6); // Including null terminator + + delete obj; + } + TestEnd(); + + // Test PtrTryGet methods + TestStart("Pointer_PtrTryGetInt"); + { + JSONObject obj = new JSONObject(); + obj.PtrSetInt("/value", 42); + + int value; + AssertTrue(obj.PtrTryGetInt("/value", value)); + AssertEq(value, 42); + + AssertFalse(obj.PtrTryGetInt("/nonexistent", value)); + + delete obj; + } + TestEnd(); + + TestStart("Pointer_PtrTryGetBool"); + { + JSONObject obj = new JSONObject(); + obj.PtrSetBool("/flag", true); + + bool value; + AssertTrue(obj.PtrTryGetBool("/flag", value)); + AssertTrue(value); + + delete obj; + } + TestEnd(); + + TestStart("Pointer_PtrTryGetFloat"); + { + JSONObject obj = new JSONObject(); + obj.PtrSetFloat("/pi", 3.14); + + float value; + AssertTrue(obj.PtrTryGetFloat("/pi", value)); + AssertFloatEq(value, 3.14); + + delete obj; + } + TestEnd(); + + TestStart("Pointer_PtrTryGetString"); + { + JSONObject obj = new JSONObject(); + obj.PtrSetString("/name", "test"); + + char buffer[64]; + AssertTrue(obj.PtrTryGetString("/name", buffer, sizeof(buffer))); + AssertStrEq(buffer, "test"); + + delete obj; + } + TestEnd(); + + TestStart("Pointer_PtrTryGetInt64"); + { + JSONObject obj = new JSONObject(); + obj.PtrSetInt64("/bignum", "123456789012345"); + + char buffer[32]; + AssertTrue(obj.PtrTryGetInt64("/bignum", buffer, sizeof(buffer))); + AssertStrEq(buffer, "123456789012345"); + + delete obj; + } + TestEnd(); + + TestStart("Pointer_PtrTryGetVal"); + { + JSONObject obj = new JSONObject(); + obj.PtrSetInt("/test", 42); + + JSON value; + AssertTrue(obj.PtrTryGetVal("/test", value)); + AssertValidHandle(value); + AssertEq(value.GetInt(), 42); + + delete value; + delete obj; + } + TestEnd(); +} + +// ============================================================================ +// 2.7 Advanced Features Tests +// ============================================================================ + +void Test_AdvancedFeatures() +{ + PrintToServer("\n[Category] Advanced Features Tests"); + + // Test DeepCopy + TestStart("Advanced_DeepCopy_Object"); + { + JSONObject original = new JSONObject(); + original.SetInt("num", 42); + original.SetString("str", "test"); + + JSONObject target = new JSONObject(); + JSONObject copy = JSON.DeepCopy(target, original); + + AssertValidHandle(copy); + AssertEq(copy.GetInt("num"), 42); + + char buffer[64]; + copy.GetString("str", buffer, sizeof(buffer)); + AssertStrEq(buffer, "test"); + + delete original; + delete target; + delete copy; + } + TestEnd(); + + TestStart("Advanced_DeepCopy_Array"); + { + JSONArray original = new JSONArray(); + original.PushInt(1); + original.PushInt(2); + original.PushInt(3); + + JSONArray target = new JSONArray(); + JSONArray copy = JSON.DeepCopy(target, original); + + AssertValidHandle(copy); + AssertEq(copy.Length, 3); + AssertEq(copy.GetInt(0), 1); + AssertEq(copy.GetInt(2), 3); + + delete original; + delete target; + delete copy; + } + TestEnd(); + + // Test Equals + TestStart("Advanced_Equals_True"); + { + JSONObject obj1 = new JSONObject(); + obj1.SetInt("a", 1); + obj1.SetString("b", "test"); + + JSONObject obj2 = new JSONObject(); + obj2.SetInt("a", 1); + obj2.SetString("b", "test"); + + AssertTrue(JSON.Equals(obj1, obj2)); + + delete obj1; + delete obj2; + } + TestEnd(); + + TestStart("Advanced_Equals_False"); + { + JSONObject obj1 = new JSONObject(); + obj1.SetInt("a", 1); + + JSONObject obj2 = new JSONObject(); + obj2.SetInt("a", 2); + + AssertFalse(JSON.Equals(obj1, obj2)); + + delete obj1; + delete obj2; + } + TestEnd(); + + // Test ToMutable/ToImmutable + TestStart("Advanced_ToMutable"); + { + JSON immutable = JSON.Parse("{\"key\":\"value\"}"); + AssertTrue(immutable.IsImmutable); + + JSON mutable = immutable.ToMutable(); + AssertValidHandle(mutable); + AssertTrue(mutable.IsMutable); + + delete immutable; + delete mutable; + } + TestEnd(); + + TestStart("Advanced_ToImmutable"); + { + JSONObject mutable = new JSONObject(); + mutable.SetInt("key", 42); + AssertTrue(mutable.IsMutable); + + JSON immutable = mutable.ToImmutable(); + AssertValidHandle(immutable); + AssertTrue(immutable.IsImmutable); + + delete mutable; + delete immutable; + } + TestEnd(); + + // Test ApplyJsonPatch (new value, immutable result) + TestStart("Advanced_ApplyJsonPatch"); + { + JSONObject original = new JSONObject(); + original.SetInt("score", 10); + original.SetString("name", "bot"); + + JSON patch = JSON.Parse("[{\"op\":\"replace\",\"path\":\"/score\",\"value\":42}]"); + AssertValidHandle(patch); + + JSON result = original.ApplyJsonPatch(patch); + AssertValidHandle(result); + AssertTrue(result.IsImmutable); + + AssertEq(result.PtrGetInt("/score"), 42); + char buffer[32]; + result.PtrGetString("/name", buffer, sizeof(buffer)); + AssertStrEq(buffer, "bot"); + + // ensure original unchanged + AssertEq(original.GetInt("score"), 10); + + delete result; + delete patch; + delete original; + } + TestEnd(); + + // Test ApplyJsonPatch resultMutable = true + TestStart("Advanced_ApplyJsonPatch_MutableResult"); + { + JSONObject original = new JSONObject(); + original.SetInt("count", 1); + + JSON patch = JSON.Parse("[{\"op\":\"add\",\"path\":\"/newField\",\"value\":\"hello\"}]"); + AssertValidHandle(patch); + + JSON result = original.ApplyJsonPatch(patch, true); + AssertValidHandle(result); + AssertTrue(result.IsMutable); + + char buffer[16]; + result.PtrGetString("/newField", buffer, sizeof(buffer)); + AssertStrEq(buffer, "hello"); + + delete result; + delete patch; + delete original; + } + TestEnd(); + + // Test JsonPatchInPlace + TestStart("Advanced_JsonPatchInPlace"); + { + JSONObject target = new JSONObject(); + target.SetInt("score", 5); + target.SetInt("lives", 3); + + JSON patch = JSON.Parse("[{\"op\":\"remove\",\"path\":\"/lives\"},{\"op\":\"replace\",\"path\":\"/score\",\"value\":9}]"); + AssertValidHandle(patch); + + AssertTrue(target.JsonPatchInPlace(patch)); + AssertEq(target.GetInt("score"), 9); + AssertFalse(target.HasKey("lives")); + + delete patch; + delete target; + } + TestEnd(); + + // Test ApplyMergePatch (immutable result) + TestStart("Advanced_ApplyMergePatch"); + { + JSONObject original = new JSONObject(); + original.PtrSetString("/settings/mode", "coop"); + original.PtrSetInt("/settings/difficulty", 1); + + JSON mergePatch = JSON.Parse("{\"settings\":{\"difficulty\":3,\"friendlyFire\":true}}"); + AssertValidHandle(mergePatch); + + JSON result = original.ApplyMergePatch(mergePatch); + AssertValidHandle(result); + AssertTrue(result.IsImmutable); + + AssertEq(result.PtrGetInt("/settings/difficulty"), 3); + AssertTrue(result.PtrGetBool("/settings/friendlyFire")); + + delete result; + delete mergePatch; + delete original; + } + TestEnd(); + + // Test ApplyMergePatch resultMutable = true + TestStart("Advanced_ApplyMergePatch_MutableResult"); + { + JSONObject original = new JSONObject(); + original.PtrSetString("/profile/name", "player"); + + JSON mergePatch = JSON.Parse("{\"profile\":{\"rank\":10}}"); + AssertValidHandle(mergePatch); + + JSON result = original.ApplyMergePatch(mergePatch, true); + AssertValidHandle(result); + AssertTrue(result.IsMutable); + + AssertEq(result.PtrGetInt("/profile/rank"), 10); + char buffer[16]; + result.PtrGetString("/profile/name", buffer, sizeof(buffer)); + AssertStrEq(buffer, "player"); + + delete result; + delete mergePatch; + delete original; + } + TestEnd(); + + // Test MergePatchInPlace + TestStart("Advanced_MergePatchInPlace"); + { + JSONObject target = new JSONObject(); + target.PtrSetString("/config/mode", "coop"); + target.PtrSetInt("/config/players", 4); + + JSON mergePatch = JSON.Parse("{\"config\":{\"players\":6,\"region\":\"EU\"}}"); + AssertValidHandle(mergePatch); + + AssertTrue(target.MergePatchInPlace(mergePatch)); + AssertEq(target.PtrGetInt("/config/players"), 6); + char buffer[16]; + target.PtrGetString("/config/region", buffer, sizeof(buffer)); + AssertStrEq(buffer, "EU"); + + delete mergePatch; + delete target; + } + TestEnd(); + + // Test Pack + TestStart("Advanced_Pack_SimpleObject"); + { + JSONObject json = JSON.Pack("{s:i,s:s,s:b}", + "num", 42, + "str", "test", + "bool", true + ); + + AssertValidHandle(json); + AssertTrue(json.IsObject); + + JSONObject obj = json; + AssertEq(obj.GetInt("num"), 42); + + char buffer[64]; + obj.GetString("str", buffer, sizeof(buffer)); + AssertStrEq(buffer, "test"); + + AssertTrue(obj.GetBool("bool")); + + delete json; + } + TestEnd(); + + TestStart("Advanced_Pack_Array"); + { + JSONArray json = JSON.Pack("[i,i,i]", 1, 2, 3); + + AssertValidHandle(json); + AssertTrue(json.IsArray); + + JSONArray arr = json; + AssertEq(arr.Length, 3); + AssertEq(arr.GetInt(0), 1); + AssertEq(arr.GetInt(2), 3); + + delete json; + } + TestEnd(); + + TestStart("Advanced_Pack_Nested"); + { + JSONObject json = JSON.Pack("{s:{s:s,s:i}}", + "user", + "name", "test", + "age", 25 + ); + + AssertValidHandle(json); + + char buffer[64]; + JSONObject obj = json; + obj.PtrGetString("/user/name", buffer, sizeof(buffer)); + AssertStrEq(buffer, "test"); + AssertEq(obj.PtrGetInt("/user/age"), 25); + + delete json; + } + TestEnd(); + + // Test mixed type array sorting + TestStart("Advanced_MixedTypeSort"); + { + JSONArray json = JSON.Parse("[true, 42, \"hello\", 1.5, false]", .is_mutable_doc = true); + JSONArray arr = json; + + AssertTrue(arr.Sort(JSON_SORT_ASC)); + AssertEq(arr.Length, 5); + + delete json; + } + TestEnd(); +} + +// ============================================================================ +// 2.8 Int64 Operations Tests +// ============================================================================ + +void Test_Int64Operations() +{ + PrintToServer("\n[Category] Int64 Operations Tests"); + + // Test JSON.SetInt64 - Modify value in-place + TestStart("Int64_SetInt64_Positive"); + { + JSON val = JSON.CreateInt(100); + AssertTrue(val.SetInt64("123456789012345")); + + char buffer[32]; + AssertTrue(val.GetInt64(buffer, sizeof(buffer))); + AssertStrEq(buffer, "123456789012345"); + + delete val; + } + TestEnd(); + + TestStart("Int64_SetInt64_Negative"); + { + JSON val = JSON.CreateInt(100); + AssertTrue(val.SetInt64("-987654321098765")); + + char buffer[32]; + AssertTrue(val.GetInt64(buffer, sizeof(buffer))); + AssertStrEq(buffer, "-987654321098765"); + + delete val; + } + TestEnd(); + + TestStart("Int64_SetInt64_Zero"); + { + JSON val = JSON.CreateInt(999); + AssertTrue(val.SetInt64("0")); + + char buffer[32]; + AssertTrue(val.GetInt64(buffer, sizeof(buffer))); + AssertStrEq(buffer, "0"); + + delete val; + } + TestEnd(); + + // Test JSONArray.SetInt64 - Replace value in array + TestStart("Int64_Array_SetInt64"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(100); + arr.PushInt(200); + arr.PushInt(300); + + AssertTrue(arr.SetInt64(1, "5555555555555555")); + + char buffer[32]; + arr.GetInt64(1, buffer, sizeof(buffer)); + AssertStrEq(buffer, "5555555555555555"); + + delete arr; + } + TestEnd(); + + TestStart("Int64_Array_SetInt64_Negative"); + { + JSONArray arr = new JSONArray(); + arr.PushInt64("1111111111111111"); + + AssertTrue(arr.SetInt64(0, "-2222222222222222")); + + char buffer[32]; + arr.GetInt64(0, buffer, sizeof(buffer)); + AssertStrEq(buffer, "-2222222222222222"); + + delete arr; + } + TestEnd(); + + // Test large unsigned int64 values + TestStart("Int64_Array_PushLargeUnsigned"); + { + JSONArray arr = new JSONArray(); + AssertTrue(arr.PushInt64("18446744073709551615")); + + char buffer[32]; + arr.GetInt64(0, buffer, sizeof(buffer)); + AssertStrEq(buffer, "18446744073709551615"); + + delete arr; + } + TestEnd(); + + TestStart("Int64_Array_IndexOfLargeValues"); + { + JSONArray arr = new JSONArray(); + arr.PushInt64("11111111111111111"); + arr.PushInt64("22222222222222222"); + arr.PushInt64("33333333333333333"); + + AssertEq(arr.IndexOfInt64("22222222222222222"), 1); + AssertEq(arr.IndexOfInt64("99999999999999999"), -1); + + delete arr; + } + TestEnd(); + + // Test PtrAddInt64 + TestStart("Int64_Pointer_PtrAddInt64"); + { + JSONObject obj = new JSONObject(); + obj.PtrSetInt("/numbers/0", 1); + AssertTrue(obj.PtrAddInt64("/numbers/1", "7777777777777777")); + + char buffer[32]; + AssertTrue(obj.PtrGetInt64("/numbers/1", buffer, sizeof(buffer))); + AssertStrEq(buffer, "7777777777777777"); + + delete obj; + } + TestEnd(); + + TestStart("Int64_Pointer_PtrAddInt64_Negative"); + { + JSONObject obj = new JSONObject(); + obj.PtrSetInt("/data/0", 1); + AssertTrue(obj.PtrAddInt64("/data/1", "-8888888888888888")); + + char buffer[32]; + AssertTrue(obj.PtrGetInt64("/data/1", buffer, sizeof(buffer))); + AssertStrEq(buffer, "-8888888888888888"); + + delete obj; + } + TestEnd(); + + // Test PtrAddInt64 with large unsigned value + TestStart("Int64_Pointer_PtrAddInt64_LargeUnsigned"); + { + JSONObject obj = new JSONObject(); + obj.PtrSetInt("/bigvals/0", 1); + AssertTrue(obj.PtrAddInt64("/bigvals/1", "18446744073709551615")); + + char buffer[32]; + AssertTrue(obj.PtrGetInt64("/bigvals/1", buffer, sizeof(buffer))); + AssertStrEq(buffer, "18446744073709551615"); + + delete obj; + } + TestEnd(); + + // Test mixed signed/unsigned values + TestStart("Int64_Mixed_SignedUnsigned_Array"); + { + JSONArray arr = new JSONArray(); + arr.PushInt64("-9223372036854775808"); + arr.PushInt64("18446744073709551615"); + arr.PushInt64("0"); + arr.PushInt64("9223372036854775807"); + + AssertEq(arr.Length, 4); + + char buffer[32]; + arr.GetInt64(0, buffer, sizeof(buffer)); + AssertStrEq(buffer, "-9223372036854775808"); + + arr.GetInt64(1, buffer, sizeof(buffer)); + AssertStrEq(buffer, "18446744073709551615"); + + arr.GetInt64(2, buffer, sizeof(buffer)); + AssertStrEq(buffer, "0"); + + arr.GetInt64(3, buffer, sizeof(buffer)); + AssertStrEq(buffer, "9223372036854775807"); + + delete arr; + } + TestEnd(); + + TestStart("Int64_Mixed_SignedUnsigned_Object"); + { + JSONObject obj = new JSONObject(); + obj.SetInt64("min_int64", "-9223372036854775808"); + obj.SetInt64("max_int64", "9223372036854775807"); + obj.SetInt64("max_uint64", "18446744073709551615"); + + char buffer[32]; + + AssertTrue(obj.GetInt64("min_int64", buffer, sizeof(buffer))); + AssertStrEq(buffer, "-9223372036854775808"); + + AssertTrue(obj.GetInt64("max_int64", buffer, sizeof(buffer))); + AssertStrEq(buffer, "9223372036854775807"); + + AssertTrue(obj.GetInt64("max_uint64", buffer, sizeof(buffer))); + AssertStrEq(buffer, "18446744073709551615"); + + delete obj; + } + TestEnd(); + + // Test serialization with int64 + TestStart("Int64_Serialization_ToString"); + { + JSONObject obj = new JSONObject(); + obj.SetInt64("large_num", "9007199254740992"); + obj.SetInt64("negative_num", "-9007199254740992"); + + char json[256]; + int len = obj.ToString(json, sizeof(json)); + AssertTrue(len > 0); + + // Parse it back + JSONObject parsed = JSONObject.FromString(json); + AssertValidHandle(parsed); + + char buffer[32]; + AssertTrue(parsed.GetInt64("large_num", buffer, sizeof(buffer))); + AssertStrEq(buffer, "9007199254740992"); + + AssertTrue(parsed.GetInt64("negative_num", buffer, sizeof(buffer))); + AssertStrEq(buffer, "-9007199254740992"); + + delete obj; + delete parsed; + } + TestEnd(); + + TestStart("Int64_Serialization_Array"); + { + JSONArray arr = new JSONArray(); + arr.PushInt64("1234567890123456"); + arr.PushInt64("-9876543210987654"); + arr.PushInt64("18000000000000000000"); + + char json[256]; + int len = arr.ToString(json, sizeof(json)); + AssertTrue(len > 0); + + // Parse it back + JSONArray parsed = JSONArray.FromString(json); + AssertValidHandle(parsed); + AssertEq(parsed.Length, 3); + + char buffer[32]; + parsed.GetInt64(0, buffer, sizeof(buffer)); + AssertStrEq(buffer, "1234567890123456"); + + parsed.GetInt64(1, buffer, sizeof(buffer)); + AssertStrEq(buffer, "-9876543210987654"); + + parsed.GetInt64(2, buffer, sizeof(buffer)); + AssertStrEq(buffer, "18000000000000000000"); + + delete arr; + delete parsed; + } + TestEnd(); + + // Test boundary values + TestStart("Int64_Boundary_JustAboveInt32Max"); + { + JSON val = JSON.CreateInt64("2147483648"); + AssertValidHandle(val); + + char buffer[32]; + AssertTrue(val.GetInt64(buffer, sizeof(buffer))); + AssertStrEq(buffer, "2147483648"); + + delete val; + } + TestEnd(); + + TestStart("Int64_Boundary_JustBelowInt32Min"); + { + JSON val = JSON.CreateInt64("-2147483649"); + AssertValidHandle(val); + + char buffer[32]; + AssertTrue(val.GetInt64(buffer, sizeof(buffer))); + AssertStrEq(buffer, "-2147483649"); + + delete val; + } + TestEnd(); + + TestStart("Int64_Boundary_UInt32Max"); + { + JSON val = JSON.CreateInt64("4294967295"); + AssertValidHandle(val); + + char buffer[32]; + AssertTrue(val.GetInt64(buffer, sizeof(buffer))); + AssertStrEq(buffer, "4294967295"); + + delete val; + } + TestEnd(); + + TestStart("Int64_Boundary_JustAboveUInt32Max"); + { + JSON val = JSON.CreateInt64("4294967296"); + AssertValidHandle(val); + + char buffer[32]; + AssertTrue(val.GetInt64(buffer, sizeof(buffer))); + AssertStrEq(buffer, "4294967296"); + + delete val; + } + TestEnd(); + + // Test Int64 in nested structures + TestStart("Int64_Nested_ObjectInObject"); + { + JSONObject obj = new JSONObject(); + obj.PtrSetInt64("/outer/inner/deep", "5432109876543210"); + + char buffer[32]; + AssertTrue(obj.PtrGetInt64("/outer/inner/deep", buffer, sizeof(buffer))); + AssertStrEq(buffer, "5432109876543210"); + + delete obj; + } + TestEnd(); + + TestStart("Int64_Nested_ArrayInObject"); + { + JSONObject obj = new JSONObject(); + obj.PtrSetInt64("/data/values/0", "1111111111111111"); + obj.PtrAddInt64("/data/values/1", "2222222222222222"); + obj.PtrAddInt64("/data/values/2", "3333333333333333"); + + char buffer[32]; + AssertTrue(obj.PtrGetInt64("/data/values/0", buffer, sizeof(buffer))); + AssertStrEq(buffer, "1111111111111111"); + + AssertTrue(obj.PtrGetInt64("/data/values/1", buffer, sizeof(buffer))); + AssertStrEq(buffer, "2222222222222222"); + + AssertTrue(obj.PtrGetInt64("/data/values/2", buffer, sizeof(buffer))); + AssertStrEq(buffer, "3333333333333333"); + + delete obj; + } + TestEnd(); + + // Test DeepCopy with int64 + TestStart("Int64_DeepCopy_Object"); + { + JSONObject original = new JSONObject(); + original.SetInt64("bignum", "9999999999999999"); + original.SetInt64("negative", "-8888888888888888"); + + JSONObject target = new JSONObject(); + JSONObject copy = JSON.DeepCopy(target, original); + + AssertValidHandle(copy); + + char buffer[32]; + AssertTrue(copy.GetInt64("bignum", buffer, sizeof(buffer))); + AssertStrEq(buffer, "9999999999999999"); + + AssertTrue(copy.GetInt64("negative", buffer, sizeof(buffer))); + AssertStrEq(buffer, "-8888888888888888"); + + delete original; + delete target; + delete copy; + } + TestEnd(); + + TestStart("Int64_DeepCopy_Array"); + { + JSONArray original = new JSONArray(); + original.PushInt64("1111111111111111"); + original.PushInt64("-2222222222222222"); + original.PushInt64("18446744073709551615"); + + JSONArray target = new JSONArray(); + JSONArray copy = JSON.DeepCopy(target, original); + + AssertValidHandle(copy); + AssertEq(copy.Length, 3); + + char buffer[32]; + copy.GetInt64(0, buffer, sizeof(buffer)); + AssertStrEq(buffer, "1111111111111111"); + + copy.GetInt64(1, buffer, sizeof(buffer)); + AssertStrEq(buffer, "-2222222222222222"); + + copy.GetInt64(2, buffer, sizeof(buffer)); + AssertStrEq(buffer, "18446744073709551615"); + + delete original; + delete target; + delete copy; + } + TestEnd(); + + // Test array sorting with int64 values + TestStart("Int64_Array_Sort_Ascending"); + { + JSONArray arr = new JSONArray(); + arr.PushInt64("9999999999999999"); + arr.PushInt64("1111111111111111"); + arr.PushInt64("5555555555555555"); + arr.PushInt64("3333333333333333"); + + AssertTrue(arr.Sort(JSON_SORT_ASC)); + + char buffer[32]; + arr.GetInt64(0, buffer, sizeof(buffer)); + AssertStrEq(buffer, "1111111111111111"); + + arr.GetInt64(3, buffer, sizeof(buffer)); + AssertStrEq(buffer, "9999999999999999"); + + delete arr; + } + TestEnd(); + + TestStart("Int64_Array_Sort_Descending"); + { + JSONArray arr = new JSONArray(); + arr.PushInt64("1111111111111111"); + arr.PushInt64("5555555555555555"); + arr.PushInt64("3333333333333333"); + arr.PushInt64("9999999999999999"); + + AssertTrue(arr.Sort(JSON_SORT_DESC)); + + char buffer[32]; + arr.GetInt64(0, buffer, sizeof(buffer)); + AssertStrEq(buffer, "9999999999999999"); + + arr.GetInt64(3, buffer, sizeof(buffer)); + AssertStrEq(buffer, "1111111111111111"); + + delete arr; + } + TestEnd(); + + // Test int64 with type checks + TestStart("Int64_TypeChecks"); + { + JSON val = JSON.CreateInt64("9223372036854775807"); + + AssertTrue(val.IsNum); + AssertTrue(val.IsInt); + AssertFalse(val.IsFloat); + AssertFalse(val.IsStr); + AssertFalse(val.IsBool); + + delete val; + } + TestEnd(); + + TestStart("Int64_TypeChecks_Signed"); + { + JSON val = JSON.CreateInt64("-9223372036854775808"); + + AssertTrue(val.IsNum); + AssertTrue(val.IsInt); + AssertTrue(val.IsSint); + AssertFalse(val.IsUint); + + delete val; + } + TestEnd(); + + TestStart("Int64_TypeChecks_LargeUnsigned"); + { + JSON val = JSON.CreateInt64("18446744073709551615"); + + AssertTrue(val.IsNum); + AssertTrue(val.IsInt); + // Large unsigned value should be detected as unsigned integer + AssertTrue(val.IsUint); + AssertFalse(val.IsSint); + + delete val; + } + TestEnd(); +} + +// ============================================================================ +// 2.9 Edge Cases & Error Handling Tests +// ============================================================================ + +void Test_EdgeCases() +{ + PrintToServer("\n[Category] Edge Cases & Error Handling Tests"); + + // Test empty containers + TestStart("EdgeCase_EmptyObject"); + { + JSONObject obj = new JSONObject(); + AssertEq(obj.Size, 0); + AssertFalse(obj.HasKey("anything")); + + char buffer[64]; + int len = obj.ToString(buffer, sizeof(buffer)); + AssertTrue(len > 0); + + delete obj; + } + TestEnd(); + + TestStart("EdgeCase_EmptyArray"); + { + JSONArray arr = new JSONArray(); + AssertEq(arr.Length, 0); + delete arr; + } + TestEnd(); + + // Test nonexistent keys/indices + TestStart("EdgeCase_NonexistentKey"); + { + JSONObject obj = new JSONObject(); + obj.SetInt("exists", 1); + + AssertTrue(obj.HasKey("exists")); + AssertFalse(obj.HasKey("notexists")); + + delete obj; + } + TestEnd(); + + TestStart("EdgeCase_ArrayOutOfBounds"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(1); + + // Verify array bounds + AssertEq(arr.Length, 1); + AssertEq(arr.GetInt(0), 1); + + delete arr; + } + TestEnd(); + + // Test very long strings + TestStart("EdgeCase_LongString"); + { + char longStr[1024]; + for (int i = 0; i < sizeof(longStr) - 1; i++) + { + longStr[i] = 'A' + (i % 26); + } + longStr[sizeof(longStr) - 1] = '\0'; + + JSON val = JSON.CreateString(longStr); + AssertValidHandle(val); + + char retrieved[1024]; + AssertTrue(val.GetString(retrieved, sizeof(retrieved))); + AssertStrEq(retrieved, longStr); + + delete val; + } + TestEnd(); + + // Test deep nesting + TestStart("EdgeCase_DeepNesting"); + { + JSONObject obj = new JSONObject(); + + // Create deeply nested structure + obj.PtrSetInt("/a/b/c/d/e/f/g/h/i/j", 42); + AssertEq(obj.PtrGetInt("/a/b/c/d/e/f/g/h/i/j"), 42); + + delete obj; + } + TestEnd(); + + // Test large arrays + TestStart("EdgeCase_LargeArray"); + { + JSONArray arr = new JSONArray(); + + for (int i = 0; i < 1000; i++) + { + arr.PushInt(i); + } + + AssertEq(arr.Length, 1000); + AssertEq(arr.GetInt(0), 0); + AssertEq(arr.GetInt(999), 999); + + delete arr; + } + TestEnd(); + + // Test large objects + TestStart("EdgeCase_LargeObject"); + { + JSONObject obj = new JSONObject(); + + char key[32]; + for (int i = 0; i < 100; i++) + { + FormatEx(key, sizeof(key), "key_%d", i); + obj.SetInt(key, i); + } + + AssertEq(obj.Size, 100); + AssertEq(obj.GetInt("key_0"), 0); + AssertEq(obj.GetInt("key_99"), 99); + + delete obj; + } + TestEnd(); + + // Test int64 boundaries + TestStart("EdgeCase_Int64_MaxValue"); + { + JSON val = JSON.CreateInt64("9223372036854775807"); + AssertValidHandle(val); + + char buffer[32]; + val.GetInt64(buffer, sizeof(buffer)); + AssertStrEq(buffer, "9223372036854775807"); + + delete val; + } + TestEnd(); + + TestStart("EdgeCase_Int64_MinValue"); + { + JSON val = JSON.CreateInt64("-9223372036854775808"); + AssertValidHandle(val); + + char buffer[32]; + val.GetInt64(buffer, sizeof(buffer)); + AssertStrEq(buffer, "-9223372036854775808"); + + delete val; + } + TestEnd(); + + // Test special float values + TestStart("EdgeCase_Float_VerySmall"); + { + JSON val = JSON.CreateFloat(0.000001); + AssertValidHandle(val); + AssertFloatEq(val.GetFloat(), 0.000001); + delete val; + } + TestEnd(); + + TestStart("EdgeCase_Float_VeryLarge"); + { + JSON val = JSON.CreateFloat(999999.999999); + AssertValidHandle(val); + AssertFloatEq(val.GetFloat(), 999999.999999, "", 0.001); + delete val; + } + TestEnd(); + + // Test special characters in strings + TestStart("EdgeCase_SpecialCharacters"); + { + JSON val = JSON.CreateString("Line1\nLine2\tTabbed\"Quoted\""); + AssertValidHandle(val); + + char buffer[128]; + val.GetString(buffer, sizeof(buffer)); + Assert(StrContains(buffer, "Line1") != -1); + + delete val; + } + TestEnd(); + + // Test removing from empty array + TestStart("EdgeCase_RemoveFromEmptyArray"); + { + JSONArray arr = new JSONArray(); + AssertFalse(arr.RemoveFirst()); + AssertFalse(arr.RemoveLast()); + delete arr; + } + TestEnd(); + + // Test clearing already empty containers + TestStart("EdgeCase_ClearEmpty"); + { + JSONObject obj = new JSONObject(); + AssertTrue(obj.Clear()); + AssertEq(obj.Size, 0); + + JSONArray arr = new JSONArray(); + AssertTrue(arr.Clear()); + AssertEq(arr.Length, 0); + + delete obj; + delete arr; + } + TestEnd(); + + // Test IndexOf on empty array + TestStart("EdgeCase_IndexOfEmpty"); + { + JSONArray arr = new JSONArray(); + AssertEq(arr.IndexOfInt(42), -1); + AssertEq(arr.IndexOfString("test"), -1); + AssertEq(arr.IndexOfBool(true), -1); + delete arr; + } + TestEnd(); + + // Test pointer to nonexistent path + TestStart("EdgeCase_PointerNonexistentPath"); + { + JSONObject obj = new JSONObject(); + + // Note: PtrGet throws exception for nonexistent paths (expected behavior) + // Use PtrTryGet methods for safe access + int intVal; + AssertFalse(obj.PtrTryGetInt("/does/not/exist", intVal)); + + delete obj; + } + TestEnd(); + + // Test sorting empty containers + TestStart("EdgeCase_SortEmpty"); + { + JSONObject obj = new JSONObject(); + AssertTrue(obj.Sort()); + + JSONArray arr = new JSONArray(); + AssertTrue(arr.Sort()); + + delete obj; + delete arr; + } + TestEnd(); + + // Test single element operations + TestStart("EdgeCase_SingleElement"); + { + JSONArray arr = new JSONArray(); + arr.PushInt(42); + + AssertTrue(arr.Sort()); + AssertEq(arr.GetInt(0), 42); + + AssertEq(arr.IndexOfInt(42), 0); + + JSON first = arr.First; + JSON last = arr.Last; + AssertEq(first.GetInt(), last.GetInt()); + + delete first; + delete last; + delete arr; + } + TestEnd(); +} \ No newline at end of file diff --git a/scripting/yyjson_test.sp b/scripting/yyjson_test.sp deleted file mode 100644 index 4b439d3..0000000 --- a/scripting/yyjson_test.sp +++ /dev/null @@ -1,566 +0,0 @@ -#include -#include - -public Plugin myinfo = -{ - name = "YYJSON Test Suite", - author = "ProjectSky", - description = "Test suite for YYJSON extension", - version = "1.0.2", - url = "https://github.com/ProjectSky/sm-ext-yyjson" -}; - -public void OnPluginStart() -{ - RegServerCmd("sm_yyjson_test", Command_RunTests, "Run YYJSON test suite"); -} - -Action Command_RunTests(int args) -{ - // Run all test cases - TestBasicOperations(); - TestArrayOperations(); - TestObjectOperations(); - TestSortOperations(); - TestSearchOperations(); - TestPointerOperations(); - TestIterationOperations(); - TestTypeOperations(); - TestFileOperations(); - TestImmutabilityOperations(); - TestPackOperations(); - TestFromStringsOperations(); - - PrintToServer("[YYJSON] All tests completed!"); - return Plugin_Handled; -} - -void TestBasicOperations() -{ - PrintToServer("[YYJSON] Testing basic operations..."); - - // Test creation and parsing - YYJSONObject obj = new YYJSONObject(); - obj.SetInt("int", 42); - obj.SetFloat("float", 3.14); - obj.SetBool("bool", true); - obj.SetString("string", "hello"); - obj.SetNull("null"); - - // Test serialization - char buffer[1024]; - obj.ToString(buffer, sizeof(buffer)); - PrintToServer("Serialized: %s", buffer); - - // Test type checking - PrintToServer("Type of 'int': %d", obj.Type); - PrintToServer("SubType of 'int': %d", obj.SubType); - PrintToServer("Is object: %d", obj.IsObject); - - // Test value existence - PrintToServer("Has 'int': %d", obj.HasKey("int")); - PrintToServer("Has 'nonexistent': %d", obj.HasKey("nonexistent")); - - // Test value retrieval - PrintToServer("Int value: %d", obj.GetInt("int")); - PrintToServer("Float value: %f", obj.GetFloat("float")); - PrintToServer("Bool value: %d", obj.GetBool("bool")); - - char strBuffer[64]; - obj.GetString("string", strBuffer, sizeof(strBuffer)); - PrintToServer("String value: %s", strBuffer); - - PrintToServer("Is 'null' null: %d", obj.IsNull("null")); - - // Test parsing - YYJSONObject parsed = YYJSON.Parse(buffer); - - // Test equality - PrintToServer("Objects are equal: %d", YYJSON.Equals(obj, parsed)); - - // Test deep copy - YYJSONObject copy = new YYJSONObject(); - YYJSONObject copyResult = YYJSON.DeepCopy(copy, obj); - PrintToServer("Copy equals original: %d", YYJSON.Equals(copyResult, obj)); - - // Test size and read size - PrintToServer("Object size: %d", obj.Size); - PrintToServer("Read size: %d", obj.ReadSize); - - // Test type description - char typeDesc[64]; - YYJSON.GetTypeDesc(obj, typeDesc, sizeof(typeDesc)); - PrintToServer("Type description: %s", typeDesc); - - delete obj; - delete parsed; - delete copy; - delete copyResult; -} - -void TestArrayOperations() -{ - PrintToServer("[YYJSON] Testing array operations..."); - - YYJSONArray arr = new YYJSONArray(); - - // Test push operations - arr.PushInt(1); - arr.PushFloat(2.5); - arr.PushBool(true); - arr.PushString("test"); - arr.PushNull(); - - PrintToServer("Array after push operations:"); - PrintJson(arr); - - // Test get operations - PrintToServer("First element: %d", arr.GetInt(0)); - PrintToServer("Second element: %f", arr.GetFloat(1)); - PrintToServer("Third element: %d", arr.GetBool(2)); - - char strBuffer[64]; - arr.GetString(3, strBuffer, sizeof(strBuffer)); - PrintToServer("Fourth element: %s", strBuffer); - - PrintToServer("Fifth element is null: %d", arr.IsNull(4)); - - YYJSON first = arr.First; - YYJSON last = arr.Last; - - // Test array properties - PrintToServer("Array length: %d", arr.Length); - PrintToServer("First value: %x", first); - PrintToServer("Last value: %x", last); - - // Test set operations - arr.SetInt(0, 100); - arr.SetFloat(1, 3.14); - arr.SetBool(2, false); - arr.SetString(3, "modified"); - arr.SetNull(4); - - PrintToServer("Array after set operations:"); - PrintJson(arr); - - // Test remove operations - arr.RemoveFirst(); - PrintToServer("After RemoveFirst:"); - PrintJson(arr); - - arr.RemoveLast(); - PrintToServer("After RemoveLast:"); - PrintJson(arr); - - arr.Remove(1); - PrintToServer("After Remove(1):"); - PrintJson(arr); - - arr.RemoveRange(0, 1); - PrintToServer("After RemoveRange(0, 1):"); - PrintJson(arr); - - arr.Clear(); - PrintToServer("Array length after Clear: %d", arr.Length); - - delete arr; - delete first; - delete last; -} - -void TestObjectOperations() -{ - PrintToServer("[YYJSON] Testing object operations..."); - - YYJSONObject obj = new YYJSONObject(); - - // Test set operations - obj.SetInt("int", 123); - obj.SetFloat("float", 3.14); - obj.SetBool("bool", true); - obj.SetString("string", "test"); - obj.SetNull("null"); - - PrintToServer("Object after set operations:"); - PrintJson(obj); - - // Test get operations - PrintToServer("Int value: %d", obj.GetInt("int")); - PrintToServer("Float value: %f", obj.GetFloat("float")); - PrintToServer("Bool value: %d", obj.GetBool("bool")); - - char strBuffer[64]; - obj.GetString("string", strBuffer, sizeof(strBuffer)); - PrintToServer("String value: %s", strBuffer); - - PrintToServer("Is null value null: %d", obj.IsNull("null")); - - // Test key operations - char key[64]; - for (int i = 0; i < obj.Size; i++) - { - obj.GetKey(i, key, sizeof(key)); - PrintToServer("Key at %d: %s", i, key); - - YYJSON value = obj.GetValueAt(i); - PrintToServer("Value type at %d: %d", i, value.Type); - delete value; - } - - // Test rename key - obj.RenameKey("int", "number"); - PrintToServer("After renaming 'int' to 'number':"); - PrintJson(obj); - - // Test remove operations - obj.Remove("number"); - PrintToServer("After removing 'number':"); - PrintJson(obj); - - obj.Clear(); - PrintToServer("Object size after Clear: %d", obj.Size); - - delete obj; -} - -void TestSortOperations() -{ - PrintToServer("[YYJSON] Testing sort operations..."); - - // Test array sorting - YYJSONArray arr = YYJSON.Parse("[3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]", .is_mutable_doc = true); - - PrintToServer("Original array:"); - PrintJson(arr); - - arr.Sort(); - PrintToServer("After ascending sort:"); - PrintJson(arr); - - arr.Sort(YYJSON_SORT_DESC); - PrintToServer("After descending sort:"); - PrintJson(arr); - - arr.Sort(YYJSON_SORT_RANDOM); - PrintToServer("After random sort:"); - PrintJson(arr); - - // Test mixed type array sorting - YYJSONArray mixed = YYJSON.Parse("[true, 42, \"hello\", 1.23, false, \"world\"]", .is_mutable_doc = true); - PrintToServer("Original mixed array:"); - PrintJson(mixed); - - mixed.Sort(); - PrintToServer("After sorting mixed array:"); - PrintJson(mixed); - - // Test object sorting - YYJSONObject obj = YYJSON.Parse("{\"zebra\": 1, \"alpha\": 2, \"beta\": 3}", .is_mutable_doc = true); - PrintToServer("Original object:"); - PrintJson(obj); - - obj.Sort(); - PrintToServer("After ascending sort:"); - PrintJson(obj); - - obj.Sort(YYJSON_SORT_DESC); - PrintToServer("After descending sort:"); - PrintJson(obj); - - obj.Sort(YYJSON_SORT_RANDOM); - PrintToServer("After random sort:"); - PrintJson(obj); - - delete arr; - delete mixed; - delete obj; -} - -void TestSearchOperations() -{ - PrintToServer("[YYJSON] Testing search operations..."); - - YYJSONArray arr = YYJSON.Parse("[42, true, \"hello\", 3.14, \"world\", false, 42]"); - PrintToServer("Test array:"); - PrintJson(arr); - - // Test all search methods - PrintToServer("Search results:"); - PrintToServer("IndexOfInt(42): %d", arr.IndexOfInt(42)); - PrintToServer("IndexOfInt(999): %d", arr.IndexOfInt(999)); - - PrintToServer("IndexOfBool(true): %d", arr.IndexOfBool(true)); - PrintToServer("IndexOfBool(false): %d", arr.IndexOfBool(false)); - - PrintToServer("IndexOfString(\"hello\"): %d", arr.IndexOfString("hello")); - PrintToServer("IndexOfString(\"missing\"): %d", arr.IndexOfString("missing")); - - PrintToServer("IndexOfFloat(3.14): %d", arr.IndexOfFloat(3.14)); - PrintToServer("IndexOfFloat(2.718): %d", arr.IndexOfFloat(2.718)); - - delete arr; -} - -void TestPointerOperations() -{ - PrintToServer("[YYJSON] Testing JSON pointer operations..."); - - YYJSONObject obj = new YYJSONObject(); - - // Test setting nested values - obj.PtrSetInt("/a/b/c", 1); - obj.PtrSetString("/a/b/name", "test"); - obj.PtrSetBool("/a/flag", true); - obj.PtrSetFloat("/a/b/pi", 3.14); - obj.PtrSetNull("/a/b/empty"); - - PrintToServer("After setting values:"); - PrintJson(obj); - - // Test getting values - PrintToServer("Pointer get operations:"); - PrintToServer("/a/b/c: %d", obj.PtrGetInt("/a/b/c")); - - char strBuffer[64]; - obj.PtrGetString("/a/b/name", strBuffer, sizeof(strBuffer)); - PrintToServer("/a/b/name: %s", strBuffer); - - PrintToServer("/a/flag: %d", obj.PtrGetBool("/a/flag")); - PrintToServer("/a/b/pi: %f", obj.PtrGetFloat("/a/b/pi")); - PrintToServer("/a/b/empty is null: %d", obj.PtrGetIsNull("/a/b/empty")); - - // Test adding values - obj.PtrAddInt("/a/b/numbers/0", 1); - obj.PtrAddInt("/a/b/numbers/1", 2); - obj.PtrAddString("/a/b/strings", "append"); - - PrintToServer("After adding values:"); - PrintJson(obj); - - // Test length - PrintToServer("Length of /a/b/numbers: %d", obj.PtrGetLength("/a/b/numbers")); - - // Test removing values - obj.PtrRemove("/a/b/c"); - PrintToServer("After removing /a/b/c:"); - PrintJson(obj); - - delete obj; -} - -void TestIterationOperations() -{ - PrintToServer("[YYJSON] Testing iteration operations..."); - - // Test object iteration - YYJSONObject obj = YYJSON.Parse("{\"a\": 1, \"b\": 2, \"c\": 3}"); - char key[64]; - YYJSON value; - - while (obj.ForeachObject(key, sizeof(key), value)) - { - PrintToServer("Key: %s", key); - delete value; - } - - // Test array iteration - YYJSONArray arr = YYJSON.Parse("[1, 2, 3, 4, 5]"); - int index; - - while (arr.ForeachArray(index, value)) - { - PrintToServer("Index: %d", index); - delete value; - } - - delete obj; - delete arr; -} - -void TestTypeOperations() -{ - PrintToServer("[YYJSON] Testing type operations..."); - - // Test value creation - YYJSON boolVal = YYJSON.CreateBool(true); - YYJSON intVal = YYJSON.CreateInt(42); - YYJSON floatVal = YYJSON.CreateFloat(3.14); - YYJSON strVal = YYJSON.CreateString("test"); - YYJSON nullVal = YYJSON.CreateNull(); - - // Test value types - PrintToServer("Value types:"); - PrintToServer("Bool type: %d", boolVal.Type); - PrintToServer("Int type: %d", intVal.Type); - PrintToServer("Float type: %d", floatVal.Type); - PrintToServer("String type: %d", strVal.Type); - PrintToServer("Null type: %d", nullVal.Type); - - // Test value retrieval - PrintToServer("Value contents:"); - PrintToServer("Bool value: %d", YYJSON.GetBool(boolVal)); - PrintToServer("Int value: %d", YYJSON.GetInt(intVal)); - PrintToServer("Float value: %f", YYJSON.GetFloat(floatVal)); - - char strBuffer[64]; - YYJSON.GetString(strVal, strBuffer, sizeof(strBuffer)); - PrintToServer("String value: %s", strBuffer); - - delete boolVal; - delete intVal; - delete floatVal; - delete strVal; - delete nullVal; -} - -void TestFileOperations() -{ - PrintToServer("[YYJSON] Testing file operations..."); - - // Create test data - YYJSONObject obj = new YYJSONObject(); - obj.SetInt("id", 1); - obj.SetString("name", "test"); - obj.SetBool("active", true); - - // Test file writing - PrintToServer("Writing to file..."); - obj.ToFile("test.json", YYJSON_WRITE_PRETTY_TWO_SPACES); - - // Test file reading - PrintToServer("Reading from file..."); - YYJSONObject loaded = YYJSON.Parse("test.json", true); - - PrintToServer("File content:"); - PrintJson(loaded); - delete loaded; - - delete obj; -} - -void TestImmutabilityOperations() -{ - PrintToServer("[YYJSON] Testing immutability operations..."); - - // Test immutable document creation - YYJSONObject immutable = YYJSON.Parse("{\"key\": 123, \"str\": \"test\"}"); - PrintToServer("Created immutable document:"); - PrintJson(immutable); - - // Test property checks - PrintToServer("Is mutable: %d", immutable.IsMutable); - PrintToServer("Is immutable: %d", immutable.IsImmutable); - - // Test read operations (should succeed) - PrintToServer("Read operations on immutable document:"); - PrintToServer("Int value: %d", immutable.GetInt("key")); - char buffer[64]; - immutable.GetString("str", buffer, sizeof(buffer)); - PrintToServer("String value: %s", buffer); - - // Test conversion to mutable - YYJSONObject mutable = immutable.ToMutable(); - PrintToServer("\nConverted to mutable document:"); - PrintToServer("Is mutable: %d", mutable.IsMutable); - PrintToServer("Is immutable: %d", mutable.IsImmutable); - - // Now modifications should work - mutable.SetInt("key", 456) - PrintToServer("Successfully modified mutable document:"); - PrintJson(mutable); - - // Test conversion back to immutable - YYJSONObject backToImmutable = mutable.ToImmutable(); - PrintToServer("\nConverted back to immutable:"); - PrintToServer("Is mutable: %d", backToImmutable.IsMutable); - PrintToServer("Is immutable: %d", backToImmutable.IsImmutable); - delete backToImmutable; - delete mutable; - delete immutable; - - // Test file operations with immutability - PrintToServer("\nTesting file operations with immutability..."); - - // Create and write a mutable document - YYJSONObject writeObj = new YYJSONObject(); - writeObj.SetInt("test", 123); - writeObj.ToFile("test_immutable.json"); - delete writeObj; - - // Read as immutable - YYJSONObject readImmutable = YYJSON.Parse("test_immutable.json", true); - PrintToServer("Read as immutable document:"); - PrintJson(readImmutable); - PrintToServer("Is mutable: %d", readImmutable.IsMutable); - PrintToServer("Is immutable: %d", readImmutable.IsImmutable); - delete readImmutable; -} - -void TestPackOperations() -{ - PrintToServer("[YYJSON] Testing pack operations..."); - - // Test basic pack operation with different types - YYJSON packed = YYJSON.Pack("{s:s,s:i,s:f,s:b,s:n}", - "name", "test", - "age", 25, - "height", 1.75, - "active", true, - "extra" - ); - - PrintToServer("Packed JSON:"); - PrintJson(packed); - - // Test nested object packing - YYJSON nested = YYJSON.Pack("{s:{s:s,s:[i,i,i]}}", - "user", - "name", "test", - "scores", 85, 90, 95 - ); - - PrintToServer("Nested packed JSON:"); - PrintJson(nested); - - // Test array packing with mixed types - YYJSON array = YYJSON.Pack("[s,i,f,b,n]", - "test", 42, 3.14, true - ); - - PrintToServer("Array packed JSON:"); - PrintJson(array); - - delete packed; - delete nested; - delete array; -} - -void TestFromStringsOperations() -{ - PrintToServer("[YYJSON] Testing FromStrings operations..."); - - // Test object creation from key-value string arrays - char pairs[][] = {"name", "test", "type", "demo", "version", "1.0.0"}; - - YYJSONObject obj = YYJSONObject.FromStrings(pairs, sizeof(pairs)); - PrintToServer("Object from strings:"); - PrintJson(obj); - - // Test array creation from string array - char items[][] = {"apple", "banana", "orange", "grape"}; - YYJSONArray arr = YYJSONArray.FromStrings(items, sizeof(items)); - PrintToServer("Array from strings:"); - PrintJson(arr); - - delete obj; - delete arr; -} - -// Helper function to print json contents -void PrintJson(YYJSON data) -{ - int len = data.GetSerializedSize(YYJSON_WRITE_PRETTY_TWO_SPACES); - char[] buffer = new char[len]; - data.ToString(buffer, len, YYJSON_WRITE_PRETTY_TWO_SPACES); - PrintToServer("%s", buffer); -} \ No newline at end of file diff --git a/src/JsonManager.cpp b/src/JsonManager.cpp new file mode 100755 index 0000000..e95a93f --- /dev/null +++ b/src/JsonManager.cpp @@ -0,0 +1,5281 @@ +#include "JsonManager.h" +#include "extension.h" + +static inline void ReadInt64FromVal(yyjson_val* val, std::variant* out_value) { + if (yyjson_is_uint(val)) { + *out_value = yyjson_get_uint(val); + } else { + *out_value = yyjson_get_sint(val); + } +} + +static inline void ReadInt64FromMutVal(yyjson_mut_val* val, std::variant* out_value) { + if (yyjson_mut_is_uint(val)) { + *out_value = yyjson_mut_get_uint(val); + } else { + *out_value = yyjson_mut_get_sint(val); + } +} + +// Set error message safely +static inline void SetErrorSafe(char* error, size_t error_size, const char* format, ...) { + if (!error || error_size == 0) return; + + va_list args; + va_start(args, format); + int needed = vsnprintf(error, error_size, format, args); + va_end(args); + + if (needed >= static_cast(error_size)) { + error[error_size - 1] = '\0'; + } +} + +std::unique_ptr JsonManager::CreateWrapper() { + return std::make_unique(); +} + +RefPtr JsonManager::WrapDocument(yyjson_mut_doc* doc) { + if (!doc) { + return RefPtr(); + } + return make_ref(doc); +} + +RefPtr JsonManager::CopyDocument(yyjson_doc* doc) { + return WrapDocument(yyjson_doc_mut_copy(doc, nullptr)); +} + +RefPtr JsonManager::CreateDocument() { + return WrapDocument(yyjson_mut_doc_new(nullptr)); +} + +RefPtr JsonManager::WrapImmutableDocument(yyjson_doc* doc) { + if (!doc) { + return RefPtr(); + } + return make_ref(doc); +} + +RefPtr JsonManager::CloneValueToMutable(JsonValue* value) { + if (!value) { + return RefPtr(); + } + + if (value->IsMutable()) { + yyjson_mut_doc* dup = yyjson_mut_doc_mut_copy(value->m_pDocument_mut->get(), nullptr); + return WrapDocument(dup); + } + + if (!value->m_pDocument) { + return RefPtr(); + } + + return CopyDocument(value->m_pDocument->get()); +} + +static yyjson_mut_val* CopyValueIntoDoc(JsonValue* value, yyjson_mut_doc* doc, char* error, size_t error_size) { + if (!value || !doc) { + SetErrorSafe(error, error_size, "Invalid JSON value or document"); + return nullptr; + } + + yyjson_mut_val* copy = nullptr; + if (value->IsMutable()) { + if (!value->m_pVal_mut) { + SetErrorSafe(error, error_size, "Mutable JSON value has no root"); + return nullptr; + } + copy = yyjson_mut_val_mut_copy(doc, value->m_pVal_mut); + } else { + if (!value->m_pVal) { + SetErrorSafe(error, error_size, "Immutable JSON value has no root"); + return nullptr; + } + copy = yyjson_val_mut_copy(doc, value->m_pVal); + } + + if (!copy) { + SetErrorSafe(error, error_size, "Failed to copy JSON value"); + } + return copy; +} + +JsonManager::JsonManager(): m_randomGenerator(m_randomDevice()) {} + +JsonManager::~JsonManager() {} + +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) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Invalid JSON string"); + } + return nullptr; + } + + yyjson_read_err readError; + yyjson_doc* idoc; + auto pJSONValue = CreateWrapper(); + + if (is_file) { + char realpath[PLATFORM_MAX_PATH]; + smutils->BuildPath(Path_Game, realpath, sizeof(realpath), "%s", json_str); + idoc = yyjson_read_file(realpath, read_flg, nullptr, &readError); + } else { + idoc = yyjson_read_opts(const_cast(json_str), strlen(json_str), read_flg, nullptr, &readError); + } + + if (!idoc || readError.code) { + if (error && error_size > 0) { + if (is_file) { + SetErrorSafe(error, error_size, "Failed to parse JSON file: %s (error code: %u, msg: %s, position: %zu)", + json_str, readError.code, readError.msg, readError.pos); + } else { + SetErrorSafe(error, error_size, "Failed to parse JSON str: %s (error code: %u, position: %zu)", + readError.msg, readError.code, readError.pos); + } + } + return nullptr; + } + + pJSONValue->m_readSize = yyjson_doc_get_read_size(idoc); + + if (is_mutable) { + pJSONValue->m_pDocument_mut = CopyDocument(idoc); + yyjson_doc_free(idoc); + if (!pJSONValue->m_pDocument_mut) { + SetErrorSafe(error, error_size, "Failed to create mutable JSON document"); + return nullptr; + } + pJSONValue->m_pVal_mut = yyjson_mut_doc_get_root(pJSONValue->m_pDocument_mut->get()); + if (!pJSONValue->m_pVal_mut) { + SetErrorSafe(error, error_size, "Mutable JSON document has no root value"); + return nullptr; + } + } else { + pJSONValue->m_pDocument = WrapImmutableDocument(idoc); + if (!pJSONValue->m_pDocument) { + yyjson_doc_free(idoc); + SetErrorSafe(error, error_size, "Failed to create immutable JSON document"); + return nullptr; + } + pJSONValue->m_pVal = yyjson_doc_get_root(idoc); + if (!pJSONValue->m_pVal) { + yyjson_doc_free(idoc); + SetErrorSafe(error, error_size, "Immutable JSON document has no root value"); + return nullptr; + } + } + + return pJSONValue.release(); +} + +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) { + return false; + } + + size_t written; + + if (handle->IsMutable()) { + written = yyjson_mut_val_write_buf(buffer, buffer_size, handle->m_pVal_mut, write_flg, nullptr); + } else { + written = yyjson_val_write_buf(buffer, buffer_size, handle->m_pVal, write_flg, nullptr); + } + + if (written == 0) { + return false; + } + + if (written + 1 > buffer_size) { + return false; + } + + buffer[written] = '\0'; + + if (out_size) { + *out_size = written + 1; + } + return true; +} + +char* JsonManager::WriteToStringPtr(JsonValue* handle, yyjson_write_flag write_flg, size_t* out_size) +{ + if (!handle) { + return nullptr; + } + + size_t json_size = 0; + char* json_str; + + if (handle->IsMutable()) { + json_str = yyjson_mut_val_write(handle->m_pVal_mut, write_flg, &json_size); + } else { + json_str = yyjson_val_write(handle->m_pVal, write_flg, &json_size); + } + + if (json_str && out_size) { + *out_size = json_size + 1; + } + + return json_str; +} + +JsonValue* JsonManager::ApplyJsonPatch(JsonValue* target, JsonValue* patch, bool result_mutable, + char* error, size_t error_size) +{ + if (!target || !patch) { + SetErrorSafe(error, error_size, "Target or patch JSON value is null"); + return nullptr; + } + + auto docRef = CloneValueToMutable(target); + if (!docRef) { + SetErrorSafe(error, error_size, "Failed to clone target JSON value"); + return nullptr; + } + + yyjson_mut_doc* doc = docRef->get(); + yyjson_mut_val* root = yyjson_mut_doc_get_root(doc); + if (!root) { + SetErrorSafe(error, error_size, "Target JSON has no root value"); + return nullptr; + } + + yyjson_mut_val* patchCopy = CopyValueIntoDoc(patch, doc, error, error_size); + if (!patchCopy) { + return nullptr; + } + + yyjson_patch_err patch_err = {0}; + yyjson_mut_val* resultRoot = yyjson_mut_patch(doc, root, patchCopy, &patch_err); + if (!resultRoot) { + SetErrorSafe(error, error_size, "JSON patch failed (code %u, op index %zu, message: %s)", + patch_err.code, patch_err.idx, patch_err.msg); + return nullptr; + } + + yyjson_mut_doc_set_root(doc, resultRoot); + + if (result_mutable) { + auto wrapper = CreateWrapper(); + wrapper->m_pDocument_mut = docRef; + wrapper->m_pVal_mut = yyjson_mut_doc_get_root(doc); + docRef.reset(); + return wrapper.release(); + } + + yyjson_doc* imutDoc = yyjson_mut_doc_imut_copy(doc, nullptr); + if (!imutDoc) { + SetErrorSafe(error, error_size, "Failed to convert patched JSON to immutable document"); + return nullptr; + } + + auto wrapper = CreateWrapper(); + wrapper->m_pDocument = WrapImmutableDocument(imutDoc); + if (!wrapper->m_pDocument) { + yyjson_doc_free(imutDoc); + SetErrorSafe(error, error_size, "Failed to wrap immutable JSON document"); + return nullptr; + } + wrapper->m_pVal = yyjson_doc_get_root(imutDoc); + return wrapper.release(); +} + +bool JsonManager::JsonPatchInPlace(JsonValue* target, JsonValue* patch, + char* error, size_t error_size) +{ + if (!target || !patch) { + SetErrorSafe(error, error_size, "Target or patch JSON value is null"); + return false; + } + + if (!target->IsMutable()) { + SetErrorSafe(error, error_size, "Target JSON must be mutable for in-place JSON Patch"); + return false; + } + + yyjson_mut_doc* doc = target->m_pDocument_mut->get(); + yyjson_mut_val* root = target->m_pVal_mut; + + if (!doc || !root) { + SetErrorSafe(error, error_size, "Target JSON has no root value"); + return false; + } + + yyjson_mut_val* patchCopy = CopyValueIntoDoc(patch, doc, error, error_size); + if (!patchCopy) { + return false; + } + + yyjson_patch_err patch_err = {0}; + yyjson_mut_val* resultRoot = yyjson_mut_patch(doc, root, patchCopy, &patch_err); + if (!resultRoot) { + SetErrorSafe(error, error_size, "JSON patch failed (code %u, op index %zu, message: %s)", + patch_err.code, patch_err.idx, patch_err); + return false; + } + + yyjson_mut_doc_set_root(doc, resultRoot); + target->m_pVal_mut = yyjson_mut_doc_get_root(doc); + return true; +} + +JsonValue* JsonManager::ApplyMergePatch(JsonValue* target, JsonValue* patch, bool result_mutable, + char* error, size_t error_size) +{ + if (!target || !patch) { + SetErrorSafe(error, error_size, "Target or patch JSON value is null"); + return nullptr; + } + + auto docRef = CloneValueToMutable(target); + if (!docRef) { + SetErrorSafe(error, error_size, "Failed to clone target JSON value"); + return nullptr; + } + + yyjson_mut_doc* doc = docRef->get(); + yyjson_mut_val* root = yyjson_mut_doc_get_root(doc); + if (!root) { + SetErrorSafe(error, error_size, "Target JSON has no root value"); + return nullptr; + } + + yyjson_mut_val* patchCopy = CopyValueIntoDoc(patch, doc, error, error_size); + if (!patchCopy) { + return nullptr; + } + + yyjson_mut_val* resultRoot = yyjson_mut_merge_patch(doc, root, patchCopy); + if (!resultRoot) { + SetErrorSafe(error, error_size, "Failed to apply JSON Merge Patch"); + return nullptr; + } + + yyjson_mut_doc_set_root(doc, resultRoot); + + if (result_mutable) { + auto wrapper = CreateWrapper(); + wrapper->m_pDocument_mut = docRef; + wrapper->m_pVal_mut = yyjson_mut_doc_get_root(doc); + docRef.reset(); + return wrapper.release(); + } + + yyjson_doc* imutDoc = yyjson_mut_doc_imut_copy(doc, nullptr); + if (!imutDoc) { + SetErrorSafe(error, error_size, "Failed to convert patched JSON to immutable document"); + return nullptr; + } + + auto wrapper = CreateWrapper(); + wrapper->m_pDocument = WrapImmutableDocument(imutDoc); + if (!wrapper->m_pDocument) { + yyjson_doc_free(imutDoc); + SetErrorSafe(error, error_size, "Failed to wrap immutable JSON document"); + return nullptr; + } + wrapper->m_pVal = yyjson_doc_get_root(imutDoc); + return wrapper.release(); +} + +bool JsonManager::MergePatchInPlace(JsonValue* target, JsonValue* patch, + char* error, size_t error_size) +{ + if (!target || !patch) { + SetErrorSafe(error, error_size, "Target or patch JSON value is null"); + return false; + } + + if (!target->IsMutable()) { + SetErrorSafe(error, error_size, "Target JSON must be mutable for in-place merge patch"); + return false; + } + + yyjson_mut_doc* doc = target->m_pDocument_mut->get(); + yyjson_mut_val* root = target->m_pVal_mut; + + if (!doc || !root) { + SetErrorSafe(error, error_size, "Target JSON has no root value"); + return false; + } + + yyjson_mut_val* patchCopy = CopyValueIntoDoc(patch, doc, error, error_size); + if (!patchCopy) { + return false; + } + + yyjson_mut_val* resultRoot = yyjson_mut_merge_patch(doc, root, patchCopy); + if (!resultRoot) { + SetErrorSafe(error, error_size, "Failed to apply JSON Merge Patch in place"); + return false; + } + + yyjson_mut_doc_set_root(doc, resultRoot); + target->m_pVal_mut = yyjson_mut_doc_get_root(doc); + return true; +} + +bool JsonManager::WriteToFile(JsonValue* handle, const char* path, yyjson_write_flag write_flg, + char* error, size_t error_size) +{ + if (!handle || !path) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Invalid parameters"); + } + return false; + } + + char realpath[PLATFORM_MAX_PATH]; + smutils->BuildPath(Path_Game, realpath, sizeof(realpath), "%s", path); + + yyjson_write_err writeError; + bool is_success; + + if (handle->IsMutable()) { + is_success = yyjson_mut_write_file(realpath, handle->m_pDocument_mut->get(), write_flg, nullptr, &writeError); + } else { + is_success = yyjson_write_file(realpath, handle->m_pDocument->get(), write_flg, nullptr, &writeError); + } + + if (writeError.code && error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to write JSON to file: %s (error code: %u)", writeError.msg, writeError.code); + } + + return is_success; +} + +bool JsonManager::Equals(JsonValue* handle1, JsonValue* handle2) +{ + if (!handle1 || !handle2) { + return false; + } + + if (handle1->IsMutable() && handle2->IsMutable()) { + return yyjson_mut_equals(handle1->m_pVal_mut, handle2->m_pVal_mut); + } + + if (!handle1->IsMutable() && !handle2->IsMutable()) { + auto doc1_mut = CopyDocument(handle1->m_pDocument->get()); + auto doc2_mut = CopyDocument(handle2->m_pDocument->get()); + + if (!doc1_mut || !doc2_mut) { + return false; + } + + yyjson_mut_val* val1_mut = yyjson_mut_doc_get_root(doc1_mut->get()); + yyjson_mut_val* val2_mut = yyjson_mut_doc_get_root(doc2_mut->get()); + + if (!val1_mut || !val2_mut) { + return false; + } + + return yyjson_mut_equals(val1_mut, val2_mut); + } + + JsonValue* immutable = handle1->IsMutable() ? handle2 : handle1; + JsonValue* mutable_doc = handle1->IsMutable() ? handle1 : handle2; + + auto doc_mut = CopyDocument(immutable->m_pDocument->get()); + if (!doc_mut) { + return false; + } + + yyjson_mut_val* val_mut = yyjson_mut_doc_get_root(doc_mut->get()); + if (!val_mut) { + return false; + } + + return yyjson_mut_equals(mutable_doc->m_pVal_mut, val_mut); +} + +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 pJSONValue = CreateWrapper(); + + if (targetDoc->IsMutable()) { + pJSONValue->m_pDocument_mut = CreateDocument(); + if (!pJSONValue->m_pDocument_mut) { + return nullptr; + } + + yyjson_mut_val* val_copy; + if (sourceValue->IsMutable()) { + val_copy = yyjson_mut_val_mut_copy(pJSONValue->m_pDocument_mut->get(), sourceValue->m_pVal_mut); + } else { + val_copy = yyjson_val_mut_copy(pJSONValue->m_pDocument_mut->get(), sourceValue->m_pVal); + } + + if (!val_copy) { + return nullptr; + } + + yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut->get(), val_copy); + pJSONValue->m_pVal_mut = val_copy; + } else { + yyjson_mut_doc* temp_doc = yyjson_mut_doc_new(nullptr); + if (!temp_doc) { + return nullptr; + } + + yyjson_mut_val* temp_val; + if (sourceValue->IsMutable()) { + temp_val = yyjson_mut_val_mut_copy(temp_doc, sourceValue->m_pVal_mut); + } else { + temp_val = yyjson_val_mut_copy(temp_doc, sourceValue->m_pVal); + } + + if (!temp_val) { + yyjson_mut_doc_free(temp_doc); + return nullptr; + } + + yyjson_mut_doc_set_root(temp_doc, temp_val); + + yyjson_doc* doc = yyjson_mut_doc_imut_copy(temp_doc, nullptr); + yyjson_mut_doc_free(temp_doc); + + if (!doc) { + return nullptr; + } + + pJSONValue->m_pDocument = WrapImmutableDocument(doc); + pJSONValue->m_pVal = yyjson_doc_get_root(doc); + } + + return pJSONValue.release(); +} + +const char* JsonManager::GetTypeDesc(JsonValue* handle) +{ + if (!handle) { + return "invalid"; + } + + if (handle->IsMutable()) { + return yyjson_mut_get_type_desc(handle->m_pVal_mut); + } else { + return yyjson_get_type_desc(handle->m_pVal); + } +} + +size_t JsonManager::GetSerializedSize(JsonValue* handle, yyjson_write_flag write_flg) +{ + if (!handle) { + return 0; + } + + size_t json_size; + char* json_str; + + if (handle->IsMutable()) { + json_str = yyjson_mut_val_write(handle->m_pVal_mut, write_flg, &json_size); + } else { + json_str = yyjson_val_write(handle->m_pVal, write_flg, &json_size); + } + + if (json_str) { + free(json_str); + return json_size + 1; + } + + return 0; +} + +JsonValue* JsonManager::ToMutable(JsonValue* handle) +{ + if (!handle || handle->IsMutable()) { + return nullptr; + } + + auto pJSONValue = CreateWrapper(); + pJSONValue->m_pDocument_mut = CopyDocument(handle->m_pDocument->get()); + if (!pJSONValue->m_pDocument_mut) { + return nullptr; + } + pJSONValue->m_pVal_mut = yyjson_mut_doc_get_root(pJSONValue->m_pDocument_mut->get()); + if (!pJSONValue->m_pVal_mut) { + return nullptr; + } + + return pJSONValue.release(); +} + +JsonValue* JsonManager::ToImmutable(JsonValue* handle) +{ + if (!handle || !handle->IsMutable()) { + return nullptr; + } + + auto pJSONValue = CreateWrapper(); + yyjson_doc* mdoc = yyjson_mut_doc_imut_copy(handle->m_pDocument_mut->get(), nullptr); + if (!mdoc) { + return nullptr; + } + pJSONValue->m_pDocument = WrapImmutableDocument(mdoc); + if (!pJSONValue->m_pDocument) { + yyjson_doc_free(mdoc); + return nullptr; + } + pJSONValue->m_pVal = yyjson_doc_get_root(pJSONValue->m_pDocument->get()); + + return pJSONValue.release(); +} + +yyjson_type JsonManager::GetType(JsonValue* handle) +{ + if (!handle) { + return YYJSON_TYPE_NONE; + } + + if (handle->IsMutable()) { + return yyjson_mut_get_type(handle->m_pVal_mut); + } else { + return yyjson_get_type(handle->m_pVal); + } +} + +yyjson_subtype JsonManager::GetSubtype(JsonValue* handle) +{ + if (!handle) { + return YYJSON_SUBTYPE_NONE; + } + + if (handle->IsMutable()) { + return yyjson_mut_get_subtype(handle->m_pVal_mut); + } else { + return yyjson_get_subtype(handle->m_pVal); + } +} + +bool JsonManager::IsArray(JsonValue* handle) +{ + if (!handle) { + return false; + } + + if (handle->IsMutable()) { + return yyjson_mut_is_arr(handle->m_pVal_mut); + } else { + return yyjson_is_arr(handle->m_pVal); + } +} + +bool JsonManager::IsObject(JsonValue* handle) +{ + if (!handle) { + return false; + } + + if (handle->IsMutable()) { + return yyjson_mut_is_obj(handle->m_pVal_mut); + } else { + return yyjson_is_obj(handle->m_pVal); + } +} + +bool JsonManager::IsInt(JsonValue* handle) +{ + if (!handle) { + return false; + } + + if (handle->IsMutable()) { + return yyjson_mut_is_int(handle->m_pVal_mut); + } else { + return yyjson_is_int(handle->m_pVal); + } +} + +bool JsonManager::IsUint(JsonValue* handle) +{ + if (!handle) { + return false; + } + + if (handle->IsMutable()) { + return yyjson_mut_is_uint(handle->m_pVal_mut); + } else { + return yyjson_is_uint(handle->m_pVal); + } +} + +bool JsonManager::IsSint(JsonValue* handle) +{ + if (!handle) { + return false; + } + + if (handle->IsMutable()) { + return yyjson_mut_is_sint(handle->m_pVal_mut); + } else { + return yyjson_is_sint(handle->m_pVal); + } +} + +bool JsonManager::IsNum(JsonValue* handle) +{ + if (!handle) { + return false; + } + + if (handle->IsMutable()) { + return yyjson_mut_is_num(handle->m_pVal_mut); + } else { + return yyjson_is_num(handle->m_pVal); + } +} + +bool JsonManager::IsBool(JsonValue* handle) +{ + if (!handle) { + return false; + } + + if (handle->IsMutable()) { + return yyjson_mut_is_bool(handle->m_pVal_mut); + } else { + return yyjson_is_bool(handle->m_pVal); + } +} + +bool JsonManager::IsTrue(JsonValue* handle) +{ + if (!handle) { + return false; + } + + if (handle->IsMutable()) { + return yyjson_mut_is_true(handle->m_pVal_mut); + } else { + return yyjson_is_true(handle->m_pVal); + } +} + +bool JsonManager::IsFalse(JsonValue* handle) +{ + if (!handle) { + return false; + } + + if (handle->IsMutable()) { + return yyjson_mut_is_false(handle->m_pVal_mut); + } else { + return yyjson_is_false(handle->m_pVal); + } +} + +bool JsonManager::IsFloat(JsonValue* handle) +{ + if (!handle) { + return false; + } + + if (handle->IsMutable()) { + return yyjson_mut_is_real(handle->m_pVal_mut); + } else { + return yyjson_is_real(handle->m_pVal); + } +} + +bool JsonManager::IsStr(JsonValue* handle) +{ + if (!handle) { + return false; + } + + if (handle->IsMutable()) { + return yyjson_mut_is_str(handle->m_pVal_mut); + } else { + return yyjson_is_str(handle->m_pVal); + } +} + +bool JsonManager::IsNull(JsonValue* handle) +{ + if (!handle) { + return false; + } + + if (handle->IsMutable()) { + return yyjson_mut_is_null(handle->m_pVal_mut); + } else { + return yyjson_is_null(handle->m_pVal); + } +} + +bool JsonManager::IsCtn(JsonValue* handle) +{ + if (!handle) { + return false; + } + + if (handle->IsMutable()) { + return yyjson_mut_is_ctn(handle->m_pVal_mut); + } else { + return yyjson_is_ctn(handle->m_pVal); + } +} + +bool JsonManager::IsMutable(JsonValue* handle) +{ + if (!handle) { + return false; + } + + return handle->IsMutable(); +} + +bool JsonManager::IsImmutable(JsonValue* handle) +{ + if (!handle) { + return false; + } + + return handle->IsImmutable(); +} + +size_t JsonManager::GetReadSize(JsonValue* handle) +{ + if (!handle) { + return 0; + } + + if (handle->m_readSize == 0) { + return 0; + } + + return handle->m_readSize + 1; +} + +size_t JsonManager::GetRefCount(JsonValue* handle) +{ + if (!handle) { + return 0; + } + return handle->GetDocumentRefCount(); +} + +size_t JsonManager::GetValCount(JsonValue* handle) +{ + if (!handle || !handle->IsImmutable()) { + return 0; + } + return yyjson_doc_get_val_count(handle->m_pDocument->get()); +} + +JsonValue* JsonManager::ObjectInit() +{ + auto pJSONValue = CreateWrapper(); + pJSONValue->m_pDocument_mut = CreateDocument(); + + if (!pJSONValue->m_pDocument_mut) { + return nullptr; + } + + pJSONValue->m_pVal_mut = yyjson_mut_obj(pJSONValue->m_pDocument_mut->get()); + + if (!pJSONValue->m_pVal_mut) { + return nullptr; + } + + yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut->get(), pJSONValue->m_pVal_mut); + + return pJSONValue.release(); +} + +JsonValue* JsonManager::ObjectInitWithStrings(const char** pairs, size_t count) +{ + if (!pairs || count == 0) { + return nullptr; + } + + auto pJSONValue = CreateWrapper(); + pJSONValue->m_pDocument_mut = CreateDocument(); + + if (!pJSONValue->m_pDocument_mut) { + return nullptr; + } + + pJSONValue->m_pVal_mut = yyjson_mut_obj_with_kv( + pJSONValue->m_pDocument_mut->get(), + pairs, + count + ); + + if (!pJSONValue->m_pVal_mut) { + return nullptr; + } + + yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut->get(), pJSONValue->m_pVal_mut); + + return pJSONValue.release(); +} + +JsonValue* JsonManager::ObjectParseString(const char* str, yyjson_read_flag read_flg, + char* error, size_t error_size) +{ + if (!str) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Invalid string"); + } + return nullptr; + } + + auto pJSONValue = CreateWrapper(); + + yyjson_read_err readError; + yyjson_doc* idoc = yyjson_read_opts(const_cast(str), strlen(str), read_flg, nullptr, &readError); + + if (!idoc || readError.code) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to parse JSON str: %s (error code: %u, position: %zu)", + readError.msg, readError.code, readError.pos); + } + return nullptr; + } + + yyjson_val* root = yyjson_doc_get_root(idoc); + + if (!yyjson_is_obj(root)) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Root value is not an object (got %s)", yyjson_get_type_desc(root)); + } + yyjson_doc_free(idoc); + return nullptr; + } + + pJSONValue->m_readSize = yyjson_doc_get_read_size(idoc); + pJSONValue->m_pDocument = WrapImmutableDocument(idoc); + pJSONValue->m_pVal = root; + + return pJSONValue.release(); +} + +JsonValue* JsonManager::ObjectParseFile(const char* path, yyjson_read_flag read_flg, + char* error, size_t error_size) +{ + if (!path) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Invalid path"); + } + return nullptr; + } + + char realpath[PLATFORM_MAX_PATH]; + smutils->BuildPath(Path_Game, realpath, sizeof(realpath), "%s", path); + auto pJSONValue = CreateWrapper(); + + yyjson_read_err readError; + yyjson_doc* idoc = yyjson_read_file(realpath, read_flg, nullptr, &readError); + + if (!idoc || readError.code) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to parse JSON file: %s (error code: %u, msg: %s, position: %zu)", + realpath, readError.code, readError.msg, readError.pos); + } + return nullptr; + } + + yyjson_val* root = yyjson_doc_get_root(idoc); + + if (!yyjson_is_obj(root)) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Root value in file is not an object (got %s)", yyjson_get_type_desc(root)); + } + yyjson_doc_free(idoc); + return nullptr; + } + + pJSONValue->m_readSize = yyjson_doc_get_read_size(idoc); + pJSONValue->m_pDocument = WrapImmutableDocument(idoc); + pJSONValue->m_pVal = root; + + return pJSONValue.release(); +} + +size_t JsonManager::ObjectGetSize(JsonValue* handle) +{ + if (!handle) { + return 0; + } + + if (handle->IsMutable()) { + return yyjson_mut_obj_size(handle->m_pVal_mut); + } else { + return yyjson_obj_size(handle->m_pVal); + } +} + +bool JsonManager::ObjectGetKey(JsonValue* handle, size_t index, const char** out_key) +{ + if (!handle || !out_key) { + return false; + } + + if (handle->IsMutable()) { + size_t obj_size = yyjson_mut_obj_size(handle->m_pVal_mut); + if (index >= obj_size) { + return false; + } + + yyjson_mut_obj_iter iter; + yyjson_mut_obj_iter_init(handle->m_pVal_mut, &iter); + + for (size_t i = 0; i < index; i++) { + yyjson_mut_obj_iter_next(&iter); + } + + yyjson_mut_val* key = yyjson_mut_obj_iter_next(&iter); + if (!key) { + return false; + } + + *out_key = yyjson_mut_get_str(key); + return true; + } else { + size_t obj_size = yyjson_obj_size(handle->m_pVal); + if (index >= obj_size) { + return false; + } + + yyjson_obj_iter iter; + yyjson_obj_iter_init(handle->m_pVal, &iter); + + for (size_t i = 0; i < index; i++) { + yyjson_obj_iter_next(&iter); + } + + yyjson_val* key = yyjson_obj_iter_next(&iter); + if (!key) { + return false; + } + + *out_key = yyjson_get_str(key); + return true; + } +} + +JsonValue* JsonManager::ObjectGetValueAt(JsonValue* handle, size_t index) +{ + if (!handle) { + return nullptr; + } + + 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()) { + yyjson_mut_obj_iter iter; + yyjson_mut_obj_iter_init(handle->m_pVal_mut, &iter); + + for (size_t i = 0; i < index; i++) { + yyjson_mut_obj_iter_next(&iter); + } + + yyjson_mut_val* key = yyjson_mut_obj_iter_next(&iter); + if (!key) { + return nullptr; + } + + pJSONValue->m_pDocument_mut = handle->m_pDocument_mut; + pJSONValue->m_pVal_mut = yyjson_mut_obj_iter_get_val(key); + } else { + yyjson_obj_iter iter; + yyjson_obj_iter_init(handle->m_pVal, &iter); + + yyjson_val* key; + for (size_t i = 0; i <= index; i++) { + key = yyjson_obj_iter_next(&iter); + if (!key) { + return nullptr; + } + } + + pJSONValue->m_pDocument = handle->m_pDocument; + pJSONValue->m_pVal = yyjson_obj_iter_get_val(key); + } + + return pJSONValue.release(); +} + +JsonValue* JsonManager::ObjectGet(JsonValue* handle, const char* key) +{ + if (!handle || !key) { + return nullptr; + } + + auto pJSONValue = CreateWrapper(); + + if (handle->IsMutable()) { + yyjson_mut_val* val = yyjson_mut_obj_get(handle->m_pVal_mut, key); + if (!val) { + return nullptr; + } + + 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; + } + + pJSONValue->m_pDocument = handle->m_pDocument; + pJSONValue->m_pVal = val; + } + + return pJSONValue.release(); +} + +bool JsonManager::ObjectGetBool(JsonValue* handle, const char* key, bool* out_value) +{ + if (!handle || !key || !out_value) { + return false; + } + + if (handle->IsMutable()) { + yyjson_mut_val* val = yyjson_mut_obj_get(handle->m_pVal_mut, key); + if (!val || !yyjson_mut_is_bool(val)) { + return false; + } + + *out_value = yyjson_mut_get_bool(val); + return true; + } else { + yyjson_val* val = yyjson_obj_get(handle->m_pVal, key); + if (!val || !yyjson_is_bool(val)) { + return false; + } + + *out_value = yyjson_get_bool(val); + return true; + } +} + +bool JsonManager::ObjectGetDouble(JsonValue* handle, const char* key, double* out_value) +{ + if (!handle || !key || !out_value) { + return false; + } + + if (handle->IsMutable()) { + yyjson_mut_val* val = yyjson_mut_obj_get(handle->m_pVal_mut, key); + if (!val || !yyjson_mut_is_num(val)) { + return false; + } + + *out_value = yyjson_mut_get_num(val); + return true; + } else { + yyjson_val* val = yyjson_obj_get(handle->m_pVal, key); + if (!val || !yyjson_is_num(val)) { + return false; + } + + *out_value = yyjson_get_num(val); + return true; + } +} + +bool JsonManager::ObjectGetInt(JsonValue* handle, const char* key, int* out_value) +{ + if (!handle || !key || !out_value) { + return false; + } + + if (handle->IsMutable()) { + yyjson_mut_val* val = yyjson_mut_obj_get(handle->m_pVal_mut, key); + if (!val || !yyjson_mut_is_int(val)) { + return false; + } + + *out_value = yyjson_mut_get_int(val); + return true; + } else { + yyjson_val* val = yyjson_obj_get(handle->m_pVal, key); + if (!val || !yyjson_is_int(val)) { + return false; + } + + *out_value = yyjson_get_int(val); + return true; + } +} + +bool JsonManager::ObjectGetInt64(JsonValue* handle, const char* key, std::variant* out_value) +{ + if (!handle || !key || !out_value) { + return false; + } + + if (handle->IsMutable()) { + yyjson_mut_val* val = yyjson_mut_obj_get(handle->m_pVal_mut, key); + if (!val || !yyjson_mut_is_int(val)) { + return false; + } + + ReadInt64FromMutVal(val, out_value); + return true; + } else { + yyjson_val* val = yyjson_obj_get(handle->m_pVal, key); + if (!val || !yyjson_is_int(val)) { + return false; + } + + ReadInt64FromVal(val, out_value); + return true; + } +} + +bool JsonManager::ObjectGetString(JsonValue* handle, const char* key, const char** out_str, size_t* out_len) +{ + if (!handle || !key || !out_str) { + return false; + } + + if (handle->IsMutable()) { + yyjson_mut_val* val = yyjson_mut_obj_get(handle->m_pVal_mut, key); + if (!val || !yyjson_mut_is_str(val)) { + return false; + } + + *out_str = yyjson_mut_get_str(val); + if (out_len) { + *out_len = yyjson_mut_get_len(val); + } + return true; + } else { + yyjson_val* val = yyjson_obj_get(handle->m_pVal, key); + if (!val || !yyjson_is_str(val)) { + return false; + } + + *out_str = yyjson_get_str(val); + if (out_len) { + *out_len = yyjson_get_len(val); + } + return true; + } +} + +bool JsonManager::ObjectIsNull(JsonValue* handle, const char* key, bool* out_is_null) +{ + if (!handle || !key || !out_is_null) { + return false; + } + + if (handle->IsMutable()) { + yyjson_mut_val* val = yyjson_mut_obj_get(handle->m_pVal_mut, key); + if (!val) { + return false; + } + + *out_is_null = yyjson_mut_is_null(val); + return true; + } else { + yyjson_val* val = yyjson_obj_get(handle->m_pVal, key); + if (!val) { + return false; + } + + *out_is_null = yyjson_is_null(val); + return true; + } +} + +bool JsonManager::ObjectHasKey(JsonValue* handle, const char* key, bool use_pointer) +{ + if (!handle || !key) { + return false; + } + + if (handle->IsMutable()) { + if (use_pointer) { + return yyjson_mut_doc_ptr_get(handle->m_pDocument_mut->get(), key) != nullptr; + } else { + yyjson_mut_obj_iter iter = yyjson_mut_obj_iter_with(handle->m_pVal_mut); + return yyjson_mut_obj_iter_get(&iter, key) != nullptr; + } + } else { + if (use_pointer) { + return yyjson_doc_ptr_get(handle->m_pDocument->get(), key) != nullptr; + } else { + yyjson_obj_iter iter = yyjson_obj_iter_with(handle->m_pVal); + return yyjson_obj_iter_get(&iter, key) != nullptr; + } + } +} + +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; + } + + if (!yyjson_mut_obj_get(handle->m_pVal_mut, old_key)) { + return false; + } + + if (!allow_duplicate && yyjson_mut_obj_get(handle->m_pVal_mut, new_key)) { + return false; + } + + return yyjson_mut_obj_rename_key(handle->m_pDocument_mut->get(), handle->m_pVal_mut, old_key, new_key); +} + +bool JsonManager::ObjectSet(JsonValue* handle, const char* key, JsonValue* value) +{ + if (!handle || !handle->IsMutable() || !key || !value) { + return false; + } + + yyjson_mut_val* val_copy; + if (value->IsMutable()) { + val_copy = yyjson_mut_val_mut_copy(handle->m_pDocument_mut->get(), value->m_pVal_mut); + } else { + val_copy = yyjson_val_mut_copy(handle->m_pDocument_mut->get(), value->m_pVal); + } + + if (!val_copy) { + return false; + } + + return yyjson_mut_obj_put(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut->get(), key), val_copy); +} + +bool JsonManager::ObjectSetBool(JsonValue* handle, const char* key, bool value) +{ + 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_bool(handle->m_pDocument_mut->get(), value)); +} + +bool JsonManager::ObjectSetDouble(JsonValue* handle, const char* key, double 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_real(handle->m_pDocument_mut->get(), value)); +} + +bool JsonManager::ObjectSetInt(JsonValue* handle, const char* key, int 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_int(handle->m_pDocument_mut->get(), value)); +} + +bool JsonManager::ObjectSetInt64(JsonValue* handle, const char* key, std::variant value) +{ + if (!handle || !handle->IsMutable() || !key) { + return false; + } + + if (std::holds_alternative(value)) { + return yyjson_mut_obj_put(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut->get(), key), yyjson_mut_sint(handle->m_pDocument_mut->get(), std::get(value))); + } else { + return yyjson_mut_obj_put(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut->get(), key), yyjson_mut_uint(handle->m_pDocument_mut->get(), std::get(value))); + } +} + +bool JsonManager::ObjectSetNull(JsonValue* handle, const char* key) +{ + 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_null(handle->m_pDocument_mut->get())); +} + +bool JsonManager::ObjectSetString(JsonValue* handle, const char* key, const char* value) +{ + if (!handle || !handle->IsMutable() || !key || !value) { + return false; + } + + return yyjson_mut_obj_put(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut->get(), key), yyjson_mut_strcpy(handle->m_pDocument_mut->get(), value)); +} + +bool JsonManager::ObjectRemove(JsonValue* handle, const char* key) +{ + if (!handle || !handle->IsMutable() || !key) { + return false; + } + + return yyjson_mut_obj_remove_key(handle->m_pVal_mut, key) != nullptr; +} + +bool JsonManager::ObjectClear(JsonValue* handle) +{ + if (!handle || !handle->IsMutable()) { + return false; + } + + return yyjson_mut_obj_clear(handle->m_pVal_mut); +} + +bool JsonManager::ObjectSort(JsonValue* handle, JSON_SORT_ORDER sort_mode) +{ + if (!handle || !handle->IsMutable()) { + return false; + } + + if (!yyjson_mut_is_obj(handle->m_pVal_mut)) { + return false; + } + + 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; + + 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) { + 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 == JSON_SORT_RANDOM) { + std::shuffle(pairs.begin(), pairs.end(), m_randomGenerator); + } + else { + 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.key, pair.val); + } + + return true; +} + +bool JsonManager::ObjectRotate(JsonValue* handle, size_t idx) +{ + if (!handle || !handle->IsMutable()) { + return false; + } + + if (!yyjson_mut_is_obj(handle->m_pVal_mut)) { + return false; + } + + return yyjson_mut_obj_rotate(handle->m_pVal_mut, idx); +} + +JsonValue* JsonManager::ArrayInit() +{ + auto pJSONValue = CreateWrapper(); + pJSONValue->m_pDocument_mut = CreateDocument(); + + if (!pJSONValue->m_pDocument_mut) { + return nullptr; + } + + pJSONValue->m_pVal_mut = yyjson_mut_arr(pJSONValue->m_pDocument_mut->get()); + + if (!pJSONValue->m_pVal_mut) { + return nullptr; + } + + yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut->get(), pJSONValue->m_pVal_mut); + + return pJSONValue.release(); +} + +JsonValue* JsonManager::ArrayInitWithStrings(const char** strings, size_t count) +{ + if (!strings) { + return nullptr; + } + + auto pJSONValue = CreateWrapper(); + pJSONValue->m_pDocument_mut = CreateDocument(); + + if (!pJSONValue->m_pDocument_mut) { + return nullptr; + } + + pJSONValue->m_pVal_mut = yyjson_mut_arr_with_strcpy( + pJSONValue->m_pDocument_mut->get(), + strings, + count + ); + + if (!pJSONValue->m_pVal_mut) { + return nullptr; + } + + yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut->get(), pJSONValue->m_pVal_mut); + + return pJSONValue.release(); +} + +JsonValue* JsonManager::ArrayInitWithInt32(const int32_t* values, size_t count) +{ + if (!values) { + return nullptr; + } + + auto pJSONValue = CreateWrapper(); + pJSONValue->m_pDocument_mut = CreateDocument(); + + if (!pJSONValue->m_pDocument_mut) { + return nullptr; + } + + pJSONValue->m_pVal_mut = yyjson_mut_arr_with_sint32( + pJSONValue->m_pDocument_mut->get(), + values, + count + ); + + if (!pJSONValue->m_pVal_mut) { + return nullptr; + } + + yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut->get(), pJSONValue->m_pVal_mut); + + 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) { + SetErrorSafe(error, error_size, "Invalid values parameter"); + } + return nullptr; + } + + if (count == 0) { + auto pJSONValue = CreateWrapper(); + pJSONValue->m_pDocument_mut = CreateDocument(); + + if (!pJSONValue->m_pDocument_mut) { + return nullptr; + } + + pJSONValue->m_pVal_mut = yyjson_mut_arr(pJSONValue->m_pDocument_mut->get()); + + if (!pJSONValue->m_pVal_mut) { + return nullptr; + } + + yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut->get(), pJSONValue->m_pVal_mut); + + return pJSONValue.release(); + } + + auto pJSONValue = CreateWrapper(); + pJSONValue->m_pDocument_mut = CreateDocument(); + + if (!pJSONValue->m_pDocument_mut) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to create document"); + } + return nullptr; + } + + auto doc = pJSONValue->m_pDocument_mut->get(); + pJSONValue->m_pVal_mut = yyjson_mut_arr(doc); + + if (!pJSONValue->m_pVal_mut) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to create array"); + } + 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; + if (std::holds_alternative(variant_value)) { + val = yyjson_mut_sint(doc, std::get(variant_value)); + } else { + val = yyjson_mut_uint(doc, std::get(variant_value)); + } + + if (!val || !yyjson_mut_arr_append(pJSONValue->m_pVal_mut, val)) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to append value at index %zu", i); + } + return nullptr; + } + } + + yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut->get(), pJSONValue->m_pVal_mut); + + return pJSONValue.release(); +} + +JsonValue* JsonManager::ArrayInitWithBool(const bool* values, size_t count) +{ + if (!values) { + return nullptr; + } + + auto pJSONValue = CreateWrapper(); + pJSONValue->m_pDocument_mut = CreateDocument(); + + if (!pJSONValue->m_pDocument_mut) { + return nullptr; + } + + pJSONValue->m_pVal_mut = yyjson_mut_arr_with_bool( + pJSONValue->m_pDocument_mut->get(), + values, + count + ); + + if (!pJSONValue->m_pVal_mut) { + return nullptr; + } + + yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut->get(), pJSONValue->m_pVal_mut); + + return pJSONValue.release(); +} + +JsonValue* JsonManager::ArrayInitWithDouble(const double* values, size_t count) +{ + if (!values) { + return nullptr; + } + + auto pJSONValue = CreateWrapper(); + pJSONValue->m_pDocument_mut = CreateDocument(); + + if (!pJSONValue->m_pDocument_mut) { + return nullptr; + } + + pJSONValue->m_pVal_mut = yyjson_mut_arr_with_real( + pJSONValue->m_pDocument_mut->get(), + values, + count + ); + + if (!pJSONValue->m_pVal_mut) { + return nullptr; + } + + yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut->get(), pJSONValue->m_pVal_mut); + + return pJSONValue.release(); +} + +JsonValue* JsonManager::ArrayParseString(const char* str, yyjson_read_flag read_flg, + char* error, size_t error_size) +{ + if (!str) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Invalid string"); + } + return nullptr; + } + + auto pJSONValue = CreateWrapper(); + + yyjson_read_err readError; + yyjson_doc* idoc = yyjson_read_opts(const_cast(str), strlen(str), read_flg, nullptr, &readError); + + if (!idoc || readError.code) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to parse JSON string: %s (error code: %u, position: %zu)", + readError.msg, readError.code, readError.pos); + } + return nullptr; + } + + yyjson_val* root = yyjson_doc_get_root(idoc); + + if (!yyjson_is_arr(root)) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Root value is not an array (got %s)", yyjson_get_type_desc(root)); + } + yyjson_doc_free(idoc); + return nullptr; + } + + pJSONValue->m_readSize = yyjson_doc_get_read_size(idoc); + pJSONValue->m_pDocument = WrapImmutableDocument(idoc); + pJSONValue->m_pVal = root; + + return pJSONValue.release(); +} + +JsonValue* JsonManager::ArrayParseFile(const char* path, yyjson_read_flag read_flg, + char* error, size_t error_size) +{ + if (!path) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Invalid path"); + } + return nullptr; + } + + char realpath[PLATFORM_MAX_PATH]; + smutils->BuildPath(Path_Game, realpath, sizeof(realpath), "%s", path); + auto pJSONValue = CreateWrapper(); + + yyjson_read_err readError; + yyjson_doc* idoc = yyjson_read_file(realpath, read_flg, nullptr, &readError); + + if (!idoc || readError.code) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to parse JSON file: %s (error code: %u, msg: %s, position: %zu)", + realpath, readError.code, readError.msg, readError.pos); + } + return nullptr; + } + + yyjson_val* root = yyjson_doc_get_root(idoc); + + if (!yyjson_is_arr(root)) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Root value in file is not an array (got %s)", yyjson_get_type_desc(root)); + } + yyjson_doc_free(idoc); + return nullptr; + } + + pJSONValue->m_readSize = yyjson_doc_get_read_size(idoc); + pJSONValue->m_pDocument = WrapImmutableDocument(idoc); + pJSONValue->m_pVal = root; + + return pJSONValue.release(); +} + +size_t JsonManager::ArrayGetSize(JsonValue* handle) +{ + if (!handle) { + return 0; + } + + if (handle->IsMutable()) { + return yyjson_mut_arr_size(handle->m_pVal_mut); + } else { + return yyjson_arr_size(handle->m_pVal); + } +} + +JsonValue* JsonManager::ArrayGet(JsonValue* handle, size_t index) +{ + if (!handle) { + return nullptr; + } + + auto pJSONValue = CreateWrapper(); + + if (handle->IsMutable()) { + size_t arr_size = yyjson_mut_arr_size(handle->m_pVal_mut); + if (index >= arr_size) { + return nullptr; + } + + yyjson_mut_val* val = yyjson_mut_arr_get(handle->m_pVal_mut, index); + if (!val) { + return nullptr; + } + + 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) { + return nullptr; + } + + yyjson_val* val = yyjson_arr_get(handle->m_pVal, index); + if (!val) { + return nullptr; + } + + pJSONValue->m_pDocument = handle->m_pDocument; + pJSONValue->m_pVal = val; + } + + return pJSONValue.release(); +} + +JsonValue* JsonManager::ArrayGetFirst(JsonValue* handle) +{ + if (!handle) { + return nullptr; + } + + auto pJSONValue = CreateWrapper(); + + if (handle->IsMutable()) { + size_t arr_size = yyjson_mut_arr_size(handle->m_pVal_mut); + if (arr_size == 0) { + return nullptr; + } + + yyjson_mut_val* val = yyjson_mut_arr_get_first(handle->m_pVal_mut); + if (!val) { + return nullptr; + } + + 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) { + return nullptr; + } + + yyjson_val* val = yyjson_arr_get_first(handle->m_pVal); + if (!val) { + return nullptr; + } + + pJSONValue->m_pDocument = handle->m_pDocument; + pJSONValue->m_pVal = val; + } + + return pJSONValue.release(); +} + +JsonValue* JsonManager::ArrayGetLast(JsonValue* handle) +{ + if (!handle) { + return nullptr; + } + + auto pJSONValue = CreateWrapper(); + + if (handle->IsMutable()) { + size_t arr_size = yyjson_mut_arr_size(handle->m_pVal_mut); + if (arr_size == 0) { + return nullptr; + } + + yyjson_mut_val* val = yyjson_mut_arr_get_last(handle->m_pVal_mut); + if (!val) { + return nullptr; + } + + 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) { + return nullptr; + } + + yyjson_val* val = yyjson_arr_get_last(handle->m_pVal); + if (!val) { + return nullptr; + } + + pJSONValue->m_pDocument = handle->m_pDocument; + pJSONValue->m_pVal = val; + } + + return pJSONValue.release(); +} + +bool JsonManager::ArrayGetBool(JsonValue* handle, size_t index, bool* out_value) +{ + if (!handle || !out_value) { + return false; + } + + if (handle->IsMutable()) { + size_t arr_size = yyjson_mut_arr_size(handle->m_pVal_mut); + if (index >= arr_size) { + return false; + } + + yyjson_mut_val* val = yyjson_mut_arr_get(handle->m_pVal_mut, index); + if (!yyjson_mut_is_bool(val)) { + return false; + } + + *out_value = yyjson_mut_get_bool(val); + return true; + } else { + size_t arr_size = yyjson_arr_size(handle->m_pVal); + if (index >= arr_size) { + return false; + } + + yyjson_val* val = yyjson_arr_get(handle->m_pVal, index); + if (!yyjson_is_bool(val)) { + return false; + } + + *out_value = yyjson_get_bool(val); + return true; + } +} + +bool JsonManager::ArrayGetDouble(JsonValue* handle, size_t index, double* out_value) +{ + if (!handle || !out_value) { + return false; + } + + if (handle->IsMutable()) { + size_t arr_size = yyjson_mut_arr_size(handle->m_pVal_mut); + if (index >= arr_size) { + return false; + } + + yyjson_mut_val* val = yyjson_mut_arr_get(handle->m_pVal_mut, index); + if (!yyjson_mut_is_num(val)) { + return false; + } + + *out_value = yyjson_mut_get_num(val); + return true; + } else { + size_t arr_size = yyjson_arr_size(handle->m_pVal); + if (index >= arr_size) { + return false; + } + + yyjson_val* val = yyjson_arr_get(handle->m_pVal, index); + if (!yyjson_is_num(val)) { + return false; + } + + *out_value = yyjson_get_num(val); + return true; + } +} + +bool JsonManager::ArrayGetInt(JsonValue* handle, size_t index, int* out_value) +{ + if (!handle || !out_value) { + return false; + } + + if (handle->IsMutable()) { + size_t arr_size = yyjson_mut_arr_size(handle->m_pVal_mut); + if (index >= arr_size) { + return false; + } + + yyjson_mut_val* val = yyjson_mut_arr_get(handle->m_pVal_mut, index); + if (!yyjson_mut_is_int(val)) { + return false; + } + + *out_value = yyjson_mut_get_int(val); + return true; + } else { + size_t arr_size = yyjson_arr_size(handle->m_pVal); + if (index >= arr_size) { + return false; + } + + yyjson_val* val = yyjson_arr_get(handle->m_pVal, index); + if (!yyjson_is_int(val)) { + return false; + } + + *out_value = yyjson_get_int(val); + return true; + } +} + +bool JsonManager::ArrayGetInt64(JsonValue* handle, size_t index, std::variant* out_value) +{ + if (!handle || !out_value) { + return false; + } + + if (handle->IsMutable()) { + size_t arr_size = yyjson_mut_arr_size(handle->m_pVal_mut); + if (index >= arr_size) { + return false; + } + + yyjson_mut_val* val = yyjson_mut_arr_get(handle->m_pVal_mut, index); + if (!yyjson_mut_is_int(val)) { + return false; + } + + ReadInt64FromMutVal(val, out_value); + return true; + } else { + size_t arr_size = yyjson_arr_size(handle->m_pVal); + if (index >= arr_size) { + return false; + } + + yyjson_val* val = yyjson_arr_get(handle->m_pVal, index); + if (!yyjson_is_int(val)) { + return false; + } + + ReadInt64FromVal(val, out_value); + return true; + } +} + +bool JsonManager::ArrayGetString(JsonValue* handle, size_t index, const char** out_str, size_t* out_len) +{ + if (!handle || !out_str) { + return false; + } + + if (handle->IsMutable()) { + size_t arr_size = yyjson_mut_arr_size(handle->m_pVal_mut); + if (index >= arr_size) { + return false; + } + + yyjson_mut_val* val = yyjson_mut_arr_get(handle->m_pVal_mut, index); + if (!yyjson_mut_is_str(val)) { + return false; + } + + *out_str = yyjson_mut_get_str(val); + if (out_len) { + *out_len = yyjson_mut_get_len(val); + } + return true; + } else { + size_t arr_size = yyjson_arr_size(handle->m_pVal); + if (index >= arr_size) { + return false; + } + + yyjson_val* val = yyjson_arr_get(handle->m_pVal, index); + if (!yyjson_is_str(val)) { + return false; + } + + *out_str = yyjson_get_str(val); + if (out_len) { + *out_len = yyjson_get_len(val); + } + return true; + } +} + +bool JsonManager::ArrayIsNull(JsonValue* handle, size_t index) +{ + if (!handle) { + return false; + } + + if (handle->IsMutable()) { + size_t arr_size = yyjson_mut_arr_size(handle->m_pVal_mut); + if (index >= arr_size) { + return false; + } + + yyjson_mut_val* val = yyjson_mut_arr_get(handle->m_pVal_mut, index); + return yyjson_mut_is_null(val); + } else { + size_t arr_size = yyjson_arr_size(handle->m_pVal); + if (index >= arr_size) { + return false; + } + + yyjson_val* val = yyjson_arr_get(handle->m_pVal, index); + return yyjson_is_null(val); + } +} + +bool JsonManager::ArrayReplace(JsonValue* handle, size_t index, JsonValue* value) +{ + if (!handle || !handle->IsMutable() || !value) { + return false; + } + + size_t arr_size = yyjson_mut_arr_size(handle->m_pVal_mut); + if (index >= arr_size) { + return false; + } + + yyjson_mut_val* val_copy; + if (value->IsMutable()) { + val_copy = yyjson_mut_val_mut_copy(handle->m_pDocument_mut->get(), value->m_pVal_mut); + } else { + val_copy = yyjson_val_mut_copy(handle->m_pDocument_mut->get(), value->m_pVal); + } + + if (!val_copy) { + return false; + } + + return yyjson_mut_arr_replace(handle->m_pVal_mut, index, val_copy) != nullptr; +} + +bool JsonManager::ArrayReplaceBool(JsonValue* handle, size_t index, bool value) +{ + if (!handle || !handle->IsMutable()) { + return false; + } + + size_t arr_size = yyjson_mut_arr_size(handle->m_pVal_mut); + if (index >= arr_size) { + return false; + } + + return yyjson_mut_arr_replace(handle->m_pVal_mut, index, yyjson_mut_bool(handle->m_pDocument_mut->get(), value)) != nullptr; +} + +bool JsonManager::ArrayReplaceDouble(JsonValue* handle, size_t index, double value) +{ + if (!handle || !handle->IsMutable()) { + return false; + } + + size_t arr_size = yyjson_mut_arr_size(handle->m_pVal_mut); + if (index >= arr_size) { + return false; + } + + return yyjson_mut_arr_replace(handle->m_pVal_mut, index, yyjson_mut_real(handle->m_pDocument_mut->get(), value)) != nullptr; +} + +bool JsonManager::ArrayReplaceInt(JsonValue* handle, size_t index, int value) +{ + if (!handle || !handle->IsMutable()) { + return false; + } + + size_t arr_size = yyjson_mut_arr_size(handle->m_pVal_mut); + if (index >= arr_size) { + return false; + } + + return yyjson_mut_arr_replace(handle->m_pVal_mut, index, yyjson_mut_int(handle->m_pDocument_mut->get(), value)) != nullptr; +} + +bool JsonManager::ArrayReplaceInt64(JsonValue* handle, size_t index, std::variant value) +{ + if (!handle || !handle->IsMutable()) { + return false; + } + + size_t arr_size = yyjson_mut_arr_size(handle->m_pVal_mut); + if (index >= arr_size) { + return false; + } + + if (std::holds_alternative(value)) { + return yyjson_mut_arr_replace(handle->m_pVal_mut, index, yyjson_mut_sint(handle->m_pDocument_mut->get(), std::get(value))) != nullptr; + } else { + return yyjson_mut_arr_replace(handle->m_pVal_mut, index, yyjson_mut_uint(handle->m_pDocument_mut->get(), std::get(value))) != nullptr; + } +} + +bool JsonManager::ArrayReplaceNull(JsonValue* handle, size_t index) +{ + if (!handle || !handle->IsMutable()) { + return false; + } + + size_t arr_size = yyjson_mut_arr_size(handle->m_pVal_mut); + if (index >= arr_size) { + return false; + } + + return yyjson_mut_arr_replace(handle->m_pVal_mut, index, yyjson_mut_null(handle->m_pDocument_mut->get())) != nullptr; +} + +bool JsonManager::ArrayReplaceString(JsonValue* handle, size_t index, const char* value) +{ + if (!handle || !handle->IsMutable() || !value) { + return false; + } + + size_t arr_size = yyjson_mut_arr_size(handle->m_pVal_mut); + if (index >= arr_size) { + return false; + } + + return yyjson_mut_arr_replace(handle->m_pVal_mut, index, yyjson_mut_strcpy(handle->m_pDocument_mut->get(), value)) != nullptr; +} + +bool JsonManager::ArrayAppend(JsonValue* handle, JsonValue* value) +{ + if (!handle || !handle->IsMutable() || !value) { + return false; + } + + yyjson_mut_val* val_copy; + if (value->IsMutable()) { + val_copy = yyjson_mut_val_mut_copy(handle->m_pDocument_mut->get(), value->m_pVal_mut); + } else { + val_copy = yyjson_val_mut_copy(handle->m_pDocument_mut->get(), value->m_pVal); + } + + if (!val_copy) { + return false; + } + + return yyjson_mut_arr_append(handle->m_pVal_mut, val_copy); +} + +bool JsonManager::ArrayAppendBool(JsonValue* handle, bool value) +{ + if (!handle || !handle->IsMutable()) { + return false; + } + + return yyjson_mut_arr_append(handle->m_pVal_mut, yyjson_mut_bool(handle->m_pDocument_mut->get(), value)); +} + +bool JsonManager::ArrayAppendDouble(JsonValue* handle, double value) +{ + if (!handle || !handle->IsMutable()) { + return false; + } + + return yyjson_mut_arr_append(handle->m_pVal_mut, yyjson_mut_real(handle->m_pDocument_mut->get(), value)); +} + +bool JsonManager::ArrayAppendInt(JsonValue* handle, int value) +{ + if (!handle || !handle->IsMutable()) { + return false; + } + + return yyjson_mut_arr_append(handle->m_pVal_mut, yyjson_mut_int(handle->m_pDocument_mut->get(), value)); +} + +bool JsonManager::ArrayAppendInt64(JsonValue* handle, std::variant value) +{ + if (!handle || !handle->IsMutable()) { + return false; + } + + if (std::holds_alternative(value)) { + return yyjson_mut_arr_append(handle->m_pVal_mut, yyjson_mut_sint(handle->m_pDocument_mut->get(), std::get(value))); + } else { + return yyjson_mut_arr_append(handle->m_pVal_mut, yyjson_mut_uint(handle->m_pDocument_mut->get(), std::get(value))); + } +} + +bool JsonManager::ArrayAppendNull(JsonValue* handle) +{ + if (!handle || !handle->IsMutable()) { + return false; + } + + return yyjson_mut_arr_append(handle->m_pVal_mut, yyjson_mut_null(handle->m_pDocument_mut->get())); +} + +bool JsonManager::ArrayAppendString(JsonValue* handle, const char* value) +{ + if (!handle || !handle->IsMutable() || !value) { + return false; + } + + return yyjson_mut_arr_append(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut->get(), value)); +} + +bool JsonManager::ArrayInsert(JsonValue* handle, size_t index, JsonValue* value) +{ + if (!handle || !handle->IsMutable() || !value) { + return false; + } + + size_t arr_size = yyjson_mut_arr_size(handle->m_pVal_mut); + if (index > arr_size) { + return false; + } + + yyjson_mut_val* val_copy; + if (value->IsMutable()) { + val_copy = yyjson_mut_val_mut_copy(handle->m_pDocument_mut->get(), value->m_pVal_mut); + } else { + val_copy = yyjson_val_mut_copy(handle->m_pDocument_mut->get(), value->m_pVal); + } + + if (!val_copy) { + return false; + } + + return yyjson_mut_arr_insert(handle->m_pVal_mut, val_copy, index); +} + +bool JsonManager::ArrayInsertBool(JsonValue* handle, size_t index, bool value) +{ + 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; + if (std::holds_alternative(value)) { + val = yyjson_mut_sint(handle->m_pDocument_mut->get(), std::get(value)); + } else { + val = yyjson_mut_uint(handle->m_pDocument_mut->get(), std::get(value)); + } + + if (!val) { + return false; + } + + return yyjson_mut_arr_insert(handle->m_pVal_mut, val, index); +} + +bool JsonManager::ArrayInsertDouble(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; + } + + yyjson_mut_val* val_copy; + if (value->IsMutable()) { + val_copy = yyjson_mut_val_mut_copy(handle->m_pDocument_mut->get(), value->m_pVal_mut); + } else { + val_copy = yyjson_val_mut_copy(handle->m_pDocument_mut->get(), value->m_pVal); + } + + if (!val_copy) { + return false; + } + + return yyjson_mut_arr_prepend(handle->m_pVal_mut, val_copy); +} + +bool JsonManager::ArrayPrependBool(JsonValue* handle, bool value) +{ + 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; + if (std::holds_alternative(value)) { + val = yyjson_mut_sint(handle->m_pDocument_mut->get(), std::get(value)); + } else { + val = yyjson_mut_uint(handle->m_pDocument_mut->get(), std::get(value)); + } + + if (!val) { + return false; + } + + return yyjson_mut_arr_prepend(handle->m_pVal_mut, val); +} + +bool JsonManager::ArrayPrependDouble(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; + } + + size_t arr_size = yyjson_mut_arr_size(handle->m_pVal_mut); + if (index >= arr_size) { + return false; + } + + return yyjson_mut_arr_remove(handle->m_pVal_mut, index) != nullptr; +} + +bool JsonManager::ArrayRemoveFirst(JsonValue* handle) +{ + if (!handle || !handle->IsMutable()) { + return false; + } + + if (yyjson_mut_arr_size(handle->m_pVal_mut) == 0) { + return false; + } + + return yyjson_mut_arr_remove_first(handle->m_pVal_mut) != nullptr; +} + +bool JsonManager::ArrayRemoveLast(JsonValue* handle) +{ + if (!handle || !handle->IsMutable()) { + return false; + } + + if (yyjson_mut_arr_size(handle->m_pVal_mut) == 0) { + return false; + } + + return yyjson_mut_arr_remove_last(handle->m_pVal_mut) != nullptr; +} + +bool JsonManager::ArrayRemoveRange(JsonValue* handle, size_t start_index, size_t count) +{ + if (!handle || !handle->IsMutable()) { + return false; + } + + size_t arr_size = yyjson_mut_arr_size(handle->m_pVal_mut); + + if (start_index >= arr_size) { + return false; + } + + if (count == 0) { + return true; + } + + if (count > (arr_size - start_index)) { + return false; + } + + return yyjson_mut_arr_remove_range(handle->m_pVal_mut, start_index, count); +} + +bool JsonManager::ArrayClear(JsonValue* handle) +{ + if (!handle || !handle->IsMutable()) { + return false; + } + + return yyjson_mut_arr_clear(handle->m_pVal_mut); +} + +int JsonManager::ArrayIndexOfBool(JsonValue* handle, bool 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_bool(val) && yyjson_mut_get_bool(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_bool(val) && yyjson_get_bool(val) == search_value) { + return static_cast(idx); + } + } + } + + return -1; +} + +int JsonManager::ArrayIndexOfString(JsonValue* handle, const char* search_value) +{ + if (!handle || !search_value) { + 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_str(val) && strcmp(yyjson_mut_get_str(val), search_value) == 0) { + return static_cast(idx); + } + } + } else { + size_t idx, max; + yyjson_val *val; + yyjson_arr_foreach(handle->m_pVal, idx, max, val) { + if (yyjson_is_str(val) && strcmp(yyjson_get_str(val), search_value) == 0) { + return static_cast(idx); + } + } + } + + return -1; +} + +int JsonManager::ArrayIndexOfInt(JsonValue* handle, int 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_int(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_int(val) == search_value) { + return static_cast(idx); + } + } + } + + return -1; +} + +int JsonManager::ArrayIndexOfInt64(JsonValue* handle, std::variant search_value) +{ + if (!handle) { + return -1; + } + + bool is_unsigned = std::holds_alternative(search_value); + + if (handle->IsMutable()) { + size_t idx, max; + yyjson_mut_val *val; + yyjson_mut_arr_foreach(handle->m_pVal_mut, idx, max, val) { + if (yyjson_mut_is_int(val)) { + if (is_unsigned) { + if (yyjson_mut_get_uint(val) == std::get(search_value)) { + return static_cast(idx); + } + } else { + if (yyjson_mut_get_sint(val) == std::get(search_value)) { + return static_cast(idx); + } + } + } + } + } else { + size_t idx, max; + yyjson_val *val; + yyjson_arr_foreach(handle->m_pVal, idx, max, val) { + if (yyjson_is_int(val)) { + if (is_unsigned) { + if (yyjson_get_uint(val) == std::get(search_value)) { + return static_cast(idx); + } + } else { + if (yyjson_get_sint(val) == std::get(search_value)) { + return static_cast(idx); + } + } + } + } + } + + return -1; +} + +int JsonManager::ArrayIndexOfDouble(JsonValue* handle, double 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_real(val)) { + double val_num = yyjson_mut_get_real(val); + if (yyjson_equals_fp(val_num, 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_real(val)) { + double val_num = yyjson_get_real(val); + if (yyjson_equals_fp(val_num, search_value)) { + return static_cast(idx); + } + } + } + } + + return -1; +} + +bool JsonManager::ArraySort(JsonValue* handle, JSON_SORT_ORDER sort_mode) +{ + if (!handle || !handle->IsMutable()) { + return false; + } + + if (!yyjson_mut_is_arr(handle->m_pVal_mut)) { + return false; + } + + 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; + + struct ValueInfo { + yyjson_mut_val* val; + uint8_t type; + uint8_t subtype; // 0=double, 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) { + 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 == JSON_SORT_RANDOM) { + std::shuffle(values.begin(), values.end(), m_randomGenerator); + } + else { + auto compare = [sort_mode](const ValueInfo& a, const ValueInfo& b) { + if (a.val == b.val) return false; + + if (a.type != b.type) { + return sort_mode == JSON_SORT_ASC ? a.type < b.type : a.type > b.type; + } + + switch (a.type) { + case YYJSON_TYPE_STR: { + 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 == JSON_SORT_ASC ? cmp < 0 : cmp > 0; + } + case YYJSON_TYPE_NUM: { + 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.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.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; + } + }; + + std::sort(values.begin(), values.end(), compare); + } + + yyjson_mut_arr_clear(handle->m_pVal_mut); + for (const auto& info : values) { + yyjson_mut_arr_append(handle->m_pVal_mut, info.val); + } + + return true; +} + +bool JsonManager::ArrayRotate(JsonValue* handle, size_t idx) +{ + if (!handle || !handle->IsMutable()) { + return false; + } + + if (!yyjson_mut_is_arr(handle->m_pVal_mut)) { + return false; + } + + return yyjson_mut_arr_rotate(handle->m_pVal_mut, idx); +} + +const char* JsonManager::SkipSeparators(const char* ptr) +{ + while (*ptr && (isspace(*ptr) || *ptr == ':' || *ptr == ',')) { + ptr++; + } + return ptr; +} + +yyjson_mut_val* JsonManager::PackImpl(yyjson_mut_doc* doc, const char* format, + IPackParamProvider* provider, + char* error, size_t error_size, + const char** out_end_ptr) +{ + if (!doc || !format || !*format) { + SetErrorSafe(error, error_size, "Invalid argument(s)"); + return nullptr; + } + + yyjson_mut_val* root; + const char* ptr = format; + + bool is_obj = false; + if (*ptr == '{') { + root = yyjson_mut_obj(doc); + is_obj = true; + ptr = SkipSeparators(ptr + 1); + } else if (*ptr == '[') { + root = yyjson_mut_arr(doc); + ptr = SkipSeparators(ptr + 1); + } else { + SetErrorSafe(error, error_size, "Invalid format string: expected '{' or '['"); + return nullptr; + } + + if (!root) { + SetErrorSafe(error, error_size, "Failed to create root object/array"); + return nullptr; + } + + yyjson_mut_val* key_val; + yyjson_mut_val* val; + + while (*ptr && *ptr != '}' && *ptr != ']') { + if (is_obj) { + if (*ptr != 's') { + SetErrorSafe(error, error_size, "Object key must be string, got '%c'", *ptr); + return nullptr; + } + } + switch (*ptr) { + case 's': { + if (is_obj) { + const char* key; + if (!provider->GetNextString(&key)) { + SetErrorSafe(error, error_size, "Invalid string key"); + return nullptr; + } + key_val = yyjson_mut_strcpy(doc, key); + if (!key_val) { + SetErrorSafe(error, error_size, "Failed to create key"); + return nullptr; + } + + ptr = SkipSeparators(ptr + 1); + if (*ptr != 's' && *ptr != 'i' && *ptr != 'f' && *ptr != 'b' && + *ptr != 'n' && *ptr != '{' && *ptr != '[') { + SetErrorSafe(error, error_size, "Invalid value type after key"); + return nullptr; + } + + if (*ptr == '{' || *ptr == '[') { + val = PackImpl(doc, ptr, provider, error, error_size, &ptr); + if (!val) { + return nullptr; + } + } else { + switch (*ptr) { + case 's': { + const char* val_str; + if (!provider->GetNextString(&val_str)) { + SetErrorSafe(error, error_size, "Invalid string value"); + return nullptr; + } + val = yyjson_mut_strcpy(doc, val_str); + ptr++; + break; + } + case 'i': { + int val_int; + if (!provider->GetNextInt(&val_int)) { + SetErrorSafe(error, error_size, "Invalid integer value"); + return nullptr; + } + val = yyjson_mut_int(doc, val_int); + ptr++; + break; + } + case 'f': { + float val_float; + if (!provider->GetNextFloat(&val_float)) { + SetErrorSafe(error, error_size, "Invalid float value"); + return nullptr; + } + val = yyjson_mut_real(doc, val_float); + ptr++; + break; + } + case 'b': { + bool val_bool; + if (!provider->GetNextBool(&val_bool)) { + SetErrorSafe(error, error_size, "Invalid boolean value"); + return nullptr; + } + val = yyjson_mut_bool(doc, val_bool); + ptr++; + break; + } + case 'n': { + val = yyjson_mut_null(doc); + ptr++; + break; + } + } + } + + if (!val) { + SetErrorSafe(error, error_size, "Failed to create value"); + return nullptr; + } + + if (!yyjson_mut_obj_add(root, key_val, val)) { + SetErrorSafe(error, error_size, "Failed to add value to object"); + return nullptr; + } + } else { + const char* val_str; + if (!provider->GetNextString(&val_str)) { + SetErrorSafe(error, error_size, "Invalid string value"); + return nullptr; + } + if (!yyjson_mut_arr_add_strcpy(doc, root, val_str)) { + SetErrorSafe(error, error_size, "Failed to add string to array"); + return nullptr; + } + ptr++; + } + break; + } + case 'i': { + int val_int; + if (!provider->GetNextInt(&val_int)) { + SetErrorSafe(error, error_size, "Invalid integer value"); + return nullptr; + } + if (!yyjson_mut_arr_add_int(doc, root, val_int)) { + SetErrorSafe(error, error_size, "Failed to add integer to array"); + return nullptr; + } + ptr++; + break; + } + case 'b': { + bool val_bool; + if (!provider->GetNextBool(&val_bool)) { + SetErrorSafe(error, error_size, "Invalid boolean value"); + return nullptr; + } + if (!yyjson_mut_arr_add_bool(doc, root, val_bool)) { + SetErrorSafe(error, error_size, "Failed to add boolean to array"); + return nullptr; + } + ptr++; + break; + } + case 'n': { + if (!yyjson_mut_arr_add_null(doc, root)) { + SetErrorSafe(error, error_size, "Failed to add null to array"); + return nullptr; + } + ptr++; + break; + } + case 'f': { + float val_float; + if (!provider->GetNextFloat(&val_float)) { + SetErrorSafe(error, error_size, "Invalid float value"); + return nullptr; + } + if (!yyjson_mut_arr_add_real(doc, root, val_float)) { + SetErrorSafe(error, error_size, "Failed to add float to array"); + return nullptr; + } + ptr++; + break; + } + case '{': + case '[': { + val = PackImpl(doc, ptr, provider, error, error_size, &ptr); + if (!val) { + return nullptr; + } + if (!yyjson_mut_arr_append(root, val)) { + SetErrorSafe(error, error_size, "Failed to add nested value to array"); + return nullptr; + } + break; + } + default: { + SetErrorSafe(error, error_size, "Invalid format character: %c", *ptr); + return nullptr; + } + } + ptr = SkipSeparators(ptr); + } + + if (*ptr != (is_obj ? '}' : ']')) { + SetErrorSafe(error, error_size, "Unexpected end of format string"); + return nullptr; + } + + if (out_end_ptr) { + *out_end_ptr = ptr + 1; + } + + return root; +} + +JsonValue* JsonManager::Pack(const char* format, IPackParamProvider* param_provider, char* error, size_t error_size) +{ + if (!format || !param_provider) { + SetErrorSafe(error, error_size, "Invalid arguments"); + return nullptr; + } + + auto pJSONValue = CreateWrapper(); + pJSONValue->m_pDocument_mut = CreateDocument(); + + if (!pJSONValue->m_pDocument_mut) { + SetErrorSafe(error, error_size, "Failed to create document"); + return nullptr; + } + + const char* end_ptr; + pJSONValue->m_pVal_mut = PackImpl(pJSONValue->m_pDocument_mut->get(), format, + param_provider, error, error_size, &end_ptr); + + if (!pJSONValue->m_pVal_mut) { + return nullptr; + } + + yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut->get(), pJSONValue->m_pVal_mut); + + return pJSONValue.release(); +} + +JsonValue* JsonManager::CreateBool(bool value) +{ + auto pJSONValue = CreateWrapper(); + pJSONValue->m_pDocument_mut = CreateDocument(); + + if (!pJSONValue->m_pDocument_mut) { + return nullptr; + } + + pJSONValue->m_pVal_mut = yyjson_mut_bool(pJSONValue->m_pDocument_mut->get(), value); + + if (!pJSONValue->m_pVal_mut) { + return nullptr; + } + + yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut->get(), pJSONValue->m_pVal_mut); + + return pJSONValue.release(); +} + +JsonValue* JsonManager::CreateDouble(double value) +{ + auto pJSONValue = CreateWrapper(); + pJSONValue->m_pDocument_mut = CreateDocument(); + + if (!pJSONValue->m_pDocument_mut) { + return nullptr; + } + + pJSONValue->m_pVal_mut = yyjson_mut_real(pJSONValue->m_pDocument_mut->get(), value); + + if (!pJSONValue->m_pVal_mut) { + return nullptr; + } + + yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut->get(), pJSONValue->m_pVal_mut); + + return pJSONValue.release(); +} + +JsonValue* JsonManager::CreateInt(int value) +{ + auto pJSONValue = CreateWrapper(); + pJSONValue->m_pDocument_mut = CreateDocument(); + + if (!pJSONValue->m_pDocument_mut) { + return nullptr; + } + + pJSONValue->m_pVal_mut = yyjson_mut_int(pJSONValue->m_pDocument_mut->get(), value); + + if (!pJSONValue->m_pVal_mut) { + return nullptr; + } + + yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut->get(), pJSONValue->m_pVal_mut); + + return pJSONValue.release(); +} + +JsonValue* JsonManager::CreateInt64(std::variant value) +{ + auto pJSONValue = CreateWrapper(); + pJSONValue->m_pDocument_mut = CreateDocument(); + + if (!pJSONValue->m_pDocument_mut) { + return nullptr; + } + + auto* doc = pJSONValue->m_pDocument_mut->get(); + + if (std::holds_alternative(value)) { + pJSONValue->m_pVal_mut = yyjson_mut_sint(doc, std::get(value)); + } else { + pJSONValue->m_pVal_mut = yyjson_mut_uint(doc, std::get(value)); + } + + if (!pJSONValue->m_pVal_mut) { + return nullptr; + } + + yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut->get(), pJSONValue->m_pVal_mut); + + return pJSONValue.release(); +} + +JsonValue* JsonManager::CreateNull() +{ + auto pJSONValue = CreateWrapper(); + pJSONValue->m_pDocument_mut = CreateDocument(); + + if (!pJSONValue->m_pDocument_mut) { + return nullptr; + } + + pJSONValue->m_pVal_mut = yyjson_mut_null(pJSONValue->m_pDocument_mut->get()); + + if (!pJSONValue->m_pVal_mut) { + return nullptr; + } + + yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut->get(), pJSONValue->m_pVal_mut); + + return pJSONValue.release(); +} + +JsonValue* JsonManager::CreateString(const char* value) +{ + if (!value) { + return nullptr; + } + + auto pJSONValue = CreateWrapper(); + pJSONValue->m_pDocument_mut = CreateDocument(); + + if (!pJSONValue->m_pDocument_mut) { + return nullptr; + } + + pJSONValue->m_pVal_mut = yyjson_mut_strcpy(pJSONValue->m_pDocument_mut->get(), value); + + if (!pJSONValue->m_pVal_mut) { + return nullptr; + } + + yyjson_mut_doc_set_root(pJSONValue->m_pDocument_mut->get(), pJSONValue->m_pVal_mut); + + return pJSONValue.release(); +} + +bool JsonManager::GetBool(JsonValue* handle, bool* out_value) +{ + if (!handle || !out_value) { + return false; + } + + if (handle->IsMutable()) { + if (!yyjson_mut_is_bool(handle->m_pVal_mut)) { + return false; + } + *out_value = yyjson_mut_get_bool(handle->m_pVal_mut); + return true; + } else { + if (!yyjson_is_bool(handle->m_pVal)) { + return false; + } + *out_value = yyjson_get_bool(handle->m_pVal); + return true; + } +} + +bool JsonManager::GetDouble(JsonValue* handle, double* out_value) +{ + if (!handle || !out_value) { + return false; + } + + if (handle->IsMutable()) { + if (!yyjson_mut_is_num(handle->m_pVal_mut)) { + return false; + } + *out_value = yyjson_mut_get_num(handle->m_pVal_mut); + return true; + } else { + if (!yyjson_is_num(handle->m_pVal)) { + return false; + } + *out_value = yyjson_get_num(handle->m_pVal); + return true; + } +} + +bool JsonManager::GetInt(JsonValue* handle, int* out_value) +{ + if (!handle || !out_value) { + return false; + } + + if (handle->IsMutable()) { + if (!yyjson_mut_is_int(handle->m_pVal_mut)) { + return false; + } + *out_value = yyjson_mut_get_int(handle->m_pVal_mut); + return true; + } else { + if (!yyjson_is_int(handle->m_pVal)) { + return false; + } + *out_value = yyjson_get_int(handle->m_pVal); + return true; + } +} + +bool JsonManager::GetInt64(JsonValue* handle, std::variant* out_value) +{ + if (!handle || !out_value) { + return false; + } + + if (handle->IsMutable()) { + if (!yyjson_mut_is_int(handle->m_pVal_mut)) { + return false; + } + ReadInt64FromMutVal(handle->m_pVal_mut, out_value); + return true; + } else { + if (!yyjson_is_int(handle->m_pVal)) { + return false; + } + ReadInt64FromVal(handle->m_pVal, out_value); + return true; + } +} + +bool JsonManager::GetString(JsonValue* handle, const char** out_str, size_t* out_len) +{ + if (!handle || !out_str) { + return false; + } + + if (handle->IsMutable()) { + if (!yyjson_mut_is_str(handle->m_pVal_mut)) { + return false; + } + *out_str = yyjson_mut_get_str(handle->m_pVal_mut); + if (out_len) { + *out_len = yyjson_mut_get_len(handle->m_pVal_mut); + } + return true; + } else { + if (!yyjson_is_str(handle->m_pVal)) { + return false; + } + *out_str = yyjson_get_str(handle->m_pVal); + if (out_len) { + *out_len = yyjson_get_len(handle->m_pVal); + } + return true; + } +} + +JsonValue* JsonManager::PtrGet(JsonValue* handle, const char* path, char* error, size_t error_size) +{ + if (!handle || !path) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Invalid parameters"); + } + return nullptr; + } + + auto pJSONValue = 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) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); + } + return nullptr; + } + + 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) { + SetErrorSafe(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); + } + return nullptr; + } + + pJSONValue->m_pDocument = handle->m_pDocument; + pJSONValue->m_pVal = val; + } + + return pJSONValue.release(); +} + +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) { + SetErrorSafe(error, error_size, "Invalid parameters"); + } + 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 (ptrGetError.code) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); + } + return false; + } + + if (!yyjson_mut_is_bool(val)) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Type mismatch at path '%s': expected boolean value, got %s", path, yyjson_mut_get_type_desc(val)); + } + return false; + } + + *out_value = yyjson_mut_get_bool(val); + return true; + } else { + yyjson_val* val = yyjson_doc_ptr_getx(handle->m_pDocument->get(), path, strlen(path), &ptrGetError); + + if (ptrGetError.code) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); + } + return false; + } + + if (!yyjson_is_bool(val)) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Type mismatch at path '%s': expected boolean value, got %s", path, yyjson_get_type_desc(val)); + } + return false; + } + + *out_value = yyjson_get_bool(val); + return true; + } +} + +bool JsonManager::PtrGetDouble(JsonValue* handle, const char* path, double* out_value, char* error, size_t error_size) +{ + if (!handle || !path || !out_value) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Invalid parameters"); + } + 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 (ptrGetError.code) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); + } + return false; + } + + if (!yyjson_mut_is_num(val)) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Type mismatch at path '%s': expected number value, got %s", path, yyjson_mut_get_type_desc(val)); + } + return false; + } + + *out_value = yyjson_mut_get_num(val); + return true; + } else { + yyjson_val* val = yyjson_doc_ptr_getx(handle->m_pDocument->get(), path, strlen(path), &ptrGetError); + + if (ptrGetError.code) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); + } + return false; + } + + if (!yyjson_is_num(val)) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Type mismatch at path '%s': expected number value, got %s", path, yyjson_get_type_desc(val)); + } + return false; + } + + *out_value = yyjson_get_num(val); + return true; + } +} + +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) { + SetErrorSafe(error, error_size, "Invalid parameters"); + } + 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 (ptrGetError.code) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); + } + return false; + } + + if (!yyjson_mut_is_int(val)) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Type mismatch at path '%s': expected integer value, got %s", path, yyjson_mut_get_type_desc(val)); + } + return false; + } + + *out_value = yyjson_mut_get_int(val); + return true; + } else { + yyjson_val* val = yyjson_doc_ptr_getx(handle->m_pDocument->get(), path, strlen(path), &ptrGetError); + + if (ptrGetError.code) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); + } + return false; + } + + if (!yyjson_is_int(val)) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Type mismatch at path '%s': expected integer value, got %s", path, yyjson_get_type_desc(val)); + } + return false; + } + + *out_value = yyjson_get_int(val); + return true; + } +} + +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) { + SetErrorSafe(error, error_size, "Invalid parameters"); + } + 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) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); + } + return false; + } + + if (!yyjson_mut_is_int(val)) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Type mismatch at path '%s': expected integer64 value, got %s", path, yyjson_mut_get_type_desc(val)); + } + return false; + } + + ReadInt64FromMutVal(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) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); + } + return false; + } + + if (!yyjson_is_int(val)) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Type mismatch at path '%s': expected integer64 value, got %s", path, yyjson_get_type_desc(val)); + } + return false; + } + + ReadInt64FromVal(val, out_value); + return true; + } +} + +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) { + SetErrorSafe(error, error_size, "Invalid parameters"); + } + 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 (ptrGetError.code) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); + } + return false; + } + + if (!yyjson_mut_is_str(val)) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Type mismatch at path '%s': expected string value, got %s", path, yyjson_mut_get_type_desc(val)); + } + return false; + } + + *out_str = yyjson_mut_get_str(val); + if (out_len) { + *out_len = yyjson_mut_get_len(val); + } + return true; + } else { + yyjson_val* val = yyjson_doc_ptr_getx(handle->m_pDocument->get(), path, strlen(path), &ptrGetError); + + if (ptrGetError.code) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); + } + return false; + } + + if (!yyjson_is_str(val)) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Type mismatch at path '%s': expected string value, got %s", path, yyjson_get_type_desc(val)); + } + return false; + } + + *out_str = yyjson_get_str(val); + if (out_len) { + *out_len = yyjson_get_len(val); + } + return true; + } +} + +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) { + SetErrorSafe(error, error_size, "Invalid parameters"); + } + 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 (ptrGetError.code) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); + } + return false; + } + + *out_is_null = yyjson_mut_is_null(val); + return true; + } else { + yyjson_val* val = yyjson_doc_ptr_getx(handle->m_pDocument->get(), path, strlen(path), &ptrGetError); + + if (ptrGetError.code) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); + } + return false; + } + + *out_is_null = yyjson_is_null(val); + return true; + } +} + +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) { + SetErrorSafe(error, error_size, "Invalid parameters"); + } + 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 (ptrGetError.code) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); + } + return false; + } + + if (yyjson_mut_is_str(val)) { + *out_len = yyjson_mut_get_len(val) + 1; + } else { + *out_len = yyjson_mut_get_len(val); + } + return true; + } else { + yyjson_val* val = yyjson_doc_ptr_getx(handle->m_pDocument->get(), path, strlen(path), &ptrGetError); + + if (ptrGetError.code) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to resolve JSON pointer: %s (error code: %u, position: %zu, path: %s)", + ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); + } + return false; + } + + if (yyjson_is_str(val)) { + *out_len = yyjson_get_len(val) + 1; + } else { + *out_len = yyjson_get_len(val); + } + return true; + } +} + +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) { + SetErrorSafe(error, error_size, "Invalid parameters or immutable document"); + } + return false; + } + + yyjson_mut_val* val_copy; + if (value->IsMutable()) { + val_copy = yyjson_mut_val_mut_copy(handle->m_pDocument_mut->get(), value->m_pVal_mut); + } else { + val_copy = yyjson_val_mut_copy(handle->m_pDocument_mut->get(), value->m_pVal); + } + + if (!val_copy) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to copy JSON value"); + } + return false; + } + + yyjson_ptr_err ptrSetError; + bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut->get(), path, strlen(path), val_copy, true, nullptr, &ptrSetError); + + if (!success && ptrSetError.code && error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", + ptrSetError.msg, ptrSetError.code, ptrSetError.pos, path); + } + + return success; +} + +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) { + SetErrorSafe(error, error_size, "Invalid parameters or immutable document"); + } + return false; + } + + yyjson_mut_val* val = yyjson_mut_bool(handle->m_pDocument_mut->get(), value); + if (!val) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to create JSON value"); + } + return false; + } + + yyjson_ptr_err ptrSetError; + bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut->get(), path, strlen(path), val, true, nullptr, &ptrSetError); + + if (!success && ptrSetError.code && error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", + ptrSetError.msg, ptrSetError.code, ptrSetError.pos, path); + } + + return success; +} + +bool JsonManager::PtrSetDouble(JsonValue* handle, const char* path, double value, char* error, size_t error_size) +{ + if (!handle || !handle->IsMutable() || !path) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Invalid parameters or immutable document"); + } + return false; + } + + yyjson_mut_val* val = yyjson_mut_real(handle->m_pDocument_mut->get(), value); + if (!val) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to create JSON value"); + } + return false; + } + + yyjson_ptr_err ptrSetError; + bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut->get(), path, strlen(path), val, true, nullptr, &ptrSetError); + + if (!success && ptrSetError.code && error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", + ptrSetError.msg, ptrSetError.code, ptrSetError.pos, path); + } + + return success; +} + +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) { + SetErrorSafe(error, error_size, "Invalid parameters or immutable document"); + } + return false; + } + + yyjson_mut_val* val = yyjson_mut_int(handle->m_pDocument_mut->get(), value); + if (!val) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to create JSON value"); + } + return false; + } + + yyjson_ptr_err ptrSetError; + bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut->get(), path, strlen(path), val, true, nullptr, &ptrSetError); + + if (!success && ptrSetError.code && error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", + ptrSetError.msg, ptrSetError.code, ptrSetError.pos, path); + } + + return success; +} + +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) { + SetErrorSafe(error, error_size, "Invalid parameters or immutable document"); + } + return false; + } + + yyjson_mut_val* val; + if (std::holds_alternative(value)) { + val = yyjson_mut_sint(handle->m_pDocument_mut->get(), std::get(value)); + } else { + val = yyjson_mut_uint(handle->m_pDocument_mut->get(), std::get(value)); + } + if (!val) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to create JSON value"); + } + return false; + } + + yyjson_ptr_err ptrSetError; + bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut->get(), path, strlen(path), val, true, nullptr, &ptrSetError); + + if (!success && ptrSetError.code && error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", + ptrSetError.msg, ptrSetError.code, ptrSetError.pos, path); + } + + return success; +} + +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) { + SetErrorSafe(error, error_size, "Invalid parameters or immutable document"); + } + return false; + } + + yyjson_mut_val* val = yyjson_mut_strcpy(handle->m_pDocument_mut->get(), value); + if (!val) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to create JSON value"); + } + return false; + } + + yyjson_ptr_err ptrSetError; + bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut->get(), path, strlen(path), val, true, nullptr, &ptrSetError); + + if (!success && ptrSetError.code && error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", + ptrSetError.msg, ptrSetError.code, ptrSetError.pos, path); + } + + return success; +} + +bool JsonManager::PtrSetNull(JsonValue* handle, const char* path, char* error, size_t error_size) +{ + if (!handle || !handle->IsMutable() || !path) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Invalid parameters or immutable document"); + } + return false; + } + + yyjson_mut_val* val = yyjson_mut_null(handle->m_pDocument_mut->get()); + if (!val) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to create JSON value"); + } + return false; + } + + yyjson_ptr_err ptrSetError; + bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut->get(), path, strlen(path), val, true, nullptr, &ptrSetError); + + if (!success && ptrSetError.code && error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to set JSON pointer: %s (error code: %u, position: %zu, path: %s)", + ptrSetError.msg, ptrSetError.code, ptrSetError.pos, path); + } + + return success; +} + +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) { + SetErrorSafe(error, error_size, "Invalid parameters or immutable document"); + } + return false; + } + + yyjson_mut_val* val_copy; + if (value->IsMutable()) { + val_copy = yyjson_mut_val_mut_copy(handle->m_pDocument_mut->get(), value->m_pVal_mut); + } else { + val_copy = yyjson_val_mut_copy(handle->m_pDocument_mut->get(), value->m_pVal); + } + + if (!val_copy) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to copy JSON value"); + } + return false; + } + + yyjson_ptr_err ptrAddError; + bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut->get(), path, strlen(path), val_copy, true, nullptr, &ptrAddError); + + if (!success && ptrAddError.code && error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", + ptrAddError.msg, ptrAddError.code, ptrAddError.pos, path); + } + + return success; +} + +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) { + SetErrorSafe(error, error_size, "Invalid parameters or immutable document"); + } + return false; + } + + yyjson_mut_val* val = yyjson_mut_bool(handle->m_pDocument_mut->get(), value); + if (!val) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to create JSON value"); + } + return false; + } + + yyjson_ptr_err ptrAddError; + bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut->get(), path, strlen(path), val, true, nullptr, &ptrAddError); + + if (!success && ptrAddError.code && error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", + ptrAddError.msg, ptrAddError.code, ptrAddError.pos, path); + } + + return success; +} + +bool JsonManager::PtrAddDouble(JsonValue* handle, const char* path, double value, char* error, size_t error_size) +{ + if (!handle || !handle->IsMutable() || !path) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Invalid parameters or immutable document"); + } + return false; + } + + yyjson_mut_val* val = yyjson_mut_real(handle->m_pDocument_mut->get(), value); + if (!val) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to create JSON value"); + } + return false; + } + + yyjson_ptr_err ptrAddError; + bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut->get(), path, strlen(path), val, true, nullptr, &ptrAddError); + + if (!success && ptrAddError.code && error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", + ptrAddError.msg, ptrAddError.code, ptrAddError.pos, path); + } + + return success; +} + +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) { + SetErrorSafe(error, error_size, "Invalid parameters or immutable document"); + } + return false; + } + + yyjson_mut_val* val = yyjson_mut_int(handle->m_pDocument_mut->get(), value); + if (!val) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to create JSON value"); + } + return false; + } + + yyjson_ptr_err ptrAddError; + bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut->get(), path, strlen(path), val, true, nullptr, &ptrAddError); + + if (!success && ptrAddError.code && error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", + ptrAddError.msg, ptrAddError.code, ptrAddError.pos, path); + } + + return success; +} + +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) { + SetErrorSafe(error, error_size, "Invalid parameters or immutable document"); + } + return false; + } + + yyjson_mut_val* val; + if (std::holds_alternative(value)) { + val = yyjson_mut_sint(handle->m_pDocument_mut->get(), std::get(value)); + } else { + val = yyjson_mut_uint(handle->m_pDocument_mut->get(), std::get(value)); + } + if (!val) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to create JSON value"); + } + return false; + } + + yyjson_ptr_err ptrAddError; + bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut->get(), path, strlen(path), val, true, nullptr, &ptrAddError); + + if (!success && ptrAddError.code && error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", + ptrAddError.msg, ptrAddError.code, ptrAddError.pos, path); + } + + return success; +} + +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) { + SetErrorSafe(error, error_size, "Invalid parameters or immutable document"); + } + return false; + } + + yyjson_mut_val* val = yyjson_mut_strcpy(handle->m_pDocument_mut->get(), value); + if (!val) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to create JSON value"); + } + return false; + } + + yyjson_ptr_err ptrAddError; + bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut->get(), path, strlen(path), val, true, nullptr, &ptrAddError); + + if (!success && ptrAddError.code && error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", + ptrAddError.msg, ptrAddError.code, ptrAddError.pos, path); + } + + return success; +} + +bool JsonManager::PtrAddNull(JsonValue* handle, const char* path, char* error, size_t error_size) +{ + if (!handle || !handle->IsMutable() || !path) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Invalid parameters or immutable document"); + } + return false; + } + + yyjson_mut_val* val = yyjson_mut_null(handle->m_pDocument_mut->get()); + if (!val) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to create JSON value"); + } + return false; + } + + yyjson_ptr_err ptrAddError; + bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut->get(), path, strlen(path), val, true, nullptr, &ptrAddError); + + if (!success && ptrAddError.code && error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to add JSON pointer: %s (error code: %u, position: %zu, path: %s)", + ptrAddError.msg, ptrAddError.code, ptrAddError.pos, path); + } + + return success; +} + +bool JsonManager::PtrRemove(JsonValue* handle, const char* path, char* error, size_t error_size) +{ + if (!handle || !handle->IsMutable() || !path) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Invalid parameters or immutable document"); + } + return false; + } + + yyjson_ptr_err ptrRemoveError; + bool success = yyjson_mut_doc_ptr_removex(handle->m_pDocument_mut->get(), path, strlen(path), nullptr, &ptrRemoveError) != nullptr; + + if (!success && ptrRemoveError.code && error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to remove JSON pointer: %s (error code: %u, position: %zu, path: %s)", + ptrRemoveError.msg, ptrRemoveError.code, ptrRemoveError.pos, path); + } + + return success; +} + +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 result = PtrGetValueInternal(handle, path); + if (!result.success) { + return nullptr; + } + + 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 pJSONValue.release(); +} + +bool JsonManager::PtrTryGetBool(JsonValue* handle, const char* path, bool* out_value) +{ + if (!handle || !path || !out_value) { + return false; + } + + auto result = PtrGetValueInternal(handle, path); + if (!result.success) { + return false; + } + + if (handle->IsMutable()) { + if (!yyjson_mut_is_bool(result.mut_val)) { + return false; + } + *out_value = yyjson_mut_get_bool(result.mut_val); + return true; + } else { + if (!yyjson_is_bool(result.imm_val)) { + return false; + } + *out_value = yyjson_get_bool(result.imm_val); + return true; + } +} + +bool JsonManager::PtrTryGetDouble(JsonValue* handle, const char* path, double* out_value) +{ + if (!handle || !path || !out_value) { + return false; + } + + auto result = PtrGetValueInternal(handle, path); + if (!result.success) { + return false; + } + + if (handle->IsMutable()) { + if (!yyjson_mut_is_num(result.mut_val)) { + return false; + } + *out_value = yyjson_mut_get_num(result.mut_val); + return true; + } else { + if (!yyjson_is_num(result.imm_val)) { + return false; + } + *out_value = yyjson_get_num(result.imm_val); + return true; + } +} + +bool JsonManager::PtrTryGetInt(JsonValue* handle, const char* path, int* out_value) +{ + if (!handle || !path || !out_value) { + return false; + } + + auto result = PtrGetValueInternal(handle, path); + if (!result.success) { + return false; + } + + if (handle->IsMutable()) { + if (!yyjson_mut_is_int(result.mut_val)) { + return false; + } + *out_value = yyjson_mut_get_int(result.mut_val); + return true; + } else { + if (!yyjson_is_int(result.imm_val)) { + return false; + } + *out_value = yyjson_get_int(result.imm_val); + return true; + } +} + +bool JsonManager::PtrTryGetInt64(JsonValue* handle, const char* path, std::variant* out_value) +{ + if (!handle || !path || !out_value) { + return false; + } + + auto result = PtrGetValueInternal(handle, path); + if (!result.success) { + return false; + } + + if (handle->IsMutable()) { + if (!yyjson_mut_is_int(result.mut_val)) { + return false; + } + ReadInt64FromMutVal(result.mut_val, out_value); + return true; + } else { + if (!yyjson_is_int(result.imm_val)) { + return false; + } + ReadInt64FromVal(result.imm_val, out_value); + return true; + } +} + +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; + } + + if (handle->IsMutable()) { + if (!yyjson_mut_is_str(result.mut_val)) { + return false; + } + *out_str = yyjson_mut_get_str(result.mut_val); + if (out_len) { + *out_len = yyjson_mut_get_len(result.mut_val); + } + return true; + } else { + if (!yyjson_is_str(result.imm_val)) { + return false; + } + *out_str = yyjson_get_str(result.imm_val); + if (out_len) { + *out_len = yyjson_get_len(result.imm_val); + } + return true; + } +} + +bool JsonManager::ObjectForeachNext(JsonValue* handle, const char** out_key, + size_t* out_key_len, JsonValue** out_value) +{ + if (!handle || !IsObject(handle)) { + return false; + } + + if (IsMutable(handle)) { + if (!handle->m_iterInitialized) { + if (!yyjson_mut_obj_iter_init(handle->m_pVal_mut, &handle->m_iterObj)) { + return false; + } + handle->m_iterInitialized = true; + } + + yyjson_mut_val* key = yyjson_mut_obj_iter_next(&handle->m_iterObj); + if (key) { + yyjson_mut_val* val = yyjson_mut_obj_iter_get_val(key); + + *out_key = yyjson_mut_get_str(key); + if (out_key_len) { + *out_key_len = yyjson_mut_get_len(key); + } + + auto pWrapper = CreateWrapper(); + pWrapper->m_pDocument_mut = handle->m_pDocument_mut; + pWrapper->m_pVal_mut = val; + *out_value = pWrapper.release(); + + return true; + } + } else { + if (!handle->m_iterInitialized) { + if (!yyjson_obj_iter_init(handle->m_pVal, &handle->m_iterObjImm)) { + return false; + } + handle->m_iterInitialized = true; + } + + yyjson_val* key = yyjson_obj_iter_next(&handle->m_iterObjImm); + if (key) { + yyjson_val* val = yyjson_obj_iter_get_val(key); + + *out_key = yyjson_get_str(key); + if (out_key_len) { + *out_key_len = yyjson_get_len(key); + } + + auto pWrapper = CreateWrapper(); + pWrapper->m_pDocument = handle->m_pDocument; + pWrapper->m_pVal = val; + *out_value = pWrapper.release(); + + return true; + } + } + + handle->ResetObjectIterator(); + return false; +} + +bool JsonManager::ArrayForeachNext(JsonValue* handle, size_t* out_index, + JsonValue** out_value) +{ + if (!handle || !IsArray(handle)) { + return false; + } + + if (IsMutable(handle)) { + if (!handle->m_iterInitialized) { + if (!yyjson_mut_arr_iter_init(handle->m_pVal_mut, &handle->m_iterArr)) { + return false; + } + handle->m_iterInitialized = true; + } + + yyjson_mut_val* val = yyjson_mut_arr_iter_next(&handle->m_iterArr); + if (val) { + *out_index = handle->m_arrayIndex; + + auto pWrapper = CreateWrapper(); + pWrapper->m_pDocument_mut = handle->m_pDocument_mut; + pWrapper->m_pVal_mut = val; + *out_value = pWrapper.release(); + + handle->m_arrayIndex++; + return true; + } + } else { + if (!handle->m_iterInitialized) { + if (!yyjson_arr_iter_init(handle->m_pVal, &handle->m_iterArrImm)) { + return false; + } + handle->m_iterInitialized = true; + } + + yyjson_val* val = yyjson_arr_iter_next(&handle->m_iterArrImm); + if (val) { + *out_index = handle->m_arrayIndex; + + auto pWrapper = CreateWrapper(); + pWrapper->m_pDocument = handle->m_pDocument; + pWrapper->m_pVal = val; + *out_value = pWrapper.release(); + + handle->m_arrayIndex++; + return true; + } + } + + handle->ResetArrayIterator(); + return false; +} + +bool JsonManager::ObjectForeachKeyNext(JsonValue* handle, const char** out_key, + size_t* out_key_len) +{ + if (!handle || !IsObject(handle)) { + return false; + } + + if (IsMutable(handle)) { + if (!handle->m_iterInitialized) { + if (!yyjson_mut_obj_iter_init(handle->m_pVal_mut, &handle->m_iterObj)) { + return false; + } + handle->m_iterInitialized = true; + } + + yyjson_mut_val* key = yyjson_mut_obj_iter_next(&handle->m_iterObj); + if (key) { + *out_key = yyjson_mut_get_str(key); + if (out_key_len) { + *out_key_len = yyjson_mut_get_len(key); + } + return true; + } + } else { + if (!handle->m_iterInitialized) { + if (!yyjson_obj_iter_init(handle->m_pVal, &handle->m_iterObjImm)) { + return false; + } + handle->m_iterInitialized = true; + } + + yyjson_val* key = yyjson_obj_iter_next(&handle->m_iterObjImm); + if (key) { + *out_key = yyjson_get_str(key); + if (out_key_len) { + *out_key_len = yyjson_get_len(key); + } + return true; + } + } + + handle->ResetObjectIterator(); + return false; +} + +bool JsonManager::ArrayForeachIndexNext(JsonValue* handle, size_t* out_index) +{ + if (!handle || !IsArray(handle)) { + return false; + } + + if (IsMutable(handle)) { + if (!handle->m_iterInitialized) { + if (!yyjson_mut_arr_iter_init(handle->m_pVal_mut, &handle->m_iterArr)) { + return false; + } + handle->m_iterInitialized = true; + } + + yyjson_mut_val* val = yyjson_mut_arr_iter_next(&handle->m_iterArr); + if (val) { + *out_index = handle->m_arrayIndex; + handle->m_arrayIndex++; + return true; + } + } else { + if (!handle->m_iterInitialized) { + if (!yyjson_arr_iter_init(handle->m_pVal, &handle->m_iterArrImm)) { + return false; + } + handle->m_iterInitialized = true; + } + + yyjson_val* val = yyjson_arr_iter_next(&handle->m_iterArrImm); + if (val) { + *out_index = handle->m_arrayIndex; + handle->m_arrayIndex++; + return true; + } + } + + handle->ResetArrayIterator(); + return false; +} + +void JsonManager::Release(JsonValue* value) +{ + if (value) { + delete value; + } +} + +HandleType_t JsonManager::GetJsonHandleType() +{ + return g_JsonType; +} + +JsonValue* JsonManager::GetValueFromHandle(IPluginContext* pContext, Handle_t handle) +{ + HandleError err; + HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity()); + + JsonValue* pJSONValue; + if ((err = handlesys->ReadHandle(handle, g_JsonType, &sec, (void**)&pJSONValue)) != HandleError_None) + { + pContext->ReportError("Invalid JSON handle %x (error %d)", handle, err); + return nullptr; + } + + return pJSONValue; +} + +JsonArrIter* JsonManager::ArrIterInit(JsonValue* handle) +{ + return ArrIterWith(handle); +} + +JsonArrIter* JsonManager::ArrIterWith(JsonValue* handle) +{ + if (!handle || !IsArray(handle)) { + return nullptr; + } + + auto iter = new JsonArrIter(); + iter->m_isMutable = handle->IsMutable(); + iter->m_pDocument_mut = handle->m_pDocument_mut; + iter->m_pDocument = handle->m_pDocument; + iter->m_rootMut = nullptr; + iter->m_rootImm = nullptr; + + if (handle->IsMutable()) { + iter->m_rootMut = handle->m_pVal_mut; + if (!iter->m_rootMut || !yyjson_mut_arr_iter_init(iter->m_rootMut, &iter->m_iterMut)) { + delete iter; + return nullptr; + } + } else { + iter->m_rootImm = handle->m_pVal; + if (!iter->m_rootImm || !yyjson_arr_iter_init(iter->m_rootImm, &iter->m_iterImm)) { + delete iter; + return nullptr; + } + } + + iter->m_initialized = true; + return iter; +} + +bool JsonManager::ArrIterReset(JsonArrIter* iter) +{ + if (!iter || !iter->m_initialized) { + return false; + } + + bool success; + if (iter->m_isMutable) { + success = iter->m_rootMut && yyjson_mut_arr_iter_init(iter->m_rootMut, &iter->m_iterMut); + } else { + success = iter->m_rootImm && yyjson_arr_iter_init(iter->m_rootImm, &iter->m_iterImm); + } + + if (!success) { + iter->m_initialized = false; + return false; + } + + return true; +} + +JsonValue* JsonManager::ArrIterNext(JsonArrIter* iter) +{ + if (!iter || !iter->m_initialized) { + return nullptr; + } + + JsonValue* val; + + 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; + iter->m_rootMut = nullptr; + iter->m_rootImm = nullptr; + + if (handle->IsMutable()) { + iter->m_rootMut = handle->m_pVal_mut; + if (!iter->m_rootMut || !yyjson_mut_obj_iter_init(iter->m_rootMut, &iter->m_iterMut)) { + delete iter; + return nullptr; + } + } else { + iter->m_rootImm = handle->m_pVal; + if (!iter->m_rootImm || !yyjson_obj_iter_init(iter->m_rootImm, &iter->m_iterImm)) { + delete iter; + return nullptr; + } + } + + iter->m_initialized = true; + return iter; +} + +bool JsonManager::ObjIterReset(JsonObjIter* iter) +{ + if (!iter || !iter->m_initialized) { + return false; + } + + bool success; + if (iter->m_isMutable) { + success = iter->m_rootMut && yyjson_mut_obj_iter_init(iter->m_rootMut, &iter->m_iterMut); + } else { + success = iter->m_rootImm && yyjson_obj_iter_init(iter->m_rootImm, &iter->m_iterImm); + } + + if (!success) { + iter->m_initialized = false; + return false; + } + + iter->m_currentKey = nullptr; + return true; +} + +void* JsonManager::ObjIterNext(JsonObjIter* iter) +{ + if (!iter || !iter->m_initialized) { + 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); +} + +bool JsonManager::ObjIterGetKeyString(JsonObjIter* iter, void* key, const char** out_str, size_t* out_len) +{ + if (!iter || !key || !out_str) { + return false; + } + + if (iter->m_isMutable) { + *out_str = yyjson_mut_get_str(reinterpret_cast(key)); + if (out_len) { + *out_len = yyjson_mut_get_len(reinterpret_cast(key)); + } + } else { + *out_str = yyjson_get_str(reinterpret_cast(key)); + if (out_len) { + *out_len = yyjson_get_len(reinterpret_cast(key)); + } + } + + return *out_str != nullptr; +} + +void JsonManager::ReleaseArrIter(JsonArrIter* iter) +{ + if (iter) { + 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) { + SetErrorSafe(error, error_size, "Invalid input data"); + } + return nullptr; + } + + auto pJSONValue = CreateWrapper(); + pJSONValue->m_pDocument_mut = CreateDocument(); + + if (!pJSONValue->m_pDocument_mut) { + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Failed to create number document"); + } + return nullptr; + } + + yyjson_mut_val* val = yyjson_mut_int(pJSONValue->m_pDocument_mut->get(), 0); + if (!val) { + if (error && error_size > 0) { + SetErrorSafe(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) { + SetErrorSafe(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; + 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; + } + + if (handle->IsMutable()) { + if (std::holds_alternative(value)) { + return yyjson_mut_set_sint(handle->m_pVal_mut, std::get(value)); + } else { + return yyjson_mut_set_uint(handle->m_pVal_mut, std::get(value)); + } + } else { + if (std::holds_alternative(value)) { + return yyjson_set_sint(handle->m_pVal, std::get(value)); + } else { + return yyjson_set_uint(handle->m_pVal, std::get(value)); + } + } +} + +bool JsonManager::SetDouble(JsonValue* handle, double value) +{ + if (!handle) { + return false; + } + + if (handle->IsMutable()) { + return yyjson_mut_set_real(handle->m_pVal_mut, value); + } else { + return yyjson_set_real(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) { + SetErrorSafe(error, error_size, "Empty integer64 value"); + } + return false; + } + + if (!out_value) { + if (error && error_size > 0) { + SetErrorSafe(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) { + SetErrorSafe(error, error_size, "Integer64 value out of range: %s", value); + } else { + SetErrorSafe(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) { + SetErrorSafe(error, error_size, "Integer64 value out of range: %s", value); + } else { + SetErrorSafe(error, error_size, "Invalid integer64 value: %s", value); + } + } + return false; + } + + if (error && error_size > 0) { + SetErrorSafe(error, error_size, "Invalid integer64 value: %s", value); + } + return false; +} \ No newline at end of file diff --git a/src/JsonManager.h b/src/JsonManager.h new file mode 100755 index 0000000..c53b36f --- /dev/null +++ b/src/JsonManager.h @@ -0,0 +1,587 @@ +#ifndef _INCLUDE_JSONMANAGER_H_ +#define _INCLUDE_JSONMANAGER_H_ + +#include +#include +#include +#include +#include + +/** + * @brief Base class for intrusive reference counting + * + * Objects inheriting from this class can be managed by RefPtr. + * Reference count starts at 0 and is incremented when RefPtr takes ownership. + */ +class RefCounted { +private: + mutable size_t ref_count_ = 0; + +protected: + virtual ~RefCounted() = default; + RefCounted() = default; + + RefCounted(const RefCounted &) = delete; + RefCounted &operator=(const RefCounted &) = delete; + + RefCounted(RefCounted &&) noexcept : ref_count_(0) {} + RefCounted &operator=(RefCounted &&) noexcept { return *this; } + +public: + void add_ref() const noexcept { ++ref_count_; } + + void release() const noexcept { + assert(ref_count_ > 0 && "Reference count underflow"); + if (--ref_count_ == 0) { + delete this; + } + } + + size_t use_count() const noexcept { return ref_count_; } +}; + +/** + * @brief Smart pointer for intrusive reference counting + * + * Similar to std::shared_ptr but uses intrusive reference counting. + * Automatically manages object lifetime through add_ref() and release(). + * @warning This implementation is not thread-safe. It must only be used + * in single-threaded contexts or with external synchronization. + */ +template class RefPtr { +private: + T *ptr_; + +public: + RefPtr() noexcept : ptr_(nullptr) {} + + explicit RefPtr(T *p) noexcept : ptr_(p) { + if (ptr_) + ptr_->add_ref(); + } + + RefPtr(const RefPtr &other) noexcept : ptr_(other.ptr_) { + if (ptr_) + ptr_->add_ref(); + } + + RefPtr(RefPtr &&other) noexcept : ptr_(other.ptr_) { other.ptr_ = nullptr; } + + ~RefPtr() { + if (ptr_) + ptr_->release(); + } + + RefPtr &operator=(const RefPtr &other) noexcept { + if (ptr_ != other.ptr_) { + if (other.ptr_) + other.ptr_->add_ref(); + if (ptr_) + ptr_->release(); + ptr_ = other.ptr_; + } + return *this; + } + + RefPtr &operator=(RefPtr &&other) noexcept { + if (this != &other) { + if (ptr_) + ptr_->release(); + ptr_ = other.ptr_; + other.ptr_ = nullptr; + } + return *this; + } + + RefPtr &operator=(T *p) noexcept { + reset(p); + return *this; + } + + T *operator->() const noexcept { return ptr_; } + T &operator*() const noexcept { return *ptr_; } + T *get() const noexcept { return ptr_; } + + explicit operator bool() const noexcept { return ptr_ != nullptr; } + + size_t use_count() const noexcept { return ptr_ ? ptr_->use_count() : 0; } + + void reset(T *p = nullptr) noexcept { + if (ptr_ != p) { + if (p) + p->add_ref(); + if (ptr_) + ptr_->release(); + ptr_ = p; + } + } + + bool operator==(const RefPtr &other) const noexcept { return ptr_ == other.ptr_; } + bool operator!=(const RefPtr &other) const noexcept { return ptr_ != other.ptr_; } + bool operator==(std::nullptr_t) const noexcept { return ptr_ == nullptr; } + bool operator!=(std::nullptr_t) const noexcept { return ptr_ != nullptr; } +}; + +/** + * @brief Factory function to create RefPtr from new object + */ +template RefPtr make_ref(Args &&...args) { + return RefPtr(new T(std::forward(args)...)); +} + +/** + * @brief Wrapper for yyjson_mut_doc with intrusive reference counting + */ +class RefCountedMutDoc : public RefCounted { +private: + yyjson_mut_doc *doc_; + +public: + explicit RefCountedMutDoc(yyjson_mut_doc *doc) noexcept : doc_(doc) {} + + RefCountedMutDoc(const RefCountedMutDoc &) = delete; + RefCountedMutDoc &operator=(const RefCountedMutDoc &) = delete; + + ~RefCountedMutDoc() noexcept override { + if (doc_) { + yyjson_mut_doc_free(doc_); + } + } + + yyjson_mut_doc *get() const noexcept { return doc_; } +}; + +/** + * @brief Wrapper for yyjson_doc with intrusive reference counting + */ +class RefCountedImmutableDoc : public RefCounted { +private: + yyjson_doc *doc_; + +public: + explicit RefCountedImmutableDoc(yyjson_doc *doc) noexcept : doc_(doc) {} + + RefCountedImmutableDoc(const RefCountedImmutableDoc &) = delete; + RefCountedImmutableDoc &operator=(const RefCountedImmutableDoc &) = delete; + + ~RefCountedImmutableDoc() noexcept override { + if (doc_) { + yyjson_doc_free(doc_); + } + } + + yyjson_doc *get() const noexcept { return doc_; } +}; + +/** + * @brief JSON value wrapper + * + * Wraps json mutable/immutable documents and values. + * Used as the primary data type for JSON operations. + */ +class JsonValue { +public: + JsonValue() = default; + ~JsonValue() = default; + + 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; + } + + size_t GetDocumentRefCount() const { + if (m_pDocument_mut) { + return m_pDocument_mut.use_count(); + } else if (m_pDocument) { + return m_pDocument.use_count(); + } + return 0; + } + + // Mutable document + RefPtr m_pDocument_mut; + yyjson_mut_val* m_pVal_mut{ nullptr }; + + // Immutable document + RefPtr 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; + } + + RefPtr m_pDocument_mut; + RefPtr m_pDocument; + + yyjson_mut_arr_iter m_iterMut; + yyjson_arr_iter m_iterImm; + yyjson_mut_val* m_rootMut{ nullptr }; + yyjson_val* m_rootImm{ nullptr }; + + Handle_t m_handle{ BAD_HANDLE }; + bool m_isMutable{ false }; + 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; + } + + RefPtr m_pDocument_mut; + RefPtr m_pDocument; + + yyjson_mut_obj_iter m_iterMut; + yyjson_obj_iter m_iterImm; + yyjson_mut_val* m_rootMut{ nullptr }; + yyjson_val* m_rootImm{ nullptr }; + + void* m_currentKey{ nullptr }; + + 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 char* WriteToStringPtr(JsonValue* handle, yyjson_write_flag write_flg, size_t* out_size) override; + virtual JsonValue* ApplyJsonPatch(JsonValue* target, JsonValue* patch, bool result_mutable, + char* error, size_t error_size) override; + virtual bool JsonPatchInPlace(JsonValue* target, JsonValue* patch, + char* error, size_t error_size) override; + virtual JsonValue* ApplyMergePatch(JsonValue* target, JsonValue* patch, bool result_mutable, + char* error, size_t error_size) override; + virtual bool MergePatchInPlace(JsonValue* target, JsonValue* patch, + char* error, size_t error_size) override; + virtual bool WriteToFile(JsonValue* handle, const char* path, yyjson_write_flag write_flg, + char* error, size_t error_size) override; + virtual bool Equals(JsonValue* handle1, JsonValue* handle2) override; + 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; + virtual size_t GetRefCount(JsonValue* handle) override; + virtual size_t GetValCount(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 ObjectGetDouble(JsonValue* handle, const char* key, double* out_value) override; + virtual bool ObjectGetInt(JsonValue* handle, const char* key, int* out_value) override; + virtual bool ObjectGetInt64(JsonValue* handle, const char* key, std::variant* out_value) override; + virtual bool ObjectGetString(JsonValue* handle, const char* key, const char** out_str, size_t* out_len) override; + 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 ObjectSetDouble(JsonValue* handle, const char* key, double value) override; + virtual bool ObjectSetInt(JsonValue* handle, const char* key, int value) override; + virtual bool ObjectSetInt64(JsonValue* handle, const char* key, std::variant value) override; + virtual bool ObjectSetNull(JsonValue* handle, const char* key) override; + 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; + virtual bool ObjectRotate(JsonValue* handle, size_t idx) 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* ArrayInitWithDouble(const double* values, size_t count) override; + virtual JsonValue* ArrayParseString(const char* str, yyjson_read_flag read_flg, + char* error, size_t error_size) override; + virtual JsonValue* ArrayParseFile(const char* path, yyjson_read_flag read_flg, + 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 ArrayGetDouble(JsonValue* handle, size_t index, double* out_value) override; + virtual bool ArrayGetInt(JsonValue* handle, size_t index, int* out_value) override; + virtual bool ArrayGetInt64(JsonValue* handle, size_t index, std::variant* out_value) override; + virtual bool ArrayGetString(JsonValue* handle, size_t index, const char** out_str, size_t* out_len) override; + virtual bool ArrayIsNull(JsonValue* handle, size_t index) override; + virtual bool ArrayReplace(JsonValue* handle, size_t index, JsonValue* value) override; + virtual bool ArrayReplaceBool(JsonValue* handle, size_t index, bool value) override; + virtual bool ArrayReplaceDouble(JsonValue* handle, size_t index, double value) override; + virtual bool ArrayReplaceInt(JsonValue* handle, size_t index, int value) override; + virtual bool ArrayReplaceInt64(JsonValue* handle, size_t index, std::variant value) override; + virtual bool ArrayReplaceNull(JsonValue* handle, size_t index) override; + virtual bool ArrayReplaceString(JsonValue* handle, size_t index, const char* value) override; + virtual bool ArrayAppend(JsonValue* handle, JsonValue* value) override; + virtual bool ArrayAppendBool(JsonValue* handle, bool value) override; + virtual bool ArrayAppendDouble(JsonValue* handle, double value) override; + virtual bool ArrayAppendInt(JsonValue* handle, int value) override; + virtual bool ArrayAppendInt64(JsonValue* handle, std::variant value) override; + virtual bool ArrayAppendNull(JsonValue* handle) override; + 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 ArrayInsertDouble(JsonValue* handle, size_t index, double value) override; + virtual bool ArrayInsertString(JsonValue* handle, size_t index, const char* value) override; + virtual bool ArrayInsertNull(JsonValue* handle, size_t index) override; + virtual bool ArrayPrepend(JsonValue* handle, JsonValue* value) override; + virtual bool ArrayPrependBool(JsonValue* handle, bool value) override; + virtual bool ArrayPrependInt(JsonValue* handle, int value) override; + virtual bool ArrayPrependInt64(JsonValue* handle, std::variant value) override; + virtual bool ArrayPrependDouble(JsonValue* handle, double value) override; + virtual bool ArrayPrependString(JsonValue* handle, const char* value) override; + virtual bool ArrayPrependNull(JsonValue* handle) override; + virtual bool ArrayRemove(JsonValue* handle, size_t index) override; + virtual bool ArrayRemoveFirst(JsonValue* handle) override; + virtual bool ArrayRemoveLast(JsonValue* handle) override; + virtual bool ArrayRemoveRange(JsonValue* handle, size_t start_index, size_t count) override; + virtual bool ArrayClear(JsonValue* handle) override; + virtual int ArrayIndexOfBool(JsonValue* handle, bool search_value) override; + virtual int ArrayIndexOfString(JsonValue* handle, const char* search_value) override; + virtual int ArrayIndexOfInt(JsonValue* handle, int search_value) override; + virtual int ArrayIndexOfInt64(JsonValue* handle, std::variant search_value) override; + virtual int ArrayIndexOfDouble(JsonValue* handle, double search_value) override; + virtual bool ArraySort(JsonValue* handle, JSON_SORT_ORDER sort_mode) override; + virtual bool ArrayRotate(JsonValue* handle, size_t idx) override; + + // ========== Value Operations ========== + virtual JsonValue* Pack(const char* format, IPackParamProvider* param_provider, char* error, size_t error_size) override; + virtual JsonValue* CreateBool(bool value) override; + virtual JsonValue* CreateDouble(double value) override; + virtual JsonValue* CreateInt(int value) override; + virtual JsonValue* CreateInt64(std::variant value) override; + virtual JsonValue* CreateNull() override; + virtual JsonValue* CreateString(const char* value) override; + virtual bool GetBool(JsonValue* handle, bool* out_value) override; + virtual bool GetDouble(JsonValue* handle, double* out_value) override; + virtual bool GetInt(JsonValue* handle, int* out_value) override; + virtual bool GetInt64(JsonValue* handle, std::variant* out_value) override; + virtual bool GetString(JsonValue* handle, const char** out_str, size_t* out_len) override; + + // ========== 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 PtrGetDouble(JsonValue* handle, const char* path, double* out_value, char* error, size_t error_size) override; + virtual bool PtrGetInt(JsonValue* handle, const char* path, int* out_value, char* error, size_t error_size) override; + virtual bool PtrGetInt64(JsonValue* handle, const char* path, std::variant* out_value, char* error, size_t error_size) override; + virtual bool PtrGetString(JsonValue* handle, const char* path, const char** out_str, size_t* out_len, char* error, size_t error_size) override; + 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 PtrSetDouble(JsonValue* handle, const char* path, double value, char* error, size_t error_size) override; + virtual bool PtrSetInt(JsonValue* handle, const char* path, int value, char* error, size_t error_size) override; + virtual bool PtrSetInt64(JsonValue* handle, const char* path, std::variant value, char* error, size_t error_size) override; + virtual bool PtrSetString(JsonValue* handle, const char* path, const char* value, char* error, size_t error_size) override; + virtual bool PtrSetNull(JsonValue* handle, const char* path, char* error, size_t error_size) override; + virtual bool PtrAdd(JsonValue* handle, const char* path, JsonValue* value, char* error, size_t error_size) override; + virtual bool PtrAddBool(JsonValue* handle, const char* path, bool value, char* error, size_t error_size) override; + virtual bool PtrAddDouble(JsonValue* handle, const char* path, double value, char* error, size_t error_size) override; + virtual bool PtrAddInt(JsonValue* handle, const char* path, int value, char* error, size_t error_size) override; + virtual bool PtrAddInt64(JsonValue* handle, const char* path, std::variant value, char* error, size_t error_size) override; + virtual bool PtrAddString(JsonValue* handle, const char* path, const char* value, char* error, size_t error_size) override; + 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 PtrTryGetDouble(JsonValue* handle, const char* path, double* out_value) override; + virtual bool PtrTryGetInt(JsonValue* handle, const char* path, int* out_value) override; + virtual bool PtrTryGetInt64(JsonValue* handle, const char* path, std::variant* out_value) override; + virtual bool PtrTryGetString(JsonValue* handle, const char* path, const char** out_str, size_t* out_len) override; + + // ========== 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 bool ArrIterReset(JsonArrIter* iter) 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 bool ObjIterReset(JsonObjIter* iter) override; + virtual void* ObjIterNext(JsonObjIter* iter) override; + virtual bool ObjIterHasNext(JsonObjIter* iter) override; + virtual JsonValue* ObjIterGetVal(JsonObjIter* iter, void* key) override; + virtual JsonValue* ObjIterGet(JsonObjIter* iter, const char* key) override; + virtual size_t ObjIterGetIndex(JsonObjIter* iter) override; + virtual void* ObjIterRemove(JsonObjIter* iter) override; + virtual bool ObjIterGetKeyString(JsonObjIter* iter, void* key, const char** out_str, size_t* out_len = nullptr) override; + + // ========== Iterator Release Operations ========== + virtual void ReleaseArrIter(JsonArrIter* iter) override; + 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 GetJsonHandleType() override; + + // ========== Handle Operations ========== + virtual JsonValue* GetValueFromHandle(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 SetDouble(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 RefPtr WrapDocument(yyjson_mut_doc* doc); + static RefPtr CopyDocument(yyjson_doc* doc); + static RefPtr CreateDocument(); + static RefPtr WrapImmutableDocument(yyjson_doc* doc); + static RefPtr CloneValueToMutable(JsonValue* value); + + // Pack helper methods + static const char* SkipSeparators(const char* ptr); + 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_JSONMANAGER_H_ \ No newline at end of file diff --git a/src/JsonNatives.cpp b/src/JsonNatives.cpp new file mode 100755 index 0000000..63f432c --- /dev/null +++ b/src/JsonNatives.cpp @@ -0,0 +1,3546 @@ +#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: Convert int64 variant to string + * @param value The variant containing int64_t or uint64_t + * @param buffer Output buffer + * @param buffer_size Size of the buffer + * @return true on success, false on error + */ +static inline bool Int64VariantToString(const std::variant& value, char* buffer, size_t buffer_size) +{ + if (!buffer || buffer_size < 2) { + return false; + } + + std::to_chars_result result; + + if (std::holds_alternative(value)) { + result = std::to_chars(buffer, buffer + buffer_size - 1, std::get(value)); + } else { + result = std::to_chars(buffer, buffer + buffer_size - 1, std::get(value)); + } + + if (result.ec == std::errc()) { + *result.ptr = '\0'; + return true; + } + + return false; +} + +/** + * Helper function: Create a SourceMod handle for JsonValue and return it directly + * Used by functions that return Handle_t + * + * @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_ERROR_BUFFER_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]; + uint32_t read_flg = static_cast(params[4]); + + char error[JSON_ERROR_BUFFER_SIZE]; + JsonValue* pJSONValue = g_pJsonManager->ParseJSON(str, is_file, is_mutable_doc, read_flg, error, sizeof(error)); + + 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->GetValueFromHandle(pContext, params[1]); + JsonValue* handle2 = g_pJsonManager->GetValueFromHandle(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->GetValueFromHandle(pContext, params[1]); + JsonValue* sourceValue = g_pJsonManager->GetValueFromHandle(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->GetValueFromHandle(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); + uint32_t read_flg = static_cast(params[2]); + + char error[JSON_ERROR_BUFFER_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); + uint32_t read_flg = static_cast(params[2]); + + char error[JSON_ERROR_BUFFER_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); + uint32_t read_flg = static_cast(params[2]); + + char error[JSON_ERROR_BUFFER_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); + uint32_t read_flg = static_cast(params[2]); + + char error[JSON_ERROR_BUFFER_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->GetValueFromHandle(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->GetValueFromHandle(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->GetValueFromHandle(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->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + char* searchStr; + pContext->LocalToString(params[2], &searchStr); + + std::variant variant_value; + char error[JSON_ERROR_BUFFER_SIZE]; + + if (!g_pJsonManager->ParseInt64Variant(searchStr, &variant_value, error, sizeof(error))) { + return pContext->ThrowNativeError("%s", error); + } + + return g_pJsonManager->ArrayIndexOfInt64(handle, variant_value); +} + +static cell_t json_arr_index_of_float(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + return g_pJsonManager->ArrayIndexOfDouble(handle, sp_ctof(params[2])); +} + +static cell_t json_get_type(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetValueFromHandle(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->GetValueFromHandle(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->GetValueFromHandle(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->GetValueFromHandle(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->GetValueFromHandle(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->GetValueFromHandle(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->GetValueFromHandle(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->GetValueFromHandle(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->GetValueFromHandle(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->GetValueFromHandle(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->GetValueFromHandle(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->GetValueFromHandle(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->GetValueFromHandle(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->GetValueFromHandle(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->GetValueFromHandle(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->GetValueFromHandle(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->GetValueFromHandle(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->CreateDouble(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->GetValueFromHandle(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->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + double value; + if (!g_pJsonManager->GetDouble(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->GetValueFromHandle(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->GetValueFromHandle(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 (!Int64VariantToString(value, result, sizeof(result))) { + return pContext->ThrowNativeError("Failed to convert integer64 to string"); + } + 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->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + const char* str; + size_t len; + + 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->GetValueFromHandle(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->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + uint32_t 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->GetValueFromHandle(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_get_ref_count(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + return g_pJsonManager->GetRefCount(handle); +} + +static cell_t json_get_val_count(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + return g_pJsonManager->GetValCount(handle); +} + +static cell_t json_create_null(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* pJSONValue = g_pJsonManager->CreateNull(); + 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]; + + // std::vector is specialized and doesn't work with .data() so we use a unique_ptr + auto values = std::make_unique(array_size); + + for (cell_t i = 0; i < array_size; i++) { + values[i] = (addr[i] != 0); + } + + 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->ArrayInitWithDouble(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->GetValueFromHandle(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->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); + + 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->GetValueFromHandle(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->GetValueFromHandle(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->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); + + 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->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); + + double value; + if (!g_pJsonManager->ArrayGetDouble(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->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); + + 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->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); + + 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 (!Int64VariantToString(value, result, sizeof(result))) { + return pContext->ThrowNativeError("Failed to convert integer64 to string"); + } + 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->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); + + const char* str; + size_t len; + if (!g_pJsonManager->ArrayGetString(handle, index, &str, &len)) { + return pContext->ThrowNativeError("Failed to get string at index %d", index); + } + + 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->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); + + return g_pJsonManager->ArrayIsNull(handle, index); +} + +static cell_t json_arr_replace_val(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle1 = g_pJsonManager->GetValueFromHandle(pContext, params[1]); + JsonValue* handle2 = g_pJsonManager->GetValueFromHandle(pContext, params[3]); + + if (!handle1 || !handle2) return 0; + + if (!handle1->IsMutable()) { + return pContext->ThrowNativeError("Cannot replace value in an immutable JSON array"); + } + + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); + + return g_pJsonManager->ArrayReplace(handle1, index, handle2); +} + +static cell_t json_arr_replace_bool(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot replace value in an immutable JSON array"); + } + + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); + + return g_pJsonManager->ArrayReplaceBool(handle, index, params[3]); +} + +static cell_t json_arr_replace_float(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot replace value in an immutable JSON array"); + } + + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); + + return g_pJsonManager->ArrayReplaceDouble(handle, index, sp_ctof(params[3])); +} + +static cell_t json_arr_replace_integer(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot replace value in an immutable JSON array"); + } + + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); + + return g_pJsonManager->ArrayReplaceInt(handle, index, params[3]); +} + +static cell_t json_arr_replace_integer64(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot 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); + } + + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); + + return g_pJsonManager->ArrayReplaceInt64(handle, index, variant_value); +} + +static cell_t json_arr_replace_null(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot replace value in an immutable JSON array"); + } + + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); + + return g_pJsonManager->ArrayReplaceNull(handle, index); +} + +static cell_t json_arr_replace_str(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot replace value in an immutable JSON array"); + } + + char* val; + pContext->LocalToString(params[3], &val); + + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); + + return g_pJsonManager->ArrayReplaceString(handle, index, val); +} + +static cell_t json_arr_append_val(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle1 = g_pJsonManager->GetValueFromHandle(pContext, params[1]); + JsonValue* handle2 = g_pJsonManager->GetValueFromHandle(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->GetValueFromHandle(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->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot append value to an immutable JSON array"); + } + + return g_pJsonManager->ArrayAppendDouble(handle, sp_ctof(params[2])); +} + +static cell_t json_arr_append_int(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetValueFromHandle(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->GetValueFromHandle(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->GetValueFromHandle(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->GetValueFromHandle(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); +} + +static cell_t json_arr_insert(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot insert value into an immutable JSON array"); + } + + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); + + size_t arr_size = g_pJsonManager->ArrayGetSize(handle); + if (index > arr_size) { + return pContext->ThrowNativeError("Index is out of bounds (got %d, max %zu)", index, arr_size); + } + + JsonValue* value = g_pJsonManager->GetValueFromHandle(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->GetValueFromHandle(pContext, params[1]); + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot insert value into an immutable JSON array"); + } + + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); + + size_t arr_size = g_pJsonManager->ArrayGetSize(handle); + if (index > arr_size) { + return pContext->ThrowNativeError("Index is out of bounds (got %d, max %zu)", index, arr_size); + } + + bool value = params[3] != 0; + + return g_pJsonManager->ArrayInsertBool(handle, index, value); +} + +static cell_t json_arr_insert_int(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot insert value into an immutable JSON array"); + } + + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); + + size_t arr_size = g_pJsonManager->ArrayGetSize(handle); + if (index > arr_size) { + return pContext->ThrowNativeError("Index is out of bounds (got %d, max %zu)", index, arr_size); + } + + int value = params[3]; + + return g_pJsonManager->ArrayInsertInt(handle, index, value); +} + +static cell_t json_arr_insert_int64(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot insert value into an immutable JSON array"); + } + + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); + + size_t arr_size = g_pJsonManager->ArrayGetSize(handle); + if (index > arr_size) { + return pContext->ThrowNativeError("Index is out of bounds (got %d, max %zu)", index, arr_size); + } + + char* value; + pContext->LocalToString(params[3], &value); + + std::variant variant_value; + char error[JSON_ERROR_BUFFER_SIZE]; + + if (!g_pJsonManager->ParseInt64Variant(value, &variant_value, error, sizeof(error))) { + return pContext->ThrowNativeError("%s", error); + } + + 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->GetValueFromHandle(pContext, params[1]); + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot insert value into an immutable JSON array"); + } + + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); + + size_t arr_size = g_pJsonManager->ArrayGetSize(handle); + if (index > arr_size) { + return pContext->ThrowNativeError("Index is out of bounds (got %d, max %zu)", index, arr_size); + } + + return g_pJsonManager->ArrayInsertDouble(handle, index, sp_ctof(params[3])); +} + +static cell_t json_arr_insert_str(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot insert value into an immutable JSON array"); + } + + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); + + size_t arr_size = g_pJsonManager->ArrayGetSize(handle); + if (index > arr_size) { + return pContext->ThrowNativeError("Index is out of bounds (got %d, max %zu)", index, arr_size); + } + + char* str; + pContext->LocalToString(params[3], &str); + + return g_pJsonManager->ArrayInsertString(handle, index, str); +} + +static cell_t json_arr_insert_null(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot insert value into an immutable JSON array"); + } + + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); + + size_t arr_size = g_pJsonManager->ArrayGetSize(handle); + if (index > arr_size) { + return pContext->ThrowNativeError("Index is out of bounds (got %d, max %zu)", index, arr_size); + } + + return g_pJsonManager->ArrayInsertNull(handle, index); +} + +static cell_t json_arr_prepend(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot prepend value to an immutable JSON array"); + } + + JsonValue* value = g_pJsonManager->GetValueFromHandle(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->GetValueFromHandle(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->GetValueFromHandle(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->GetValueFromHandle(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->GetValueFromHandle(pContext, params[1]); + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot prepend value to an immutable JSON array"); + } + + return g_pJsonManager->ArrayPrependDouble(handle, sp_ctof(params[2])); +} + +static cell_t json_arr_prepend_str(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetValueFromHandle(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->GetValueFromHandle(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->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot remove value from an immutable JSON array"); + } + + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); + + return g_pJsonManager->ArrayRemove(handle, index); +} + +static cell_t json_arr_remove_first(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot 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->GetValueFromHandle(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->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot remove value from an immutable JSON array"); + } + + cell_t start_index_param = params[2]; + cell_t count_param = params[3]; + if (start_index_param < 0) { + return pContext->ThrowNativeError("Start index must be >= 0 (got %d)", start_index_param); + } + if (count_param < 0) { + return pContext->ThrowNativeError("Count must be >= 0 (got %d)", count_param); + } + + size_t start_index = static_cast(start_index_param); + size_t count = static_cast(count_param); + return g_pJsonManager->ArrayRemoveRange(handle, start_index, count); +} + +static cell_t json_arr_clear(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetValueFromHandle(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->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + size_t buffer_size = static_cast(params[3]); + uint32_t write_flg = static_cast(params[4]); + + size_t json_size; + char* json_str = g_pJsonManager->WriteToStringPtr(handle, write_flg, &json_size); + + if (!json_str) { + return pContext->ThrowNativeError("Failed to serialize JSON"); + } + + if (json_size > buffer_size) { + free(json_str); + return pContext->ThrowNativeError("Buffer too small (need %d, have %d)", json_size, buffer_size); + } + + pContext->StringToLocalUTF8(params[2], buffer_size, json_str, nullptr); + free(json_str); + + return static_cast(json_size); +} + +static cell_t json_doc_write_to_file(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + char* path; + pContext->LocalToString(params[2], &path); + uint32_t write_flg = static_cast(params[3]); + + char error[JSON_ERROR_BUFFER_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->GetValueFromHandle(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->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); + const char* key; + + 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->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + cell_t index_param = params[2]; + if (index_param < 0) { + return pContext->ThrowNativeError("Index must be >= 0 (got %d)", index_param); + } + size_t index = static_cast(index_param); + + 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->GetValueFromHandle(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->GetValueFromHandle(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->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + char* key; + pContext->LocalToString(params[2], &key); + + double value; + if (!g_pJsonManager->ObjectGetDouble(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->GetValueFromHandle(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->GetValueFromHandle(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 (!Int64VariantToString(value, result, sizeof(result))) { + return pContext->ThrowNativeError("Failed to convert integer64 to string"); + } + 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->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + char* key; + pContext->LocalToString(params[2], &key); + + const char* str; + size_t len; + if (!g_pJsonManager->ObjectGetString(handle, key, &str, &len)) { + return pContext->ThrowNativeError("Failed to get string for key '%s'", key); + } + + 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->GetValueFromHandle(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->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + char* key; + pContext->LocalToString(params[2], &key); + + bool is_null; + 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->GetValueFromHandle(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->GetValueFromHandle(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->GetValueFromHandle(pContext, params[1]); + JsonValue* handle2 = g_pJsonManager->GetValueFromHandle(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->GetValueFromHandle(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->GetValueFromHandle(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->ObjectSetDouble(handle, key, sp_ctof(params[3])); +} + +static cell_t json_obj_set_int(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot 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->GetValueFromHandle(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->GetValueFromHandle(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->GetValueFromHandle(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->GetValueFromHandle(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->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + char* path; + pContext->LocalToString(params[2], &path); + + char error[JSON_ERROR_BUFFER_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->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + char* path; + pContext->LocalToString(params[2], &path); + + bool value; + char error[JSON_ERROR_BUFFER_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->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + char* path; + pContext->LocalToString(params[2], &path); + + double value; + char error[JSON_ERROR_BUFFER_SIZE]; + if (!g_pJsonManager->PtrGetDouble(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->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + char* path; + pContext->LocalToString(params[2], &path); + + int value; + char error[JSON_ERROR_BUFFER_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->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + char* path; + pContext->LocalToString(params[2], &path); + + std::variant value; + char error[JSON_ERROR_BUFFER_SIZE]; + if (!g_pJsonManager->PtrGetInt64(handle, path, &value, error, sizeof(error))) { + return pContext->ThrowNativeError("%s", error); + } + + char result[JSON_INT64_BUFFER_SIZE]; + if (!Int64VariantToString(value, result, sizeof(result))) { + return pContext->ThrowNativeError("Failed to convert integer64 to string"); + } + 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->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + char* path; + pContext->LocalToString(params[2], &path); + + const char* str; + size_t len; + char error[JSON_ERROR_BUFFER_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->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + char* path; + pContext->LocalToString(params[2], &path); + + bool is_null; + char error[JSON_ERROR_BUFFER_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->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + char* path; + pContext->LocalToString(params[2], &path); + + size_t len; + char error[JSON_ERROR_BUFFER_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->GetValueFromHandle(pContext, params[1]); + JsonValue* handle2 = g_pJsonManager->GetValueFromHandle(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_ERROR_BUFFER_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->GetValueFromHandle(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_ERROR_BUFFER_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->GetValueFromHandle(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_ERROR_BUFFER_SIZE]; + if (!g_pJsonManager->PtrSetDouble(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->GetValueFromHandle(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_ERROR_BUFFER_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->GetValueFromHandle(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_ERROR_BUFFER_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->GetValueFromHandle(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_ERROR_BUFFER_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->GetValueFromHandle(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_ERROR_BUFFER_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->GetValueFromHandle(pContext, params[1]); + JsonValue* handle2 = g_pJsonManager->GetValueFromHandle(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_ERROR_BUFFER_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->GetValueFromHandle(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_ERROR_BUFFER_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->GetValueFromHandle(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_ERROR_BUFFER_SIZE]; + if (!g_pJsonManager->PtrAddDouble(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->GetValueFromHandle(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_ERROR_BUFFER_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->GetValueFromHandle(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_ERROR_BUFFER_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->GetValueFromHandle(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_ERROR_BUFFER_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->GetValueFromHandle(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_ERROR_BUFFER_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->GetValueFromHandle(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_ERROR_BUFFER_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->GetValueFromHandle(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->GetValueFromHandle(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->GetValueFromHandle(pContext, params[1]); + if (!handle) return 0; + + char* path; + pContext->LocalToString(params[2], &path); + + double value; + if (!g_pJsonManager->PtrTryGetDouble(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->GetValueFromHandle(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->GetValueFromHandle(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 (!Int64VariantToString(value, result, sizeof(result))) { + return 0; + } + 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->GetValueFromHandle(pContext, params[1]); + if (!handle) return 0; + + char* path; + pContext->LocalToString(params[2], &path); + + const char* str; + size_t len; + + 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->GetValueFromHandle(pContext, params[1]); + if (!handle) return 0; + + const char* key; + JsonValue* pJSONValue; + + 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->GetValueFromHandle(pContext, params[1]); + if (!handle) return 0; + + size_t index; + JsonValue* pJSONValue; + + 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->GetValueFromHandle(pContext, params[1]); + if (!handle) return 0; + + const char* key; + + 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->GetValueFromHandle(pContext, params[1]); + if (!handle) return 0; + + size_t index; + + 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->GetValueFromHandle(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_arr_rotate(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot rotate an immutable JSON array"); + } + + size_t idx = static_cast(params[2]); + return g_pJsonManager->ArrayRotate(handle, idx); +} + +static cell_t json_obj_sort(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetValueFromHandle(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_obj_rotate(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetValueFromHandle(pContext, params[1]); + + if (!handle) return 0; + + if (!handle->IsMutable()) { + return pContext->ThrowNativeError("Cannot rotate an immutable JSON object"); + } + + size_t idx = static_cast(params[2]); + return g_pJsonManager->ObjectRotate(handle, idx); +} + +static cell_t json_doc_to_mutable(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetValueFromHandle(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->GetValueFromHandle(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_apply_json_patch(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* target = g_pJsonManager->GetValueFromHandle(pContext, params[1]); + JsonValue* patch = g_pJsonManager->GetValueFromHandle(pContext, params[2]); + + if (!target || !patch) return 0; + + bool resultMutable = params[3] != 0; + char error[JSON_ERROR_BUFFER_SIZE] = {0}; + + JsonValue* result = g_pJsonManager->ApplyJsonPatch(target, patch, resultMutable, error, sizeof(error)); + if (!result) { + if (error[0] != '\0') { + return pContext->ThrowNativeError("%s", error); + } + return pContext->ThrowNativeError("Failed to apply JSON Patch"); + } + + return CreateAndReturnHandle(pContext, result, "JSON patch result"); +} + +static cell_t json_json_patch_in_place(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* target = g_pJsonManager->GetValueFromHandle(pContext, params[1]); + JsonValue* patch = g_pJsonManager->GetValueFromHandle(pContext, params[2]); + + if (!target || !patch) return 0; + + char error[JSON_ERROR_BUFFER_SIZE] = {0}; + if (!g_pJsonManager->JsonPatchInPlace(target, patch, error, sizeof(error))) { + if (error[0] != '\0') { + return pContext->ThrowNativeError("%s", error); + } + return pContext->ThrowNativeError("Failed to apply JSON Patch in place"); + } + + return 1; +} + +static cell_t json_apply_merge_patch(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* target = g_pJsonManager->GetValueFromHandle(pContext, params[1]); + JsonValue* patch = g_pJsonManager->GetValueFromHandle(pContext, params[2]); + + if (!target || !patch) return 0; + + bool resultMutable = params[3] != 0; + char error[JSON_ERROR_BUFFER_SIZE] = {0}; + + JsonValue* result = g_pJsonManager->ApplyMergePatch(target, patch, resultMutable, error, sizeof(error)); + if (!result) { + if (error[0] != '\0') { + return pContext->ThrowNativeError("%s", error); + } + return pContext->ThrowNativeError("Failed to apply JSON Merge Patch"); + } + + return CreateAndReturnHandle(pContext, result, "JSON merge patch result"); +} + +static cell_t json_merge_patch_in_place(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* target = g_pJsonManager->GetValueFromHandle(pContext, params[1]); + JsonValue* patch = g_pJsonManager->GetValueFromHandle(pContext, params[2]); + + if (!target || !patch) return 0; + + char error[JSON_ERROR_BUFFER_SIZE] = {0}; + if (!g_pJsonManager->MergePatchInPlace(target, patch, error, sizeof(error))) { + if (error[0] != '\0') { + return pContext->ThrowNativeError("%s", error); + } + return pContext->ThrowNativeError("Failed to apply JSON Merge Patch in place"); + } + + return 1; +} + +static cell_t json_arr_iter_init(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetValueFromHandle(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_arr_iter_reset(IPluginContext* pContext, const cell_t* params) +{ + JsonArrIter* iter = g_pJsonManager->GetArrIterFromHandle(pContext, params[1]); + if (!iter) return 0; + + return g_pJsonManager->ArrIterReset(iter); +} + +static cell_t json_obj_iter_init(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetValueFromHandle(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 = nullptr; + if (!g_pJsonManager->ObjIterGetKeyString(iter, key, &key_str)) { + return 0; + } + + pContext->StringToLocalUTF8(params[2], params[3], key_str ? 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 = 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 pContext->ThrowNativeError("Failed to get value from iterator"); + } + + 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_obj_iter_reset(IPluginContext* pContext, const cell_t* params) +{ + JsonObjIter* iter = g_pJsonManager->GetObjIterFromHandle(pContext, params[1]); + if (!iter) return 0; + + return g_pJsonManager->ObjIterReset(iter); +} + +static cell_t json_read_number(IPluginContext* pContext, const cell_t* params) +{ + char* dat; + pContext->LocalToString(params[1], &dat); + uint32_t read_flg = static_cast(params[2]); + + char error[JSON_ERROR_BUFFER_SIZE]; + size_t consumed; + 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->GetValueFromHandle(pContext, params[1]); + if (!handle) return 0; + + cell_t buffer_size_param = params[3]; + if (buffer_size_param <= 0) { + return pContext->ThrowNativeError("Buffer size must be > 0 (got %d)", buffer_size_param); + } + + size_t buffer_size = static_cast(buffer_size_param); + char* temp_buffer = (char*)malloc(buffer_size); + if (!temp_buffer) { + return pContext->ThrowNativeError("Failed to allocate buffer"); + } + + 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->GetValueFromHandle(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->GetValueFromHandle(pContext, params[1]); + if (!handle) return 0; + + int prec = params[2]; + if (prec < 1 || prec > 7) { + return pContext->ThrowNativeError("Precision out of range (1-7)"); + } + + if (!g_pJsonManager->SetFpToFixed(handle, prec)) { + return pContext->ThrowNativeError("Failed to set floating-point format to fixed (value is not a floating-point number or precision out of range 1-7)"); + } + + return 1; +} + +static cell_t json_set_bool(IPluginContext* pContext, const cell_t* params) +{ + JsonValue* handle = g_pJsonManager->GetValueFromHandle(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->GetValueFromHandle(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->GetValueFromHandle(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->GetValueFromHandle(pContext, params[1]); + if (!handle) return 0; + + if (!g_pJsonManager->SetDouble(handle, sp_ctof(params[2]))) { + 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->GetValueFromHandle(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->GetValueFromHandle(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}, + {"JSONObject.Rotate", json_obj_rotate}, + + // 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.IndexOfFloat", json_arr_index_of_float}, + {"JSONArray.Sort", json_arr_sort}, + {"JSONArray.Rotate", json_arr_rotate}, + + // JSON UTILITY + {"JSON.ToString", json_doc_write_to_str}, + {"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.RefCount.get", json_get_ref_count}, + {"JSON.ValCount.get", json_get_val_count}, + {"JSON.Type.get", json_get_type}, + {"JSON.SubType.get", json_get_subtype}, + {"JSON.IsArray.get", json_is_array}, + {"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.ApplyJsonPatch", json_apply_json_patch}, + {"JSON.JsonPatchInPlace", json_json_patch_in_place}, + {"JSON.ApplyMergePatch", json_apply_merge_patch}, + {"JSON.MergePatchInPlace", json_merge_patch_in_place}, + {"JSON.ReadNumber", json_read_number}, + {"JSON.WriteNumber", json_write_number}, + {"JSON.SetFpToFloat", json_set_fp_to_float}, + {"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}, + {"JSONArrIter.Reset", json_arr_iter_reset}, + + // 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}, + {"JSONObjIter.Reset", json_obj_iter_reset}, + + {nullptr, nullptr} +}; \ No newline at end of file diff --git a/src/YYJSONManager.cpp b/src/YYJSONManager.cpp deleted file mode 100644 index f3b1fc9..0000000 --- a/src/YYJSONManager.cpp +++ /dev/null @@ -1,3656 +0,0 @@ -#include "YYJSONManager.h" -#include "extension.h" - -std::unique_ptr YYJSONManager::CreateWrapper() { - return std::make_unique(); -} - -std::shared_ptr YYJSONManager::WrapDocument(yyjson_mut_doc* doc) { - return std::shared_ptr(doc, [](yyjson_mut_doc*){}); -} - -std::shared_ptr YYJSONManager::CopyDocument(yyjson_doc* doc) { - return WrapDocument(yyjson_doc_mut_copy(doc, nullptr)); -} - -std::shared_ptr YYJSONManager::CreateDocument() { - return WrapDocument(yyjson_mut_doc_new(nullptr)); -} - -std::shared_ptr YYJSONManager::WrapImmutableDocument(yyjson_doc* doc) { - return std::shared_ptr(doc, [](yyjson_doc*){}); -} - -YYJSONManager::YYJSONManager(): m_randomGenerator(m_randomDevice()) {} - -YYJSONManager::~YYJSONManager() {} - -YYJSONValue* YYJSONManager::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) { - if (error && error_size > 0) { - snprintf(error, error_size, "Invalid JSON string"); - } - return nullptr; - } - - yyjson_read_err readError; - yyjson_doc* idoc; - auto pYYJSONValue = CreateWrapper(); - - if (is_file) { - char realpath[PLATFORM_MAX_PATH]; - smutils->BuildPath(Path_Game, realpath, sizeof(realpath), "%s", json_str); - idoc = yyjson_read_file(realpath, read_flg, nullptr, &readError); - } else { - idoc = yyjson_read_opts(const_cast(json_str), strlen(json_str), read_flg, nullptr, &readError); - } - - if (!idoc || readError.code) { - if (error && error_size > 0) { - if (is_file) { - snprintf(error, error_size, "Failed to parse JSON file: %s (error code: %u, msg: %s, position: %zu)", - json_str, readError.code, readError.msg, readError.pos); - } else { - snprintf(error, error_size, "Failed to parse JSON str: %s (error code: %u, position: %zu)", - readError.msg, readError.code, readError.pos); - } - } - if (idoc) { - yyjson_doc_free(idoc); - } - return nullptr; - } - - pYYJSONValue->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()); - yyjson_doc_free(idoc); - } else { - pYYJSONValue->m_pDocument = WrapImmutableDocument(idoc); - pYYJSONValue->m_pVal = yyjson_doc_get_root(idoc); - } - - return pYYJSONValue.release(); -} - -bool YYJSONManager::WriteToString(YYJSONValue* handle, char* buffer, size_t buffer_size, - yyjson_write_flag write_flg, size_t* out_size) -{ - if (!handle || !buffer || buffer_size == 0) { - return false; - } - - size_t json_size; - char* json_str; - - if (handle->IsMutable()) { - json_str = yyjson_mut_val_write(handle->m_pVal_mut, write_flg, &json_size); - } else { - json_str = yyjson_val_write(handle->m_pVal, write_flg, &json_size); - } - - if (!json_str) { - return false; - } - - size_t needed_size = json_size + 1; - if (needed_size > buffer_size) { - free(json_str); - return false; - } - - memcpy(buffer, json_str, json_size); - buffer[json_size] = '\0'; - free(json_str); - - if (out_size) { - *out_size = needed_size; - } - - return true; -} - -bool YYJSONManager::WriteToFile(YYJSONValue* handle, const char* path, yyjson_write_flag write_flg, - char* error, size_t error_size) -{ - if (!handle || !path) { - if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters"); - } - return false; - } - - char realpath[PLATFORM_MAX_PATH]; - smutils->BuildPath(Path_Game, realpath, sizeof(realpath), "%s", path); - - yyjson_write_err writeError; - bool is_success; - - if (handle->IsMutable()) { - is_success = yyjson_mut_write_file(realpath, handle->m_pDocument_mut.get(), write_flg, nullptr, &writeError); - } else { - is_success = yyjson_write_file(realpath, handle->m_pDocument.get(), write_flg, nullptr, &writeError); - } - - if (writeError.code && error && error_size > 0) { - snprintf(error, error_size, "Failed to write JSON to file: %s (error code: %u)", writeError.msg, writeError.code); - } - - return is_success; -} - -bool YYJSONManager::Equals(YYJSONValue* handle1, YYJSONValue* handle2) -{ - if (!handle1 || !handle2) { - return false; - } - - if (handle1->IsMutable() && handle2->IsMutable()) { - return yyjson_mut_equals(handle1->m_pVal_mut, handle2->m_pVal_mut); - } - - if (!handle1->IsMutable() && !handle2->IsMutable()) { - auto doc1_mut = CopyDocument(handle1->m_pDocument.get()); - auto doc2_mut = CopyDocument(handle2->m_pDocument.get()); - - if (!doc1_mut || !doc2_mut) { - return false; - } - - yyjson_mut_val* val1_mut = yyjson_mut_doc_get_root(doc1_mut.get()); - yyjson_mut_val* val2_mut = yyjson_mut_doc_get_root(doc2_mut.get()); - - if (!val1_mut || !val2_mut) { - return false; - } - - return yyjson_mut_equals(val1_mut, val2_mut); - } - - YYJSONValue* immutable = handle1->IsMutable() ? handle2 : handle1; - YYJSONValue* mutable_doc = handle1->IsMutable() ? handle1 : handle2; - - auto doc_mut = CopyDocument(immutable->m_pDocument.get()); - if (!doc_mut) { - return false; - } - - yyjson_mut_val* val_mut = yyjson_mut_doc_get_root(doc_mut.get()); - if (!val_mut) { - return false; - } - - return yyjson_mut_equals(mutable_doc->m_pVal_mut, val_mut); -} - -YYJSONValue* YYJSONManager::DeepCopy(YYJSONValue* targetDoc, YYJSONValue* sourceValue) -{ - if (!targetDoc || !sourceValue) { - return nullptr; - } - - auto pYYJSONValue = CreateWrapper(); - - if (targetDoc->IsMutable()) { - pYYJSONValue->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); - } else { - val_copy = yyjson_val_mut_copy(pYYJSONValue->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; - } else { - yyjson_mut_doc* temp_doc = yyjson_mut_doc_new(nullptr); - if (!temp_doc) { - return nullptr; - } - - yyjson_mut_val* temp_val = nullptr; - if (sourceValue->IsMutable()) { - temp_val = yyjson_mut_val_mut_copy(temp_doc, sourceValue->m_pVal_mut); - } else { - temp_val = yyjson_val_mut_copy(temp_doc, sourceValue->m_pVal); - } - - if (!temp_val) { - yyjson_mut_doc_free(temp_doc); - return nullptr; - } - - yyjson_mut_doc_set_root(temp_doc, temp_val); - - yyjson_doc* doc = yyjson_mut_doc_imut_copy(temp_doc, nullptr); - yyjson_mut_doc_free(temp_doc); - - if (!doc) { - return nullptr; - } - - pYYJSONValue->m_pDocument = WrapImmutableDocument(doc); - pYYJSONValue->m_pVal = yyjson_doc_get_root(doc); - } - - return pYYJSONValue.release(); -} - -const char* YYJSONManager::GetTypeDesc(YYJSONValue* handle) -{ - if (!handle) { - return "invalid"; - } - - if (handle->IsMutable()) { - return yyjson_mut_get_type_desc(handle->m_pVal_mut); - } else { - return yyjson_get_type_desc(handle->m_pVal); - } -} - -size_t YYJSONManager::GetSerializedSize(YYJSONValue* handle, yyjson_write_flag write_flg) -{ - if (!handle) { - return 0; - } - - size_t json_size; - char* json_str; - - if (handle->IsMutable()) { - json_str = yyjson_mut_val_write(handle->m_pVal_mut, write_flg, &json_size); - } else { - json_str = yyjson_val_write(handle->m_pVal, write_flg, &json_size); - } - - if (json_str) { - free(json_str); - return json_size + 1; - } - - return 0; -} - -YYJSONValue* YYJSONManager::ToMutable(YYJSONValue* 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()); - - return pYYJSONValue.release(); -} - -YYJSONValue* YYJSONManager::ToImmutable(YYJSONValue* handle) -{ - if (!handle || !handle->IsMutable()) { - return nullptr; - } - - auto pYYJSONValue = 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()); - - return pYYJSONValue.release(); -} - -yyjson_type YYJSONManager::GetType(YYJSONValue* handle) -{ - if (!handle) { - return YYJSON_TYPE_NONE; - } - - if (handle->IsMutable()) { - return yyjson_mut_get_type(handle->m_pVal_mut); - } else { - return yyjson_get_type(handle->m_pVal); - } -} - -yyjson_subtype YYJSONManager::GetSubtype(YYJSONValue* handle) -{ - if (!handle) { - return YYJSON_SUBTYPE_NONE; - } - - if (handle->IsMutable()) { - return yyjson_mut_get_subtype(handle->m_pVal_mut); - } else { - return yyjson_get_subtype(handle->m_pVal); - } -} - -bool YYJSONManager::IsArray(YYJSONValue* handle) -{ - if (!handle) { - return false; - } - - if (handle->IsMutable()) { - return yyjson_mut_is_arr(handle->m_pVal_mut); - } else { - return yyjson_is_arr(handle->m_pVal); - } -} - -bool YYJSONManager::IsObject(YYJSONValue* handle) -{ - if (!handle) { - return false; - } - - if (handle->IsMutable()) { - return yyjson_mut_is_obj(handle->m_pVal_mut); - } else { - return yyjson_is_obj(handle->m_pVal); - } -} - -bool YYJSONManager::IsInt(YYJSONValue* handle) -{ - if (!handle) { - return false; - } - - if (handle->IsMutable()) { - return yyjson_mut_is_int(handle->m_pVal_mut); - } else { - return yyjson_is_int(handle->m_pVal); - } -} - -bool YYJSONManager::IsUint(YYJSONValue* handle) -{ - if (!handle) { - return false; - } - - if (handle->IsMutable()) { - return yyjson_mut_is_uint(handle->m_pVal_mut); - } else { - return yyjson_is_uint(handle->m_pVal); - } -} - -bool YYJSONManager::IsSint(YYJSONValue* handle) -{ - if (!handle) { - return false; - } - - if (handle->IsMutable()) { - return yyjson_mut_is_sint(handle->m_pVal_mut); - } else { - return yyjson_is_sint(handle->m_pVal); - } -} - -bool YYJSONManager::IsNum(YYJSONValue* handle) -{ - if (!handle) { - return false; - } - - if (handle->IsMutable()) { - return yyjson_mut_is_num(handle->m_pVal_mut); - } else { - return yyjson_is_num(handle->m_pVal); - } -} - -bool YYJSONManager::IsBool(YYJSONValue* handle) -{ - if (!handle) { - return false; - } - - if (handle->IsMutable()) { - return yyjson_mut_is_bool(handle->m_pVal_mut); - } else { - return yyjson_is_bool(handle->m_pVal); - } -} - -bool YYJSONManager::IsTrue(YYJSONValue* handle) -{ - if (!handle) { - return false; - } - - if (handle->IsMutable()) { - return yyjson_mut_is_true(handle->m_pVal_mut); - } else { - return yyjson_is_true(handle->m_pVal); - } -} - -bool YYJSONManager::IsFalse(YYJSONValue* handle) -{ - if (!handle) { - return false; - } - - if (handle->IsMutable()) { - return yyjson_mut_is_false(handle->m_pVal_mut); - } else { - return yyjson_is_false(handle->m_pVal); - } -} - -bool YYJSONManager::IsFloat(YYJSONValue* handle) -{ - if (!handle) { - return false; - } - - if (handle->IsMutable()) { - return yyjson_mut_is_real(handle->m_pVal_mut); - } else { - return yyjson_is_real(handle->m_pVal); - } -} - -bool YYJSONManager::IsStr(YYJSONValue* handle) -{ - if (!handle) { - return false; - } - - if (handle->IsMutable()) { - return yyjson_mut_is_str(handle->m_pVal_mut); - } else { - return yyjson_is_str(handle->m_pVal); - } -} - -bool YYJSONManager::IsNull(YYJSONValue* handle) -{ - if (!handle) { - return false; - } - - if (handle->IsMutable()) { - return yyjson_mut_is_null(handle->m_pVal_mut); - } else { - return yyjson_is_null(handle->m_pVal); - } -} - -bool YYJSONManager::IsCtn(YYJSONValue* handle) -{ - if (!handle) { - return false; - } - - if (handle->IsMutable()) { - return yyjson_mut_is_ctn(handle->m_pVal_mut); - } else { - return yyjson_is_ctn(handle->m_pVal); - } -} - -bool YYJSONManager::IsMutable(YYJSONValue* handle) -{ - if (!handle) { - return false; - } - - return handle->IsMutable(); -} - -bool YYJSONManager::IsImmutable(YYJSONValue* handle) -{ - if (!handle) { - return false; - } - - return handle->IsImmutable(); -} - -size_t YYJSONManager::GetReadSize(YYJSONValue* 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; - } - - return handle->m_readSize + 1; -} - -YYJSONValue* YYJSONManager::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); - - return pYYJSONValue.release(); -} - -YYJSONValue* YYJSONManager::ObjectInitWithStrings(const char** pairs, size_t count) -{ - if (!pairs || count == 0) { - return nullptr; - } - - auto pYYJSONValue = CreateWrapper(); - pYYJSONValue->m_pDocument_mut = CreateDocument(); - - pYYJSONValue->m_pVal_mut = yyjson_mut_obj_with_kv( - pYYJSONValue->m_pDocument_mut.get(), - pairs, - count - ); - - if (!pYYJSONValue->m_pVal_mut) { - return nullptr; - } - - return pYYJSONValue.release(); -} - -YYJSONValue* YYJSONManager::ObjectParseString(const char* str, yyjson_read_flag read_flg, - char* error, size_t error_size) -{ - if (!str) { - if (error && error_size > 0) { - snprintf(error, error_size, "Invalid string"); - } - return nullptr; - } - - auto pYYJSONValue = CreateWrapper(); - - yyjson_read_err readError; - yyjson_doc* idoc = yyjson_read_opts(const_cast(str), strlen(str), read_flg, nullptr, &readError); - - if (!idoc || readError.code) { - if (error && error_size > 0) { - snprintf(error, error_size, "Failed to parse JSON str: %s (error code: %u, position: %zu)", - readError.msg, readError.code, readError.pos); - } - if (idoc) { - yyjson_doc_free(idoc); - } - return nullptr; - } - - yyjson_val* root = yyjson_doc_get_root(idoc); - - if (!yyjson_is_obj(root)) { - if (error && error_size > 0) { - snprintf(error, error_size, "Root value is not an object (got %s)", yyjson_get_type_desc(root)); - } - yyjson_doc_free(idoc); - return nullptr; - } - - pYYJSONValue->m_readSize = yyjson_doc_get_read_size(idoc); - pYYJSONValue->m_pDocument = WrapImmutableDocument(idoc); - pYYJSONValue->m_pVal = root; - - return pYYJSONValue.release(); -} - -YYJSONValue* YYJSONManager::ObjectParseFile(const char* path, yyjson_read_flag read_flg, - char* error, size_t error_size) -{ - if (!path) { - if (error && error_size > 0) { - snprintf(error, error_size, "Invalid path"); - } - return nullptr; - } - - char realpath[PLATFORM_MAX_PATH]; - smutils->BuildPath(Path_Game, realpath, sizeof(realpath), "%s", path); - auto pYYJSONValue = CreateWrapper(); - - yyjson_read_err readError; - yyjson_doc* idoc = yyjson_read_file(realpath, read_flg, nullptr, &readError); - - if (!idoc || readError.code) { - if (error && error_size > 0) { - snprintf(error, error_size, "Failed to parse JSON file: %s (error code: %u, msg: %s, position: %zu)", - realpath, readError.code, readError.msg, readError.pos); - } - if (idoc) { - yyjson_doc_free(idoc); - } - return nullptr; - } - - yyjson_val* root = yyjson_doc_get_root(idoc); - - if (!yyjson_is_obj(root)) { - if (error && error_size > 0) { - snprintf(error, error_size, "Root value in file is not an object (got %s)", yyjson_get_type_desc(root)); - } - yyjson_doc_free(idoc); - return nullptr; - } - - pYYJSONValue->m_readSize = yyjson_doc_get_read_size(idoc); - pYYJSONValue->m_pDocument = WrapImmutableDocument(idoc); - pYYJSONValue->m_pVal = root; - - return pYYJSONValue.release(); -} - -size_t YYJSONManager::ObjectGetSize(YYJSONValue* handle) -{ - if (!handle) { - return 0; - } - - if (handle->IsMutable()) { - return yyjson_mut_obj_size(handle->m_pVal_mut); - } else { - return yyjson_obj_size(handle->m_pVal); - } -} - -bool YYJSONManager::ObjectGetKey(YYJSONValue* handle, size_t index, const char** out_key) -{ - if (!handle || !out_key) { - return false; - } - - if (handle->IsMutable()) { - size_t obj_size = yyjson_mut_obj_size(handle->m_pVal_mut); - if (index >= obj_size) { - return false; - } - - yyjson_mut_obj_iter iter; - yyjson_mut_obj_iter_init(handle->m_pVal_mut, &iter); - - for (size_t i = 0; i < index; i++) { - yyjson_mut_obj_iter_next(&iter); - } - - yyjson_mut_val* key = yyjson_mut_obj_iter_next(&iter); - if (!key) { - return false; - } - - *out_key = yyjson_mut_get_str(key); - return true; - } else { - size_t obj_size = yyjson_obj_size(handle->m_pVal); - if (index >= obj_size) { - return false; - } - - yyjson_obj_iter iter; - yyjson_obj_iter_init(handle->m_pVal, &iter); - - for (size_t i = 0; i < index; i++) { - yyjson_obj_iter_next(&iter); - } - - yyjson_val* key = yyjson_obj_iter_next(&iter); - if (!key) { - return false; - } - - *out_key = yyjson_get_str(key); - return true; - } -} - -YYJSONValue* YYJSONManager::ObjectGetValueAt(YYJSONValue* handle, size_t index) -{ - if (!handle) { - return nullptr; - } - - auto pYYJSONValue = 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); - - for (size_t i = 0; i < index; i++) { - yyjson_mut_obj_iter_next(&iter); - } - - yyjson_mut_val* key = yyjson_mut_obj_iter_next(&iter); - if (!key) { - return nullptr; - } - - pYYJSONValue->m_pDocument_mut = handle->m_pDocument_mut; - pYYJSONValue->m_pVal_mut = yyjson_mut_obj_iter_get_val(key); - } else { - size_t obj_size = yyjson_obj_size(handle->m_pVal); - if (index >= obj_size) { - return nullptr; - } - - yyjson_obj_iter iter; - yyjson_obj_iter_init(handle->m_pVal, &iter); - - for (size_t i = 0; i < index; i++) { - yyjson_obj_iter_next(&iter); - } - - yyjson_val* key = yyjson_obj_iter_next(&iter); - if (!key) { - return nullptr; - } - - pYYJSONValue->m_pDocument = handle->m_pDocument; - pYYJSONValue->m_pVal = yyjson_obj_iter_get_val(key); - } - - return pYYJSONValue.release(); -} - -YYJSONValue* YYJSONManager::ObjectGet(YYJSONValue* handle, const char* key) -{ - if (!handle || !key) { - return nullptr; - } - - auto pYYJSONValue = CreateWrapper(); - - if (handle->IsMutable()) { - yyjson_mut_val* val = yyjson_mut_obj_get(handle->m_pVal_mut, key); - if (!val) { - return nullptr; - } - - pYYJSONValue->m_pDocument_mut = handle->m_pDocument_mut; - pYYJSONValue->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; - } - - return pYYJSONValue.release(); -} - -bool YYJSONManager::ObjectGetBool(YYJSONValue* handle, const char* key, bool* out_value) -{ - if (!handle || !key || !out_value) { - return false; - } - - if (handle->IsMutable()) { - yyjson_mut_val* val = yyjson_mut_obj_get(handle->m_pVal_mut, key); - if (!val || !yyjson_mut_is_bool(val)) { - return false; - } - - *out_value = yyjson_mut_get_bool(val); - return true; - } else { - yyjson_val* val = yyjson_obj_get(handle->m_pVal, key); - if (!val || !yyjson_is_bool(val)) { - return false; - } - - *out_value = yyjson_get_bool(val); - return true; - } -} - -bool YYJSONManager::ObjectGetFloat(YYJSONValue* handle, const char* key, double* out_value) -{ - if (!handle || !key || !out_value) { - return false; - } - - if (handle->IsMutable()) { - yyjson_mut_val* val = yyjson_mut_obj_get(handle->m_pVal_mut, key); - if (!val || !yyjson_mut_is_real(val)) { - return false; - } - - *out_value = yyjson_mut_get_real(val); - return true; - } else { - yyjson_val* val = yyjson_obj_get(handle->m_pVal, key); - if (!val || !yyjson_is_real(val)) { - return false; - } - - *out_value = yyjson_get_real(val); - return true; - } -} - -bool YYJSONManager::ObjectGetInt(YYJSONValue* handle, const char* key, int* out_value) -{ - if (!handle || !key || !out_value) { - return false; - } - - if (handle->IsMutable()) { - yyjson_mut_val* val = yyjson_mut_obj_get(handle->m_pVal_mut, key); - if (!val || !yyjson_mut_is_int(val)) { - return false; - } - - *out_value = yyjson_mut_get_int(val); - return true; - } else { - yyjson_val* val = yyjson_obj_get(handle->m_pVal, key); - if (!val || !yyjson_is_int(val)) { - return false; - } - - *out_value = yyjson_get_int(val); - return true; - } -} - -bool YYJSONManager::ObjectGetInt64(YYJSONValue* handle, const char* key, int64_t* out_value) -{ - if (!handle || !key || !out_value) { - return false; - } - - if (handle->IsMutable()) { - yyjson_mut_val* val = yyjson_mut_obj_get(handle->m_pVal_mut, key); - if (!val || !yyjson_mut_is_int(val)) { - return false; - } - - *out_value = yyjson_mut_get_sint(val); - return true; - } else { - yyjson_val* val = yyjson_obj_get(handle->m_pVal, key); - if (!val || !yyjson_is_int(val)) { - return false; - } - - *out_value = yyjson_get_sint(val); - return true; - } -} - -bool YYJSONManager::ObjectGetString(YYJSONValue* handle, const char* key, const char** out_str, size_t* out_len) -{ - if (!handle || !key || !out_str) { - return false; - } - - if (handle->IsMutable()) { - yyjson_mut_val* val = yyjson_mut_obj_get(handle->m_pVal_mut, key); - if (!val || !yyjson_mut_is_str(val)) { - return false; - } - - *out_str = yyjson_mut_get_str(val); - if (out_len) { - *out_len = yyjson_mut_get_len(val); - } - return true; - } else { - yyjson_val* val = yyjson_obj_get(handle->m_pVal, key); - if (!val || !yyjson_is_str(val)) { - return false; - } - - *out_str = yyjson_get_str(val); - if (out_len) { - *out_len = yyjson_get_len(val); - } - return true; - } -} - -bool YYJSONManager::ObjectIsNull(YYJSONValue* handle, const char* key, bool* out_is_null) -{ - if (!handle || !key || !out_is_null) { - return false; - } - - if (handle->IsMutable()) { - yyjson_mut_val* val = yyjson_mut_obj_get(handle->m_pVal_mut, key); - if (!val) { - return false; - } - - *out_is_null = yyjson_mut_is_null(val); - return true; - } else { - yyjson_val* val = yyjson_obj_get(handle->m_pVal, key); - if (!val) { - return false; - } - - *out_is_null = yyjson_is_null(val); - return true; - } -} - -bool YYJSONManager::ObjectHasKey(YYJSONValue* handle, const char* key, bool use_pointer) -{ - if (!handle || !key) { - return false; - } - - if (handle->IsMutable()) { - if (use_pointer) { - return yyjson_mut_doc_ptr_get(handle->m_pDocument_mut.get(), key) != nullptr; - } else { - yyjson_mut_obj_iter iter = yyjson_mut_obj_iter_with(handle->m_pVal_mut); - return yyjson_mut_obj_iter_get(&iter, key) != nullptr; - } - } else { - if (use_pointer) { - return yyjson_doc_ptr_get(handle->m_pDocument.get(), key) != nullptr; - } else { - yyjson_obj_iter iter = yyjson_obj_iter_with(handle->m_pVal); - return yyjson_obj_iter_get(&iter, key) != nullptr; - } - } -} - -bool YYJSONManager::ObjectRenameKey(YYJSONValue* handle, const char* old_key, const char* new_key, bool allow_duplicate) -{ - if (!handle || !handle->IsMutable() || !old_key || !new_key) { - return false; - } - - if (!yyjson_mut_obj_get(handle->m_pVal_mut, old_key)) { - return false; - } - - if (!allow_duplicate && yyjson_mut_obj_get(handle->m_pVal_mut, new_key)) { - return false; - } - - 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) -{ - if (!handle || !handle->IsMutable() || !key || !value) { - return false; - } - - yyjson_mut_val* val_copy = nullptr; - if (value->IsMutable()) { - val_copy = yyjson_mut_val_mut_copy(handle->m_pDocument_mut.get(), value->m_pVal_mut); - } else { - val_copy = yyjson_val_mut_copy(handle->m_pDocument_mut.get(), value->m_pVal); - } - - if (!val_copy) { - return false; - } - - return yyjson_mut_obj_put(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut.get(), key), val_copy); -} - -bool YYJSONManager::ObjectSetBool(YYJSONValue* handle, const char* key, bool 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_bool(handle->m_pDocument_mut.get(), value)); -} - -bool YYJSONManager::ObjectSetFloat(YYJSONValue* handle, const char* key, double 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_real(handle->m_pDocument_mut.get(), value)); -} - -bool YYJSONManager::ObjectSetInt(YYJSONValue* handle, const char* key, int 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_int(handle->m_pDocument_mut.get(), value)); -} - -bool YYJSONManager::ObjectSetInt64(YYJSONValue* handle, const char* key, int64_t 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)); -} - -bool YYJSONManager::ObjectSetNull(YYJSONValue* handle, const char* key) -{ - 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_null(handle->m_pDocument_mut.get())); -} - -bool YYJSONManager::ObjectSetString(YYJSONValue* handle, const char* key, const char* value) -{ - if (!handle || !handle->IsMutable() || !key || !value) { - return false; - } - - return yyjson_mut_obj_put(handle->m_pVal_mut, yyjson_mut_strcpy(handle->m_pDocument_mut.get(), key), yyjson_mut_strcpy(handle->m_pDocument_mut.get(), value)); -} - -bool YYJSONManager::ObjectRemove(YYJSONValue* handle, const char* key) -{ - if (!handle || !handle->IsMutable() || !key) { - return false; - } - - return yyjson_mut_obj_remove_key(handle->m_pVal_mut, key) != nullptr; -} - -bool YYJSONManager::ObjectClear(YYJSONValue* handle) -{ - if (!handle || !handle->IsMutable()) { - return false; - } - - return yyjson_mut_obj_clear(handle->m_pVal_mut); -} - -bool YYJSONManager::ObjectSort(YYJSONValue* handle, YYJSON_SORT_ORDER sort_mode) -{ - if (!handle || !handle->IsMutable()) { - return false; - } - - if (!yyjson_mut_is_obj(handle->m_pVal_mut)) { - return false; - } - - if (sort_mode < YYJSON_SORT_ASC || sort_mode > YYJSON_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(); - 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); - } - - if (sort_mode == YYJSON_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; - }; - - 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); - } - - return true; -} - -YYJSONValue* YYJSONManager::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); - - return pYYJSONValue.release(); -} - -YYJSONValue* YYJSONManager::ArrayInitWithStrings(const char** strings, size_t count) -{ - if (!strings) { - return nullptr; - } - - auto pYYJSONValue = CreateWrapper(); - pYYJSONValue->m_pDocument_mut = CreateDocument(); - - pYYJSONValue->m_pVal_mut = yyjson_mut_arr_with_strcpy( - pYYJSONValue->m_pDocument_mut.get(), - strings, - count - ); - - if (!pYYJSONValue->m_pVal_mut) { - return nullptr; - } - - return pYYJSONValue.release(); -} - -YYJSONValue* YYJSONManager::ArrayParseString(const char* str, yyjson_read_flag read_flg, - char* error, size_t error_size) -{ - if (!str) { - if (error && error_size > 0) { - snprintf(error, error_size, "Invalid string"); - } - return nullptr; - } - - auto pYYJSONValue = CreateWrapper(); - - yyjson_read_err readError; - yyjson_doc* idoc = yyjson_read_opts(const_cast(str), strlen(str), read_flg, nullptr, &readError); - - if (!idoc || readError.code) { - if (error && error_size > 0) { - snprintf(error, error_size, "Failed to parse JSON string: %s (error code: %u, position: %zu)", - readError.msg, readError.code, readError.pos); - } - if (idoc) { - yyjson_doc_free(idoc); - } - return nullptr; - } - - yyjson_val* root = yyjson_doc_get_root(idoc); - - if (!yyjson_is_arr(root)) { - if (error && error_size > 0) { - snprintf(error, error_size, "Root value is not an array (got %s)", yyjson_get_type_desc(root)); - } - yyjson_doc_free(idoc); - return nullptr; - } - - pYYJSONValue->m_readSize = yyjson_doc_get_read_size(idoc); - pYYJSONValue->m_pDocument = WrapImmutableDocument(idoc); - pYYJSONValue->m_pVal = root; - - return pYYJSONValue.release(); -} - -YYJSONValue* YYJSONManager::ArrayParseFile(const char* path, yyjson_read_flag read_flg, - char* error, size_t error_size) -{ - if (!path) { - if (error && error_size > 0) { - snprintf(error, error_size, "Invalid path"); - } - return nullptr; - } - - char realpath[PLATFORM_MAX_PATH]; - smutils->BuildPath(Path_Game, realpath, sizeof(realpath), "%s", path); - auto pYYJSONValue = CreateWrapper(); - - yyjson_read_err readError; - yyjson_doc* idoc = yyjson_read_file(realpath, read_flg, nullptr, &readError); - - if (!idoc || readError.code) { - if (error && error_size > 0) { - snprintf(error, error_size, "Failed to parse JSON file: %s (error code: %u, msg: %s, position: %zu)", - realpath, readError.code, readError.msg, readError.pos); - } - if (idoc) { - yyjson_doc_free(idoc); - } - return nullptr; - } - - yyjson_val* root = yyjson_doc_get_root(idoc); - - if (!yyjson_is_arr(root)) { - if (error && error_size > 0) { - snprintf(error, error_size, "Root value in file is not an array (got %s)", yyjson_get_type_desc(root)); - } - yyjson_doc_free(idoc); - return nullptr; - } - - pYYJSONValue->m_readSize = yyjson_doc_get_read_size(idoc); - pYYJSONValue->m_pDocument = WrapImmutableDocument(idoc); - pYYJSONValue->m_pVal = root; - - return pYYJSONValue.release(); -} - -size_t YYJSONManager::ArrayGetSize(YYJSONValue* handle) -{ - if (!handle) { - return 0; - } - - if (handle->IsMutable()) { - return yyjson_mut_arr_size(handle->m_pVal_mut); - } else { - return yyjson_arr_size(handle->m_pVal); - } -} - -YYJSONValue* YYJSONManager::ArrayGet(YYJSONValue* handle, size_t index) -{ - if (!handle) { - return nullptr; - } - - auto pYYJSONValue = CreateWrapper(); - - if (handle->IsMutable()) { - size_t arr_size = yyjson_mut_arr_size(handle->m_pVal_mut); - if (index >= arr_size) { - return nullptr; - } - - yyjson_mut_val* val = yyjson_mut_arr_get(handle->m_pVal_mut, index); - if (!val) { - return nullptr; - } - - pYYJSONValue->m_pDocument_mut = handle->m_pDocument_mut; - pYYJSONValue->m_pVal_mut = val; - } else { - size_t arr_size = yyjson_arr_size(handle->m_pVal); - if (index >= arr_size) { - return nullptr; - } - - yyjson_val* val = yyjson_arr_get(handle->m_pVal, index); - if (!val) { - return nullptr; - } - - pYYJSONValue->m_pDocument = handle->m_pDocument; - pYYJSONValue->m_pVal = val; - } - - return pYYJSONValue.release(); -} - -YYJSONValue* YYJSONManager::ArrayGetFirst(YYJSONValue* handle) -{ - if (!handle) { - return nullptr; - } - - auto pYYJSONValue = CreateWrapper(); - - if (handle->IsMutable()) { - size_t arr_size = yyjson_mut_arr_size(handle->m_pVal_mut); - if (arr_size == 0) { - return nullptr; - } - - yyjson_mut_val* val = yyjson_mut_arr_get_first(handle->m_pVal_mut); - if (!val) { - return nullptr; - } - - pYYJSONValue->m_pDocument_mut = handle->m_pDocument_mut; - pYYJSONValue->m_pVal_mut = val; - } else { - size_t arr_size = yyjson_arr_size(handle->m_pVal); - if (arr_size == 0) { - return nullptr; - } - - yyjson_val* val = yyjson_arr_get_first(handle->m_pVal); - if (!val) { - return nullptr; - } - - pYYJSONValue->m_pDocument = handle->m_pDocument; - pYYJSONValue->m_pVal = val; - } - - return pYYJSONValue.release(); -} - -YYJSONValue* YYJSONManager::ArrayGetLast(YYJSONValue* handle) -{ - if (!handle) { - return nullptr; - } - - auto pYYJSONValue = CreateWrapper(); - - if (handle->IsMutable()) { - size_t arr_size = yyjson_mut_arr_size(handle->m_pVal_mut); - if (arr_size == 0) { - return nullptr; - } - - yyjson_mut_val* val = yyjson_mut_arr_get_last(handle->m_pVal_mut); - if (!val) { - return nullptr; - } - - pYYJSONValue->m_pDocument_mut = handle->m_pDocument_mut; - pYYJSONValue->m_pVal_mut = val; - } else { - size_t arr_size = yyjson_arr_size(handle->m_pVal); - if (arr_size == 0) { - return nullptr; - } - - yyjson_val* val = yyjson_arr_get_last(handle->m_pVal); - if (!val) { - return nullptr; - } - - pYYJSONValue->m_pDocument = handle->m_pDocument; - pYYJSONValue->m_pVal = val; - } - - return pYYJSONValue.release(); -} - -bool YYJSONManager::ArrayGetBool(YYJSONValue* handle, size_t index, bool* out_value) -{ - if (!handle || !out_value) { - return false; - } - - if (handle->IsMutable()) { - size_t arr_size = yyjson_mut_arr_size(handle->m_pVal_mut); - if (index >= arr_size) { - return false; - } - - yyjson_mut_val* val = yyjson_mut_arr_get(handle->m_pVal_mut, index); - if (!yyjson_mut_is_bool(val)) { - return false; - } - - *out_value = yyjson_mut_get_bool(val); - return true; - } else { - size_t arr_size = yyjson_arr_size(handle->m_pVal); - if (index >= arr_size) { - return false; - } - - yyjson_val* val = yyjson_arr_get(handle->m_pVal, index); - if (!yyjson_is_bool(val)) { - return false; - } - - *out_value = yyjson_get_bool(val); - return true; - } -} - -bool YYJSONManager::ArrayGetFloat(YYJSONValue* handle, size_t index, double* out_value) -{ - if (!handle || !out_value) { - return false; - } - - if (handle->IsMutable()) { - size_t arr_size = yyjson_mut_arr_size(handle->m_pVal_mut); - if (index >= arr_size) { - return false; - } - - yyjson_mut_val* val = yyjson_mut_arr_get(handle->m_pVal_mut, index); - if (!yyjson_mut_is_real(val)) { - return false; - } - - *out_value = yyjson_mut_get_real(val); - return true; - } else { - size_t arr_size = yyjson_arr_size(handle->m_pVal); - if (index >= arr_size) { - return false; - } - - yyjson_val* val = yyjson_arr_get(handle->m_pVal, index); - if (!yyjson_is_real(val)) { - return false; - } - - *out_value = yyjson_get_real(val); - return true; - } -} - -bool YYJSONManager::ArrayGetInt(YYJSONValue* handle, size_t index, int* out_value) -{ - if (!handle || !out_value) { - return false; - } - - if (handle->IsMutable()) { - size_t arr_size = yyjson_mut_arr_size(handle->m_pVal_mut); - if (index >= arr_size) { - return false; - } - - yyjson_mut_val* val = yyjson_mut_arr_get(handle->m_pVal_mut, index); - if (!yyjson_mut_is_int(val)) { - return false; - } - - *out_value = yyjson_mut_get_int(val); - return true; - } else { - size_t arr_size = yyjson_arr_size(handle->m_pVal); - if (index >= arr_size) { - return false; - } - - yyjson_val* val = yyjson_arr_get(handle->m_pVal, index); - if (!yyjson_is_int(val)) { - return false; - } - - *out_value = yyjson_get_int(val); - return true; - } -} - -bool YYJSONManager::ArrayGetInt64(YYJSONValue* handle, size_t index, int64_t* out_value) -{ - if (!handle || !out_value) { - return false; - } - - if (handle->IsMutable()) { - size_t arr_size = yyjson_mut_arr_size(handle->m_pVal_mut); - if (index >= arr_size) { - return false; - } - - yyjson_mut_val* val = yyjson_mut_arr_get(handle->m_pVal_mut, index); - if (!yyjson_mut_is_int(val)) { - return false; - } - - *out_value = yyjson_mut_get_sint(val); - return true; - } else { - size_t arr_size = yyjson_arr_size(handle->m_pVal); - if (index >= arr_size) { - return false; - } - - yyjson_val* val = yyjson_arr_get(handle->m_pVal, index); - if (!yyjson_is_int(val)) { - return false; - } - - *out_value = yyjson_get_sint(val); - return true; - } -} - -bool YYJSONManager::ArrayGetString(YYJSONValue* handle, size_t index, const char** out_str, size_t* out_len) -{ - if (!handle || !out_str) { - return false; - } - - if (handle->IsMutable()) { - size_t arr_size = yyjson_mut_arr_size(handle->m_pVal_mut); - if (index >= arr_size) { - return false; - } - - yyjson_mut_val* val = yyjson_mut_arr_get(handle->m_pVal_mut, index); - if (!yyjson_mut_is_str(val)) { - return false; - } - - *out_str = yyjson_mut_get_str(val); - if (out_len) { - *out_len = yyjson_mut_get_len(val); - } - return true; - } else { - size_t arr_size = yyjson_arr_size(handle->m_pVal); - if (index >= arr_size) { - return false; - } - - yyjson_val* val = yyjson_arr_get(handle->m_pVal, index); - if (!yyjson_is_str(val)) { - return false; - } - - *out_str = yyjson_get_str(val); - if (out_len) { - *out_len = yyjson_get_len(val); - } - return true; - } -} - -bool YYJSONManager::ArrayIsNull(YYJSONValue* handle, size_t index) -{ - if (!handle) { - return false; - } - - if (handle->IsMutable()) { - size_t arr_size = yyjson_mut_arr_size(handle->m_pVal_mut); - if (index >= arr_size) { - return false; - } - - yyjson_mut_val* val = yyjson_mut_arr_get(handle->m_pVal_mut, index); - return yyjson_mut_is_null(val); - } else { - size_t arr_size = yyjson_arr_size(handle->m_pVal); - if (index >= arr_size) { - return false; - } - - yyjson_val* val = yyjson_arr_get(handle->m_pVal, index); - return yyjson_is_null(val); - } -} - -bool YYJSONManager::ArrayReplace(YYJSONValue* handle, size_t index, YYJSONValue* value) -{ - if (!handle || !handle->IsMutable() || !value) { - return false; - } - - size_t arr_size = yyjson_mut_arr_size(handle->m_pVal_mut); - if (index >= arr_size) { - return false; - } - - yyjson_mut_val* val_copy = nullptr; - if (value->IsMutable()) { - val_copy = yyjson_mut_val_mut_copy(handle->m_pDocument_mut.get(), value->m_pVal_mut); - } else { - val_copy = yyjson_val_mut_copy(handle->m_pDocument_mut.get(), value->m_pVal); - } - - if (!val_copy) { - return false; - } - - return yyjson_mut_arr_replace(handle->m_pVal_mut, index, val_copy) != nullptr; -} - -bool YYJSONManager::ArrayReplaceBool(YYJSONValue* handle, size_t index, bool value) -{ - if (!handle || !handle->IsMutable()) { - return false; - } - - size_t arr_size = yyjson_mut_arr_size(handle->m_pVal_mut); - if (index >= arr_size) { - return false; - } - - 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) -{ - if (!handle || !handle->IsMutable()) { - return false; - } - - size_t arr_size = yyjson_mut_arr_size(handle->m_pVal_mut); - if (index >= arr_size) { - return false; - } - - 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) -{ - if (!handle || !handle->IsMutable()) { - return false; - } - - size_t arr_size = yyjson_mut_arr_size(handle->m_pVal_mut); - if (index >= arr_size) { - return false; - } - - 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) -{ - if (!handle || !handle->IsMutable()) { - return false; - } - - size_t arr_size = yyjson_mut_arr_size(handle->m_pVal_mut); - if (index >= arr_size) { - return false; - } - - return yyjson_mut_arr_replace(handle->m_pVal_mut, index, yyjson_mut_sint(handle->m_pDocument_mut.get(), value)) != nullptr; -} - -bool YYJSONManager::ArrayReplaceNull(YYJSONValue* handle, size_t index) -{ - if (!handle || !handle->IsMutable()) { - return false; - } - - size_t arr_size = yyjson_mut_arr_size(handle->m_pVal_mut); - if (index >= arr_size) { - return false; - } - - 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) -{ - if (!handle || !handle->IsMutable() || !value) { - return false; - } - - size_t arr_size = yyjson_mut_arr_size(handle->m_pVal_mut); - if (index >= arr_size) { - return false; - } - - 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) -{ - if (!handle || !handle->IsMutable() || !value) { - return false; - } - - yyjson_mut_val* val_copy = nullptr; - if (value->IsMutable()) { - val_copy = yyjson_mut_val_mut_copy(handle->m_pDocument_mut.get(), value->m_pVal_mut); - } else { - val_copy = yyjson_val_mut_copy(handle->m_pDocument_mut.get(), value->m_pVal); - } - - if (!val_copy) { - return false; - } - - return yyjson_mut_arr_append(handle->m_pVal_mut, val_copy); -} - -bool YYJSONManager::ArrayAppendBool(YYJSONValue* handle, bool value) -{ - if (!handle || !handle->IsMutable()) { - return false; - } - - 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) -{ - if (!handle || !handle->IsMutable()) { - return false; - } - - 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) -{ - if (!handle || !handle->IsMutable()) { - return false; - } - - 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) -{ - if (!handle || !handle->IsMutable()) { - return false; - } - - return yyjson_mut_arr_append(handle->m_pVal_mut, yyjson_mut_sint(handle->m_pDocument_mut.get(), value)); -} - -bool YYJSONManager::ArrayAppendNull(YYJSONValue* handle) -{ - if (!handle || !handle->IsMutable()) { - return false; - } - - 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) -{ - if (!handle || !handle->IsMutable() || !value) { - return false; - } - - 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) -{ - if (!handle || !handle->IsMutable()) { - return false; - } - - size_t arr_size = yyjson_mut_arr_size(handle->m_pVal_mut); - if (index >= arr_size) { - return false; - } - - return yyjson_mut_arr_remove(handle->m_pVal_mut, index) != nullptr; -} - -bool YYJSONManager::ArrayRemoveFirst(YYJSONValue* handle) -{ - if (!handle || !handle->IsMutable()) { - return false; - } - - if (yyjson_mut_arr_size(handle->m_pVal_mut) == 0) { - return false; - } - - return yyjson_mut_arr_remove_first(handle->m_pVal_mut) != nullptr; -} - -bool YYJSONManager::ArrayRemoveLast(YYJSONValue* handle) -{ - if (!handle || !handle->IsMutable()) { - return false; - } - - if (yyjson_mut_arr_size(handle->m_pVal_mut) == 0) { - return false; - } - - return yyjson_mut_arr_remove_last(handle->m_pVal_mut) != nullptr; -} - -bool YYJSONManager::ArrayRemoveRange(YYJSONValue* handle, size_t start_index, size_t end_index) -{ - if (!handle || !handle->IsMutable()) { - return false; - } - - size_t arr_size = yyjson_mut_arr_size(handle->m_pVal_mut); - - if (start_index >= arr_size || end_index > arr_size || start_index > end_index) { - return false; - } - - return yyjson_mut_arr_remove_range(handle->m_pVal_mut, start_index, end_index); -} - -bool YYJSONManager::ArrayClear(YYJSONValue* handle) -{ - if (!handle || !handle->IsMutable()) { - return false; - } - - return yyjson_mut_arr_clear(handle->m_pVal_mut); -} - -int YYJSONManager::ArrayIndexOfBool(YYJSONValue* handle, bool 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_bool(val) && yyjson_mut_get_bool(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_bool(val) && yyjson_get_bool(val) == search_value) { - return static_cast(idx); - } - } - } - - return -1; -} - -int YYJSONManager::ArrayIndexOfString(YYJSONValue* handle, const char* search_value) -{ - if (!handle || !search_value) { - 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_str(val) && strcmp(yyjson_mut_get_str(val), search_value) == 0) { - return static_cast(idx); - } - } - } else { - size_t idx, max; - yyjson_val *val; - yyjson_arr_foreach(handle->m_pVal, idx, max, val) { - if (yyjson_is_str(val) && strcmp(yyjson_get_str(val), search_value) == 0) { - return static_cast(idx); - } - } - } - - return -1; -} - -int YYJSONManager::ArrayIndexOfInt(YYJSONValue* handle, int 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_int(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_int(val) == search_value) { - return static_cast(idx); - } - } - } - - return -1; -} - -int YYJSONManager::ArrayIndexOfInt64(YYJSONValue* handle, int64_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_sint(val) == search_value) { - return static_cast(idx); - } - } - } else { - size_t idx, max; - yyjson_val *val; - yyjson_arr_foreach(handle->m_pVal, idx, max, val) { - if (yyjson_is_int(val) && yyjson_get_sint(val) == search_value) { - return static_cast(idx); - } - } - } - - return -1; -} - -int YYJSONManager::ArrayIndexOfFloat(YYJSONValue* handle, double 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_real(val)) { - double val_num = yyjson_mut_get_real(val); - if (yyjson_equals_fp(val_num, 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_real(val)) { - double val_num = yyjson_get_real(val); - if (yyjson_equals_fp(val_num, search_value)) { - return static_cast(idx); - } - } - } - } - - return -1; -} - -bool YYJSONManager::ArraySort(YYJSONValue* handle, YYJSON_SORT_ORDER sort_mode) -{ - if (!handle || !handle->IsMutable()) { - return false; - } - - if (!yyjson_mut_is_arr(handle->m_pVal_mut)) { - return false; - } - - if (sort_mode < YYJSON_SORT_ASC || sort_mode > YYJSON_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(); - 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); - } - - if (sort_mode == YYJSON_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; - - 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; - } - - switch (type_a) { - case YYJSON_TYPE_STR: { - const char* str_a = yyjson_mut_get_str(a); - const char* str_b = yyjson_mut_get_str(b); - int cmp = strcmp(str_a, str_b); - return sort_mode == YYJSON_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; - } - 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; - } - 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; - } - default: - return false; - } - }; - - std::sort(values.begin(), values.end(), compare); - } - - yyjson_mut_arr_clear(handle->m_pVal_mut); - for (auto val : values) { - yyjson_mut_arr_append(handle->m_pVal_mut, val); - } - - return true; -} - -const char* YYJSONManager::SkipSeparators(const char* ptr) -{ - while (*ptr && (isspace(*ptr) || *ptr == ':' || *ptr == ',')) { - ptr++; - } - return ptr; -} - -void YYJSONManager::SetPackError(char* error, size_t error_size, const char* fmt, ...) -{ - if (error && error_size > 0) { - va_list args; - va_start(args, fmt); - vsnprintf(error, error_size, fmt, args); - va_end(args); - error[error_size - 1] = '\0'; - } -} - -yyjson_mut_val* YYJSONManager::PackImpl(yyjson_mut_doc* doc, const char* format, - IPackParamProvider* provider, - char* error, size_t error_size, - const char** out_end_ptr) -{ - if (!doc || !format || !*format) { - SetPackError(error, error_size, "Invalid argument(s)"); - return nullptr; - } - - yyjson_mut_val* root = nullptr; - const char* ptr = format; - - bool is_obj = false; - if (*ptr == '{') { - root = yyjson_mut_obj(doc); - is_obj = true; - ptr = SkipSeparators(ptr + 1); - } else if (*ptr == '[') { - root = yyjson_mut_arr(doc); - ptr = SkipSeparators(ptr + 1); - } else { - SetPackError(error, error_size, "Invalid format string: expected '{' or '['"); - return nullptr; - } - - if (!root) { - SetPackError(error, error_size, "Failed to create root object/array"); - return nullptr; - } - - yyjson_mut_val* key_val = nullptr; - yyjson_mut_val* val = nullptr; - - while (*ptr && *ptr != '}' && *ptr != ']') { - if (is_obj) { - if (*ptr != 's') { - SetPackError(error, error_size, "Object key must be string, got '%c'", *ptr); - return nullptr; - } - } - switch (*ptr) { - case 's': { - if (is_obj) { - const char* key; - if (!provider->GetNextString(&key)) { - SetPackError(error, error_size, "Invalid string key"); - return nullptr; - } - key_val = yyjson_mut_strcpy(doc, key); - if (!key_val) { - SetPackError(error, error_size, "Failed to create key"); - return nullptr; - } - - ptr = SkipSeparators(ptr + 1); - if (*ptr != 's' && *ptr != 'i' && *ptr != 'f' && *ptr != 'b' && - *ptr != 'n' && *ptr != '{' && *ptr != '[') { - SetPackError(error, error_size, "Invalid value type after key"); - return nullptr; - } - - if (*ptr == '{' || *ptr == '[') { - val = PackImpl(doc, ptr, provider, error, error_size, &ptr); - if (!val) { - return nullptr; - } - } else { - switch (*ptr) { - case 's': { - const char* val_str; - if (!provider->GetNextString(&val_str)) { - SetPackError(error, error_size, "Invalid string value"); - return nullptr; - } - val = yyjson_mut_strcpy(doc, val_str); - ptr++; - break; - } - case 'i': { - int val_int; - if (!provider->GetNextInt(&val_int)) { - SetPackError(error, error_size, "Invalid integer value"); - return nullptr; - } - val = yyjson_mut_int(doc, val_int); - ptr++; - break; - } - case 'f': { - float val_float; - if (!provider->GetNextFloat(&val_float)) { - SetPackError(error, error_size, "Invalid float value"); - return nullptr; - } - val = yyjson_mut_real(doc, val_float); - ptr++; - break; - } - case 'b': { - bool val_bool; - if (!provider->GetNextBool(&val_bool)) { - SetPackError(error, error_size, "Invalid boolean value"); - return nullptr; - } - val = yyjson_mut_bool(doc, val_bool); - ptr++; - break; - } - case 'n': { - val = yyjson_mut_null(doc); - ptr++; - break; - } - } - } - - if (!val) { - SetPackError(error, error_size, "Failed to create value"); - return nullptr; - } - - if (!yyjson_mut_obj_add(root, key_val, val)) { - SetPackError(error, error_size, "Failed to add value to object"); - return nullptr; - } - } else { - const char* val_str; - if (!provider->GetNextString(&val_str)) { - SetPackError(error, error_size, "Invalid string value"); - return nullptr; - } - if (!yyjson_mut_arr_add_strcpy(doc, root, val_str)) { - SetPackError(error, error_size, "Failed to add string to array"); - return nullptr; - } - ptr++; - } - break; - } - case 'i': { - int val_int; - if (!provider->GetNextInt(&val_int)) { - SetPackError(error, error_size, "Invalid integer value"); - return nullptr; - } - if (!yyjson_mut_arr_add_int(doc, root, val_int)) { - SetPackError(error, error_size, "Failed to add integer to array"); - return nullptr; - } - ptr++; - break; - } - case 'b': { - bool val_bool; - if (!provider->GetNextBool(&val_bool)) { - SetPackError(error, error_size, "Invalid boolean value"); - return nullptr; - } - if (!yyjson_mut_arr_add_bool(doc, root, val_bool)) { - SetPackError(error, error_size, "Failed to add boolean to array"); - return nullptr; - } - ptr++; - break; - } - case 'n': { - if (!yyjson_mut_arr_add_null(doc, root)) { - SetPackError(error, error_size, "Failed to add null to array"); - return nullptr; - } - ptr++; - break; - } - case 'f': { - float val_float; - if (!provider->GetNextFloat(&val_float)) { - SetPackError(error, error_size, "Invalid float value"); - return nullptr; - } - if (!yyjson_mut_arr_add_real(doc, root, val_float)) { - SetPackError(error, error_size, "Failed to add float to array"); - return nullptr; - } - ptr++; - break; - } - case '{': - case '[': { - val = PackImpl(doc, ptr, provider, error, error_size, &ptr); - if (!val) { - return nullptr; - } - if (!yyjson_mut_arr_append(root, val)) { - SetPackError(error, error_size, "Failed to add nested value to array"); - return nullptr; - } - break; - } - default: { - SetPackError(error, error_size, "Invalid format character: %c", *ptr); - return nullptr; - } - } - ptr = SkipSeparators(ptr); - } - - if (*ptr != (is_obj ? '}' : ']')) { - SetPackError(error, error_size, "Unexpected end of format string"); - return nullptr; - } - - if (out_end_ptr) { - *out_end_ptr = ptr + 1; - } - - return root; -} - -YYJSONValue* YYJSONManager::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(); - - if (!pYYJSONValue->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, - param_provider, error, error_size, &end_ptr); - - if (!pYYJSONValue->m_pVal_mut) { - return nullptr; - } - - yyjson_mut_doc_set_root(pYYJSONValue->m_pDocument_mut.get(), pYYJSONValue->m_pVal_mut); - - return pYYJSONValue.release(); -} - -YYJSONValue* YYJSONManager::CreateBool(bool value) -{ - auto pYYJSONValue = CreateWrapper(); - pYYJSONValue->m_pDocument_mut = CreateDocument(); - pYYJSONValue->m_pVal_mut = yyjson_mut_bool(pYYJSONValue->m_pDocument_mut.get(), value); - - if (!pYYJSONValue->m_pVal_mut) { - return nullptr; - } - - return pYYJSONValue.release(); -} - -YYJSONValue* YYJSONManager::CreateFloat(double value) -{ - auto pYYJSONValue = CreateWrapper(); - pYYJSONValue->m_pDocument_mut = CreateDocument(); - pYYJSONValue->m_pVal_mut = yyjson_mut_real(pYYJSONValue->m_pDocument_mut.get(), value); - - if (!pYYJSONValue->m_pVal_mut) { - return nullptr; - } - - return pYYJSONValue.release(); -} - -YYJSONValue* YYJSONManager::CreateInt(int value) -{ - auto pYYJSONValue = CreateWrapper(); - pYYJSONValue->m_pDocument_mut = CreateDocument(); - pYYJSONValue->m_pVal_mut = yyjson_mut_int(pYYJSONValue->m_pDocument_mut.get(), value); - - if (!pYYJSONValue->m_pVal_mut) { - return nullptr; - } - - return pYYJSONValue.release(); -} - -YYJSONValue* YYJSONManager::CreateInt64(int64_t value) -{ - auto pYYJSONValue = CreateWrapper(); - pYYJSONValue->m_pDocument_mut = CreateDocument(); - pYYJSONValue->m_pVal_mut = yyjson_mut_sint(pYYJSONValue->m_pDocument_mut.get(), value); - - if (!pYYJSONValue->m_pVal_mut) { - return nullptr; - } - - return pYYJSONValue.release(); -} - -YYJSONValue* YYJSONManager::CreateNull() -{ - auto pYYJSONValue = CreateWrapper(); - pYYJSONValue->m_pDocument_mut = CreateDocument(); - pYYJSONValue->m_pVal_mut = yyjson_mut_null(pYYJSONValue->m_pDocument_mut.get()); - - if (!pYYJSONValue->m_pVal_mut) { - return nullptr; - } - - return pYYJSONValue.release(); -} - -YYJSONValue* YYJSONManager::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); - - if (!pYYJSONValue->m_pVal_mut) { - return nullptr; - } - - return pYYJSONValue.release(); -} - -bool YYJSONManager::GetBool(YYJSONValue* handle, bool* out_value) -{ - if (!handle || !out_value) { - return false; - } - - if (handle->IsMutable()) { - if (!yyjson_mut_is_bool(handle->m_pVal_mut)) { - return false; - } - *out_value = yyjson_mut_get_bool(handle->m_pVal_mut); - return true; - } else { - if (!yyjson_is_bool(handle->m_pVal)) { - return false; - } - *out_value = yyjson_get_bool(handle->m_pVal); - return true; - } -} - -bool YYJSONManager::GetFloat(YYJSONValue* handle, double* out_value) -{ - if (!handle || !out_value) { - return false; - } - - if (handle->IsMutable()) { - if (!yyjson_mut_is_real(handle->m_pVal_mut)) { - return false; - } - *out_value = yyjson_mut_get_real(handle->m_pVal_mut); - return true; - } else { - if (!yyjson_is_real(handle->m_pVal)) { - return false; - } - *out_value = yyjson_get_real(handle->m_pVal); - return true; - } -} - -bool YYJSONManager::GetInt(YYJSONValue* handle, int* out_value) -{ - if (!handle || !out_value) { - return false; - } - - if (handle->IsMutable()) { - if (!yyjson_mut_is_int(handle->m_pVal_mut)) { - return false; - } - *out_value = yyjson_mut_get_int(handle->m_pVal_mut); - return true; - } else { - if (!yyjson_is_int(handle->m_pVal)) { - return false; - } - *out_value = yyjson_get_int(handle->m_pVal); - return true; - } -} - -bool YYJSONManager::GetInt64(YYJSONValue* handle, int64_t* out_value) -{ - if (!handle || !out_value) { - return false; - } - - if (handle->IsMutable()) { - if (!yyjson_mut_is_int(handle->m_pVal_mut)) { - return false; - } - *out_value = yyjson_mut_get_sint(handle->m_pVal_mut); - return true; - } else { - if (!yyjson_is_int(handle->m_pVal)) { - return false; - } - *out_value = yyjson_get_sint(handle->m_pVal); - return true; - } -} - -bool YYJSONManager::GetString(YYJSONValue* handle, const char** out_str, size_t* out_len) -{ - if (!handle || !out_str) { - return false; - } - - if (handle->IsMutable()) { - if (!yyjson_mut_is_str(handle->m_pVal_mut)) { - return false; - } - *out_str = yyjson_mut_get_str(handle->m_pVal_mut); - if (out_len) { - *out_len = yyjson_mut_get_len(handle->m_pVal_mut); - } - return true; - } else { - if (!yyjson_is_str(handle->m_pVal)) { - return false; - } - *out_str = yyjson_get_str(handle->m_pVal); - if (out_len) { - *out_len = yyjson_get_len(handle->m_pVal); - } - return true; - } -} - -YYJSONValue* YYJSONManager::PtrGet(YYJSONValue* handle, const char* path, char* error, size_t error_size) -{ - if (!handle || !path) { - if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters"); - } - 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) { - if (error && error_size > 0) { - 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; - } 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)", - ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); - } - return nullptr; - } - - pYYJSONValue->m_pDocument = handle->m_pDocument; - pYYJSONValue->m_pVal = val; - } - - return pYYJSONValue.release(); -} - -bool YYJSONManager::PtrGetBool(YYJSONValue* handle, const char* path, bool* out_value, char* error, size_t error_size) -{ - if (!handle || !path || !out_value) { - if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters"); - } - 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 (ptrGetError.code) { - if (error && error_size > 0) { - 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; - } - - if (!yyjson_mut_is_bool(val)) { - if (error && error_size > 0) { - snprintf(error, error_size, "Type mismatch at path '%s': expected boolean value, got %s", path, yyjson_mut_get_type_desc(val)); - } - return false; - } - - *out_value = yyjson_mut_get_bool(val); - return true; - } else { - yyjson_val* val = yyjson_doc_ptr_getx(handle->m_pDocument.get(), path, strlen(path), &ptrGetError); - - 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)", - ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); - } - return false; - } - - if (!yyjson_is_bool(val)) { - if (error && error_size > 0) { - snprintf(error, error_size, "Type mismatch at path '%s': expected boolean value, got %s", path, yyjson_get_type_desc(val)); - } - return false; - } - - *out_value = yyjson_get_bool(val); - return true; - } -} - -bool YYJSONManager::PtrGetFloat(YYJSONValue* handle, const char* path, double* out_value, char* error, size_t error_size) -{ - if (!handle || !path || !out_value) { - if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters"); - } - 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 (ptrGetError.code) { - if (error && error_size > 0) { - 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; - } - - if (!yyjson_mut_is_real(val)) { - if (error && error_size > 0) { - snprintf(error, error_size, "Type mismatch at path '%s': expected float value, got %s", path, yyjson_mut_get_type_desc(val)); - } - return false; - } - - *out_value = yyjson_mut_get_real(val); - return true; - } else { - yyjson_val* val = yyjson_doc_ptr_getx(handle->m_pDocument.get(), path, strlen(path), &ptrGetError); - - 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)", - ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); - } - return false; - } - - if (!yyjson_is_real(val)) { - if (error && error_size > 0) { - snprintf(error, error_size, "Type mismatch at path '%s': expected float value, got %s", path, yyjson_get_type_desc(val)); - } - return false; - } - - *out_value = yyjson_get_real(val); - return true; - } -} - -bool YYJSONManager::PtrGetInt(YYJSONValue* handle, const char* path, int* out_value, char* error, size_t error_size) -{ - if (!handle || !path || !out_value) { - if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters"); - } - 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 (ptrGetError.code) { - if (error && error_size > 0) { - 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; - } - - if (!yyjson_mut_is_int(val)) { - if (error && error_size > 0) { - snprintf(error, error_size, "Type mismatch at path '%s': expected integer value, got %s", path, yyjson_mut_get_type_desc(val)); - } - return false; - } - - *out_value = yyjson_mut_get_int(val); - return true; - } else { - yyjson_val* val = yyjson_doc_ptr_getx(handle->m_pDocument.get(), path, strlen(path), &ptrGetError); - - 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)", - ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); - } - return false; - } - - if (!yyjson_is_int(val)) { - if (error && error_size > 0) { - snprintf(error, error_size, "Type mismatch at path '%s': expected integer value, got %s", path, yyjson_get_type_desc(val)); - } - return false; - } - - *out_value = yyjson_get_int(val); - return true; - } -} - -bool YYJSONManager::PtrGetInt64(YYJSONValue* handle, const char* path, int64_t* out_value, char* error, size_t error_size) -{ - if (!handle || !path || !out_value) { - if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters"); - } - 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 (ptrGetError.code) { - if (error && error_size > 0) { - 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; - } - - if (!yyjson_mut_is_int(val)) { - if (error && error_size > 0) { - snprintf(error, error_size, "Type mismatch at path '%s': expected integer64 value, got %s", path, yyjson_mut_get_type_desc(val)); - } - return false; - } - - *out_value = yyjson_mut_get_sint(val); - return true; - } else { - yyjson_val* val = yyjson_doc_ptr_getx(handle->m_pDocument.get(), path, strlen(path), &ptrGetError); - - 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)", - ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); - } - return false; - } - - if (!yyjson_is_int(val)) { - if (error && error_size > 0) { - snprintf(error, error_size, "Type mismatch at path '%s': expected integer64 value, got %s", path, yyjson_get_type_desc(val)); - } - return false; - } - - *out_value = yyjson_get_sint(val); - return true; - } -} - -bool YYJSONManager::PtrGetString(YYJSONValue* 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) { - snprintf(error, error_size, "Invalid parameters"); - } - 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 (ptrGetError.code) { - if (error && error_size > 0) { - 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; - } - - if (!yyjson_mut_is_str(val)) { - if (error && error_size > 0) { - snprintf(error, error_size, "Type mismatch at path '%s': expected string value, got %s", path, yyjson_mut_get_type_desc(val)); - } - return false; - } - - *out_str = yyjson_mut_get_str(val); - if (out_len) { - *out_len = yyjson_mut_get_len(val); - } - return true; - } else { - yyjson_val* val = yyjson_doc_ptr_getx(handle->m_pDocument.get(), path, strlen(path), &ptrGetError); - - 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)", - ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); - } - return false; - } - - if (!yyjson_is_str(val)) { - if (error && error_size > 0) { - snprintf(error, error_size, "Type mismatch at path '%s': expected string value, got %s", path, yyjson_get_type_desc(val)); - } - return false; - } - - *out_str = yyjson_get_str(val); - if (out_len) { - *out_len = yyjson_get_len(val); - } - return true; - } -} - -bool YYJSONManager::PtrGetIsNull(YYJSONValue* 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) { - snprintf(error, error_size, "Invalid parameters"); - } - 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 (ptrGetError.code) { - if (error && error_size > 0) { - 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; - } - - *out_is_null = yyjson_mut_is_null(val); - return true; - } else { - yyjson_val* val = yyjson_doc_ptr_getx(handle->m_pDocument.get(), path, strlen(path), &ptrGetError); - - 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)", - ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); - } - return false; - } - - *out_is_null = yyjson_is_null(val); - return true; - } -} - -bool YYJSONManager::PtrGetLength(YYJSONValue* handle, const char* path, size_t* out_len, char* error, size_t error_size) -{ - if (!handle || !path || !out_len) { - if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters"); - } - 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 (ptrGetError.code) { - if (error && error_size > 0) { - 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; - } - - if (yyjson_mut_is_str(val)) { - *out_len = yyjson_mut_get_len(val) + 1; - } else { - *out_len = yyjson_mut_get_len(val); - } - return true; - } else { - yyjson_val* val = yyjson_doc_ptr_getx(handle->m_pDocument.get(), path, strlen(path), &ptrGetError); - - 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)", - ptrGetError.msg, ptrGetError.code, ptrGetError.pos, path); - } - return false; - } - - if (yyjson_is_str(val)) { - *out_len = yyjson_get_len(val) + 1; - } else { - *out_len = yyjson_get_len(val); - } - return true; - } -} - -bool YYJSONManager::PtrSet(YYJSONValue* handle, const char* path, YYJSONValue* value, char* error, size_t error_size) -{ - if (!handle || !handle->IsMutable() || !path || !value) { - if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters or immutable document"); - } - return false; - } - - yyjson_mut_val* val_copy = nullptr; - if (value->IsMutable()) { - val_copy = yyjson_mut_val_mut_copy(handle->m_pDocument_mut.get(), value->m_pVal_mut); - } else { - val_copy = yyjson_val_mut_copy(handle->m_pDocument_mut.get(), value->m_pVal); - } - - if (!val_copy) { - if (error && error_size > 0) { - snprintf(error, error_size, "Failed to copy JSON value"); - } - return false; - } - - yyjson_ptr_err ptrSetError; - bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut.get(), path, strlen(path), val_copy, true, nullptr, &ptrSetError); - - if (ptrSetError.code && error && error_size > 0) { - 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) -{ - if (!handle || !handle->IsMutable() || !path) { - if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters or immutable document"); - } - return false; - } - - yyjson_ptr_err ptrSetError; - bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_bool(handle->m_pDocument_mut.get(), value), true, nullptr, &ptrSetError); - - if (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::PtrSetFloat(YYJSONValue* handle, const char* path, double value, char* error, size_t error_size) -{ - if (!handle || !handle->IsMutable() || !path) { - if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters or immutable document"); - } - return false; - } - - yyjson_ptr_err ptrSetError; - bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_real(handle->m_pDocument_mut.get(), value), true, nullptr, &ptrSetError); - - if (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::PtrSetInt(YYJSONValue* handle, const char* path, int value, char* error, size_t error_size) -{ - if (!handle || !handle->IsMutable() || !path) { - if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters or immutable document"); - } - return false; - } - - yyjson_ptr_err ptrSetError; - bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_int(handle->m_pDocument_mut.get(), value), true, nullptr, &ptrSetError); - - if (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::PtrSetInt64(YYJSONValue* handle, const char* path, int64_t value, char* error, size_t error_size) -{ - if (!handle || !handle->IsMutable() || !path) { - if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters or immutable document"); - } - return false; - } - - 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); - - if (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) -{ - if (!handle || !handle->IsMutable() || !path || !value) { - if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters or immutable document"); - } - return false; - } - - yyjson_ptr_err ptrSetError; - bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_strcpy(handle->m_pDocument_mut.get(), value), true, nullptr, &ptrSetError); - - if (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::PtrSetNull(YYJSONValue* handle, const char* path, char* error, size_t error_size) -{ - if (!handle || !handle->IsMutable() || !path) { - if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters or immutable document"); - } - return false; - } - - yyjson_ptr_err ptrSetError; - bool success = yyjson_mut_doc_ptr_setx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_null(handle->m_pDocument_mut.get()), true, nullptr, &ptrSetError); - - if (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::PtrAdd(YYJSONValue* handle, const char* path, YYJSONValue* value, char* error, size_t error_size) -{ - if (!handle || !handle->IsMutable() || !path || !value) { - if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters or immutable document"); - } - return false; - } - - yyjson_mut_val* val_copy = nullptr; - if (value->IsMutable()) { - val_copy = yyjson_mut_val_mut_copy(handle->m_pDocument_mut.get(), value->m_pVal_mut); - } else { - val_copy = yyjson_val_mut_copy(handle->m_pDocument_mut.get(), value->m_pVal); - } - - if (!val_copy) { - if (error && error_size > 0) { - snprintf(error, error_size, "Failed to copy JSON value"); - } - return false; - } - - yyjson_ptr_err ptrAddError; - bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut.get(), path, strlen(path), val_copy, true, nullptr, &ptrAddError); - - if (ptrAddError.code && error && error_size > 0) { - 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) -{ - if (!handle || !handle->IsMutable() || !path) { - if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters or immutable document"); - } - return false; - } - - yyjson_ptr_err ptrAddError; - bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_bool(handle->m_pDocument_mut.get(), value), true, nullptr, &ptrAddError); - - if (ptrAddError.code && error && error_size > 0) { - 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) -{ - if (!handle || !handle->IsMutable() || !path) { - if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters or immutable document"); - } - return false; - } - - yyjson_ptr_err ptrAddError; - bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_real(handle->m_pDocument_mut.get(), value), true, nullptr, &ptrAddError); - - if (ptrAddError.code && error && error_size > 0) { - 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) -{ - if (!handle || !handle->IsMutable() || !path) { - if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters or immutable document"); - } - return false; - } - - yyjson_ptr_err ptrAddError; - bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_int(handle->m_pDocument_mut.get(), value), true, nullptr, &ptrAddError); - - if (ptrAddError.code && error && error_size > 0) { - 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) -{ - if (!handle || !handle->IsMutable() || !path) { - if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters or immutable document"); - } - return false; - } - - 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); - - if (ptrAddError.code && error && error_size > 0) { - 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) -{ - if (!handle || !handle->IsMutable() || !path || !value) { - if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters or immutable document"); - } - return false; - } - - yyjson_ptr_err ptrAddError; - bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_strcpy(handle->m_pDocument_mut.get(), value), true, nullptr, &ptrAddError); - - if (ptrAddError.code && error && error_size > 0) { - 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) -{ - if (!handle || !handle->IsMutable() || !path) { - if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters or immutable document"); - } - return false; - } - - yyjson_ptr_err ptrAddError; - bool success = yyjson_mut_doc_ptr_addx(handle->m_pDocument_mut.get(), path, strlen(path), yyjson_mut_null(handle->m_pDocument_mut.get()), true, nullptr, &ptrAddError); - - if (ptrAddError.code && error && error_size > 0) { - 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) -{ - if (!handle || !handle->IsMutable() || !path) { - if (error && error_size > 0) { - snprintf(error, error_size, "Invalid parameters or immutable document"); - } - return false; - } - - yyjson_ptr_err ptrRemoveError; - bool success = yyjson_mut_doc_ptr_removex(handle->m_pDocument_mut.get(), path, strlen(path), nullptr, &ptrRemoveError) != nullptr; - - if (ptrRemoveError.code && error && error_size > 0) { - 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) -{ - 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) { - return nullptr; - } - - pYYJSONValue->m_pDocument = handle->m_pDocument; - pYYJSONValue->m_pVal = val; - } - - return pYYJSONValue.release(); -} - -bool YYJSONManager::PtrTryGetBool(YYJSONValue* handle, const char* path, bool* out_value) -{ - if (!handle || !path || !out_value) { - 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_bool(val)) { - return false; - } - - *out_value = yyjson_mut_get_bool(val); - return true; - } else { - yyjson_val* val = yyjson_doc_ptr_getx(handle->m_pDocument.get(), path, strlen(path), &ptrGetError); - - if (!val || ptrGetError.code || !yyjson_is_bool(val)) { - return false; - } - - *out_value = yyjson_get_bool(val); - return true; - } -} - -bool YYJSONManager::PtrTryGetFloat(YYJSONValue* handle, const char* path, double* out_value) -{ - if (!handle || !path || !out_value) { - 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_num(val)) { - return false; - } - - *out_value = yyjson_mut_get_num(val); - return true; - } else { - yyjson_val* val = yyjson_doc_ptr_getx(handle->m_pDocument.get(), path, strlen(path), &ptrGetError); - - if (!val || ptrGetError.code || !yyjson_is_num(val)) { - return false; - } - - *out_value = yyjson_get_num(val); - return true; - } -} - -bool YYJSONManager::PtrTryGetInt(YYJSONValue* handle, const char* path, int* out_value) -{ - if (!handle || !path || !out_value) { - 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_int(val)) { - return false; - } - *out_value = yyjson_mut_get_int(val); - return true; - } else { - yyjson_val* val = yyjson_doc_ptr_getx(handle->m_pDocument.get(), path, strlen(path), &ptrGetError); - - if (!val || ptrGetError.code || !yyjson_is_int(val)) { - return false; - } - *out_value = yyjson_get_int(val); - return true; - } -} - -bool YYJSONManager::PtrTryGetInt64(YYJSONValue* handle, const char* path, int64_t* out_value) -{ - if (!handle || !path || !out_value) { - 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_int(val)) { - return false; - } - *out_value = yyjson_mut_get_sint(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)) { - return false; - } - *out_value = yyjson_get_sint(val); - return true; - } -} - -bool YYJSONManager::PtrTryGetString(YYJSONValue* handle, const char* path, const char** out_str, size_t* out_len) -{ - if (!handle || !path || !out_str) { - 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)) { - return false; - } - - *out_str = yyjson_mut_get_str(val); - if (out_len) { - *out_len = yyjson_mut_get_len(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)) { - return false; - } - - *out_str = yyjson_get_str(val); - if (out_len) { - *out_len = yyjson_get_len(val); - } - return true; - } -} - -bool YYJSONManager::ObjectForeachNext(YYJSONValue* handle, const char** out_key, - size_t* out_key_len, YYJSONValue** out_value) -{ - if (!handle || !IsObject(handle)) { - return false; - } - - if (IsMutable(handle)) { - if (!handle->m_iterInitialized) { - if (!yyjson_mut_obj_iter_init(handle->m_pVal_mut, &handle->m_iterObj)) { - return false; - } - handle->m_iterInitialized = true; - } - - yyjson_mut_val* key = yyjson_mut_obj_iter_next(&handle->m_iterObj); - if (key) { - yyjson_mut_val* val = yyjson_mut_obj_iter_get_val(key); - - *out_key = yyjson_mut_get_str(key); - if (out_key_len) { - *out_key_len = yyjson_mut_get_len(key); - } - - auto pWrapper = CreateWrapper(); - pWrapper->m_pDocument_mut = handle->m_pDocument_mut; - pWrapper->m_pVal_mut = val; - *out_value = pWrapper.release(); - - return true; - } - } else { - if (!handle->m_iterInitialized) { - if (!yyjson_obj_iter_init(handle->m_pVal, &handle->m_iterObjImm)) { - return false; - } - handle->m_iterInitialized = true; - } - - yyjson_val* key = yyjson_obj_iter_next(&handle->m_iterObjImm); - if (key) { - yyjson_val* val = yyjson_obj_iter_get_val(key); - - *out_key = yyjson_get_str(key); - if (out_key_len) { - *out_key_len = yyjson_get_len(key); - } - - auto pWrapper = CreateWrapper(); - pWrapper->m_pDocument = handle->m_pDocument; - pWrapper->m_pVal = val; - *out_value = pWrapper.release(); - - return true; - } - } - - handle->ResetObjectIterator(); - return false; -} - -bool YYJSONManager::ArrayForeachNext(YYJSONValue* handle, size_t* out_index, - YYJSONValue** out_value) -{ - if (!handle || !IsArray(handle)) { - return false; - } - - if (IsMutable(handle)) { - if (!handle->m_iterInitialized) { - if (!yyjson_mut_arr_iter_init(handle->m_pVal_mut, &handle->m_iterArr)) { - return false; - } - handle->m_iterInitialized = true; - } - - yyjson_mut_val* val = yyjson_mut_arr_iter_next(&handle->m_iterArr); - if (val) { - *out_index = handle->m_arrayIndex; - - auto pWrapper = CreateWrapper(); - pWrapper->m_pDocument_mut = handle->m_pDocument_mut; - pWrapper->m_pVal_mut = val; - *out_value = pWrapper.release(); - - handle->m_arrayIndex++; - return true; - } - } else { - if (!handle->m_iterInitialized) { - if (!yyjson_arr_iter_init(handle->m_pVal, &handle->m_iterArrImm)) { - return false; - } - handle->m_iterInitialized = true; - } - - yyjson_val* val = yyjson_arr_iter_next(&handle->m_iterArrImm); - if (val) { - *out_index = handle->m_arrayIndex; - - auto pWrapper = CreateWrapper(); - pWrapper->m_pDocument = handle->m_pDocument; - pWrapper->m_pVal = val; - *out_value = pWrapper.release(); - - handle->m_arrayIndex++; - return true; - } - } - - handle->ResetArrayIterator(); - return false; -} - -bool YYJSONManager::ObjectForeachKeyNext(YYJSONValue* handle, const char** out_key, - size_t* out_key_len) -{ - if (!handle || !IsObject(handle)) { - return false; - } - - if (IsMutable(handle)) { - if (!handle->m_iterInitialized) { - if (!yyjson_mut_obj_iter_init(handle->m_pVal_mut, &handle->m_iterObj)) { - return false; - } - handle->m_iterInitialized = true; - } - - yyjson_mut_val* key = yyjson_mut_obj_iter_next(&handle->m_iterObj); - if (key) { - *out_key = yyjson_mut_get_str(key); - if (out_key_len) { - *out_key_len = yyjson_mut_get_len(key); - } - return true; - } - } else { - if (!handle->m_iterInitialized) { - if (!yyjson_obj_iter_init(handle->m_pVal, &handle->m_iterObjImm)) { - return false; - } - handle->m_iterInitialized = true; - } - - yyjson_val* key = yyjson_obj_iter_next(&handle->m_iterObjImm); - if (key) { - *out_key = yyjson_get_str(key); - if (out_key_len) { - *out_key_len = yyjson_get_len(key); - } - return true; - } - } - - handle->ResetObjectIterator(); - return false; -} - -bool YYJSONManager::ArrayForeachIndexNext(YYJSONValue* handle, size_t* out_index) -{ - if (!handle || !IsArray(handle)) { - return false; - } - - if (IsMutable(handle)) { - if (!handle->m_iterInitialized) { - if (!yyjson_mut_arr_iter_init(handle->m_pVal_mut, &handle->m_iterArr)) { - return false; - } - handle->m_iterInitialized = true; - } - - yyjson_mut_val* val = yyjson_mut_arr_iter_next(&handle->m_iterArr); - if (val) { - *out_index = handle->m_arrayIndex; - handle->m_arrayIndex++; - return true; - } - } else { - if (!handle->m_iterInitialized) { - if (!yyjson_arr_iter_init(handle->m_pVal, &handle->m_iterArrImm)) { - return false; - } - handle->m_iterInitialized = true; - } - - yyjson_val* val = yyjson_arr_iter_next(&handle->m_iterArrImm); - if (val) { - *out_index = handle->m_arrayIndex; - handle->m_arrayIndex++; - return true; - } - } - - handle->ResetArrayIterator(); - return false; -} - -void YYJSONManager::Release(YYJSONValue* value) -{ - if (value) { - delete value; - } -} - -HandleType_t YYJSONManager::GetHandleType() -{ - return g_htJSON; -} - -YYJSONValue* YYJSONManager::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) - { - pContext->ReportError("Invalid YYJSON handle %x (error %d)", handle, err); - return nullptr; - } - - return pYYJSONValue; -} \ No newline at end of file diff --git a/src/YYJSONManager.h b/src/YYJSONManager.h deleted file mode 100644 index 802e5c5..0000000 --- a/src/YYJSONManager.h +++ /dev/null @@ -1,264 +0,0 @@ -#ifndef _INCLUDE_SM_YYJSON_YYJSONMANAGER_H_ -#define _INCLUDE_SM_YYJSON_YYJSONMANAGER_H_ - -#include -#include -#include -#include -#include - -/** - * @brief JSON value wrapper - * - * Wraps yyjson mutable/immutable documents and values. - * Used as the primary data type for JSON operations. - */ -class YYJSONValue { -public: - YYJSONValue() = default; - ~YYJSONValue() { - if (m_pDocument_mut.unique()) { - yyjson_mut_doc_free(m_pDocument_mut.get()); - } - if (m_pDocument.unique()) { - yyjson_doc_free(m_pDocument.get()); - } - } - - YYJSONValue(const YYJSONValue&) = delete; - YYJSONValue& operator=(const YYJSONValue&) = delete; - - void ResetObjectIterator() { - m_iterInitialized = false; - } - - void ResetArrayIterator() { - m_iterInitialized = false; - m_arrayIndex = 0; - } - - bool IsMutable() const { - return m_pDocument_mut != nullptr; - } - - bool IsImmutable() const { - return m_pDocument != nullptr; - } - - // Mutable document - std::shared_ptr m_pDocument_mut; - yyjson_mut_val* m_pVal_mut{ nullptr }; - - // Immutable document - std::shared_ptr m_pDocument; - yyjson_val* m_pVal{ nullptr }; - - // Mutable document iterators - yyjson_mut_obj_iter m_iterObj; - yyjson_mut_arr_iter m_iterArr; - - // Immutable document iterators - yyjson_obj_iter m_iterObjImm; - yyjson_arr_iter m_iterArrImm; - - Handle_t m_handle{ BAD_HANDLE }; - size_t m_arrayIndex{ 0 }; - size_t m_readSize{ 0 }; - bool m_iterInitialized{ false }; -}; - -class YYJSONManager : public IYYJSONManager -{ -public: - YYJSONManager(); - ~YYJSONManager(); - -public: - // ========== Document Operations ========== - virtual YYJSONValue* ParseJSON(const char* json_str, bool is_file, bool is_mutable, - yyjson_read_flag read_flg, char* error, size_t error_size) override; - virtual bool WriteToString(YYJSONValue* handle, char* buffer, size_t buffer_size, - yyjson_write_flag write_flg, size_t* out_size) override; - virtual bool WriteToFile(YYJSONValue* handle, const char* path, yyjson_write_flag write_flg, - char* error, size_t error_size) override; - virtual bool Equals(YYJSONValue* handle1, YYJSONValue* handle2) override; - virtual YYJSONValue* DeepCopy(YYJSONValue* targetDoc, YYJSONValue* sourceValue) override; - virtual const char* GetTypeDesc(YYJSONValue* handle) override; - virtual size_t GetSerializedSize(YYJSONValue* handle, yyjson_write_flag write_flg) override; - virtual YYJSONValue* ToMutable(YYJSONValue* handle) override; - virtual YYJSONValue* ToImmutable(YYJSONValue* handle) override; - virtual yyjson_type GetType(YYJSONValue* handle) override; - virtual yyjson_subtype GetSubtype(YYJSONValue* handle) override; - virtual bool IsArray(YYJSONValue* handle) override; - virtual bool IsObject(YYJSONValue* handle) override; - virtual bool IsInt(YYJSONValue* handle) override; - virtual bool IsUint(YYJSONValue* handle) override; - virtual bool IsSint(YYJSONValue* handle) override; - virtual bool IsNum(YYJSONValue* handle) override; - virtual bool IsBool(YYJSONValue* handle) override; - virtual bool IsTrue(YYJSONValue* handle) override; - virtual bool IsFalse(YYJSONValue* handle) override; - virtual bool IsFloat(YYJSONValue* handle) override; - virtual bool IsStr(YYJSONValue* handle) override; - virtual bool IsNull(YYJSONValue* handle) override; - virtual bool IsCtn(YYJSONValue* handle) override; - virtual bool IsMutable(YYJSONValue* handle) override; - virtual bool IsImmutable(YYJSONValue* handle) override; - virtual size_t GetReadSize(YYJSONValue* handle) override; - - // ========== Object Operations ========== - virtual YYJSONValue* ObjectInit() override; - virtual YYJSONValue* ObjectInitWithStrings(const char** pairs, size_t count) override; - virtual YYJSONValue* ObjectParseString(const char* str, yyjson_read_flag read_flg, - char* error, size_t error_size) override; - virtual YYJSONValue* ObjectParseFile(const char* path, yyjson_read_flag read_flg, - char* error, size_t error_size) override; - virtual size_t ObjectGetSize(YYJSONValue* handle) override; - virtual bool ObjectGetKey(YYJSONValue* handle, size_t index, const char** out_key) override; - virtual YYJSONValue* ObjectGetValueAt(YYJSONValue* handle, size_t index) override; - virtual YYJSONValue* ObjectGet(YYJSONValue* handle, const char* key) override; - virtual bool ObjectGetBool(YYJSONValue* handle, const char* key, bool* out_value) override; - virtual bool ObjectGetFloat(YYJSONValue* handle, const char* key, double* out_value) override; - virtual bool ObjectGetInt(YYJSONValue* handle, const char* key, int* out_value) override; - virtual bool ObjectGetInt64(YYJSONValue* handle, const char* key, int64_t* out_value) override; - virtual bool ObjectGetString(YYJSONValue* handle, const char* key, const char** out_str, size_t* out_len) override; - virtual bool ObjectIsNull(YYJSONValue* handle, const char* key, bool* out_is_null) override; - virtual bool ObjectHasKey(YYJSONValue* handle, const char* key, bool use_pointer) override; - virtual bool ObjectRenameKey(YYJSONValue* handle, const char* old_key, const char* new_key, bool allow_duplicate) override; - virtual bool ObjectSet(YYJSONValue* handle, const char* key, YYJSONValue* value) override; - virtual bool ObjectSetBool(YYJSONValue* handle, const char* key, bool value) override; - virtual bool ObjectSetFloat(YYJSONValue* handle, const char* key, double value) override; - virtual bool ObjectSetInt(YYJSONValue* handle, const char* key, int value) override; - virtual bool ObjectSetInt64(YYJSONValue* handle, const char* key, int64_t value) override; - virtual bool ObjectSetNull(YYJSONValue* handle, const char* key) override; - virtual bool ObjectSetString(YYJSONValue* handle, const char* key, const char* value) override; - virtual bool ObjectRemove(YYJSONValue* handle, const char* key) override; - virtual bool ObjectClear(YYJSONValue* handle) override; - virtual bool ObjectSort(YYJSONValue* handle, YYJSON_SORT_ORDER sort_mode) override; - - // ========== Array Operations ========== - virtual YYJSONValue* ArrayInit() override; - virtual YYJSONValue* ArrayInitWithStrings(const char** strings, size_t count) override; - virtual YYJSONValue* ArrayParseString(const char* str, yyjson_read_flag read_flg, - char* error, size_t error_size) override; - virtual YYJSONValue* ArrayParseFile(const char* path, yyjson_read_flag read_flg, - char* error, size_t error_size) override; - virtual size_t ArrayGetSize(YYJSONValue* handle) override; - virtual YYJSONValue* ArrayGet(YYJSONValue* handle, size_t index) override; - virtual YYJSONValue* ArrayGetFirst(YYJSONValue* handle) override; - virtual YYJSONValue* ArrayGetLast(YYJSONValue* handle) override; - virtual bool ArrayGetBool(YYJSONValue* handle, size_t index, bool* out_value) override; - virtual bool ArrayGetFloat(YYJSONValue* handle, size_t index, double* out_value) override; - virtual bool ArrayGetInt(YYJSONValue* handle, size_t index, int* out_value) override; - virtual bool ArrayGetInt64(YYJSONValue* handle, size_t index, int64_t* out_value) override; - virtual bool ArrayGetString(YYJSONValue* handle, size_t index, const char** out_str, size_t* out_len) override; - virtual bool ArrayIsNull(YYJSONValue* handle, size_t index) override; - virtual bool ArrayReplace(YYJSONValue* handle, size_t index, YYJSONValue* value) override; - virtual bool ArrayReplaceBool(YYJSONValue* handle, size_t index, bool value) override; - virtual bool ArrayReplaceFloat(YYJSONValue* handle, size_t index, double value) override; - virtual bool ArrayReplaceInt(YYJSONValue* handle, size_t index, int value) override; - virtual bool ArrayReplaceInt64(YYJSONValue* handle, size_t index, int64_t value) override; - virtual bool ArrayReplaceNull(YYJSONValue* handle, size_t index) override; - virtual bool ArrayReplaceString(YYJSONValue* handle, size_t index, const char* value) override; - virtual bool ArrayAppend(YYJSONValue* handle, YYJSONValue* value) override; - virtual bool ArrayAppendBool(YYJSONValue* handle, bool value) override; - virtual bool ArrayAppendFloat(YYJSONValue* handle, double value) override; - virtual bool ArrayAppendInt(YYJSONValue* handle, int value) override; - virtual bool ArrayAppendInt64(YYJSONValue* handle, int64_t value) override; - virtual bool ArrayAppendNull(YYJSONValue* handle) override; - virtual bool ArrayAppendString(YYJSONValue* handle, const char* value) override; - virtual bool ArrayRemove(YYJSONValue* handle, size_t index) override; - virtual bool ArrayRemoveFirst(YYJSONValue* handle) override; - virtual bool ArrayRemoveLast(YYJSONValue* handle) override; - virtual bool ArrayRemoveRange(YYJSONValue* handle, size_t start_index, size_t end_index) override; - virtual bool ArrayClear(YYJSONValue* handle) override; - virtual int ArrayIndexOfBool(YYJSONValue* handle, bool search_value) override; - virtual int ArrayIndexOfString(YYJSONValue* handle, const char* search_value) override; - virtual int ArrayIndexOfInt(YYJSONValue* handle, int search_value) override; - virtual int ArrayIndexOfInt64(YYJSONValue* handle, int64_t search_value) override; - virtual int ArrayIndexOfFloat(YYJSONValue* handle, double search_value) override; - virtual bool ArraySort(YYJSONValue* handle, YYJSON_SORT_ORDER sort_mode) override; - - // ========== Value Operations ========== - virtual YYJSONValue* Pack(const char* format, IPackParamProvider* param_provider, char* error, size_t error_size) override; - virtual YYJSONValue* CreateBool(bool value) override; - virtual YYJSONValue* CreateFloat(double value) override; - virtual YYJSONValue* CreateInt(int value) override; - virtual YYJSONValue* CreateInt64(int64_t value) override; - virtual YYJSONValue* CreateNull() override; - virtual YYJSONValue* CreateString(const char* value) override; - virtual bool GetBool(YYJSONValue* handle, bool* out_value) override; - virtual bool GetFloat(YYJSONValue* handle, double* out_value) override; - virtual bool GetInt(YYJSONValue* handle, int* out_value) override; - virtual bool GetInt64(YYJSONValue* handle, int64_t* out_value) override; - virtual bool GetString(YYJSONValue* handle, const char** out_str, size_t* out_len) override; - - // ========== Pointer Operations ========== - virtual YYJSONValue* PtrGet(YYJSONValue* handle, const char* path, char* error, size_t error_size) override; - virtual bool PtrGetBool(YYJSONValue* handle, const char* path, bool* out_value, char* error, size_t error_size) override; - virtual bool PtrGetFloat(YYJSONValue* handle, const char* path, double* out_value, char* error, size_t error_size) override; - virtual bool PtrGetInt(YYJSONValue* handle, const char* path, int* out_value, char* error, size_t error_size) override; - virtual bool PtrGetInt64(YYJSONValue* handle, const char* path, int64_t* out_value, char* error, size_t error_size) override; - virtual bool PtrGetString(YYJSONValue* handle, const char* path, const char** out_str, size_t* out_len, char* error, size_t error_size) override; - virtual bool PtrGetIsNull(YYJSONValue* handle, const char* path, bool* out_is_null, char* error, size_t error_size) override; - virtual bool PtrGetLength(YYJSONValue* handle, const char* path, size_t* out_len, char* error, size_t error_size) override; - virtual bool PtrSet(YYJSONValue* handle, const char* path, YYJSONValue* value, char* error, size_t error_size) override; - virtual bool PtrSetBool(YYJSONValue* handle, const char* path, bool value, char* error, size_t error_size) override; - virtual bool PtrSetFloat(YYJSONValue* handle, const char* path, double value, char* error, size_t error_size) override; - virtual bool PtrSetInt(YYJSONValue* handle, const char* path, int value, char* error, size_t error_size) override; - virtual bool PtrSetInt64(YYJSONValue* handle, const char* path, int64_t value, char* error, size_t error_size) override; - virtual bool PtrSetString(YYJSONValue* handle, const char* path, const char* value, char* error, size_t error_size) override; - virtual bool PtrSetNull(YYJSONValue* handle, const char* path, char* error, size_t error_size) override; - virtual bool PtrAdd(YYJSONValue* handle, const char* path, YYJSONValue* value, char* error, size_t error_size) override; - virtual bool PtrAddBool(YYJSONValue* handle, const char* path, bool value, char* error, size_t error_size) override; - virtual bool PtrAddFloat(YYJSONValue* handle, const char* path, double value, char* error, size_t error_size) override; - virtual bool PtrAddInt(YYJSONValue* handle, const char* path, int value, char* error, size_t error_size) override; - virtual bool PtrAddInt64(YYJSONValue* handle, const char* path, int64_t value, char* error, size_t error_size) override; - virtual bool PtrAddString(YYJSONValue* handle, const char* path, const char* value, char* error, size_t error_size) override; - virtual bool PtrAddNull(YYJSONValue* handle, const char* path, char* error, size_t error_size) override; - virtual bool PtrRemove(YYJSONValue* handle, const char* path, char* error, size_t error_size) override; - virtual YYJSONValue* PtrTryGet(YYJSONValue* handle, const char* path) override; - virtual bool PtrTryGetBool(YYJSONValue* handle, const char* path, bool* out_value) override; - virtual bool PtrTryGetFloat(YYJSONValue* handle, const char* path, double* out_value) override; - virtual bool PtrTryGetInt(YYJSONValue* handle, const char* path, int* out_value) override; - virtual bool PtrTryGetInt64(YYJSONValue* handle, const char* path, int64_t* out_value) override; - virtual bool PtrTryGetString(YYJSONValue* handle, const char* path, const char** out_str, size_t* out_len) override; - - // ========== Iterator Operations ========== - virtual bool ObjectForeachNext(YYJSONValue* handle, const char** out_key, - size_t* out_key_len, YYJSONValue** out_value) override; - virtual bool ArrayForeachNext(YYJSONValue* handle, size_t* out_index, - YYJSONValue** out_value) override; - virtual bool ObjectForeachKeyNext(YYJSONValue* handle, const char** out_key, - size_t* out_key_len) override; - virtual bool ArrayForeachIndexNext(YYJSONValue* handle, size_t* out_index) override; - - // ========== Release Operations ========== - virtual void Release(YYJSONValue* value) override; - - // ========== Handle Type Operations ========== - virtual HandleType_t GetHandleType() override; - - // ========== Handle Operations ========== - virtual YYJSONValue* GetFromHandle(IPluginContext* pContext, Handle_t handle) override; - -private: - std::random_device m_randomDevice; - std::mt19937 m_randomGenerator; - - // Helper methods - static std::unique_ptr CreateWrapper(); - static std::shared_ptr WrapDocument(yyjson_mut_doc* doc); - static std::shared_ptr CopyDocument(yyjson_doc* doc); - static std::shared_ptr CreateDocument(); - static std::shared_ptr WrapImmutableDocument(yyjson_doc* doc); - - // Pack helper methods - static const char* SkipSeparators(const char* ptr); - static void SetPackError(char* error, size_t error_size, const char* fmt, ...); - static yyjson_mut_val* PackImpl(yyjson_mut_doc* doc, const char* format, - IPackParamProvider* provider, char* error, - size_t error_size, const char** out_end_ptr); -}; - -#endif // _INCLUDE_SM_YYJSON_YYJSONMANAGER_H_ \ No newline at end of file diff --git a/src/extension.cpp b/src/extension.cpp index f927f23..4e2b15d 100755 --- a/src/extension.cpp +++ b/src/extension.cpp @@ -1,57 +1,88 @@ #include "extension.h" -#include "YYJSONManager.h" +#include "JsonManager.h" -JsonExtension g_JsonExtension; -SMEXT_LINK(&g_JsonExtension); +JsonExtension g_JsonExt; +SMEXT_LINK(&g_JsonExt); -HandleType_t g_htJSON; -JSONHandler g_JSONHandler; -IYYJSONManager* g_pYYJSONManager; +HandleType_t g_JsonType; +HandleType_t g_ArrIterType; +HandleType_t g_ObjIterType; +JsonHandler g_JsonHandler; +ArrIterHandler g_ArrIterHandler; +ObjIterHandler g_ObjIterHandler; +IJsonManager* g_pJsonManager; bool JsonExtension::SDK_OnLoad(char* error, size_t maxlen, bool late) { - sharesys->AddNatives(myself, json_natives); - sharesys->RegisterLibrary(myself, "yyjson"); + sharesys->AddNatives(myself, g_JsonNatives); + sharesys->RegisterLibrary(myself, "json"); - HandleAccess haJSON; - handlesys->InitAccessDefaults(nullptr, &haJSON); - haJSON.access[HandleAccess_Read] = 0; - haJSON.access[HandleAccess_Delete] = 0; + HandleAccess haDefault; + handlesys->InitAccessDefaults(nullptr, &haDefault); + haDefault.access[HandleAccess_Read] = 0; + haDefault.access[HandleAccess_Delete] = 0; + + TypeAccess taDefault; + handlesys->InitAccessDefaults(&taDefault, nullptr); + taDefault.access[HTypeAccess_Create] = true; HandleError err; - g_htJSON = handlesys->CreateType("YYJSON", &g_JSONHandler, 0, nullptr, &haJSON, myself->GetIdentity(), &err); + g_JsonType = handlesys->CreateType("JSON", &g_JsonHandler, 0, &taDefault, &haDefault, myself->GetIdentity(), &err); + + if (!g_JsonType) { + snprintf(error, maxlen, "Failed to create JSON handle type (err: %d)", err); + return false; + } + + g_ArrIterType = handlesys->CreateType("JSONArrIter", &g_ArrIterHandler, 0, &taDefault, &haDefault, myself->GetIdentity(), &err); + if (!g_ArrIterType) { + snprintf(error, maxlen, "Failed to create JSONArrIter handle type (err: %d)", err); + return false; + } - if (!g_htJSON) { - snprintf(error, maxlen, "Failed to create YYJSON handle type (err: %d)", err); + g_ObjIterType = handlesys->CreateType("JSONObjIter", &g_ObjIterHandler, 0, &taDefault, &haDefault, myself->GetIdentity(), &err); + if (!g_ObjIterType) { + snprintf(error, maxlen, "Failed to create JSONObjIter handle type (err: %d)", err); return false; } - // Delete the existing instance if it exists - if (g_pYYJSONManager) { - delete g_pYYJSONManager; - g_pYYJSONManager = nullptr; + if (g_pJsonManager) { + delete g_pJsonManager; + g_pJsonManager = nullptr; } - g_pYYJSONManager = new YYJSONManager(); - if (!g_pYYJSONManager) { - snprintf(error, maxlen, "Failed to create YYJSONManager instance"); + g_pJsonManager = new(std::nothrow) JsonManager(); + if (!g_pJsonManager) { + snprintf(error, maxlen, "Failed to create JSON manager instance"); return false; } - return sharesys->AddInterface(myself, g_pYYJSONManager); + return sharesys->AddInterface(myself, g_pJsonManager); } void JsonExtension::SDK_OnUnload() { - handlesys->RemoveType(g_htJSON, myself->GetIdentity()); + handlesys->RemoveType(g_JsonType, myself->GetIdentity()); + handlesys->RemoveType(g_ArrIterType, myself->GetIdentity()); + handlesys->RemoveType(g_ObjIterType, myself->GetIdentity()); - if (g_pYYJSONManager) { - delete g_pYYJSONManager; - g_pYYJSONManager = nullptr; + if (g_pJsonManager) { + delete g_pJsonManager; + g_pJsonManager = nullptr; } } -void JSONHandler::OnHandleDestroy(HandleType_t type, void* object) +void JsonHandler::OnHandleDestroy(HandleType_t type, void* object) +{ + delete (JsonValue*)object; +} + +void ArrIterHandler::OnHandleDestroy(HandleType_t type, void* object) +{ + delete (JsonArrIter*)object; +} + +void ObjIterHandler::OnHandleDestroy(HandleType_t type, void* object) { - delete (YYJSONValue*)object; + delete (JsonObjIter*)object; } \ No newline at end of file diff --git a/src/extension.h b/src/extension.h index 0d1d503..3114905 100755 --- a/src/extension.h +++ b/src/extension.h @@ -2,9 +2,7 @@ #define _INCLUDE_SOURCEMOD_EXTENSION_PROPER_H_ #include "smsdk_ext.h" -#include -#include -#include "IYYJSONManager.h" +#include "IJsonManager.h" class JsonExtension : public SDKExtension { @@ -13,16 +11,32 @@ class JsonExtension : public SDKExtension virtual void SDK_OnUnload(); }; -class JSONHandler : public IHandleTypeDispatch +class JsonHandler : public IHandleTypeDispatch { public: void OnHandleDestroy(HandleType_t type, void *object); }; -extern JsonExtension g_JsonExtension; -extern HandleType_t g_htJSON; -extern JSONHandler g_JSONHandler; -extern const sp_nativeinfo_t json_natives[]; -extern IYYJSONManager* g_pYYJSONManager; +class ArrIterHandler : public IHandleTypeDispatch +{ +public: + void OnHandleDestroy(HandleType_t type, void *object); +}; + +class ObjIterHandler : public IHandleTypeDispatch +{ +public: + void OnHandleDestroy(HandleType_t type, void *object); +}; + +extern JsonExtension g_JsonExt; +extern HandleType_t g_JsonType; +extern HandleType_t g_ArrIterType; +extern HandleType_t g_ObjIterType; +extern JsonHandler g_JsonHandler; +extern ArrIterHandler g_ArrIterHandler; +extern ObjIterHandler g_ObjIterHandler; +extern const sp_nativeinfo_t g_JsonNatives[]; +extern IJsonManager* g_pJsonManager; #endif // _INCLUDE_SOURCEMOD_EXTENSION_PROPER_H_ \ No newline at end of file diff --git a/src/json_natives.cpp b/src/json_natives.cpp deleted file mode 100755 index b4774e6..0000000 --- a/src/json_natives.cpp +++ /dev/null @@ -1,2479 +0,0 @@ -#include "extension.h" -#include "YYJSONManager.h" - -class SourceModPackParamProvider : public IPackParamProvider -{ -private: - IPluginContext* m_pContext; - const cell_t* m_pParams; - unsigned int m_currentIndex; - -public: - SourceModPackParamProvider(IPluginContext* pContext, const cell_t* params, unsigned int startIndex) - : m_pContext(pContext), m_pParams(params), m_currentIndex(startIndex) {} - - bool GetNextString(const char** out_str) override { - char* str; - if (m_pContext->LocalToString(m_pParams[m_currentIndex++], &str) != SP_ERROR_NONE) { - return false; - } - *out_str = str; - return str != nullptr; - } - - bool GetNextInt(int* out_value) override { - cell_t* val; - if (m_pContext->LocalToPhysAddr(m_pParams[m_currentIndex++], &val) != SP_ERROR_NONE) { - return false; - } - *out_value = *val; - return true; - } - - bool GetNextFloat(float* out_value) override { - cell_t* val; - if (m_pContext->LocalToPhysAddr(m_pParams[m_currentIndex++], &val) != SP_ERROR_NONE) { - return false; - } - *out_value = sp_ctof(*val); - return true; - } - - bool GetNextBool(bool* out_value) override { - cell_t* val; - if (m_pContext->LocalToPhysAddr(m_pParams[m_currentIndex++], &val) != SP_ERROR_NONE) { - return false; - } - *out_value = (*val != 0); - return true; - } -}; - -/** - * Helper function: Create a SourceMod handle for YYJSONValue and return it directly - * Used by functions that return Handle_t - * - * @param pContext Plugin context - * @param pYYJSONValue JSON value to wrap (will be released on failure) - * @param error_context Descriptive context for error messages - * @return Handle on success, throws native error on failure - */ -static cell_t CreateAndReturnHandle(IPluginContext* pContext, YYJSONValue* pYYJSONValue, const char* error_context) -{ - if (!pYYJSONValue) { - return pContext->ThrowNativeError("Failed to create %s", error_context); - } - - HandleError err; - HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity()); - pYYJSONValue->m_handle = handlesys->CreateHandleEx(g_htJSON, pYYJSONValue, &sec, nullptr, &err); - - if (!pYYJSONValue->m_handle) { - g_pYYJSONManager->Release(pYYJSONValue); - return pContext->ThrowNativeError("Failed to create handle for %s (error code: %d)", error_context, err); - } - - return pYYJSONValue->m_handle; -} - -/** - * Helper function: Create a SourceMod handle for YYJSONValue and assign to output parameter - * Used by iterator functions (foreach) that assign handle via reference - * - * @param pContext Plugin context - * @param pYYJSONValue JSON value to wrap (will be released on failure) - * @param param_index Parameter index for output handle - * @param error_context Descriptive context for error messages - * @return true on success, false on failure (throws native error) - */ -static bool CreateAndAssignHandle(IPluginContext* pContext, YYJSONValue* pYYJSONValue, - cell_t param_index, const char* error_context) -{ - HandleError err; - HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity()); - pYYJSONValue->m_handle = handlesys->CreateHandleEx(g_htJSON, pYYJSONValue, &sec, nullptr, &err); - - if (!pYYJSONValue->m_handle) { - g_pYYJSONManager->Release(pYYJSONValue); - pContext->ThrowNativeError("Failed to create handle for %s (error code: %d)", error_context, err); - return false; - } - - cell_t* valHandle; - pContext->LocalToPhysAddr(param_index, &valHandle); - *valHandle = pYYJSONValue->m_handle; - return true; -} - -static cell_t json_val_pack(IPluginContext* pContext, const cell_t* params) { - char* fmt; - pContext->LocalToString(params[1], &fmt); - - SourceModPackParamProvider provider(pContext, params, 2); - - char error[YYJSON_PACK_ERROR_SIZE]; - YYJSONValue* pYYJSONValue = g_pYYJSONManager->Pack(fmt, &provider, error, sizeof(error)); - - if (!pYYJSONValue) { - return pContext->ThrowNativeError("Failed to pack JSON: %s", error); - } - - return CreateAndReturnHandle(pContext, pYYJSONValue, "packed JSON"); -} - -static cell_t json_doc_parse(IPluginContext* pContext, const cell_t* params) -{ - char* str; - pContext->LocalToString(params[1], &str); - - bool is_file = params[2]; - bool is_mutable_doc = params[3]; - yyjson_read_flag read_flg = static_cast(params[4]); - - char error[YYJSON_ERROR_BUFFER_SIZE]; - YYJSONValue* pYYJSONValue = g_pYYJSONManager->ParseJSON(str, is_file, is_mutable_doc, read_flg, error, sizeof(error)); - - if (!pYYJSONValue) { - return pContext->ThrowNativeError(error); - } - - return CreateAndReturnHandle(pContext, pYYJSONValue, "parsed JSON document"); -} - -static cell_t json_doc_equals(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle1 = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - YYJSONValue* handle2 = g_pYYJSONManager->GetFromHandle(pContext, params[2]); - - if (!handle1 || !handle2) return 0; - - return g_pYYJSONManager->Equals(handle1, handle2); -} - -static cell_t json_doc_copy_deep(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* targetDoc = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - YYJSONValue* sourceValue = g_pYYJSONManager->GetFromHandle(pContext, params[2]); - - if (!targetDoc || !sourceValue) return 0; - - YYJSONValue* pYYJSONValue = g_pYYJSONManager->DeepCopy(targetDoc, sourceValue); - - if (!pYYJSONValue) { - return pContext->ThrowNativeError("Failed to copy JSON value"); - } - - return CreateAndReturnHandle(pContext, pYYJSONValue, "copied JSON value"); -} - -static cell_t json_val_get_type_desc(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - const char* type_desc = g_pYYJSONManager->GetTypeDesc(handle); - pContext->StringToLocalUTF8(params[2], params[3], type_desc, nullptr); - - return 1; -} - -static cell_t json_obj_parse_str(IPluginContext* pContext, const cell_t* params) -{ - char* str; - pContext->LocalToString(params[1], &str); - yyjson_read_flag read_flg = static_cast(params[2]); - - char error[YYJSON_ERROR_BUFFER_SIZE]; - YYJSONValue* pYYJSONValue = g_pYYJSONManager->ObjectParseString(str, read_flg, error, sizeof(error)); - - if (!pYYJSONValue) { - return pContext->ThrowNativeError(error); - } - - return CreateAndReturnHandle(pContext, pYYJSONValue, "JSON object from string"); -} - -static cell_t json_obj_parse_file(IPluginContext* pContext, const cell_t* params) -{ - char* path; - pContext->LocalToString(params[1], &path); - yyjson_read_flag read_flg = static_cast(params[2]); - - char error[YYJSON_ERROR_BUFFER_SIZE]; - YYJSONValue* pYYJSONValue = g_pYYJSONManager->ObjectParseFile(path, read_flg, error, sizeof(error)); - - if (!pYYJSONValue) { - return pContext->ThrowNativeError(error); - } - - return CreateAndReturnHandle(pContext, pYYJSONValue, "JSON object from file"); -} - -static cell_t json_arr_parse_str(IPluginContext* pContext, const cell_t* params) -{ - char* str; - pContext->LocalToString(params[1], &str); - yyjson_read_flag read_flg = static_cast(params[2]); - - char error[YYJSON_ERROR_BUFFER_SIZE]; - YYJSONValue* pYYJSONValue = g_pYYJSONManager->ArrayParseString(str, read_flg, error, sizeof(error)); - - if (!pYYJSONValue) { - return pContext->ThrowNativeError(error); - } - - return CreateAndReturnHandle(pContext, pYYJSONValue, "JSON array from string"); -} - -static cell_t json_arr_parse_file(IPluginContext* pContext, const cell_t* params) -{ - char* path; - pContext->LocalToString(params[1], &path); - yyjson_read_flag read_flg = static_cast(params[2]); - - char error[YYJSON_ERROR_BUFFER_SIZE]; - YYJSONValue* pYYJSONValue = g_pYYJSONManager->ArrayParseFile(path, read_flg, error, sizeof(error)); - - if (!pYYJSONValue) { - return pContext->ThrowNativeError(error); - } - - return CreateAndReturnHandle(pContext, pYYJSONValue, "JSON array from file"); -} - -static cell_t json_arr_index_of_bool(IPluginContext *pContext, const cell_t *params) -{ - YYJSONValue *handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - bool searchValue = params[2]; - return g_pYYJSONManager->ArrayIndexOfBool(handle, searchValue); -} - -static cell_t json_arr_index_of_str(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - char* searchStr; - pContext->LocalToString(params[2], &searchStr); - - return g_pYYJSONManager->ArrayIndexOfString(handle, searchStr); -} - -static cell_t json_arr_index_of_int(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - int searchValue = params[2]; - return g_pYYJSONManager->ArrayIndexOfInt(handle, searchValue); -} - -static cell_t json_arr_index_of_integer64(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - char* searchStr; - pContext->LocalToString(params[2], &searchStr); - - char* endptr; - errno = 0; - long long searchValue = strtoll(searchStr, &endptr, 10); - - if (errno == ERANGE || *endptr != '\0') { - return pContext->ThrowNativeError("Invalid integer64 value: %s", searchStr); - } - - return g_pYYJSONManager->ArrayIndexOfInt64(handle, searchValue); -} - -static cell_t json_arr_index_of_float(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - double searchValue = static_cast(sp_ctof(params[2])); - return g_pYYJSONManager->ArrayIndexOfFloat(handle, searchValue); -} - -static cell_t json_val_get_type(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - return g_pYYJSONManager->GetType(handle); -} - -static cell_t json_val_get_subtype(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - return g_pYYJSONManager->GetSubtype(handle); -} - -static cell_t json_val_is_array(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - return g_pYYJSONManager->IsArray(handle); -} - -static cell_t json_val_is_object(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - return g_pYYJSONManager->IsObject(handle); -} - -static cell_t json_val_is_int(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - return g_pYYJSONManager->IsInt(handle); -} - -static cell_t json_val_is_uint(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - return g_pYYJSONManager->IsUint(handle); -} - -static cell_t json_val_is_sint(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - return g_pYYJSONManager->IsSint(handle); -} - -static cell_t json_val_is_num(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - return g_pYYJSONManager->IsNum(handle); -} - -static cell_t json_val_is_bool(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - return g_pYYJSONManager->IsBool(handle); -} - -static cell_t json_val_is_true(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - return g_pYYJSONManager->IsTrue(handle); -} - -static cell_t json_val_is_false(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - return g_pYYJSONManager->IsFalse(handle); -} - -static cell_t json_val_is_float(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - return g_pYYJSONManager->IsFloat(handle); -} - -static cell_t json_val_is_str(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - return g_pYYJSONManager->IsStr(handle); -} - -static cell_t json_val_is_null(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - return g_pYYJSONManager->IsNull(handle); -} - -static cell_t json_val_is_ctn(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - return g_pYYJSONManager->IsCtn(handle); -} - -static cell_t json_val_is_mutable(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - return g_pYYJSONManager->IsMutable(handle); -} - -static cell_t json_val_is_immutable(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - return g_pYYJSONManager->IsImmutable(handle); -} - -static cell_t json_obj_init(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* pYYJSONValue = g_pYYJSONManager->ObjectInit(); - return CreateAndReturnHandle(pContext, pYYJSONValue, "JSON object"); -} - -static cell_t json_obj_init_with_str(IPluginContext* pContext, const cell_t* params) -{ - cell_t* addr; - pContext->LocalToPhysAddr(params[1], &addr); - cell_t array_size = params[2]; - - if (array_size < 2) { - return pContext->ThrowNativeError("Array must contain at least one key-value pair"); - } - if (array_size % 2 != 0) { - return pContext->ThrowNativeError("Array must contain an even number of strings (got %d)", array_size); - } - - std::vector kv_pairs; - kv_pairs.reserve(array_size); - - for (cell_t i = 0; i < array_size; i += 2) { - char* key; - char* value; - - if (pContext->LocalToString(addr[i], &key) != SP_ERROR_NONE) { - return pContext->ThrowNativeError("Failed to read key at index %d", i); - } - if (!key || !key[0]) { - return pContext->ThrowNativeError("Empty key at index %d", i); - } - - if (pContext->LocalToString(addr[i + 1], &value) != SP_ERROR_NONE) { - return pContext->ThrowNativeError("Failed to read value at index %d", i + 1); - } - if (!value) { - return pContext->ThrowNativeError("Invalid value at index %d", i + 1); - } - - kv_pairs.push_back(key); - kv_pairs.push_back(value); - } - - YYJSONValue* pYYJSONValue = g_pYYJSONManager->ObjectInitWithStrings(kv_pairs.data(), array_size / 2); - - if (!pYYJSONValue) { - return pContext->ThrowNativeError("Failed to create JSON object from key-value pairs"); - } - - return CreateAndReturnHandle(pContext, pYYJSONValue, "JSON object from key-value pairs"); -} - -static cell_t json_val_create_bool(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* pYYJSONValue = g_pYYJSONManager->CreateBool(params[1]); - return CreateAndReturnHandle(pContext, pYYJSONValue, "JSON boolean value"); -} - -static cell_t json_val_create_float(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* pYYJSONValue = g_pYYJSONManager->CreateFloat(sp_ctof(params[1])); - return CreateAndReturnHandle(pContext, pYYJSONValue, "JSON float value"); -} - -static cell_t json_val_create_int(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* pYYJSONValue = g_pYYJSONManager->CreateInt(params[1]); - return CreateAndReturnHandle(pContext, pYYJSONValue, "JSON integer value"); -} - -static cell_t json_val_create_integer64(IPluginContext* pContext, const cell_t* params) -{ - char* value; - pContext->LocalToString(params[1], &value); - - char* endptr; - errno = 0; - long long num = strtoll(value, &endptr, 10); - - if (errno == ERANGE || *endptr != '\0') { - return pContext->ThrowNativeError("Invalid integer64 value: %s", value); - } - - YYJSONValue* pYYJSONValue = g_pYYJSONManager->CreateInt64(num); - - if (!pYYJSONValue) { - return pContext->ThrowNativeError("Failed to create JSON integer64 value"); - } - - return CreateAndReturnHandle(pContext, pYYJSONValue, "JSON integer64 value"); -} - -static cell_t json_val_create_str(IPluginContext* pContext, const cell_t* params) -{ - char* str; - pContext->LocalToString(params[1], &str); - - YYJSONValue* pYYJSONValue = g_pYYJSONManager->CreateString(str); - - if (!pYYJSONValue) { - return pContext->ThrowNativeError("Failed to create JSON string value"); - } - - return CreateAndReturnHandle(pContext, pYYJSONValue, "JSON string value"); -} - -static cell_t json_val_get_bool(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - bool value; - if (!g_pYYJSONManager->GetBool(handle, &value)) { - return pContext->ThrowNativeError("Type mismatch: expected boolean value"); - } - - return value; -} - -static cell_t json_val_get_float(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - double value; - if (!g_pYYJSONManager->GetFloat(handle, &value)) { - return pContext->ThrowNativeError("Type mismatch: expected float value"); - } - - return sp_ftoc(static_cast(value)); -} - -static cell_t json_val_get_int(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - int value; - if (!g_pYYJSONManager->GetInt(handle, &value)) { - return pContext->ThrowNativeError("Type mismatch: expected integer value"); - } - - return value; -} - -static cell_t json_val_get_integer64(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - int64_t value; - if (!g_pYYJSONManager->GetInt64(handle, &value)) { - return pContext->ThrowNativeError("Type mismatch: expected integer64 value"); - } - - char result[21]; - snprintf(result, sizeof(result), "%" PRId64, value); - pContext->StringToLocalUTF8(params[2], params[3], result, nullptr); - - return 1; -} - -static cell_t json_val_get_str(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - const char* str = nullptr; - size_t len = 0; - - if (!g_pYYJSONManager->GetString(handle, &str, &len)) { - return pContext->ThrowNativeError("Type mismatch: expected string value"); - } - - size_t maxlen = static_cast(params[3]); - - if (len + 1 > maxlen) { - return pContext->ThrowNativeError("Buffer is too small (need %d, have %d)", len + 1, maxlen); - } - - pContext->StringToLocalUTF8(params[2], maxlen, str, nullptr); - - return 1; -} - -static cell_t json_val_get_serialized_size(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - yyjson_write_flag write_flg = static_cast(params[2]); - size_t size = g_pYYJSONManager->GetSerializedSize(handle, write_flg); - - return static_cast(size); -} - -static cell_t json_val_get_read_size(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - size_t size = g_pYYJSONManager->GetReadSize(handle); - if (size == 0) return 0; - - return static_cast(size); -} - -static cell_t json_val_create_null(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* pYYJSONValue = g_pYYJSONManager->CreateNull(); - return CreateAndReturnHandle(pContext, pYYJSONValue, "JSON null value"); -} - -static cell_t json_arr_init(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* pYYJSONValue = g_pYYJSONManager->ArrayInit(); - return CreateAndReturnHandle(pContext, pYYJSONValue, "JSON array"); -} - -static cell_t json_arr_init_with_str(IPluginContext* pContext, const cell_t* params) -{ - cell_t* addr; - pContext->LocalToPhysAddr(params[1], &addr); - cell_t array_size = params[2]; - - std::vector strs; - strs.reserve(array_size); - - for (cell_t i = 0; i < array_size; i++) { - char* str; - pContext->LocalToString(addr[i], &str); - strs.push_back(str); - } - - YYJSONValue* pYYJSONValue = g_pYYJSONManager->ArrayInitWithStrings(strs.data(), strs.size()); - - if (!pYYJSONValue) { - return pContext->ThrowNativeError("Failed to create JSON array from strings"); - } - - return CreateAndReturnHandle(pContext, pYYJSONValue, "JSON array from strings"); -} - -static cell_t json_arr_get_size(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - size_t size = g_pYYJSONManager->ArrayGetSize(handle); - return static_cast(size); -} - -static cell_t json_arr_get_val(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - size_t index = static_cast(params[2]); - - YYJSONValue* pYYJSONValue = g_pYYJSONManager->ArrayGet(handle, index); - - if (!pYYJSONValue) { - return pContext->ThrowNativeError("Index %d is out of bounds", index); - } - - return CreateAndReturnHandle(pContext, pYYJSONValue, "JSON array value"); -} - -static cell_t json_arr_get_first(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - YYJSONValue* pYYJSONValue = g_pYYJSONManager->ArrayGetFirst(handle); - - if (!pYYJSONValue) { - return pContext->ThrowNativeError("Array is empty"); - } - - return CreateAndReturnHandle(pContext, pYYJSONValue, "first JSON array value"); -} - -static cell_t json_arr_get_last(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - YYJSONValue* pYYJSONValue = g_pYYJSONManager->ArrayGetLast(handle); - - if (!pYYJSONValue) { - return pContext->ThrowNativeError("Array is empty"); - } - - return CreateAndReturnHandle(pContext, pYYJSONValue, "last JSON array value"); -} - -static cell_t json_arr_get_bool(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - size_t index = static_cast(params[2]); - - bool value; - if (!g_pYYJSONManager->ArrayGetBool(handle, index, &value)) { - return pContext->ThrowNativeError("Failed to get boolean at index %d", index); - } - - return value; -} - -static cell_t json_arr_get_float(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - size_t index = static_cast(params[2]); - - double value; - if (!g_pYYJSONManager->ArrayGetFloat(handle, index, &value)) { - return pContext->ThrowNativeError("Failed to get float at index %d", index); - } - - return sp_ftoc(static_cast(value)); -} - -static cell_t json_arr_get_integer(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - size_t index = static_cast(params[2]); - - int value; - if (!g_pYYJSONManager->ArrayGetInt(handle, index, &value)) { - return pContext->ThrowNativeError("Failed to get integer at index %d", index); - } - - return value; -} - -static cell_t json_arr_get_integer64(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - size_t index = static_cast(params[2]); - int64_t value; - - if (!g_pYYJSONManager->ArrayGetInt64(handle, index, &value)) { - return pContext->ThrowNativeError("Failed to get integer64 at index %d", index); - } - - char result[21]; - snprintf(result, sizeof(result), "%" PRId64, value); - pContext->StringToLocalUTF8(params[3], params[4], result, nullptr); - - return 1; -} - -static cell_t json_arr_get_str(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - size_t index = static_cast(params[2]); - const char* str = nullptr; - size_t len = 0; - - if (!g_pYYJSONManager->ArrayGetString(handle, index, &str, &len)) { - return pContext->ThrowNativeError("Failed to get string at index %d", index); - } - - size_t maxlen = static_cast(params[4]); - if (len + 1 > maxlen) { - return pContext->ThrowNativeError("Buffer is too small (need %d, have %d)", len + 1, maxlen); - } - - pContext->StringToLocalUTF8(params[3], maxlen, str, nullptr); - - return 1; -} - -static cell_t json_arr_is_null(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - size_t index = static_cast(params[2]); - - return g_pYYJSONManager->ArrayIsNull(handle, index); -} - -static cell_t json_arr_replace_val(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle1 = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - YYJSONValue* handle2 = g_pYYJSONManager->GetFromHandle(pContext, params[3]); - - if (!handle1 || !handle2) return 0; - - if (!handle1->IsMutable()) { - return pContext->ThrowNativeError("Cannot replace value in an immutable JSON array"); - } - - size_t index = static_cast(params[2]); - return g_pYYJSONManager->ArrayReplace(handle1, index, handle2); -} - -static cell_t json_arr_replace_bool(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot replace value in an immutable JSON array"); - } - - size_t index = static_cast(params[2]); - return g_pYYJSONManager->ArrayReplaceBool(handle, index, params[3]); -} - -static cell_t json_arr_replace_float(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot replace value in an immutable JSON array"); - } - - size_t index = static_cast(params[2]); - return g_pYYJSONManager->ArrayReplaceFloat(handle, index, sp_ctof(params[3])); -} - -static cell_t json_arr_replace_integer(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot replace value in an immutable JSON array"); - } - - size_t index = static_cast(params[2]); - return g_pYYJSONManager->ArrayReplaceInt(handle, index, params[3]); -} - -static cell_t json_arr_replace_integer64(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot replace value in an immutable JSON array"); - } - - char* value; - pContext->LocalToString(params[3], &value); - - char* endptr; - errno = 0; - long long num = strtoll(value, &endptr, 10); - - if (errno == ERANGE || *endptr != '\0') { - return pContext->ThrowNativeError("Invalid integer64 value: %s", value); - } - - size_t index = static_cast(params[2]); - return g_pYYJSONManager->ArrayReplaceInt64(handle, index, num); -} - -static cell_t json_arr_replace_null(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot replace value in an immutable JSON array"); - } - - size_t index = static_cast(params[2]); - return g_pYYJSONManager->ArrayReplaceNull(handle, index); -} - -static cell_t json_arr_replace_str(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot replace value in an immutable JSON array"); - } - - char* val; - pContext->LocalToString(params[3], &val); - - size_t index = static_cast(params[2]); - return g_pYYJSONManager->ArrayReplaceString(handle, index, val); -} - -static cell_t json_arr_append_val(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle1 = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - YYJSONValue* handle2 = g_pYYJSONManager->GetFromHandle(pContext, params[2]); - - if (!handle1 || !handle2) return 0; - - if (!handle1->IsMutable()) { - return pContext->ThrowNativeError("Cannot append value to an immutable JSON array"); - } - - return g_pYYJSONManager->ArrayAppend(handle1, handle2); -} - -static cell_t json_arr_append_bool(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot append value to an immutable JSON array"); - } - - return g_pYYJSONManager->ArrayAppendBool(handle, params[2]); -} - -static cell_t json_arr_append_float(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot append value to an immutable JSON array"); - } - - return g_pYYJSONManager->ArrayAppendFloat(handle, sp_ctof(params[2])); -} - -static cell_t json_arr_append_int(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot append value to an immutable JSON array"); - } - - return g_pYYJSONManager->ArrayAppendInt(handle, params[2]); -} - -static cell_t json_arr_append_integer64(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot append value to an immutable JSON array"); - } - - char* value; - pContext->LocalToString(params[2], &value); - - char* endptr; - errno = 0; - long long num = strtoll(value, &endptr, 10); - - if (errno == ERANGE || *endptr != '\0') { - return pContext->ThrowNativeError("Invalid integer64 value: %s", value); - } - - return g_pYYJSONManager->ArrayAppendInt64(handle, num); -} - -static cell_t json_arr_append_null(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot append value to an immutable JSON array"); - } - - return g_pYYJSONManager->ArrayAppendNull(handle); -} - -static cell_t json_arr_append_str(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot append value to an immutable JSON array"); - } - - char* str; - pContext->LocalToString(params[2], &str); - - return g_pYYJSONManager->ArrayAppendString(handle, str); -} - -static cell_t json_arr_remove(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot remove value from an immutable JSON array"); - } - - size_t index = static_cast(params[2]); - return g_pYYJSONManager->ArrayRemove(handle, index); -} - -static cell_t json_arr_remove_first(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot remove value from an immutable JSON array"); - } - - return g_pYYJSONManager->ArrayRemoveFirst(handle); -} - -static cell_t json_arr_remove_last(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot remove value from an immutable JSON array"); - } - - return g_pYYJSONManager->ArrayRemoveLast(handle); -} - -static cell_t json_arr_remove_range(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot remove value from an immutable JSON array"); - } - - size_t start_index = static_cast(params[2]); - size_t end_index = static_cast(params[3]); - - return g_pYYJSONManager->ArrayRemoveRange(handle, start_index, end_index); -} - -static cell_t json_arr_clear(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot clear an immutable JSON array"); - } - - return g_pYYJSONManager->ArrayClear(handle); -} - -static cell_t json_doc_write_to_str(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - size_t buffer_size = static_cast(params[3]); - yyjson_write_flag write_flg = static_cast(params[4]); - - char* temp_buffer = (char*)malloc(buffer_size); - if (!temp_buffer) { - return pContext->ThrowNativeError("Failed to allocate buffer"); - } - - size_t output_len = 0; - if (!g_pYYJSONManager->WriteToString(handle, temp_buffer, buffer_size, write_flg, &output_len)) { - free(temp_buffer); - return pContext->ThrowNativeError("Buffer too small or write failed"); - } - - pContext->StringToLocalUTF8(params[2], buffer_size, temp_buffer, nullptr); - free(temp_buffer); - return static_cast(output_len); -} - -static cell_t json_doc_write_to_file(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - char* path; - pContext->LocalToString(params[2], &path); - yyjson_write_flag write_flg = static_cast(params[3]); - - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->WriteToFile(handle, path, write_flg, error, sizeof(error))) { - return pContext->ThrowNativeError(error); - } - - return true; -} - -static cell_t json_obj_get_size(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - size_t size = g_pYYJSONManager->ObjectGetSize(handle); - return static_cast(size); -} - -static cell_t json_obj_get_key(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - size_t index = static_cast(params[2]); - const char* key = nullptr; - - if (!g_pYYJSONManager->ObjectGetKey(handle, index, &key)) { - return pContext->ThrowNativeError("Index %d is out of bounds", index); - } - - pContext->StringToLocalUTF8(params[3], params[4], key, nullptr); - return 1; -} - -static cell_t json_obj_get_val_at(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - size_t index = static_cast(params[2]); - - YYJSONValue* pYYJSONValue = g_pYYJSONManager->ObjectGetValueAt(handle, index); - - if (!pYYJSONValue) { - return pContext->ThrowNativeError("Index %d is out of bounds", index); - } - - return CreateAndReturnHandle(pContext, pYYJSONValue, "JSON object value"); -} - -static cell_t json_obj_get_val(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - char* key; - pContext->LocalToString(params[2], &key); - - YYJSONValue* pYYJSONValue = g_pYYJSONManager->ObjectGet(handle, key); - - if (!pYYJSONValue) { - return pContext->ThrowNativeError("Key not found: %s", key); - } - - return CreateAndReturnHandle(pContext, pYYJSONValue, "JSON object value"); -} - -static cell_t json_obj_get_bool(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - char* key; - pContext->LocalToString(params[2], &key); - - bool value; - if (!g_pYYJSONManager->ObjectGetBool(handle, key, &value)) { - return pContext->ThrowNativeError("Failed to get boolean for key '%s'", key); - } - - return value; -} - -static cell_t json_obj_get_float(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - char* key; - pContext->LocalToString(params[2], &key); - - double value; - if (!g_pYYJSONManager->ObjectGetFloat(handle, key, &value)) { - return pContext->ThrowNativeError("Failed to get float for key '%s'", key); - } - - return sp_ftoc(static_cast(value)); -} - -static cell_t json_obj_get_int(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - char* key; - pContext->LocalToString(params[2], &key); - - int value; - if (!g_pYYJSONManager->ObjectGetInt(handle, key, &value)) { - return pContext->ThrowNativeError("Failed to get integer for key '%s'", key); - } - - return value; -} - -static cell_t json_obj_get_integer64(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - char* key; - pContext->LocalToString(params[2], &key); - - int64_t value; - if (!g_pYYJSONManager->ObjectGetInt64(handle, key, &value)) { - return pContext->ThrowNativeError("Failed to get integer64 for key '%s'", key); - } - - char result[21]; - snprintf(result, sizeof(result), "%" PRId64, value); - pContext->StringToLocalUTF8(params[3], params[4], result, nullptr); - - return 1; -} - -static cell_t json_obj_get_str(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - char* key; - pContext->LocalToString(params[2], &key); - - const char* str = nullptr; - size_t len = 0; - if (!g_pYYJSONManager->ObjectGetString(handle, key, &str, &len)) { - return pContext->ThrowNativeError("Failed to get string for key '%s'", key); - } - - size_t maxlen = static_cast(params[4]); - if (len + 1 > maxlen) { - return pContext->ThrowNativeError("Buffer is too small (need %d, have %d)", len + 1, maxlen); - } - - pContext->StringToLocalUTF8(params[3], maxlen, str, nullptr); - - return 1; -} - -static cell_t json_obj_clear(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot clear an immutable JSON object"); - } - - return g_pYYJSONManager->ObjectClear(handle); -} - -static cell_t json_obj_is_null(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - char* key; - pContext->LocalToString(params[2], &key); - - bool is_null = false; - if (!g_pYYJSONManager->ObjectIsNull(handle, key, &is_null)) { - return pContext->ThrowNativeError("Key not found: %s", key); - } - - return is_null; -} - -static cell_t json_obj_has_key(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - char* key; - pContext->LocalToString(params[2], &key); - - bool ptr_use = params[3]; - - return g_pYYJSONManager->ObjectHasKey(handle, key, ptr_use); -} - -static cell_t json_obj_rename_key(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot rename key in an immutable JSON object"); - } - - char* old_key; - pContext->LocalToString(params[2], &old_key); - - char* new_key; - pContext->LocalToString(params[3], &new_key); - - bool allow_duplicate = params[4]; - - if (!g_pYYJSONManager->ObjectRenameKey(handle, old_key, new_key, allow_duplicate)) { - return pContext->ThrowNativeError("Failed to rename key from '%s' to '%s'", old_key, new_key); - } - - return true; -} - -static cell_t json_obj_set_val(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle1 = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - YYJSONValue* handle2 = g_pYYJSONManager->GetFromHandle(pContext, params[3]); - - if (!handle1 || !handle2) return 0; - - if (!handle1->IsMutable()) { - return pContext->ThrowNativeError("Cannot set value in an immutable JSON object"); - } - - char* key; - pContext->LocalToString(params[2], &key); - - return g_pYYJSONManager->ObjectSet(handle1, key, handle2); -} - -static cell_t json_obj_set_bool(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot set value in an immutable JSON object"); - } - - char* key; - pContext->LocalToString(params[2], &key); - - return g_pYYJSONManager->ObjectSetBool(handle, key, params[3]); -} - -static cell_t json_obj_set_float(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot set value in an immutable JSON object"); - } - - char* key; - pContext->LocalToString(params[2], &key); - - return g_pYYJSONManager->ObjectSetFloat(handle, key, sp_ctof(params[3])); -} - -static cell_t json_obj_set_int(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot set value in an immutable JSON object"); - } - - char* key; - pContext->LocalToString(params[2], &key); - - return g_pYYJSONManager->ObjectSetInt(handle, key, params[3]); -} - -static cell_t json_obj_set_integer64(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot set value in an immutable JSON object"); - } - - char* key, * value; - pContext->LocalToString(params[2], &key); - pContext->LocalToString(params[3], &value); - - char* endptr; - errno = 0; - long long num = strtoll(value, &endptr, 10); - - if (errno == ERANGE || *endptr != '\0') { - return pContext->ThrowNativeError("Invalid integer64 value: %s", value); - } - - return g_pYYJSONManager->ObjectSetInt64(handle, key, num); -} - -static cell_t json_obj_set_null(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot set value in an immutable JSON object"); - } - - char* key; - pContext->LocalToString(params[2], &key); - - return g_pYYJSONManager->ObjectSetNull(handle, key); -} - -static cell_t json_obj_set_str(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot set value in an immutable JSON object"); - } - - char* key, * value; - pContext->LocalToString(params[2], &key); - pContext->LocalToString(params[3], &value); - - return g_pYYJSONManager->ObjectSetString(handle, key, value); -} - -static cell_t json_obj_remove(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot remove value from an immutable JSON object"); - } - - char* key; - pContext->LocalToString(params[2], &key); - - return g_pYYJSONManager->ObjectRemove(handle, key); -} - -static cell_t json_ptr_get_val(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - char* path; - pContext->LocalToString(params[2], &path); - - char error[YYJSON_ERROR_BUFFER_SIZE]; - YYJSONValue* pYYJSONValue = g_pYYJSONManager->PtrGet(handle, path, error, sizeof(error)); - - if (!pYYJSONValue) { - return pContext->ThrowNativeError("%s", error); - } - - return CreateAndReturnHandle(pContext, pYYJSONValue, "JSON pointer value"); -} - -static cell_t json_ptr_get_bool(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - char* path; - pContext->LocalToString(params[2], &path); - - bool value; - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->PtrGetBool(handle, path, &value, error, sizeof(error))) { - return pContext->ThrowNativeError("%s", error); - } - - return value; -} - -static cell_t json_ptr_get_float(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - char* path; - pContext->LocalToString(params[2], &path); - - double value; - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->PtrGetFloat(handle, path, &value, error, sizeof(error))) { - return pContext->ThrowNativeError("%s", error); - } - - return sp_ftoc(static_cast(value)); -} - -static cell_t json_ptr_get_int(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - char* path; - pContext->LocalToString(params[2], &path); - - int value; - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->PtrGetInt(handle, path, &value, error, sizeof(error))) { - return pContext->ThrowNativeError("%s", error); - } - - return value; -} - -static cell_t json_ptr_get_integer64(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - char* path; - pContext->LocalToString(params[2], &path); - - int64_t value; - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->PtrGetInt64(handle, path, &value, error, sizeof(error))) { - return pContext->ThrowNativeError("%s", error); - } - - char result[21]; - snprintf(result, sizeof(result), "%" PRId64, value); - pContext->StringToLocalUTF8(params[3], params[4], result, nullptr); - - return 1; -} - -static cell_t json_ptr_get_str(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - char* path; - pContext->LocalToString(params[2], &path); - - const char* str = nullptr; - size_t len = 0; - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->PtrGetString(handle, path, &str, &len, error, sizeof(error))) { - return pContext->ThrowNativeError("%s", error); - } - - size_t maxlen = static_cast(params[4]); - if (len + 1 > maxlen) { - return pContext->ThrowNativeError("Buffer is too small (need %d, have %d)", len + 1, maxlen); - } - - pContext->StringToLocalUTF8(params[3], maxlen, str, nullptr); - - return 1; -} - -static cell_t json_ptr_get_is_null(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - char* path; - pContext->LocalToString(params[2], &path); - - bool is_null; - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->PtrGetIsNull(handle, path, &is_null, error, sizeof(error))) { - return pContext->ThrowNativeError("%s", error); - } - - return is_null; -} - -static cell_t json_ptr_get_length(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - char* path; - pContext->LocalToString(params[2], &path); - - size_t len; - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->PtrGetLength(handle, path, &len, error, sizeof(error))) { - return pContext->ThrowNativeError("%s", error); - } - - return static_cast(len); -} - -static cell_t json_ptr_set_val(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle1 = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - YYJSONValue* handle2 = g_pYYJSONManager->GetFromHandle(pContext, params[3]); - - if (!handle1 || !handle2) return 0; - - if (!handle1->IsMutable()) { - return pContext->ThrowNativeError("Cannot set value in an immutable JSON document using pointer"); - } - - char* path; - pContext->LocalToString(params[2], &path); - - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->PtrSet(handle1, path, handle2, error, sizeof(error))) { - return pContext->ThrowNativeError("%s", error); - } - - return true; -} - -static cell_t json_ptr_set_bool(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot set value in an immutable JSON document using pointer"); - } - - char* path; - pContext->LocalToString(params[2], &path); - - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->PtrSetBool(handle, path, params[3], error, sizeof(error))) { - return pContext->ThrowNativeError("%s", error); - } - - return true; -} - -static cell_t json_ptr_set_float(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot set value in an immutable JSON document using pointer"); - } - - char* path; - pContext->LocalToString(params[2], &path); - - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->PtrSetFloat(handle, path, sp_ctof(params[3]), error, sizeof(error))) { - return pContext->ThrowNativeError("%s", error); - } - - return true; -} - -static cell_t json_ptr_set_int(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot set value in an immutable JSON document using pointer"); - } - - char* path; - pContext->LocalToString(params[2], &path); - - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->PtrSetInt(handle, path, params[3], error, sizeof(error))) { - return pContext->ThrowNativeError("%s", error); - } - - return true; -} - -static cell_t json_ptr_set_integer64(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot set value in an immutable JSON document using pointer"); - } - - char* path, * value; - pContext->LocalToString(params[2], &path); - pContext->LocalToString(params[3], &value); - - char* endptr; - errno = 0; - long long num = strtoll(value, &endptr, 10); - - if (errno == ERANGE || *endptr != '\0') { - return pContext->ThrowNativeError("Invalid integer64 value: %s", value); - } - - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->PtrSetInt64(handle, path, num, error, sizeof(error))) { - return pContext->ThrowNativeError("%s", error); - } - - return true; -} - -static cell_t json_ptr_set_str(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot set value in an immutable JSON document using pointer"); - } - - char* path, * str; - pContext->LocalToString(params[2], &path); - pContext->LocalToString(params[3], &str); - - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->PtrSetString(handle, path, str, error, sizeof(error))) { - return pContext->ThrowNativeError("%s", error); - } - - return true; -} - -static cell_t json_ptr_set_null(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot set value in an immutable JSON document using pointer"); - } - - char* path; - pContext->LocalToString(params[2], &path); - - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->PtrSetNull(handle, path, error, sizeof(error))) { - return pContext->ThrowNativeError("%s", error); - } - - return true; -} - -static cell_t json_ptr_add_val(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle1 = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - YYJSONValue* handle2 = g_pYYJSONManager->GetFromHandle(pContext, params[3]); - - if (!handle1 || !handle2) return 0; - - if (!handle1->IsMutable()) { - return pContext->ThrowNativeError("Cannot add value to an immutable JSON document using pointer"); - } - - char* path; - pContext->LocalToString(params[2], &path); - - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->PtrAdd(handle1, path, handle2, error, sizeof(error))) { - return pContext->ThrowNativeError("%s", error); - } - - return true; -} - -static cell_t json_ptr_add_bool(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot add value to an immutable JSON document using pointer"); - } - - char* path; - pContext->LocalToString(params[2], &path); - - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->PtrAddBool(handle, path, params[3], error, sizeof(error))) { - return pContext->ThrowNativeError("%s", error); - } - - return true; -} - -static cell_t json_ptr_add_float(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot add value to an immutable JSON document using pointer"); - } - - char* path; - pContext->LocalToString(params[2], &path); - - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->PtrAddFloat(handle, path, sp_ctof(params[3]), error, sizeof(error))) { - return pContext->ThrowNativeError("%s", error); - } - - return true; -} - -static cell_t json_ptr_add_int(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot add value to an immutable JSON document using pointer"); - } - - char* path; - pContext->LocalToString(params[2], &path); - - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->PtrAddInt(handle, path, params[3], error, sizeof(error))) { - return pContext->ThrowNativeError("%s", error); - } - - return true; -} - -static cell_t json_ptr_add_integer64(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot add value to an immutable JSON document using pointer"); - } - - char* path, * value; - pContext->LocalToString(params[2], &path); - pContext->LocalToString(params[3], &value); - - char* endptr; - errno = 0; - long long num = strtoll(value, &endptr, 10); - - if (errno == ERANGE || *endptr != '\0') { - return pContext->ThrowNativeError("Invalid integer64 value: %s", value); - } - - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->PtrAddInt64(handle, path, num, error, sizeof(error))) { - return pContext->ThrowNativeError("%s", error); - } - - return true; -} - -static cell_t json_ptr_add_str(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot add value to an immutable JSON document using pointer"); - } - - char* path, * str; - pContext->LocalToString(params[2], &path); - pContext->LocalToString(params[3], &str); - - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->PtrAddString(handle, path, str, error, sizeof(error))) { - return pContext->ThrowNativeError("%s", error); - } - - return true; -} - -static cell_t json_ptr_add_null(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot add value to an immutable JSON document using pointer"); - } - - char* path; - pContext->LocalToString(params[2], &path); - - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->PtrAddNull(handle, path, error, sizeof(error))) { - return pContext->ThrowNativeError("%s", error); - } - - return true; -} - -static cell_t json_ptr_remove_val(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot remove value from an immutable JSON document using pointer"); - } - - char* path; - pContext->LocalToString(params[2], &path); - - char error[YYJSON_ERROR_BUFFER_SIZE]; - if (!g_pYYJSONManager->PtrRemove(handle, path, error, sizeof(error))) { - return pContext->ThrowNativeError("%s", error); - } - - return true; -} - -static cell_t json_ptr_try_get_val(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - char* path; - pContext->LocalToString(params[2], &path); - - YYJSONValue* pYYJSONValue = g_pYYJSONManager->PtrTryGet(handle, path); - - if (!pYYJSONValue) { - return 0; - } - - return CreateAndAssignHandle(pContext, pYYJSONValue, params[3], "JSON pointer value"); -} - -static cell_t json_ptr_try_get_bool(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - if (!handle) return 0; - - char* path; - pContext->LocalToString(params[2], &path); - - bool value; - if (!g_pYYJSONManager->PtrTryGetBool(handle, path, &value)) { - return 0; - } - - cell_t* addr; - pContext->LocalToPhysAddr(params[3], &addr); - *addr = value; - - return 1; -} - -static cell_t json_ptr_try_get_float(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - if (!handle) return 0; - - char* path; - pContext->LocalToString(params[2], &path); - - double value; - if (!g_pYYJSONManager->PtrTryGetFloat(handle, path, &value)) { - return 0; - } - - cell_t* addr; - pContext->LocalToPhysAddr(params[3], &addr); - *addr = sp_ftoc(static_cast(value)); - - return 1; -} - -static cell_t json_ptr_try_get_int(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - if (!handle) return 0; - - char* path; - pContext->LocalToString(params[2], &path); - - int value; - if (!g_pYYJSONManager->PtrTryGetInt(handle, path, &value)) { - return 0; - } - - cell_t* addr; - pContext->LocalToPhysAddr(params[3], &addr); - *addr = value; - - return 1; -} - -static cell_t json_ptr_try_get_integer64(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - if (!handle) return 0; - - char* path; - pContext->LocalToString(params[2], &path); - - int64_t value; - if (!g_pYYJSONManager->PtrTryGetInt64(handle, path, &value)) { - return 0; - } - - size_t maxlen = static_cast(params[4]); - char result[21]; - snprintf(result, sizeof(result), "%" PRId64, value); - pContext->StringToLocalUTF8(params[3], maxlen, result, nullptr); - return 1; -} - -static cell_t json_ptr_try_get_str(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - if (!handle) return 0; - - char* path; - pContext->LocalToString(params[2], &path); - - const char* str = nullptr; - size_t len = 0; - - if (!g_pYYJSONManager->PtrTryGetString(handle, path, &str, &len)) { - return 0; - } - - size_t maxlen = static_cast(params[4]); - if (len + 1 > maxlen) { - return 0; - } - - pContext->StringToLocalUTF8(params[3], maxlen, str, nullptr); - - return 1; -} - -static cell_t json_obj_foreach(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - if (!handle) return 0; - - const char* key = nullptr; - YYJSONValue* pYYJSONValue = nullptr; - - if (!g_pYYJSONManager->ObjectForeachNext(handle, &key, nullptr, &pYYJSONValue)) { - return false; - } - - pContext->StringToLocalUTF8(params[2], params[3], key, nullptr); - - return CreateAndAssignHandle(pContext, pYYJSONValue, params[4], "JSON object value"); -} - -static cell_t json_arr_foreach(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - if (!handle) return 0; - - size_t index = 0; - YYJSONValue* pYYJSONValue = nullptr; - - if (!g_pYYJSONManager->ArrayForeachNext(handle, &index, &pYYJSONValue)) { - return false; - } - - cell_t* indexPtr; - pContext->LocalToPhysAddr(params[2], &indexPtr); - *indexPtr = static_cast(index); - - return CreateAndAssignHandle(pContext, pYYJSONValue, params[3], "JSON array value"); -} - -static cell_t json_obj_foreach_key(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - if (!handle) return 0; - - const char* key = nullptr; - - if (!g_pYYJSONManager->ObjectForeachKeyNext(handle, &key, nullptr)) { - return false; - } - - pContext->StringToLocalUTF8(params[2], params[3], key, nullptr); - - return true; -} - -static cell_t json_arr_foreach_index(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - if (!handle) return 0; - - size_t index = 0; - - if (!g_pYYJSONManager->ArrayForeachIndexNext(handle, &index)) { - return false; - } - - cell_t* indexPtr; - pContext->LocalToPhysAddr(params[2], &indexPtr); - *indexPtr = static_cast(index); - - return true; -} - -static cell_t json_arr_sort(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot sort an immutable JSON array"); - } - - YYJSON_SORT_ORDER sort_mode = static_cast(params[2]); - if (sort_mode < YYJSON_SORT_ASC || sort_mode > YYJSON_SORT_RANDOM) { - return pContext->ThrowNativeError("Invalid sort mode: %d (expected 0=ascending, 1=descending, 2=random)", sort_mode); - } - - return g_pYYJSONManager->ArraySort(handle, sort_mode); -} - -static cell_t json_obj_sort(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Cannot sort an immutable JSON object"); - } - - YYJSON_SORT_ORDER sort_mode = static_cast(params[2]); - if (sort_mode < YYJSON_SORT_ASC || sort_mode > YYJSON_SORT_RANDOM) { - return pContext->ThrowNativeError("Invalid sort mode: %d (expected 0=ascending, 1=descending, 2=random)", sort_mode); - } - - return g_pYYJSONManager->ObjectSort(handle, sort_mode); -} - -static cell_t json_doc_to_mutable(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (handle->IsMutable()) { - return pContext->ThrowNativeError("Document is already mutable"); - } - - YYJSONValue* pYYJSONValue = g_pYYJSONManager->ToMutable(handle); - - if (!pYYJSONValue) { - return pContext->ThrowNativeError("Failed to convert to mutable document"); - } - - return CreateAndReturnHandle(pContext, pYYJSONValue, "mutable JSON document"); -} - -static cell_t json_doc_to_immutable(IPluginContext* pContext, const cell_t* params) -{ - YYJSONValue* handle = g_pYYJSONManager->GetFromHandle(pContext, params[1]); - - if (!handle) return 0; - - if (!handle->IsMutable()) { - return pContext->ThrowNativeError("Document is already immutable"); - } - - YYJSONValue* pYYJSONValue = g_pYYJSONManager->ToImmutable(handle); - - if (!pYYJSONValue) { - return pContext->ThrowNativeError("Failed to convert to immutable document"); - } - - return CreateAndReturnHandle(pContext, pYYJSONValue, "immutable JSON document"); -} - -const sp_nativeinfo_t json_natives[] = -{ - // JSONObject - {"YYJSONObject.YYJSONObject", json_obj_init}, - {"YYJSONObject.FromStrings", json_obj_init_with_str}, - {"YYJSONObject.Size.get", json_obj_get_size}, - {"YYJSONObject.Get", json_obj_get_val}, - {"YYJSONObject.GetBool", json_obj_get_bool}, - {"YYJSONObject.GetFloat", json_obj_get_float}, - {"YYJSONObject.GetInt", json_obj_get_int}, - {"YYJSONObject.GetInt64", json_obj_get_integer64}, - {"YYJSONObject.GetString", json_obj_get_str}, - {"YYJSONObject.IsNull", json_obj_is_null}, - {"YYJSONObject.GetKey", json_obj_get_key}, - {"YYJSONObject.GetValueAt", json_obj_get_val_at}, - {"YYJSONObject.HasKey", json_obj_has_key}, - {"YYJSONObject.RenameKey", json_obj_rename_key}, - {"YYJSONObject.Set", json_obj_set_val}, - {"YYJSONObject.SetBool", json_obj_set_bool}, - {"YYJSONObject.SetFloat", json_obj_set_float}, - {"YYJSONObject.SetInt", json_obj_set_int}, - {"YYJSONObject.SetInt64", json_obj_set_integer64}, - {"YYJSONObject.SetNull", json_obj_set_null}, - {"YYJSONObject.SetString", json_obj_set_str}, - {"YYJSONObject.Remove", json_obj_remove}, - {"YYJSONObject.Clear", json_obj_clear}, - {"YYJSONObject.FromString", json_obj_parse_str}, - {"YYJSONObject.FromFile", json_obj_parse_file}, - {"YYJSONObject.Sort", json_obj_sort}, - - // JSONArray - {"YYJSONArray.YYJSONArray", json_arr_init}, - {"YYJSONArray.FromStrings", json_arr_init_with_str}, - {"YYJSONArray.Length.get", json_arr_get_size}, - {"YYJSONArray.Get", json_arr_get_val}, - {"YYJSONArray.First.get", json_arr_get_first}, - {"YYJSONArray.Last.get", json_arr_get_last}, - {"YYJSONArray.GetBool", json_arr_get_bool}, - {"YYJSONArray.GetFloat", json_arr_get_float}, - {"YYJSONArray.GetInt", json_arr_get_integer}, - {"YYJSONArray.GetInt64", json_arr_get_integer64}, - {"YYJSONArray.GetString", json_arr_get_str}, - {"YYJSONArray.IsNull", json_arr_is_null}, - {"YYJSONArray.Set", json_arr_replace_val}, - {"YYJSONArray.SetBool", json_arr_replace_bool}, - {"YYJSONArray.SetFloat", json_arr_replace_float}, - {"YYJSONArray.SetInt", json_arr_replace_integer}, - {"YYJSONArray.SetInt64", json_arr_replace_integer64}, - {"YYJSONArray.SetNull", json_arr_replace_null}, - {"YYJSONArray.SetString", json_arr_replace_str}, - {"YYJSONArray.Push", json_arr_append_val}, - {"YYJSONArray.PushBool", json_arr_append_bool}, - {"YYJSONArray.PushFloat", json_arr_append_float}, - {"YYJSONArray.PushInt", json_arr_append_int}, - {"YYJSONArray.PushInt64", json_arr_append_integer64}, - {"YYJSONArray.PushNull", json_arr_append_null}, - {"YYJSONArray.PushString", json_arr_append_str}, - {"YYJSONArray.Remove", json_arr_remove}, - {"YYJSONArray.RemoveFirst", json_arr_remove_first}, - {"YYJSONArray.RemoveLast", json_arr_remove_last}, - {"YYJSONArray.RemoveRange", json_arr_remove_range}, - {"YYJSONArray.Clear", json_arr_clear}, - {"YYJSONArray.FromString", json_arr_parse_str}, - {"YYJSONArray.FromFile", json_arr_parse_file}, - {"YYJSONArray.IndexOfBool", json_arr_index_of_bool}, - {"YYJSONArray.IndexOfString", json_arr_index_of_str}, - {"YYJSONArray.IndexOfInt", json_arr_index_of_int}, - {"YYJSONArray.IndexOfInt64", json_arr_index_of_integer64}, - {"YYJSONArray.IndexOfFloat", json_arr_index_of_float}, - {"YYJSONArray.Sort", json_arr_sort}, - - // JSON - {"YYJSON.ToString", json_doc_write_to_str}, - {"YYJSON.ToFile", json_doc_write_to_file}, - {"YYJSON.Parse", json_doc_parse}, - {"YYJSON.Equals", json_doc_equals}, - {"YYJSON.DeepCopy", json_doc_copy_deep}, - {"YYJSON.GetTypeDesc", json_val_get_type_desc}, - {"YYJSON.GetSerializedSize", json_val_get_serialized_size}, - {"YYJSON.ReadSize.get", json_val_get_read_size}, - {"YYJSON.Type.get", json_val_get_type}, - {"YYJSON.SubType.get", json_val_get_subtype}, - {"YYJSON.IsArray.get", json_val_is_array}, - {"YYJSON.IsObject.get", json_val_is_object}, - {"YYJSON.IsInt.get", json_val_is_int}, - {"YYJSON.IsUint.get", json_val_is_uint}, - {"YYJSON.IsSint.get", json_val_is_sint}, - {"YYJSON.IsNum.get", json_val_is_num}, - {"YYJSON.IsBool.get", json_val_is_bool}, - {"YYJSON.IsTrue.get", json_val_is_true}, - {"YYJSON.IsFalse.get", json_val_is_false}, - {"YYJSON.IsFloat.get", json_val_is_float}, - {"YYJSON.IsStr.get", json_val_is_str}, - {"YYJSON.IsNull.get", json_val_is_null}, - {"YYJSON.IsCtn.get", json_val_is_ctn}, - {"YYJSON.IsMutable.get", json_val_is_mutable}, - {"YYJSON.IsImmutable.get", json_val_is_immutable}, - {"YYJSON.ForeachObject", json_obj_foreach}, - {"YYJSON.ForeachArray", json_arr_foreach}, - {"YYJSON.ForeachKey", json_obj_foreach_key}, - {"YYJSON.ForeachIndex", json_arr_foreach_index}, - {"YYJSON.ToMutable", json_doc_to_mutable}, - {"YYJSON.ToImmutable", json_doc_to_immutable}, - - // JSON CREATE & GET - {"YYJSON.Pack", json_val_pack}, - {"YYJSON.CreateBool", json_val_create_bool}, - {"YYJSON.CreateFloat", json_val_create_float}, - {"YYJSON.CreateInt", json_val_create_int}, - {"YYJSON.CreateInt64", json_val_create_integer64}, - {"YYJSON.CreateNull", json_val_create_null}, - {"YYJSON.CreateString", json_val_create_str}, - {"YYJSON.GetBool", json_val_get_bool}, - {"YYJSON.GetFloat", json_val_get_float}, - {"YYJSON.GetInt", json_val_get_int}, - {"YYJSON.GetInt64", json_val_get_integer64}, - {"YYJSON.GetString", json_val_get_str}, - - // JSON POINTER - {"YYJSON.PtrGet", json_ptr_get_val}, - {"YYJSON.PtrGetBool", json_ptr_get_bool}, - {"YYJSON.PtrGetFloat", json_ptr_get_float}, - {"YYJSON.PtrGetInt", json_ptr_get_int}, - {"YYJSON.PtrGetInt64", json_ptr_get_integer64}, - {"YYJSON.PtrGetString", json_ptr_get_str}, - {"YYJSON.PtrGetIsNull", json_ptr_get_is_null}, - {"YYJSON.PtrGetLength", json_ptr_get_length}, - {"YYJSON.PtrSet", json_ptr_set_val}, - {"YYJSON.PtrSetBool", json_ptr_set_bool}, - {"YYJSON.PtrSetFloat", json_ptr_set_float}, - {"YYJSON.PtrSetInt", json_ptr_set_int}, - {"YYJSON.PtrSetInt64", json_ptr_set_integer64}, - {"YYJSON.PtrSetString", json_ptr_set_str}, - {"YYJSON.PtrSetNull", json_ptr_set_null}, - {"YYJSON.PtrAdd", json_ptr_add_val}, - {"YYJSON.PtrAddBool", json_ptr_add_bool}, - {"YYJSON.PtrAddFloat", json_ptr_add_float}, - {"YYJSON.PtrAddInt", json_ptr_add_int}, - {"YYJSON.PtrAddInt64", json_ptr_add_integer64}, - {"YYJSON.PtrAddString", json_ptr_add_str}, - {"YYJSON.PtrAddNull", json_ptr_add_null}, - {"YYJSON.PtrRemove", json_ptr_remove_val}, - {"YYJSON.PtrTryGetVal", json_ptr_try_get_val}, - {"YYJSON.PtrTryGetBool", json_ptr_try_get_bool}, - {"YYJSON.PtrTryGetFloat", json_ptr_try_get_float}, - {"YYJSON.PtrTryGetInt", json_ptr_try_get_int}, - {"YYJSON.PtrTryGetInt64", json_ptr_try_get_integer64}, - {"YYJSON.PtrTryGetString", json_ptr_try_get_str}, - {nullptr, nullptr} -}; \ No newline at end of file diff --git a/src/smsdk_config.h b/src/smsdk_config.h index acee57d..7abb641 100755 --- a/src/smsdk_config.h +++ b/src/smsdk_config.h @@ -1,12 +1,12 @@ #ifndef _INCLUDE_SOURCEMOD_EXTENSION_CONFIG_H_ #define _INCLUDE_SOURCEMOD_EXTENSION_CONFIG_H_ -#define SMEXT_CONF_NAME "SourceMod YYJSON Extension" +#define SMEXT_CONF_NAME "SourceMod JSON Extension" #define SMEXT_CONF_DESCRIPTION "Provide JSON Native" -#define SMEXT_CONF_VERSION "1.1.4a" +#define SMEXT_CONF_VERSION "1.1.5e" #define SMEXT_CONF_AUTHOR "ProjectSky" #define SMEXT_CONF_URL "https://github.com/ProjectSky/sm-ext-yyjson" -#define SMEXT_CONF_LOGTAG "yyjson" +#define SMEXT_CONF_LOGTAG "json" #define SMEXT_CONF_LICENSE "GPL" #define SMEXT_CONF_DATESTRING __DATE__ diff --git a/third_party/yyjson/AMBuilder b/third_party/yyjson/AMBuilder deleted file mode 100755 index e9d9a49..0000000 --- a/third_party/yyjson/AMBuilder +++ /dev/null @@ -1,20 +0,0 @@ -# vim: sts=2 ts=8 sw=2 tw=99 et ft=python: -import os - -builder.SetBuildFolder('yyjson') - -rvalue = {} -for cxx in builder.targets: - binary = Extension.StaticLibrary(builder, cxx, 'yyjson') - binary.compiler.includes += [ - os.path.join(builder.sourcePath, 'third_party', 'yyjson'), - ] - - if binary.compiler.family == 'clang': - binary.compiler.cflags += ['-Wno-attributes'] - - binary.sources += [ - 'yyjson.c', - ] - - rvalue[binary.compiler.target.arch] = builder.Add(binary) diff --git a/third_party/yyjson/LICENSE b/third_party/yyjson/LICENSE old mode 100644 new mode 100755 diff --git a/third_party/yyjson/yyjson.c b/third_party/yyjson/yyjson.c old mode 100644 new mode 100755 diff --git a/third_party/yyjson/yyjson.h b/third_party/yyjson/yyjson.h old mode 100644 new mode 100755