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
|
/**
* vim: set ts=4 :
* =============================================================================
* sm-json
* Provides a pure SourcePawn implementation of JSON encoding and decoding.
* https://github.com/clugg/sm-json
*
* sm-json (C)2019 James Dickens. (clug)
* SourceMod (C)2004-2008 AlliedModders LLC. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, AlliedModders LLC gives you permission to link the
* code of this program (as well as its derivative works) to "Half-Life 2," the
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, AlliedModders LLC grants
* this exception to all derivative works. AlliedModders LLC defines further
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
* or <http://www.sourcemod.net/license.php>.
*/
#if defined _json_object_included
#endinput
#endif
#define _json_object_included
#include <string>
#include <json/definitions>
#include <json/helpers/encode>
#include <json>
methodmap JSON_Object < StringMap
{
/**
* Creates a new JSON_Object.
*
* @param is_array Should the object created be an array? [default: false]
* @returns A new JSON_Object.
*/
public JSON_Object(bool is_array = false)
{
StringMap self = CreateTrie();
if (is_array) {
self.SetValue(JSON_ARRAY_INDEX_KEY, 0);
}
return view_as<JSON_Object>(self);
}
/**
* Checks whether the object has a key.
*
* @param key Key to check existence of.
* @returns True if the key exists, false otherwise.
*/
public bool HasKey(const char[] key)
{
int dummy_int;
char dummy_str[1];
return this.GetValue(key, dummy_int)
|| this.GetString(key, dummy_str, sizeof(dummy_str));
}
/**
* @section Array helpers.
*/
/**
* Whether the current object is an array.
*/
property bool IsArray {
public get()
{
return this.HasKey(JSON_ARRAY_INDEX_KEY);
}
}
/**
* The current index of the object if it is an array, or -1 otherwise.
*/
property int CurrentIndex {
public get()
{
if (! this.IsArray) {
return -1;
}
int result;
return (this.GetValue(JSON_ARRAY_INDEX_KEY, result)) ? result : -1;
}
public set(int value)
{
this.SetValue(JSON_ARRAY_INDEX_KEY, value);
}
}
/**
* The number of items in the object if it is an array,
* or the number of keys (including meta-keys) otherwise.
*/
property int Length {
public get()
{
StringMapSnapshot snap = this.Snapshot();
int length = (this.IsArray) ? this.CurrentIndex : snap.Length;
delete snap;
return length;
}
}
/**
* Increments the current index of the object.
*
* @returns True on success, false if the current object is not an array.
*/
public bool IncrementIndex()
{
if (! this.HasKey(JSON_ARRAY_INDEX_KEY)) {
return false;
}
this.CurrentIndex += 1;
return true;
}
/**
* Checks whether the object has an index.
*
* @param index Index to check existence of.
* @returns True if the index exists, false otherwise.
*/
public bool HasIndex(int index)
{
return index >= 0 && index < this.Length;
}
/**
* Gets the string representation of an array index.
*
* @param output String buffer to store output.
* @param max_size Maximum size of string buffer.
* @param key Key to get string for. [default: current index]
* @returns True on success, false otherwise.
*/
public int GetIndexString(char[] output, int max_size, int key = -1)
{
key = (key == -1) ? this.CurrentIndex : key;
if (key == -1) {
return false;
}
return IntToString(key, output, max_size);
}
/**
* @section Internal Getters
*/
/**
* Gets the cell type stored at a key.
*
* @param key Key to get value type for.
* @returns Value type for key provided,
* or Type_Invalid if it does not exist.
*/
public JSON_CELL_TYPE GetKeyType(const char[] key)
{
int max_size = strlen(key) + strlen(JSON_META_TYPE_KEY) + 1;
char[] type_key = new char[max_size];
Format(type_key, max_size, "%s%s", key, JSON_META_TYPE_KEY);
JSON_CELL_TYPE type;
return (this.GetValue(type_key, type)) ? type : Type_Invalid;
}
/**
* Gets the cell type stored at an index.
*
* @param index Index to get value type for.
* @returns Value type for index provided, or Type_Invalid if it does not exist.
*/
public JSON_CELL_TYPE GetKeyTypeIndexed(int index)
{
char key[JSON_INDEX_BUFFER_SIZE];
if (! this.GetIndexString(key, sizeof(key), index)) {
return Type_Invalid;
}
return this.GetKeyType(key);
}
/**
* Gets the length of the string stored at a key.
*
* @param key Key to get string length for.
* @returns Length of string at key provided,
* or -1 if it is not a string/does not exist.
*/
public int GetKeyLength(const char[] key)
{
int max_size = strlen(key) + strlen(JSON_META_LENGTH_KEY) + 1;
char[] length_key = new char[max_size];
Format(length_key, max_size, "%s%s", key, JSON_META_LENGTH_KEY);
int length;
return (this.GetValue(length_key, length)) ? length : -1;
}
/**
* Gets the length of the string stored at an index.
*
* @param index Index to get string length for.
* @returns Length of string at index provided, or -1 if it is not a string/does not exist.
*/
public int GetKeyLengthIndexed(int index)
{
char key[JSON_INDEX_BUFFER_SIZE];
if (! this.GetIndexString(key, sizeof(key), index)) {
return -1;
}
return this.GetKeyLength(key);
}
/**
* Gets whether the key should be hidden from encoding.
*
* @param key Key to get hidden state for.
* @returns Whether or not the key should be hidden.
*/
public bool GetKeyHidden(const char[] key)
{
int max_size = strlen(key) + strlen(JSON_META_HIDDEN_KEY) + 1;
char[] length_key = new char[max_size];
Format(length_key, max_size, "%s%s", key, JSON_META_HIDDEN_KEY);
bool hidden;
return (this.GetValue(length_key, hidden)) ? hidden : false;
}
/**
* Gets whether the index should be hidden from encoding.
*
* @param index Index to get hidden state for.
* @returns Whether or not the index should be hidden.
*/
public bool GetKeyHiddenIndexed(int index)
{
char key[JSON_INDEX_BUFFER_SIZE];
if (! this.GetIndexString(key, sizeof(key), index)) {
return false;
}
return this.GetKeyHidden(key);
}
/**
* @section Internal Setters
*/
/**
* Sets the cell type stored at a key.
*
* @param key Key to set value type for.
* @param type Type to set key to.
* @returns True on success, false otherwise.
*/
public bool SetKeyType(const char[] key, JSON_CELL_TYPE type)
{
int max_size = strlen(key) + strlen(JSON_META_TYPE_KEY) + 1;
char[] type_key = new char[max_size];
Format(type_key, max_size, "%s%s", key, JSON_META_TYPE_KEY);
return this.SetValue(type_key, type);
}
/**
* Sets the cell type stored at an index.
*
* @param index Index to set value type for.
* @param type Type to set index to.
* @returns True on success, false otherwise.
*/
public bool SetKeyTypeIndexed(int index, JSON_CELL_TYPE value)
{
char key[JSON_INDEX_BUFFER_SIZE];
if (! this.GetIndexString(key, sizeof(key), index)) {
return false;
}
return this.SetKeyType(key, value);
}
/**
* Sets the length of the string stored at a key.
*
* @param key Key to set string length for.
* @param length Length to set string to.
* @returns True on success, false otherwise.
*/
public bool SetKeyLength(const char[] key, int length)
{
int max_size = strlen(key) + strlen(JSON_META_LENGTH_KEY) + 1;
char[] length_key = new char[max_size];
Format(length_key, max_size, "%s%s", key, JSON_META_LENGTH_KEY);
return this.SetValue(length_key, length);
}
/**
* Sets the length of the string stored at an index.
*
* @param index Index to set string length for.
* @param length Length to set string to.
* @returns True on success, false otherwise.
*/
public bool SetKeyLengthIndexed(int index, int length)
{
char key[JSON_INDEX_BUFFER_SIZE];
if (! this.GetIndexString(key, sizeof(key), index)) {
return false;
}
return this.SetKeyLength(key, length);
}
/**
* Sets whether the key should be hidden from encoding.
*
* @param key Key to set hidden state for.
* @param hidden Wheter or not the key should be hidden.
* @returns True on success, false otherwise.
*/
public bool SetKeyHidden(const char[] key, bool hidden)
{
int max_size = strlen(key) + strlen(JSON_META_HIDDEN_KEY) + 1;
char[] hidden_key = new char[max_size];
Format(hidden_key, max_size, "%s%s", key, JSON_META_HIDDEN_KEY);
return this.SetValue(hidden_key, hidden);
}
/**
* Sets whether the index should be hidden from encoding.
*
* @param index Index to set hidden state for.
* @param hidden Wheter or not the index should be hidden.
* @returns True on success, false otherwise.
*/
public bool SetKeyHiddenIndexed(int index, bool hidden)
{
char key[JSON_INDEX_BUFFER_SIZE];
if (! this.GetIndexString(key, sizeof(key), index)) {
return false;
}
return this.SetKeyHidden(key, hidden);
}
/**
* @section Getters
*/
// GetValue is implemented natively by StringMap
/**
* Retrieves the value stored at an index.
*
* @param index Index to retrieve value for.
* @param value Variable to store value.
* @returns Value stored at index.
*/
public bool GetValueIndexed(int index, any &value)
{
char key[JSON_INDEX_BUFFER_SIZE];
if (! this.GetIndexString(key, sizeof(key), index)) {
return false;
}
return this.GetValue(key, value);
}
// GetString is implemented natively by StringMap
/**
* Retrieves the string stored at an index.
*
* @param index Index to retrieve string value for.
* @param value String buffer to store output.
* @param max_size Maximum size of string buffer.
* @returns True on success. False if the key is not set, or the key is set as a value or array (not a string).
*/
public bool GetStringIndexed(int index, char[] value, int max_size)
{
char key[JSON_INDEX_BUFFER_SIZE];
if (! this.GetIndexString(key, sizeof(key), index)) {
return false;
}
return this.GetString(key, value, max_size);
}
/**
* Retrieves the int stored at a key.
*
* @param key Key to retrieve int value for.
* @returns Value stored at key.
*/
public int GetInt(const char[] key)
{
int value;
return (this.GetValue(key, value)) ? value : -1;
}
/**
* Retrieves the int stored at an index.
*
* @param index Index to retrieve int value for.
* @returns Value stored at index.
*/
public int GetIntIndexed(int index)
{
char key[JSON_INDEX_BUFFER_SIZE];
if (! this.GetIndexString(key, sizeof(key), index)) {
return -1;
}
return this.GetInt(key);
}
/**
* Retrieves the float stored at a key.
*
* @param key Key to retrieve float value for.
* @returns Value stored at key.
*/
public float GetFloat(const char[] key)
{
float value;
return (this.GetValue(key, value)) ? value : -1.0;
}
/**
* Retrieves the float stored at an index.
*
* @param index Index to retrieve float value for.
* @returns Value stored at index.
*/
public float GetFloatIndexed(int index)
{
char key[JSON_INDEX_BUFFER_SIZE];
if (! this.GetIndexString(key, sizeof(key), index)) {
return -1.0;
}
return this.GetFloat(key);
}
/**
* Retrieves the bool stored at a key.
*
* @param key Key to retrieve bool value for.
* @returns Value stored at key.
*/
public bool GetBool(const char[] key)
{
bool value;
return (this.GetValue(key, value)) ? value : false;
}
/**
* Retrieves the bool stored at an index.
*
* @param index Index to retrieve bool value for.
* @returns Value stored at index.
*/
public bool GetBoolIndexed(int index)
{
char key[JSON_INDEX_BUFFER_SIZE];
if (! this.GetIndexString(key, sizeof(key), index)) {
return false;
}
return this.GetBool(key);
}
/**
* Retrieves the handle stored at a key.
*
* @param key Key to retrieve handle value for.
* @returns Value stored at key.
*/
public Handle GetHandle(const char[] key)
{
Handle value;
return (this.GetValue(key, value)) ? value : null;
}
/**
* Retrieves the handle stored at an index.
*
* @param index Index to retrieve handle value for.
* @returns Value stored at index.
*/
public Handle GetHandleIndexed(int index)
{
char key[JSON_INDEX_BUFFER_SIZE];
if (! this.GetIndexString(key, sizeof(key), index)) {
return null;
}
return this.GetHandle(key);
}
/**
* Retrieves the JSON object stored at a key.
*
* @param key Key to retrieve object value for.
* @returns Value stored at key.
*/
public JSON_Object GetObject(const char[] key)
{
return view_as<JSON_Object>(this.GetHandle(key));
}
/**
* Retrieves the object stored at an index.
*
* @param index Index to retrieve object value for.
* @returns Value stored at index.
*/
public JSON_Object GetObjectIndexed(int index)
{
char key[JSON_INDEX_BUFFER_SIZE];
if (! this.GetIndexString(key, sizeof(key), index)) {
return null;
}
return this.GetObject(key);
}
/**
* @section Setters
*/
/**
* Sets the string stored at a key.
*
* @param key Key to set to string value.
* @param value Value to set.
* @returns True on success, false otherwise.
*/
public bool SetString(const char[] key, const char[] value, bool replace = true)
{
return this.SetString(key, value, replace)
&& this.SetKeyType(key, Type_String)
&& this.SetKeyLength(key, strlen(value));
}
/**
* Sets the string stored at an index.
*
* @param index Index to set to string value.
* @param value Value to set.
* @returns True on success, false otherwise.
*/
public bool SetStringIndexed(int index, const char[] value, bool replace = true)
{
char key[JSON_INDEX_BUFFER_SIZE];
if (! this.GetIndexString(key, sizeof(key), index)) {
return false;
}
return this.SetString(key, value, replace);
}
/**
* Sets the int stored at a key.
*
* @param key Key to set to int value.
* @param value Value to set.
* @returns True on success, false otherwise.
*/
public bool SetInt(const char[] key, int value, bool replace = true)
{
return this.SetValue(key, value, replace)
&& this.SetKeyType(key, Type_Int);
}
/**
* Sets the int stored at an index.
*
* @param index Index to set to int value.
* @param value Value to set.
* @returns True on success, false otherwise.
*/
public bool SetIntIndexed(int index, int value, bool replace = true)
{
char key[JSON_INDEX_BUFFER_SIZE];
if (! this.GetIndexString(key, sizeof(key), index)) {
return false;
}
return this.SetInt(key, value, replace);
}
/**
* Sets the float stored at a key.
*
* @param key Key to set to float value.
* @param value Value to set.
* @returns True on success, false otherwise.
*/
public bool SetFloat(const char[] key, float value, bool replace = true)
{
return this.SetValue(key, value, replace)
&& this.SetKeyType(key, Type_Float);
}
/**
* Sets the float stored at an index.
*
* @param index Index to set to float value.
* @param value Value to set.
* @returns True on success, false otherwise.
*/
public bool SetFloatIndexed(int index, float value, bool replace = true)
{
char key[JSON_INDEX_BUFFER_SIZE];
if (! this.GetIndexString(key, sizeof(key), index)) {
return false;
}
return this.SetFloat(key, value, replace);
}
/**
* Sets the bool stored at a key.
*
* @param key Key to set to bool value.
* @param value Value to set.
* @returns True on success, false otherwise.
*/
public bool SetBool(const char[] key, bool value, bool replace = true)
{
return this.SetValue(key, value, replace)
&& this.SetKeyType(key, Type_Bool);
}
/**
* Sets the bool stored at an index.
*
* @param index Index to set to bool value.
* @param value Value to set.
* @returns True on success, false otherwise.
*/
public bool SetBoolIndexed(int index, bool value, bool replace = true)
{
char key[JSON_INDEX_BUFFER_SIZE];
if (! this.GetIndexString(key, sizeof(key), index)) {
return false;
}
return this.SetBool(key, value, replace);
}
/**
* Sets the handle stored at a key.
*
* @param key Key to set to handle value.
* @param value Value to set.
* @returns True on success, false otherwise.
*/
public bool SetHandle(const char[] key, Handle value = null, bool replace = true)
{
return this.SetValue(key, value, replace)
&& this.SetKeyType(key, Type_Null);
}
/**
* Sets the handle stored at an index.
*
* @param index Index to set to handle value.
* @param value Value to set.
* @returns True on success, false otherwise.
*/
public bool SetHandleIndexed(int index, Handle value = null, bool replace = true)
{
char key[JSON_INDEX_BUFFER_SIZE];
if (! this.GetIndexString(key, sizeof(key), index)) {
return false;
}
return this.SetHandle(key, value, replace);
}
/**
* Sets the object stored at a key.
*
* @param key Key to set to object value.
* @param value Value to set.
* @returns True on success, false otherwise.
*/
public bool SetObject(const char[] key, JSON_Object value, bool replace = true)
{
return this.SetValue(key, value, replace)
&& this.SetKeyType(key, Type_Object);
}
/**
* Sets the object stored at an index.
*
* @param index Index to set to object value.
* @param value Value to set.
* @returns True on success, false otherwise.
*/
public bool SetObjectIndexed(int index, JSON_Object value, bool replace = true)
{
char key[JSON_INDEX_BUFFER_SIZE];
if (! this.GetIndexString(key, sizeof(key), index)) {
return false;
}
return this.SetObject(key, value, replace);
}
/**
* @section Array setters.
*/
/**
* Pushes a string to the end of the array.
*
* @param value Value to push.
* @returns True on success, false otherwise.
*/
public bool PushString(const char[] value)
{
return this.SetStringIndexed(this.CurrentIndex, value)
&& this.IncrementIndex();
}
/**
* Pushes an int to the end of the array.
*
* @param value Value to push.
* @returns True on success, false otherwise.
*/
public bool PushInt(int value)
{
return this.SetIntIndexed(this.CurrentIndex, value)
&& this.IncrementIndex();
}
/**
* Pushes a float to the end of the array.
*
* @param value Value to push.
* @returns True on success, false otherwise.
*/
public bool PushFloat(float value)
{
return this.SetFloatIndexed(this.CurrentIndex, value)
&& this.IncrementIndex();
}
/**
* Pushes a bool to the end of the array.
*
* @param value Value to push.
* @returns True on success, false otherwise.
*/
public bool PushBool(bool value)
{
return this.SetBoolIndexed(this.CurrentIndex, value)
&& this.IncrementIndex();
}
/**
* Pushes a handle to the end of the array.
*
* @param value Value to push.
* @returns True on success, false otherwise.
*/
public bool PushHandle(Handle value = null)
{
return this.SetHandleIndexed(this.CurrentIndex, value)
&& this.IncrementIndex();
}
/**
* Pushes an object to the end of the array.
*
* @param value Value to push.
* @returns True on success, false otherwise.
*/
public bool PushObject(JSON_Object value)
{
return this.SetObjectIndexed(this.CurrentIndex, value)
&& this.IncrementIndex();
}
/**
* @section Generic.
*/
/**
* Finds the index of a value in the array.
*
* @param value Value to search for.
* @returns The index of the value if it is found, -1 otherwise.
*/
public int IndexOf(any value)
{
any current;
for (int i = 0; i < this.CurrentIndex; ++i) {
if (this.GetValueIndexed(i, current) && value == current) {
return i;
}
}
return -1;
}
/**
* Finds the index of a string in the array.
*
* @param value String to search for.
* @returns The index of the string if it is found, -1 otherwise.
*/
public int IndexOfString(const char[] value)
{
for (int i = 0; i < this.CurrentIndex; ++i) {
if (this.GetKeyTypeIndexed(i) != Type_String) {
continue;
}
int current_size = this.GetKeyLengthIndexed(i) + 1;
char[] current = new char[current_size];
this.GetStringIndexed(i, current, current_size);
if (StrEqual(value, current)) {
return i;
}
}
return -1;
}
/**
* Determines whether the array contains a value.
*
* @param value Value to search for.
* @returns True if the value is found, false otherwise.
*/
public bool Contains(any value)
{
return this.IndexOf(value) != -1;
}
/**
* Determines whether the array contains a string.
*
* @param value String to search for.
* @returns True if the string is found, false otherwise.
*/
public bool ContainsString(const char[] value)
{
return this.IndexOfString(value) != -1;
}
/**
* Removes an item from the object by key.
*
* @param key Key of object to remove.
* @returns True on success, false if the value was never set.
*/
public bool Remove(const char[] key) {
static char meta_keys[][] = {
JSON_META_TYPE_KEY, JSON_META_LENGTH_KEY, JSON_META_HIDDEN_KEY
};
// create a new char[] which will fit the longest meta-key
int meta_key_size = strlen(key) + 8;
char[] meta_key = new char[meta_key_size];
// view ourselves as a StringMap so we can call underlying Remove() method
StringMap self = view_as<StringMap>(this);
bool success = true;
for (int i = 0; i < sizeof(meta_keys); ++i) {
Format(meta_key, meta_key_size, "%s%s", key, meta_keys[i]);
if (this.HasKey(meta_key)) {
success = success && self.Remove(meta_key);
}
}
return success && self.Remove(key);
}
/**
* Removes a key and its related meta-keys from the object.
*
* @param key Key to remove.
* @returns True on success, false if the value was never set.
*/
public bool RemoveIndexed(int index)
{
if (! this.HasIndex(index)) {
return false;
}
char key[JSON_INDEX_BUFFER_SIZE];
if (! this.GetIndexString(key, sizeof(key), index)) {
return false;
}
if (! this.Remove(key)) {
return false;
}
for (int i = index + 1; i < this.CurrentIndex; ++i) {
if (! this.GetIndexString(key, sizeof(key), i)) {
return false;
}
int target = i - 1;
JSON_CELL_TYPE type = this.GetKeyTypeIndexed(i);
switch (type) {
case Type_String: {
int str_length = this.GetKeyLengthIndexed(i);
char[] str_value = new char[str_length];
this.GetStringIndexed(i, str_value, str_length + 1);
this.SetStringIndexed(target, str_value);
}
case Type_Int: {
this.SetIntIndexed(target, this.GetIntIndexed(i));
}
case Type_Float: {
this.SetFloatIndexed(target, this.GetFloatIndexed(i));
}
case Type_Bool: {
this.SetBoolIndexed(target, this.GetBoolIndexed(i));
}
case Type_Null: {
this.SetHandleIndexed(target, this.GetHandleIndexed(i));
}
case Type_Object: {
this.SetObjectIndexed(target, this.GetObjectIndexed(i));
}
}
if (this.GetKeyHiddenIndexed(i)) {
this.SetKeyHiddenIndexed(target, true);
}
this.Remove(key);
}
this.CurrentIndex -= 1;
return true;
}
/**
* Encodes the instance into its string representation.
*
* @param output String buffer to store output.
* @param max_size Maximum size of string buffer.
* @param pretty_print Should the output be pretty printed (newlines and spaces)? [default: false]
* @param depth The current depth of the encoder. [default: 0]
*/
public void Encode(char[] output, int max_size, bool pretty_print = false, int depth = 0)
{
json_encode(this, output, max_size, pretty_print, depth);
}
/**
* Decodes a JSON string into this object.
*
* @param buffer Buffer to decode.
*/
public void Decode(const char[] buffer)
{
json_decode(buffer, this);
}
/**
* Recursively cleans up the object and any objects referenced within.
*/
public void Cleanup()
{
json_cleanup(this);
}
};
|