Skip to content

Commit b296f1e

Browse files
committed
feat: add Pack and FromStrings features
Add two new features for JSON manipulation: 1. Pack: create JSON with format string and values 2. FromStrings: create JSON from string arrays Implementation includes: - Add Pack function for format string parsing - Add FromStrings methods for Object and Array - Add test cases in yyjson_test.sp - Add usage examples in README.md
1 parent e30858f commit b296f1e

8 files changed

Lines changed: 579 additions & 8 deletions

File tree

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ jobs:
1818
strategy:
1919
fail-fast: false
2020
matrix:
21-
os: [windows-latest, ubuntu-latest]
21+
os: [windows-latest, ubuntu-22.04]
2222
include:
2323
- os: windows-latest
2424
os_short: win
2525

26-
- os: ubuntu-latest
26+
- os: ubuntu-22.04
2727
os_short: linux
2828

2929
steps:

PackageScript

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,5 @@ for cxx_task in Extension.extensions:
4040

4141
# Copy include files
4242
CopyFiles('scripting/include', 'addons/sourcemod/scripting/include',
43-
[ 'yyjson.inc']
43+
[ 'yyjson.inc' ]
4444
)

README.md

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ ambuild
5858

5959
## Documentation
6060
* [API Reference](https://github.com/ProjectSky/sm-ext-yyjson/blob/main/scripting/include/yyjson.inc)
61-
* [Latest Builds](https://github.com/ProjectSky/sm-ext-yyjson/actions)
61+
* [Latest Release](https://github.com/ProjectSky/sm-ext-yyjson/releases)
6262

6363
### Basic Examples
6464

@@ -231,7 +231,7 @@ delete arr;
231231
```cpp
232232
// Array sorting
233233
YYJSONArray arr = YYJSON.Parse(
234-
"[3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]", .is_mutable_doc = true
234+
"[3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]", .is_mutable_doc = true
235235
);
236236

237237
arr.Sort(); // Ascending (default)
@@ -269,6 +269,94 @@ delete mixed;
269269
delete obj;
270270
```
271271

272+
#### Using FromStrings
273+
```cpp
274+
// Create object from key-value string arrays
275+
char pairs[][] = {"name", "test", "type", "demo", "version", "1.0.0"};
276+
277+
YYJSONObject obj = YYJSONObject.FromStrings(pairs, sizeof(pairs));
278+
279+
/* Output:
280+
{
281+
"name": "test",
282+
"type": "demo",
283+
"version": "1.0.0"
284+
}
285+
*/
286+
287+
// Create array from string array
288+
char items[][] = {"apple", "banana", "orange"};
289+
YYJSONArray arr = YYJSONArray.FromStrings(items, sizeof(items));
290+
291+
/* Output:
292+
[
293+
"apple",
294+
"banana",
295+
"orange"
296+
]
297+
*/
298+
299+
delete obj;
300+
delete arr;
301+
```
302+
303+
#### Using JSON Pack
304+
```cpp
305+
// Create object with mixed types
306+
YYJSON packed = YYJSON.Pack("{s:s,s:i,s:f,s:b,s:n}",
307+
"name", "John",
308+
"age", 25,
309+
"height", 1.75,
310+
"active", true,
311+
"extra"
312+
);
313+
314+
/* Output:
315+
{
316+
"name": "John",
317+
"age": 25,
318+
"height": 1.75,
319+
"active": true,
320+
"extra": null
321+
}
322+
*/
323+
324+
// Create nested structures
325+
YYJSON nested = YYJSON.Pack("{s:{s:s,s:[i,i,i]}}",
326+
"user",
327+
"name", "John",
328+
"scores", 85, 90, 95
329+
);
330+
331+
/* Output:
332+
{
333+
"user": {
334+
"name": "John",
335+
"scores": [85, 90, 95]
336+
}
337+
}
338+
*/
339+
340+
// Create array with mixed types
341+
YYJSON array = YYJSON.Pack("[s,i,f,b,n]",
342+
"test", 42, 3.14, true
343+
);
344+
345+
/* Output:
346+
[
347+
"test",
348+
42,
349+
3.14,
350+
true,
351+
null
352+
]
353+
*/
354+
355+
delete packed;
356+
delete nested;
357+
delete array;
358+
```
359+
272360
## Working with Immutable Documents
273361
When parsing JSON documents, you can choose whether to create a mutable or immutable document:
274362

pushbuild.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
some meaningless str
1+
some meaningless str
2+
8c68c9448936fdfc437994f46753cdda63318509

scripting/include/yyjson.inc

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,28 @@ enum YYJSON_SORT_ORDER
6666

6767
methodmap YYJSON < Handle
6868
{
69+
/**
70+
* Creates a JSON value using a format string and arguments
71+
* Format specifiers:
72+
* - s: string
73+
* - i: integer
74+
* - f: float
75+
* - b: boolean
76+
* - n: null
77+
* - {: start object
78+
* - }: end object
79+
* - [: start array
80+
* - ]: end array
81+
*
82+
* @param format Format string
83+
* @param ... Arguments based on format string
84+
*
85+
* @return JSON handle
86+
*
87+
* @error If format string is invalid or arguments don't match
88+
*/
89+
public static native any Pack(const char[] format, any ...);
90+
6991
/**
7092
* Iterates over the object's key-value pairs
7193
*
@@ -673,6 +695,19 @@ methodmap YYJSONObject < YYJSON
673695
*/
674696
public native YYJSONObject();
675697

698+
/**
699+
* Creates a new JSON object from an array of strings representing key-value pairs.
700+
* The array must contain an even number of strings, where even indices are keys
701+
* and odd indices are values. Keys cannot be empty strings.
702+
*
703+
* @param pairs Array of strings containing alternating keys and values.
704+
* @param size Total size of the array (must be even).
705+
* @return New JSON object handle.
706+
* @error If array size is invalid, any key is empty, or if creation fails.
707+
*
708+
*/
709+
public static native YYJSONObject FromStrings(const char[][] pairs, int size);
710+
676711
/**
677712
* Loads a JSON object from a file
678713
*
@@ -927,6 +962,15 @@ methodmap YYJSONArray < YYJSON
927962
*/
928963
public native YYJSONArray();
929964

965+
/**
966+
* Creates a new JSON array from an array of strings.
967+
*
968+
* @param strings Array of strings to create array from.
969+
* @param size Size of the array.
970+
* @return New JSON array handle, or null if creation failed.
971+
*/
972+
public static native YYJSONArray FromStrings(const char[][] strings, int size);
973+
930974
/**
931975
* Loads a JSON array from a file
932976
*

scripting/yyjson_test.sp

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public Plugin myinfo =
66
name = "YYJSON Test Suite",
77
author = "ProjectSky",
88
description = "Test suite for YYJSON extension",
9-
version = "1.0.1",
9+
version = "1.0.2",
1010
url = "https://github.com/ProjectSky/sm-ext-yyjson"
1111
};
1212

@@ -28,6 +28,8 @@ Action Command_RunTests(int args)
2828
TestTypeOperations();
2929
TestFileOperations();
3030
TestImmutabilityOperations();
31+
TestPackOperations();
32+
TestFromStringsOperations();
3133

3234
PrintToServer("[YYJSON] All tests completed!");
3335
return Plugin_Handled;
@@ -502,6 +504,66 @@ void TestImmutabilityOperations()
502504
delete readImmutable;
503505
}
504506

507+
void TestPackOperations()
508+
{
509+
PrintToServer("[YYJSON] Testing pack operations...");
510+
511+
// Test basic pack operation with different types
512+
YYJSON packed = YYJSON.Pack("{s:s,s:i,s:f,s:b,s:n}",
513+
"name", "test",
514+
"age", 25,
515+
"height", 1.75,
516+
"active", true,
517+
"extra"
518+
);
519+
520+
PrintToServer("Packed JSON:");
521+
PrintJson(packed);
522+
523+
// Test nested object packing
524+
YYJSON nested = YYJSON.Pack("{s:{s:s,s:[i,i,i]}}",
525+
"user",
526+
"name", "test",
527+
"scores", 85, 90, 95
528+
);
529+
530+
PrintToServer("Nested packed JSON:");
531+
PrintJson(nested);
532+
533+
// Test array packing with mixed types
534+
YYJSON array = YYJSON.Pack("[s,i,f,b,n]",
535+
"test", 42, 3.14, true
536+
);
537+
538+
PrintToServer("Array packed JSON:");
539+
PrintJson(array);
540+
541+
delete packed;
542+
delete nested;
543+
delete array;
544+
}
545+
546+
void TestFromStringsOperations()
547+
{
548+
PrintToServer("[YYJSON] Testing FromStrings operations...");
549+
550+
// Test object creation from key-value string arrays
551+
char pairs[][] = {"name", "test", "type", "demo", "version", "1.0.0"};
552+
553+
YYJSONObject obj = YYJSONObject.FromStrings(pairs, sizeof(pairs));
554+
PrintToServer("Object from strings:");
555+
PrintJson(obj);
556+
557+
// Test array creation from string array
558+
char items[][] = {"apple", "banana", "orange", "grape"};
559+
YYJSONArray arr = YYJSONArray.FromStrings(items, sizeof(items));
560+
PrintToServer("Array from strings:");
561+
PrintJson(arr);
562+
563+
delete obj;
564+
delete arr;
565+
}
566+
505567
// Helper function to print json contents
506568
void PrintJson(YYJSON data)
507569
{

0 commit comments

Comments
 (0)