Skip to content

Commit 22d94c4

Browse files
committed
feat: Implement IYYJSONManager interface for external extensions (implements #6)
Add utility methods to make the interface more convenient for external C++ extensions (WebSocket, HTTP, etc.). Changes: - Add new interface methods for external extensions: * Release(YYJSONValue*): safely release JSON value objects * GetHandleType(): get YYJSON handle type for cross-extension access * GetFromHandle(): read YYJSONValue from SourceMod handle * ... - Update yyjson library to version 0.12.0 - Fix handle permissions to allow cross-extension read access: * Set HandleAccess_Read = 0 in extension.cpp These improvements enable external extensions to: - Use more concise API calls with sensible defaults - Access JSON handles across extension boundaries - Safely manage YYJSONValue object lifetime - Avoid dependency on YYJSON extension internals All changes are backward compatible. Existing code continues to work without modification.
1 parent 5372fcb commit 22d94c4

14 files changed

Lines changed: 13781 additions & 9843 deletions

File tree

AMBuilder

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ for cxx in builder.targets:
99

1010
binary.sources += [
1111
'src/extension.cpp',
12+
'src/YYJSONManager.cpp',
1213
'src/json_natives.cpp',
1314
os.path.join(Extension.sm_root, 'public', 'smsdk_ext.cpp'),
1415
]
1516

1617
binary.compiler.includes += [
1718
os.path.join(builder.sourcePath, 'src'),
19+
os.path.join(builder.sourcePath, 'public'),
1820
os.path.join(builder.sourcePath, 'third_party', 'yyjson'),
1921
]
2022

README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ A [SourceMod](http://www.sourcemod.net/) extension that provides comprehensive J
1212
* Array and object sorting support
1313
* Iteration methods for arrays and objects
1414
* Support for both mutable and immutable JSON documents
15+
* Support SourceMod Extension API
1516

1617
## Performance
1718
Performance test results using [twitter.json](https://github.com/ibireme/yyjson_benchmark/blob/master/data/json/twitter.json) (0.60 MB):
@@ -60,6 +61,30 @@ ambuild
6061
* [API Reference](https://github.com/ProjectSky/sm-ext-yyjson/blob/main/scripting/include/yyjson.inc)
6162
* [Latest Release](https://github.com/ProjectSky/sm-ext-yyjson/releases)
6263

64+
### SourceMod Extension API
65+
```cpp
66+
#include <IYYJSONManager.h>
67+
68+
IYYJSONManager* g_pYYJSONManager = nullptr;
69+
70+
void Ext::SDK_OnAllLoaded()
71+
{
72+
SM_GET_LATE_IFACE(YYJSONMANAGER, g_pYYJSONManager);
73+
74+
char error[256];
75+
YYJSONValue* val = g_pYYJSONManager->ParseJSON("{\"name\":\"John\", \"age\":30}", false, false, 0, error, sizeof(error));
76+
77+
size_t size = g_pYYJSONManager->GetSerializedSize(val);
78+
char buffer[size];
79+
g_pYYJSONManager->WriteToString(val, buffer, size);
80+
81+
// must release the value after using
82+
g_pYYJSONManager->Release(val);
83+
84+
printf("JSON: %s\n", buffer);
85+
}
86+
```
87+
6388
### Basic Examples
6489

6590
#### Working with Objects
@@ -273,7 +298,7 @@ delete obj;
273298
```cpp
274299
// Create object from key-value string arrays
275300
char pairs[][] = {"name", "test", "type", "demo", "version", "1.0.0"};
276-
301+
277302
YYJSONObject obj = YYJSONObject.FromStrings(pairs, sizeof(pairs));
278303

279304
/* Output:

0 commit comments

Comments
 (0)