Skip to content

Commit 2212b5f

Browse files
committed
docs: fix documentation errors, typos and improve API clarity
1 parent 3200a6b commit 2212b5f

File tree

9 files changed

+2121
-618
lines changed

9 files changed

+2121
-618
lines changed

extensions/yyjson/IYYJSONManager.h

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
using SourceMod::Handle_t;
77
using SourceMod::HandleType_t;
8+
using SourceMod::SMInterface;
89
using SourcePawn::IPluginContext;
910

1011
// Forward declaration
@@ -27,7 +28,7 @@ enum YYJSON_SORT_ORDER
2728

2829
/**
2930
* @brief Parameter provider interface for Pack operation
30-
*
31+
*
3132
* Allows Pack to retrieve parameters in a platform-independent way.
3233
*/
3334
class IPackParamProvider
@@ -56,7 +57,7 @@ class IPackParamProvider
5657
* SM_GET_LATE_IFACE(YYJSONMANAGER, g_pYYJSONManager);
5758
* }
5859
*/
59-
class IYYJSONManager : public SourceMod::SMInterface
60+
class IYYJSONManager : public SMInterface
6061
{
6162
public:
6263
virtual const char *GetInterfaceName() override {
@@ -87,8 +88,11 @@ class IYYJSONManager : public SourceMod::SMInterface
8788
* @param buffer Output buffer
8889
* @param buffer_size Buffer size
8990
* @param write_flg Write flags (YYJSON_WRITE_FLAG values, default: 0)
90-
* @param out_size Pointer to receive actual size (optional, default: nullptr)
91-
* @return true on success
91+
* @param out_size Pointer to receive actual size written (including null terminator), optional
92+
* @return true on success, false if buffer is too small or on error
93+
*
94+
* @note The out_size parameter returns the size including null terminator
95+
* @note Use GetSerializedSize() with the same write_flg to determine buffer size
9296
*/
9397
virtual bool WriteToString(YYJSONValue* handle, char* buffer, size_t buffer_size,
9498
uint32_t write_flg = 0, size_t* out_size = nullptr) = 0;
@@ -126,15 +130,28 @@ class IYYJSONManager : public SourceMod::SMInterface
126130
/**
127131
* Get human-readable type description string
128132
* @param handle JSON value
129-
* @return Type description string (e.g., "object", "array", "string", "number", "true", "false", "null")
133+
* @return Type description string (e.g., "object", "array", "string", "number", "true", "false", "unknown")
130134
*/
131135
virtual const char* GetTypeDesc(YYJSONValue* handle) = 0;
132136

133137
/**
134138
* Get the size needed to serialize this JSON value
139+
*
135140
* @param handle JSON value
136141
* @param write_flg Write flags (YYJSON_WRITE_FLAG values, default: 0)
137142
* @return Size in bytes (including null terminator)
143+
*
144+
* @note The returned size depends on the write_flg parameter.
145+
* You MUST use the same flags when calling both GetSerializedSize()
146+
* and WriteToString(). Using different flags will return
147+
* different sizes and may cause buffer overflow.
148+
*
149+
* @example
150+
* // Correct usage:
151+
* auto flags = YYJSON_WRITE_PRETTY;
152+
* size_t size = g_pYYJSONManager->GetSerializedSize(handle, flags);
153+
* char* buffer = new char[size];
154+
* g_pYYJSONManager->WriteToString(handle, buffer, size, flags); // Use same flags
138155
*/
139156
virtual size_t GetSerializedSize(YYJSONValue* handle, uint32_t write_flg = 0) = 0;
140157

@@ -278,7 +295,11 @@ class IYYJSONManager : public SourceMod::SMInterface
278295
/**
279296
* Get the number of bytes read when parsing this document
280297
* @param handle JSON value
281-
* @return Number of bytes read during parsing, 0 if not from parsing
298+
* @return Number of bytes read during parsing (excluding null terminator), 0 if not from parsing
299+
*
300+
* @note This value only applies to documents created from parsing
301+
* @note Manually created documents (ObjectInit, CreateBool, etc.) will return 0
302+
* @note The returned size does not include the null terminator
282303
*/
283304
virtual size_t GetReadSize(YYJSONValue* handle) = 0;
284305

extensions/yyjson/YYJSONManager.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,12 @@ size_t YYJSONManager::GetReadSize(YYJSONValue* handle)
527527
return 0;
528528
}
529529

530-
return handle->m_readSize;
530+
// this not happen in normal case, but it's possible if the document is not from parsing.
531+
if (handle->m_readSize == 0) {
532+
return 0;
533+
}
534+
535+
return handle->m_readSize + 1;
531536
}
532537

533538
YYJSONValue* YYJSONManager::ObjectInit()

extensions/yyjson/YYJSONManager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class YYJSONValue {
6161
yyjson_obj_iter m_iterObjImm;
6262
yyjson_arr_iter m_iterArrImm;
6363

64-
SourceMod::Handle_t m_handle{ BAD_HANDLE };
64+
Handle_t m_handle{ BAD_HANDLE };
6565
size_t m_arrayIndex{ 0 };
6666
size_t m_readSize{ 0 };
6767
bool m_iterInitialized{ false };

extensions/yyjson/extension.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,4 @@ void JsonExtension::SDK_OnUnload()
5454
void JSONHandler::OnHandleDestroy(HandleType_t type, void* object)
5555
{
5656
delete (YYJSONValue*)object;
57-
}
58-
59-
IYYJSONManager* JsonExtension::GetYYJSONManager()
60-
{
61-
return g_pYYJSONManager;
6257
}

extensions/yyjson/extension.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ class JsonExtension : public SDKExtension
1111
public:
1212
virtual bool SDK_OnLoad(char *error, size_t maxlength, bool late);
1313
virtual void SDK_OnUnload();
14-
IYYJSONManager* GetYYJSONManager();
1514
};
1615

1716
class JSONHandler : public IHandleTypeDispatch

extensions/yyjson/json_natives.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ static cell_t json_val_get_read_size(IPluginContext* pContext, const cell_t* par
669669
size_t size = g_pYYJSONManager->GetReadSize(handle);
670670
if (size == 0) return 0;
671671

672-
return static_cast<cell_t>(size + 1);
672+
return static_cast<cell_t>(size);
673673
}
674674

675675
static cell_t json_val_create_null(IPluginContext* pContext, const cell_t* params)

plugins/include/yyjson.inc

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ methodmap YYJSON < Handle
198198
* @param maxlength Maximum length of the string buffer
199199
* @param flag The JSON write options
200200
*
201-
* @return Number of characters written to the buffer, including the null terminator. 0 on failure
201+
* @return Number of characters written to the buffer (including null terminator) or 0 on failure
202202
*/
203203
public native int ToString(char[] buffer, int maxlength, YYJSON_WRITE_FLAG flag = YYJSON_WRITE_NOFLAG);
204204

@@ -278,7 +278,7 @@ methodmap YYJSON < Handle
278278
public static native YYJSON CreateFloat(float value);
279279

280280
/**
281-
* Creates and returns a int value
281+
* Creates and returns an int value
282282
*
283283
* @note Needs to be freed using delete or CloseHandle()
284284
*
@@ -289,11 +289,11 @@ methodmap YYJSON < Handle
289289
public static native YYJSON CreateInt(int value);
290290

291291
/**
292-
* Creates and returns a intger64 value
292+
* Creates and returns an integer64 value
293293
*
294294
* @note Needs to be freed using delete or CloseHandle()
295295
*
296-
* @param value The intger64 value to be set
296+
* @param value The integer64 value to be set
297297
*
298298
* @return JSON handle, NULL on error
299299
*/
@@ -347,7 +347,7 @@ methodmap YYJSON < Handle
347347
public static native int GetInt(const YYJSON value);
348348

349349
/**
350-
* Get intger64 value by a JSON Handle
350+
* Get integer64 value by a JSON Handle
351351
*
352352
* @param value JSON handle
353353
* @param buffer Buffer to copy to
@@ -369,12 +369,17 @@ methodmap YYJSON < Handle
369369
public static native bool GetString(const YYJSON value, char[] buffer, int maxlength);
370370

371371
/**
372-
* Get JSON Handle serialized size in bytes (including null-terminator)
373-
*
374-
* @param flag The JSON write options
375-
*
376-
* @return serialized size
377-
*/
372+
* Get JSON Handle serialized size in bytes (including null-terminator)
373+
*
374+
* @param flag The JSON write options
375+
*
376+
* @return Size in bytes (including null terminator)
377+
*
378+
* @note The returned size depends on the flag parameter.
379+
* You MUST use the same flags when calling both GetSerializedSize()
380+
* and ToString(). Using different flags will return different sizes
381+
* and may cause buffer overflow.
382+
*/
378383
public native int GetSerializedSize(YYJSON_WRITE_FLAG flag = YYJSON_WRITE_NOFLAG);
379384

380385
/**
@@ -435,7 +440,7 @@ methodmap YYJSON < Handle
435440
*
436441
* @return True on success, false on failure
437442
*/
438-
public native bool PtrGetString(const char[] path, char[] buffer, int maxlength)
443+
public native bool PtrGetString(const char[] path, char[] buffer, int maxlength);
439444

440445
/**
441446
* Get value is null by a JSON Pointer
@@ -444,26 +449,28 @@ methodmap YYJSON < Handle
444449
*
445450
* @return True if the value is null, false otherwise
446451
*/
447-
public native bool PtrGetIsNull(const char[] path)
452+
public native bool PtrGetIsNull(const char[] path);
448453

449454
/**
450455
* Get JSON content length (string length, array size, object size)
451-
* Returns 0 if val is NULL or type is not string/array/object
452-
* if str including null-terminator
456+
*
457+
* @note For strings: returns string length including null-terminator
458+
* @note For arrays/objects: returns number of elements
459+
* @note Returns 0 if value is NULL or type is not string/array/object
453460
*
454461
* @param path The JSON pointer string
455462
*
456463
* @return JSON content length
457464
*/
458-
public native int PtrGetLength(const char[] path)
465+
public native int PtrGetLength(const char[] path);
459466

460467
/**
461468
* Set value by a JSON Pointer
462469
*
463470
* @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
464471
*
465472
* @param path The JSON pointer string
466-
* @param value The value to be set, pass NULL to remove
473+
* @param value The value to be set
467474
*
468475
* @return true if JSON pointer is valid and new value is set, false otherwise
469476
*/
@@ -475,7 +482,7 @@ methodmap YYJSON < Handle
475482
* @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
476483
*
477484
* @param path The JSON pointer string
478-
* @param value The boolean value to be set, pass NULL to remove
485+
* @param value The boolean value to be set
479486
*
480487
* @return true if JSON pointer is valid and new value is set, false otherwise
481488
*/
@@ -487,7 +494,7 @@ methodmap YYJSON < Handle
487494
* @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
488495
*
489496
* @param path The JSON pointer string
490-
* @param value The float value to be set, pass NULL to remove
497+
* @param value The float value to be set
491498
*
492499
* @return true if JSON pointer is valid and new value is set, false otherwise
493500
*/
@@ -499,19 +506,19 @@ methodmap YYJSON < Handle
499506
* @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
500507
*
501508
* @param path The JSON pointer string
502-
* @param value The integer value to be set, pass NULL to remove
509+
* @param value The integer value to be set
503510
*
504511
* @return true if JSON pointer is valid and new value is set, false otherwise
505512
*/
506513
public native bool PtrSetInt(const char[] path, int value);
507514

508515
/**
509-
* Set intger64 value by a JSON Pointer
516+
* Set integer64 value by a JSON Pointer
510517
*
511518
* @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
512519
*
513520
* @param path The JSON pointer string
514-
* @param value The intger64 value to be set, pass NULL to remove
521+
* @param value The integer64 value to be set
515522
*
516523
* @return true if JSON pointer is valid and new value is set, false otherwise
517524
*/
@@ -523,7 +530,7 @@ methodmap YYJSON < Handle
523530
* @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
524531
*
525532
* @param path The JSON pointer string
526-
* @param value The string value to be set, pass NULL to remove
533+
* @param value The string value to be set
527534
*
528535
* @return true if JSON pointer is valid and new value is set, false otherwise
529536
*/
@@ -535,7 +542,6 @@ methodmap YYJSON < Handle
535542
* @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
536543
*
537544
* @param path The JSON pointer string
538-
* @param value The null value to be set, pass NULL to remove
539545
*
540546
* @return true if JSON pointer is valid and new value is set, false otherwise
541547
*/
@@ -585,7 +591,7 @@ methodmap YYJSON < Handle
585591
* Add (insert) integer64 value by a JSON pointer
586592
*
587593
* @param path The JSON pointer string
588-
* @param value The intger64 value to be added
594+
* @param value The integer64 value to be added
589595
*
590596
* @return true if JSON pointer is valid and new value is set, false otherwise
591597
*/
@@ -617,7 +623,7 @@ methodmap YYJSON < Handle
617623
*
618624
* @return true if removed value, false otherwise
619625
*/
620-
public native bool PtrRemove(const char[] path)
626+
public native bool PtrRemove(const char[] path);
621627

622628
/**
623629
* Try to get value by a JSON Pointer
@@ -801,14 +807,16 @@ methodmap YYJSON < Handle
801807
}
802808

803809
/**
804-
* Retrieves read size of the JSON data
810+
* Retrieves the size of the JSON data as it was originally read from parsing
805811
*
806-
* @note This value reflects the size of the JSON data as read from the document
807-
* - It does not auto update if the document is modified
808-
* - For modified document, use GetSerializedSize to obtain the current size
812+
* @return Size in bytes (including null terminator) or 0 if not from parsing
809813
*
814+
* @note This value only applies to documents created from parsing (Parse, FromString, FromFile)
815+
* @note Manually created documents (new YYJSONObject(), YYJSON.CreateBool(), etc.) will return 0
816+
* @note This value does not auto-update if the document is modified
817+
* @note For modified documents, use GetSerializedSize() to obtain the current size
810818
*/
811-
property bool ReadSize {
819+
property int ReadSize {
812820
public native get();
813821
}
814822
};
@@ -1219,7 +1227,7 @@ methodmap YYJSONArray < YYJSON
12191227
public native bool SetFloat(int index, float value);
12201228

12211229
/**
1222-
* Replaces a integer value at index
1230+
* Replaces an integer value at index
12231231
*
12241232
* @param index The index to which to replace the value
12251233
* @param value The new int value to replace
@@ -1229,10 +1237,10 @@ methodmap YYJSONArray < YYJSON
12291237
public native bool SetInt(int index, int value);
12301238

12311239
/**
1232-
* Replaces a intger64 value at index
1240+
* Replaces an integer64 value at index
12331241
*
12341242
* @param index The index to which to replace the value
1235-
* @param value The new intger64 value to replace
1243+
* @param value The new integer64 value to replace
12361244
*
12371245
* @return True if succeed, false otherwise
12381246
*/
@@ -1285,7 +1293,7 @@ methodmap YYJSONArray < YYJSON
12851293
public native bool PushFloat(float value);
12861294

12871295
/**
1288-
* Inserts a integer value at the end of the array
1296+
* Inserts an integer value at the end of the array
12891297
*
12901298
* @param value integer to set
12911299
*
@@ -1294,9 +1302,9 @@ methodmap YYJSONArray < YYJSON
12941302
public native bool PushInt(int value);
12951303

12961304
/**
1297-
* Inserts a intger64 value at the end of the array
1305+
* Inserts an integer64 value at the end of the array
12981306
*
1299-
* @param value intger64 value
1307+
* @param value integer64 value
13001308
*
13011309
* @return The value to be inserted. Returns false if it is NULL
13021310
*/

0 commit comments

Comments
 (0)