1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
|
#if defined _jansson_included_
#endinput
#endif
#define _jansson_included_
/**
* --- Type
*
* The JSON specification (RFC 4627) defines the following data types:
* object, array, string, number, boolean, and null.
* JSON types are used dynamically; arrays and objects can hold any
* other data type, including themselves. For this reason, Jansson�s
* type system is also dynamic in nature. There�s one Handle type to
* represent all JSON values, and the referenced structure knows the
* type of the JSON value it holds.
*
*/
enum json_type {
JSON_OBJECT,
JSON_ARRAY,
JSON_STRING,
JSON_INTEGER,
JSON_REAL,
JSON_TRUE,
JSON_FALSE,
JSON_NULL
}
/**
* Return the type of the JSON value.
*
* @param hObj Handle to the JSON value
*
* @return json_type of the value.
*/
native json_type:json_typeof(Handle:hObj);
/**
* The type of a JSON value is queried and tested using these macros
*
* @param %1 Handle to the JSON value
*
* @return True if the value has the correct type.
*/
#define json_is_object(%1) ( json_typeof(%1) == JSON_OBJECT )
#define json_is_array(%1) ( json_typeof(%1) == JSON_ARRAY )
#define json_is_string(%1) ( json_typeof(%1) == JSON_STRING )
#define json_is_integer(%1) ( json_typeof(%1) == JSON_INTEGER )
#define json_is_real(%1) ( json_typeof(%1) == JSON_REAL )
#define json_is_true(%1) ( json_typeof(%1) == JSON_TRUE )
#define json_is_false(%1) ( json_typeof(%1) == JSON_FALSE )
#define json_is_null(%1) ( json_typeof(%1) == JSON_NULL )
#define json_is_number(%1) ( json_typeof(%1) == JSON_INTEGER || json_typeof(%1) == JSON_REAL )
#define json_is_boolean(%1) ( json_typeof(%1) == JSON_TRUE || json_typeof(%1) == JSON_FALSE )
/**
* Saves json_type as a String in output
*
* @param input json_type value to convert to string
* @param output Buffer to store the json_type value
* @param maxlength Maximum length of string buffer.
*
* @return False if the type does not exist.
*/
stock bool:Stringify_json_type(json_type:input, String:output[], maxlength) {
switch(input) {
case JSON_OBJECT: strcopy(output, maxlength, "Object");
case JSON_ARRAY: strcopy(output, maxlength, "Array");
case JSON_STRING: strcopy(output, maxlength, "String");
case JSON_INTEGER: strcopy(output, maxlength, "Integer");
case JSON_REAL: strcopy(output, maxlength, "Real");
case JSON_TRUE: strcopy(output, maxlength, "True");
case JSON_FALSE: strcopy(output, maxlength, "False");
case JSON_NULL: strcopy(output, maxlength, "Null");
default: return false;
}
return true;
}
/**
* --- Equality
*
* - Two integer or real values are equal if their contained numeric
* values are equal. An integer value is never equal to a real value,
* though.
* - Two strings are equal if their contained UTF-8 strings are equal,
* byte by byte. Unicode comparison algorithms are not implemented.
* - Two arrays are equal if they have the same number of elements and
* each element in the first array is equal to the corresponding
* element in the second array.
* - Two objects are equal if they have exactly the same keys and the
* value for each key in the first object is equal to the value of
* the corresponding key in the second object.
* - Two true, false or null values have no "contents", so they are
* equal if their types are equal.
*
*/
/**
* Test whether two JSON values are equal.
*
* @param hObj Handle to the first JSON object
* @param hOther Handle to the second JSON object
*
* @return Returns false if they are inequal or one
* or both of the pointers are NULL.
*/
native bool:json_equal(Handle:hObj, Handle:hOther);
/**
* --- Copying
*
* Jansson supports two kinds of copying: shallow and deep. There is
* a difference between these methods only for arrays and objects.
*
* Shallow copying only copies the first level value (array or object)
* and uses the same child values in the copied value.
*
* Deep copying makes a fresh copy of the child values, too. Moreover,
* all the child values are deep copied in a recursive fashion.
*
*/
/**
* Get a shallow copy of the passed object
*
* @param hObj Handle to JSON object to be copied
*
* @return Returns a shallow copy of the object,
* or INVALID_HANDLE on error.
*/
native Handle:json_copy(Handle:hObj);
/**
* Get a deep copy of the passed object
*
* @param hObj Handle to JSON object to be copied
*
* @return Returns a deep copy of the object,
* or INVALID_HANDLE on error.
*/
native Handle:json_deep_copy(Handle:hObj);
/**
* --- Objects
*
* A JSON object is a dictionary of key-value pairs, where the
* key is a Unicode string and the value is any JSON value.
*
*/
/**
* Returns a handle to a new JSON object, or INVALID_HANDLE on error.
* Initially, the object is empty.
*
* @return Handle to a new JSON object.
*/
native Handle:json_object();
/**
* Returns the number of elements in hObj
*
* @param hObj Handle to JSON object
*
* @return Number of elements in hObj,
* or 0 if hObj is not a JSON object.
*/
native json_object_size(Handle:hObj);
/**
* Get a value corresponding to sKey from hObj
*
* @param hObj Handle to JSON object to get a value from
* @param sKey Key to retrieve
*
* @return Handle to a the JSON object or
* INVALID_HANDLE on error.
*/
native Handle:json_object_get(Handle:hObj, const String:sKey[]);
/**
* Set the value of sKey to hValue in hObj.
* If there already is a value for key, it is replaced by the new value.
*
* @param hObj Handle to JSON object to set a value on
* @param sKey Key to store in the object
* Must be a valid null terminated UTF-8 encoded
* Unicode string.
* @param hValue Value to store in the object
*
* @return True on success.
*/
native bool:json_object_set(Handle:hObj, const String:sKey[], Handle:hValue);
/**
* Set the value of sKey to hValue in hObj.
* If there already is a value for key, it is replaced by the new value.
* This function automatically closes the Handle to the value object.
*
* @param hObj Handle to JSON object to set a value on
* @param sKey Key to store in the object
* Must be a valid null terminated UTF-8 encoded
* Unicode string.
* @param hValue Value to store in the object
*
* @return True on success.
*/
native bool:json_object_set_new(Handle:hObj, const String:sKey[], Handle:hValue);
/**
* Delete sKey from hObj if it exists.
*
* @param hObj Handle to JSON object to delete a key from
* @param sKey Key to delete
*
* @return True on success.
*/
native bool:json_object_del(Handle:hObj, const String:sKey[]);
/**
* Remove all elements from hObj.
*
* @param hObj Handle to JSON object to remove all
* elements from.
*
* @return True on success.
*/
native bool:json_object_clear(Handle:hObj);
/**
* Update hObj with the key-value pairs from hOther, overwriting
* existing keys.
*
* @param hObj Handle to JSON object to update
* @param hOther Handle to JSON object to get update
* keys/values from.
*
* @return True on success.
*/
native bool:json_object_update(Handle:hObj, Handle:hOther);
/**
* Like json_object_update(), but only the values of existing keys
* are updated. No new keys are created.
*
* @param hObj Handle to JSON object to update
* @param hOther Handle to JSON object to get update
* keys/values from.
*
* @return True on success.
*/
native bool:json_object_update_existing(Handle:hObj, Handle:hOther);
/**
* Like json_object_update(), but only new keys are created.
* The value of any existing key is not changed.
*
* @param hObj Handle to JSON object to update
* @param hOther Handle to JSON object to get update
* keys/values from.
*
* @return True on success.
*/
native bool:json_object_update_missing(Handle:hObj, Handle:hOther);
/**
* Object iteration
*
* Example code:
* - We assume hObj is a Handle to a valid JSON object.
*
*
* new Handle:hIterator = json_object_iter(hObj);
* while(hIterator != INVALID_HANDLE)
* {
* new String:sKey[128];
* json_object_iter_key(hIterator, sKey, sizeof(sKey));
*
* new Handle:hValue = json_object_iter_value(hIterator);
*
* // Do something with sKey and hValue
*
* CloseHandle(hValue);
*
* hIterator = json_object_iter_next(hObj, hIterator);
* }
*
*/
/**
* Returns a handle to an iterator which can be used to iterate over
* all key-value pairs in hObj.
* If you are not iterating to the end of hObj make sure to close the
* handle to the iterator manually.
*
* @param hObj Handle to JSON object to get an iterator
* for.
*
* @return Handle to JSON object iterator,
* or INVALID_HANDLE on error.
*/
native Handle:json_object_iter(Handle:hObj);
/**
* Like json_object_iter(), but returns an iterator to the key-value
* pair in object whose key is equal to key.
* Iterating forward to the end of object only yields all key-value
* pairs of the object if key happens to be the first key in the
* underlying hash table.
*
* @param hObj Handle to JSON object to get an iterator
* for.
* @param sKey Start key for the iterator
*
* @return Handle to JSON object iterator,
* or INVALID_HANDLE on error.
*/
native Handle:json_object_iter_at(Handle:hObj, const String:key[]);
/**
* Returns an iterator pointing to the next key-value pair in object.
* This automatically closes the Handle to the iterator hIter.
*
* @param hObj Handle to JSON object.
* @param hIter Handle to JSON object iterator.
*
* @return Handle to JSON object iterator,
* or INVALID_HANDLE on error, or if the
* whole object has been iterated through.
*/
native Handle:json_object_iter_next(Handle:hObj, Handle:hIter);
/**
* Extracts the associated key of hIter as a null terminated UTF-8
* encoded string in the passed buffer.
*
* @param hIter Handle to the JSON String object
* @param sKeyBuffer Buffer to store the value of the String.
* @param maxlength Maximum length of string buffer.
* @error Invalid JSON Object Iterator.
* @return Length of the returned string or -1 on error.
*/
native json_object_iter_key(Handle:hIter, String:sKeyBuffer[], maxlength);
/**
* Returns a handle to the value hIter is pointing at.
*
* @param hIter Handle to JSON object iterator.
*
* @return Handle to value or INVALID_HANDLE on error.
*/
native Handle:json_object_iter_value(Handle:hIter);
/**
* Set the value of the key-value pair in hObj, that is pointed to
* by hIter, to hValue.
*
* @param hObj Handle to JSON object.
* @param hIter Handle to JSON object iterator.
* @param hValue Handle to JSON value.
*
* @return True on success.
*/
native bool:json_object_iter_set(Handle:hObj, Handle:hIter, Handle:hValue);
/**
* Set the value of the key-value pair in hObj, that is pointed to
* by hIter, to hValue.
* This function automatically closes the Handle to the value object.
*
* @param hObj Handle to JSON object.
* @param hIter Handle to JSON object iterator.
* @param hValue Handle to JSON value.
*
* @return True on success.
*/
native bool:json_object_iter_set_new(Handle:hObj, Handle:hIter, Handle:hValue);
/**
* Arrays
*
* A JSON array is an ordered collection of other JSON values.
*
*/
/**
* Returns a handle to a new JSON array, or INVALID_HANDLE on error.
*
* @return Handle to the new JSON array
*/
native Handle:json_array();
/**
* Returns the number of elements in hArray
*
* @param hObj Handle to JSON array
*
* @return Number of elements in hArray,
* or 0 if hObj is not a JSON array.
*/
native json_array_size(Handle:hArray);
/**
* Returns the element in hArray at position iIndex.
*
* @param hArray Handle to JSON array to get a value from
* @param iIndex Position to retrieve
*
* @return Handle to a the JSON object or
* INVALID_HANDLE on error.
*/
native Handle:json_array_get(Handle:hArray, iIndex);
/**
* Replaces the element in array at position iIndex with hValue.
* The valid range for iIndex is from 0 to the return value of
* json_array_size() minus 1.
*
* @param hArray Handle to JSON array
* @param iIndex Position to replace
* @param hValue Value to store in the array
*
* @return True on success.
*/
native bool:json_array_set(Handle:hArray, iIndex, Handle:hValue);
/**
* Replaces the element in array at position iIndex with hValue.
* The valid range for iIndex is from 0 to the return value of
* json_array_size() minus 1.
* This function automatically closes the Handle to the value object.
*
* @param hArray Handle to JSON array
* @param iIndex Position to replace
* @param hValue Value to store in the array
*
* @return True on success.
*/
native bool:json_array_set_new(Handle:hArray, iIndex, Handle:hValue);
/**
* Appends value to the end of array, growing the size of array by 1.
*
* @param hArray Handle to JSON array
* @param hValue Value to append to the array
*
* @return True on success.
*/
native bool:json_array_append(Handle:hArray, Handle:hValue);
/**
* Appends value to the end of array, growing the size of array by 1.
* This function automatically closes the Handle to the value object.
*
* @param hArray Handle to JSON array
* @param hValue Value to append to the array
*
* @return True on success.
*/
native bool:json_array_append_new(Handle:hArray, Handle:hValue);
/**
* Inserts value to hArray at position iIndex, shifting the elements at
* iIndex and after it one position towards the end of the array.
*
* @param hArray Handle to JSON array
* @param iIndex Position to insert at
* @param hValue Value to store in the array
*
* @return True on success.
*/
native bool:json_array_insert(Handle:hArray, iIndex, Handle:hValue);
/**
* Inserts value to hArray at position iIndex, shifting the elements at
* iIndex and after it one position towards the end of the array.
* This function automatically closes the Handle to the value object.
*
* @param hArray Handle to JSON array
* @param iIndex Position to insert at
* @param hValue Value to store in the array
*
* @return True on success.
*/
native bool:json_array_insert_new(Handle:hArray, iIndex, Handle:hValue);
/**
* Removes the element in hArray at position iIndex, shifting the
* elements after iIndex one position towards the start of the array.
*
* @param hArray Handle to JSON array
* @param iIndex Position to insert at
*
* @return True on success.
*/
native bool:json_array_remove(Handle:hArray, iIndex);
/**
* Removes all elements from hArray.
*
* @param hArray Handle to JSON array
*
* @return True on success.
*/
native bool:json_array_clear(Handle:hArray);
/**
* Appends all elements in hOther to the end of hArray.
*
* @param hArray Handle to JSON array to be extended
* @param hOther Handle to JSON array, source to copy from
*
* @return True on success.
*/
native bool:json_array_extend(Handle:hArray, Handle:hOther);
/**
* Booleans & NULL
*
*/
/**
* Returns a handle to a new JSON Boolean with value true,
* or INVALID_HANDLE on error.
*
* @return Handle to the new Boolean object
*/
native Handle:json_true();
/**
* Returns a handle to a new JSON Boolean with value false,
* or INVALID_HANDLE on error.
*
* @return Handle to the new Boolean object
*/
native Handle:json_false();
/**
* Returns a handle to a new JSON Boolean with the value passed
* in bState or INVALID_HANDLE on error.
*
* @param bState Value for the new Boolean object
* @return Handle to the new Boolean object
*/
native Handle:json_boolean(bool:bState);
/**
* Returns a handle to a new JSON NULL or INVALID_HANDLE on error.
*
* @return Handle to the new NULL object
*/
native Handle:json_null();
/**
* Strings
*
* Jansson uses UTF-8 as the character encoding. All JSON strings must
* be valid UTF-8 (or ASCII, as it�s a subset of UTF-8). Normal null
* terminated C strings are used, so JSON strings may not contain
* embedded null characters.
*
*/
/**
* Returns a handle to a new JSON string, or INVALID_HANDLE on error.
*
* @param sValue Value for the new String object
* Must be a valid UTF-8 encoded Unicode string.
* @return Handle to the new String object
*/
native Handle:json_string(const String:sValue[]);
/**
* Saves the associated value of hString as a null terminated UTF-8
* encoded string in the passed buffer.
*
* @param hString Handle to the JSON String object
* @param sValueBuffer Buffer to store the value of the String.
* @param maxlength Maximum length of string buffer.
* @error Invalid JSON String Object.
* @return Length of the returned string or -1 on error.
*/
native json_string_value(Handle:hString, String:sValueBuffer[], maxlength);
/**
* Sets the associated value of JSON String object to value.
*
* @param hString Handle to the JSON String object
* @param sValue Value to set the object to.
* Must be a valid UTF-8 encoded Unicode string.
* @error Invalid JSON String Object.
* @return True on success.
*/
native bool:json_string_set(Handle:hString, String:sValue[]);
/**
* Numbers
*
* The JSON specification only contains one numeric type, 'number'.
* The C (and Pawn) programming language has distinct types for integer
* and floating-point numbers, so for practical reasons Jansson also has
* distinct types for the two. They are called 'integer' and 'real',
* respectively. (Whereas 'real' is a 'Float' for Pawn).
* Therefore a number is represented by either a value of the type
* JSON_INTEGER or of the type JSON_REAL.
*
*/
/**
* Returns a handle to a new JSON integer, or INVALID_HANDLE on error.
*
* @param iValue Value for the new Integer object
* @return Handle to the new Integer object
*/
native Handle:json_integer(iValue);
/**
* Returns the associated value of a JSON Integer Object.
*
* @param hInteger Handle to the JSON Integer object
* @error Invalid JSON Integer Object.
* @return Value of the hInteger,
* or 0 if hInteger is not a JSON integer.
*/
native json_integer_value(Handle:hInteger);
/**
* Sets the associated value of JSON Integer to value.
*
* @param hInteger Handle to the JSON Integer object
* @param iValue Value to set the object to.
* @error Invalid JSON Integer Object.
* @return True on success.
*/
native bool:json_integer_set(Handle:hInteger, iValue);
/**
* Returns a handle to a new JSON real, or INVALID_HANDLE on error.
*
* @param fValue Value for the new Real object
* @return Handle to the new String object
*/
native Handle:json_real(Float:fValue);
/**
* Returns the associated value of a JSON Real.
*
* @param hReal Handle to the JSON Real object
* @error Invalid JSON Real Object.
* @return Float value of hReal,
* or 0.0 if hReal is not a JSON Real.
*/
native Float:json_real_value(Handle:hReal);
/**
* Sets the associated value of JSON Real to fValue.
*
* @param hReal Handle to the JSON Integer object
* @param fValue Value to set the object to.
* @error Invalid JSON Real handle.
* @return True on success.
*/
native bool:json_real_set(Handle:hReal, Float:value);
/**
* Returns the associated value of a JSON integer or a
* JSON Real, cast to Float regardless of the actual type.
*
* @param hNumber Handle to the JSON Number
* @error Not a JSON Real or JSON Integer
* @return Float value of hNumber,
* or 0.0 on error.
*/
native Float:json_number_value(Handle:hNumber);
/**
* Decoding
*
* This sections describes the functions that can be used to decode JSON text
* to the Jansson representation of JSON data. The JSON specification requires
* that a JSON text is either a serialized array or object, and this
* requirement is also enforced with the following functions. In other words,
* the top level value in the JSON text being decoded must be either array or
* object.
*
*/
/**
* Decodes the JSON string sJSON and returns the array or object it contains.
* Errors while decoding can be found in the sourcemod error log.
*
* @param sJSON String containing valid JSON
* @return Handle to JSON object or array.
* or INVALID_HANDLE on error.
*/
native Handle:json_load(const String:sJSON[]);
/**
* Decodes the JSON string sJSON and returns the array or object it contains.
* This function provides additional error feedback and does not log errors
* to the sourcemod error log.
*
* @param sJSON String containing valid JSON
* @param sErrorText This buffer will be filled with the error
* message.
* @param maxlen Size of the buffer
* @param iLine This int will contain the line of the error
* @param iColumn This int will contain the column of the error
*
* @return Handle to JSON object or array.
* or INVALID_HANDLE on error.
*/
native Handle:json_load_ex(const String:sJSON[], String:sErrorText[], maxlen, &iLine, &iColumn);
/**
* Decodes the JSON text in file sFilePath and returns the array or object
* it contains.
* Errors while decoding can be found in the sourcemod error log.
*
* @param sFilePath Path to a file containing pure JSON
*
* @return Handle to JSON object or array.
* or INVALID_HANDLE on error.
*/
native Handle:json_load_file(const String:sFilePath[PLATFORM_MAX_PATH]);
/**
* Decodes the JSON text in file sFilePath and returns the array or object
* it contains.
* This function provides additional error feedback and does not log errors
* to the sourcemod error log.
*
* @param sFilePath Path to a file containing pure JSON
* @param sErrorText This buffer will be filled with the error
* message.
* @param maxlen Size of the buffer
* @param iLine This int will contain the line of the error
* @param iColumn This int will contain the column of the error
*
* @return Handle to JSON object or array.
* or INVALID_HANDLE on error.
*/
native Handle:json_load_file_ex(const String:sFilePath[PLATFORM_MAX_PATH], String:sErrorText[], maxlen, &iLine, &iColumn);
/**
* Encoding
*
* This sections describes the functions that can be used to encode values
* to JSON. By default, only objects and arrays can be encoded directly,
* since they are the only valid root values of a JSON text.
*
*/
/**
* Saves the JSON representation of hObject in sJSON.
*
* @param hObject String containing valid JSON
* @param sJSON Buffer to store the created JSON string.
* @param maxlength Maximum length of string buffer.
* @param iIndentWidth Indenting with iIndentWidth spaces.
* The valid range for this is between 0 and 31 (inclusive),
* other values result in an undefined output. If this is set
* to 0, no newlines are inserted between array and object items.
* @param bEnsureAscii If this is set, the output is guaranteed
* to consist only of ASCII characters. This is achieved
* by escaping all Unicode characters outside the ASCII range.
* @param bSortKeys If this flag is used, all the objects in output are sorted
* by key. This is useful e.g. if two JSON texts are diffed
* or visually compared.
* @param bPreserveOrder If this flag is used, object keys in the output are sorted
* into the same order in which they were first inserted to
* the object. For example, decoding a JSON text and then
* encoding with this flag preserves the order of object keys.
* @return Length of the returned string or -1 on error.
*/
native json_dump(Handle:hObject, String:sJSON[], maxlength, iIndentWidth = 4, bool:bEnsureAscii = false, bool:bSortKeys = false, bool:bPreserveOrder = false);
/**
* Write the JSON representation of hObject to the file sFilePath.
* If sFilePath already exists, it is overwritten.
*
* @param hObject String containing valid JSON
* @param sFilePath Buffer to store the created JSON string.
* @param iIndentWidth Indenting with iIndentWidth spaces.
* The valid range for this is between 0 and 31 (inclusive),
* other values result in an undefined output. If this is set
* to 0, no newlines are inserted between array and object items.
* @param bEnsureAscii If this is set, the output is guaranteed
* to consist only of ASCII characters. This is achieved
* by escaping all Unicode characters outside the ASCII range.
* @param bSortKeys If this flag is used, all the objects in output are sorted
* by key. This is useful e.g. if two JSON texts are diffed
* or visually compared.
* @param bPreserveOrder If this flag is used, object keys in the output are sorted
* into the same order in which they were first inserted to
* the object. For example, decoding a JSON text and then
* encoding with this flag preserves the order of object keys.
* @return Length of the returned string or -1 on error.
*/
native bool:json_dump_file(Handle:hObject, const String:sFilePath[], iIndentWidth = 4, bool:bEnsureAscii = false, bool:bSortKeys = false, bool:bPreserveOrder = false);
/**
* Convenience stocks
*
* These are some custom functions to ease the development using this
* extension.
*
*/
/**
* Returns a handle to a new JSON string, or INVALID_HANDLE on error.
* Formats the string according to the SourceMod format rules.
* The result must be a valid UTF-8 encoded Unicode string.
*
* @param sFormat Formatting rules.
* @param ... Variable number of format parameters.
* @return Handle to the new String object
*/
stock Handle:json_string_format(const String:sFormat[], any:...) {
new String:sTmp[4096];
VFormat(sTmp, sizeof(sTmp), sFormat, 2);
return json_string(sTmp);
}
/**
* Returns a handle to a new JSON string, or INVALID_HANDLE on error.
* This stock allows to specify the size of the temporary buffer used
* to create the string. Use this if the default of 4096 is not enough
* for your string.
* Formats the string according to the SourceMod format rules.
* The result must be a valid UTF-8 encoded Unicode string.
*
* @param tmpBufferLength Size of the temporary buffer
* @param sFormat Formatting rules.
* @param ... Variable number of format parameters.
* @return Handle to the new String object
*/
stock Handle:json_string_format_ex(tmpBufferLength, const String:sFormat[], any:...) {
new String:sTmp[tmpBufferLength];
VFormat(sTmp, sizeof(sTmp), sFormat, 3);
return json_string(sTmp);
}
/**
* Returns the boolean value of the element in hArray at position iIndex.
*
* @param hArray Handle to JSON array to get a value from
* @param iIndex Position to retrieve
*
* @return True if it's a boolean and TRUE,
* false otherwise.
*/
stock bool:json_array_get_bool(Handle:hArray, iIndex) {
new Handle:hElement = json_array_get(hArray, iIndex);
new bool:bResult = (json_is_true(hElement) ? true : false);
CloseHandle(hElement);
return bResult;
}
/**
* Returns the float value of the element in hArray at position iIndex.
*
* @param hArray Handle to JSON array to get a value from
* @param iIndex Position to retrieve
*
* @return Float value,
* or 0.0 if element is not a JSON Real.
*/
stock Float:json_array_get_float(Handle:hArray, iIndex) {
new Handle:hElement = json_array_get(hArray, iIndex);
new Float:fResult = (json_is_number(hElement) ? json_number_value(hElement) : 0.0);
CloseHandle(hElement);
return fResult;
}
/**
* Returns the integer value of the element in hArray at position iIndex.
*
* @param hArray Handle to JSON array to get a value from
* @param iIndex Position to retrieve
*
* @return Integer value,
* or 0 if element is not a JSON Integer.
*/
stock json_array_get_int(Handle:hArray, iIndex) {
new Handle:hElement = json_array_get(hArray, iIndex);
new iResult = (json_is_integer(hElement) ? json_integer_value(hElement) : 0);
CloseHandle(hElement);
return iResult;
}
/**
* Saves the associated value of the element in hArray at position iIndex
* as a null terminated UTF-8 encoded string in the passed buffer.
*
* @param hArray Handle to JSON array to get a value from
* @param iIndex Position to retrieve
* @param sBuffer Buffer to store the value of the String.
* @param maxlength Maximum length of string buffer.
*
* @error Element is not a JSON String.
* @return Length of the returned string or -1 on error.
*/
stock json_array_get_string(Handle:hArray, iIndex, String:sBuffer[], maxlength) {
new Handle:hElement = json_array_get(hArray, iIndex);
new iResult = -1;
if(json_is_string(hElement)) {
iResult = json_string_value(hElement, sBuffer, maxlength);
}
CloseHandle(hElement);
return iResult;
}
/**
* Returns the boolean value of the element in hObj at entry sKey.
*
* @param hObj Handle to JSON object to get a value from
* @param sKey Entry to retrieve
*
* @return True if it's a boolean and TRUE,
* false otherwise.
*/
stock bool:json_object_get_bool(Handle:hObj, const String:sKey[]) {
new Handle:hElement = json_object_get(hObj, sKey);
new bool:bResult = (json_is_true(hElement) ? true : false);
CloseHandle(hElement);
return bResult;
}
/**
* Returns the float value of the element in hObj at entry sKey.
*
* @param hObj Handle to JSON object to get a value from
* @param sKey Position to retrieve
*
* @return Float value,
* or 0.0 if element is not a JSON Real.
*/
stock Float:json_object_get_float(Handle:hObj, const String:sKey[]) {
new Handle:hElement = json_object_get(hObj, sKey);
new Float:fResult = (json_is_number(hElement) ? json_number_value(hElement) : 0.0);
CloseHandle(hElement);
return fResult;
}
/**
* Returns the integer value of the element in hObj at entry sKey.
*
* @param hObj Handle to JSON object to get a value from
* @param sKey Position to retrieve
*
* @return Integer value,
* or 0 if element is not a JSON Integer.
*/
stock json_object_get_int(Handle:hObj, const String:sKey[]) {
new Handle:hElement = json_object_get(hObj, sKey);
new iResult = (json_is_integer(hElement) ? json_integer_value(hElement) : 0);
CloseHandle(hElement);
return iResult;
}
/**
* Saves the associated value of the element in hObj at entry sKey
* as a null terminated UTF-8 encoded string in the passed buffer.
*
* @param hObj Handle to JSON object to get a value from
* @param sKey Entry to retrieve
* @param sBuffer Buffer to store the value of the String.
* @param maxlength Maximum length of string buffer.
*
* @error Element is not a JSON String.
* @return Length of the returned string or -1 on error.
*/
stock json_object_get_string(Handle:hObj, const String:sKey[], String:sBuffer[], maxlength) {
new Handle:hElement = json_object_get(hObj, sKey);
new iResult = -1;
if(json_is_string(hElement)) {
iResult = json_string_value(hElement, sBuffer, maxlength);
}
CloseHandle(hElement);
return iResult;
}
/**
* Pack String Rules
*
* Here�s the full list of format characters:
* n Output a JSON null value. No argument is consumed.
* s Output a JSON string, consuming one argument.
* b Output a JSON bool value, consuming one argument.
* i Output a JSON integer value, consuming one argument.
* f Output a JSON real value, consuming one argument.
* r Output a JSON real value, consuming one argument.
* [] Build an array with contents from the inner format string,
* recursive value building is supported.
* No argument is consumed.
* {} Build an array with contents from the inner format string.
* The first, third, etc. format character represent a key,
* and must be s (as object keys are always strings). The
* second, fourth, etc. format character represent a value.
* Recursive value building is supported.
* No argument is consumed.
*
*/
/**
* This method can be used to create json objects/arrays directly
* without having to create the structure.
* See 'Pack String Rules' for more details.
*
* @param sPackString Pack string similiar to Format()s fmt.
* See 'Pack String Rules'.
* @param hParams ADT Array containing all keys and values
* in the order they appear in the pack string.
*
* @error Invalid pack string or pack string and
* ADT Array don't match up regarding type
* or size.
* @return Handle to JSON element.
*/
stock Handle:json_pack(const String:sPackString[], Handle:hParams) {
new iPos = 0;
return json_pack_element_(sPackString, iPos, hParams);
}
/**
* Internal stocks used by json_pack(). Don't use these directly!
*
*/
stock Handle:json_pack_array_(const String:sFormat[], &iPos, Handle:hParams) {
new Handle:hObj = json_array();
new iStrLen = strlen(sFormat);
for(; iPos < iStrLen;) {
new this_char = sFormat[iPos];
if(this_char == 32 || this_char == 58 || this_char == 44) {
// Skip whitespace, ',' and ':'
iPos++;
continue;
}
if(this_char == 93) {
// array end
iPos++;
break;
}
// Get the next entry as value
// This automatically increments the position!
new Handle:hValue = json_pack_element_(sFormat, iPos, hParams);
// Append the value to the array.
json_array_append_new(hObj, hValue);
}
return hObj;
}
stock Handle:json_pack_object_(const String:sFormat[], &iPos, Handle:hParams) {
new Handle:hObj = json_object();
new iStrLen = strlen(sFormat);
for(; iPos < iStrLen;) {
new this_char = sFormat[iPos];
if(this_char == 32 || this_char == 58 || this_char == 44) {
// Skip whitespace, ',' and ':'
iPos++;
continue;
}
if(this_char == 125) {
// } --> object end
iPos++;
break;
}
if(this_char != 115) {
LogError("Object keys must be strings at %d.", iPos);
return INVALID_HANDLE;
}
// Get the key string for this object from
// the hParams array.
decl String:sKey[255];
GetArrayString(hParams, 0, sKey, sizeof(sKey));
RemoveFromArray(hParams, 0);
// Advance one character in the pack string,
// because we've just read the Key string for
// this object.
iPos++;
// Get the next entry as value
// This automatically increments the position!
new Handle:hValue = json_pack_element_(sFormat, iPos, hParams);
// Insert into object
json_object_set_new(hObj, sKey, hValue);
}
return hObj;
}
stock Handle:json_pack_element_(const String:sFormat[], &iPos, Handle:hParams) {
new this_char = sFormat[iPos];
while(this_char == 32 || this_char == 58 || this_char == 44) {
iPos++;
this_char = sFormat[iPos];
}
// Advance one character in the pack string
iPos++;
switch(this_char) {
case 91: {
// { --> Array
return json_pack_array_(sFormat, iPos, hParams);
}
case 123: {
// { --> Object
return json_pack_object_(sFormat, iPos, hParams);
}
case 98: {
// b --> Boolean
new iValue = GetArrayCell(hParams, 0);
RemoveFromArray(hParams, 0);
return json_boolean(bool:iValue);
}
case 102, 114: {
// r,f --> Real (Float)
new Float:iValue = GetArrayCell(hParams, 0);
RemoveFromArray(hParams, 0);
return json_real(iValue);
}
case 110: {
// n --> NULL
return json_null();
}
case 115: {
// s --> String
decl String:sKey[255];
GetArrayString(hParams, 0, sKey, sizeof(sKey));
RemoveFromArray(hParams, 0);
return json_string(sKey);
}
case 105: {
// i --> Integer
new iValue = GetArrayCell(hParams, 0);
RemoveFromArray(hParams, 0);
return json_integer(iValue);
}
}
SetFailState("Invalid pack String '%s'. Type '%s' not supported at %i", sFormat, this_char, iPos);
return json_null();
}
/**
* Not yet implemented
*
* native json_object_foreach(Handle:hObj, ForEachCallback:cb);
* native Handle:json_unpack(const String:sFormat[], ...);
*
*/
/**
* Do not edit below this line!
*/
public Extension:__ext_smjansson =
{
name = "SMJansson",
file = "smjansson.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 __ext_smjansson_SetNTVOptional()
{
MarkNativeAsOptional("json_typeof");
MarkNativeAsOptional("json_equal");
MarkNativeAsOptional("json_copy");
MarkNativeAsOptional("json_deep_copy");
MarkNativeAsOptional("json_object");
MarkNativeAsOptional("json_object_size");
MarkNativeAsOptional("json_object_get");
MarkNativeAsOptional("json_object_set");
MarkNativeAsOptional("json_object_set_new");
MarkNativeAsOptional("json_object_del");
MarkNativeAsOptional("json_object_clear");
MarkNativeAsOptional("json_object_update");
MarkNativeAsOptional("json_object_update_existing");
MarkNativeAsOptional("json_object_update_missing");
MarkNativeAsOptional("json_object_iter");
MarkNativeAsOptional("json_object_iter_at");
MarkNativeAsOptional("json_object_iter_next");
MarkNativeAsOptional("json_object_iter_key");
MarkNativeAsOptional("json_object_iter_value");
MarkNativeAsOptional("json_object_iter_set");
MarkNativeAsOptional("json_object_iter_set_new");
MarkNativeAsOptional("json_array");
MarkNativeAsOptional("json_array_size");
MarkNativeAsOptional("json_array_get");
MarkNativeAsOptional("json_array_set");
MarkNativeAsOptional("json_array_set_new");
MarkNativeAsOptional("json_array_append");
MarkNativeAsOptional("json_array_append_new");
MarkNativeAsOptional("json_array_insert");
MarkNativeAsOptional("json_array_insert_new");
MarkNativeAsOptional("json_array_remove");
MarkNativeAsOptional("json_array_clear");
MarkNativeAsOptional("json_array_extend");
MarkNativeAsOptional("json_string");
MarkNativeAsOptional("json_string_value");
MarkNativeAsOptional("json_string_set");
MarkNativeAsOptional("json_integer");
MarkNativeAsOptional("json_integer_value");
MarkNativeAsOptional("json_integer_set");
MarkNativeAsOptional("json_real");
MarkNativeAsOptional("json_real_value");
MarkNativeAsOptional("json_real_set");
MarkNativeAsOptional("json_number_value");
MarkNativeAsOptional("json_boolean");
MarkNativeAsOptional("json_true");
MarkNativeAsOptional("json_false");
MarkNativeAsOptional("json_null");
MarkNativeAsOptional("json_load");
MarkNativeAsOptional("json_load_file");
MarkNativeAsOptional("json_dump");
MarkNativeAsOptional("json_dump_file");
}
#endif
|