-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyyjson.inc
executable file
·1352 lines (1221 loc) · 41.7 KB
/
yyjson.inc
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
#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
}
// 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
}
// 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 the null terminator. 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 flag The JSON read options
* @param is_mutable_doc True to create a mutable document, false to create an immutable one
*
* @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);
/**
* Copies and returns a new mutable value from input, returns NULL on error. This makes a deep-copy on the mutable value. The memory was managed by mutable document
*
* @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
*
* @param value1 JSON handle1
* @param value2 JSON handle2
*
* @return JSON handle, false on failure
*/
public static native any DeepCopy(const YYJSON value1, const YYJSON value2);
/**
* 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 a 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 a intger64 value
*
* @note Needs to be freed using delete or CloseHandle()
*
* @param value The intger64 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 intger64 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 serialized size
*/
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 (UTF-8 with null-terminator)
*
* @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 (UTF-8 with null-terminator)
*
* @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 (UTF-8 with null-terminator)
*
* @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 (UTF-8 with null-terminator)
*
* @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 (UTF-8 with null-terminator)
* @param buffer Buffer to copy to
* @param maxlength Maximum size of the buffer
*
*/
public native bool PtrGetInt64(const char[] path, char[] buffer, int maxlength);
/**
* Get string value by a JSON Pointer
*
* @param path The JSON pointer string (UTF-8 with null-terminator)
* @param buffer Buffer to copy to
* @param maxlength Maximum size of the buffer
*
*/
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 (UTF-8 with null-terminator)
*
*/
public native bool PtrGetIsNull(const char[] path)
/**
* Get JSON content length (string length, array size, object size)
* Returns 0 if val is NULL or type is not string/array/object
*
* @param path The JSON pointer string (UTF-8 with null-terminator)
*
* @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 (UTF-8 with null-terminator)
* @param value The value to be set, pass NULL to remove
*
* @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 (UTF-8 with null-terminator)
* @param value The boolean value to be set, pass NULL to remove
*
* @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 (UTF-8 with null-terminator)
* @param value The float value to be set, pass NULL to remove
*
* @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 (UTF-8 with null-terminator)
* @param value The integer value to be set, pass NULL to remove
*
* @return true if JSON pointer is valid and new value is set, false otherwise
*/
public native bool PtrSetInt(const char[] path, int value);
/**
* Set intger64 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 (UTF-8 with null-terminator)
* @param value The intger64 value to be set, pass NULL to remove
*
* @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 (UTF-8 with null-terminator)
* @param value The string value to be set, pass NULL to remove
*
* @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 (UTF-8 with null-terminator)
* @param value The null value to be set, pass NULL to remove
*
* @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 (UTF-8 with null-terminator)
* @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 (UTF-8 with null-terminator)
* @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 (UTF-8 with null-terminator)
* @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 (UTF-8 with null-terminator)
* @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 (UTF-8 with null-terminator)
* @param value The intger64 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 (UTF-8 with null-terminator)
* @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 (UTF-8 with null-terminator)
*
* @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 (UTF-8 with null-terminator)
*
* @return true if removed value, false otherwise
*/
public native bool PtrRemove(const char[] path)
/**
* 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
*/
property bool IsInt {
public native get();
}
/**
* Retrieves json is boolean
*/
property bool IsBool {
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 mutable doc
*/
property bool IsMutable {
public native get();
}
/**
* Retrieves json is immutable doc
*/
property bool IsImmutable {
public native get();
}
/**
* Retrieves read size of the JSON data
*
* @note This value reflects the size of the JSON data as read from the document
* - It does not auto update if the document is modified
* - For modified document, use GetSerializedSize to obtain the current size
*
*/
property bool 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 NULL if array is NULL/empty or the index is out of bounds
*/