-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlibJSON.axi
More file actions
1621 lines (1518 loc) · 49.6 KB
/
libJSON.axi
File metadata and controls
1621 lines (1518 loc) · 49.6 KB
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
PROGRAM_NAME='libJSON v2_0'
// ---------------------------------------------------------------------------------------------------------------------
// LIBRARY: libJSON
// AUTHOR: sentry07@gmail.com
// PURPOSE: To provide functions to verify, parse, and build JSON encoded objects
//
// THIS LIBRARY RELIES ON THE FACT THAT FUNCTION PARAMETERS ARE BYREF, SO MOST DATA IS RETURNED THROUGH PARAMETERS
// MAKE SURE YOU READ THE NOTES ON ALL THE FUNCTIONS BEFORE USING THEM
//
// ---------------------------------------------------------------------------------------------------------------------
// LIBRARY NOTES:
// The two main functions you will probably use from this library are JSON_ParseObject and JSON_ParseArray.
//
// JSON_ParseObject takes in the JSON string and the return object as parameters, and returns the number of KV pairs the
// function parsed. JSON_ParseObject will call JSON_Validate before parsing to make sure it's a valid object, so calling
// JSON_Validate on a string is not necessary unless you are checking for a valid object before calling JSON_Parse.
// JSON_Validate returns compacted JSON with all extra whitespace removed. The resulting _JSON_Object has the K:V pair
// count in it, as well as an array of Key:Value pair objects, which include the value type. Please see the structures
// below.
//
// JSON_ParseArray takes a JSON array string and the return object as parameters, and returns the number of items in the
// array parsed.
//
// ---------------------------------------------------------------------------------------------------------------------
//
// Valid JSON data is repesented in Key:Value pairs, where the Key is a string literal and Value can be one of:
// * String literal (enclosed in double quotes)
// * positive or negative integer or float (non-enclosed)
// * an array of values (enclosed in square brackets)
// * a nested JSON object (enclosed in squigly brackets)
// * True/False (true or false, non-enclosed)
// * Null (null, non-enclosed)
//
// ---------------------------------------------------------------------------------------------------------------------
// Version Notes:
// v2_0: A full rewrite from the ground up to make using JSON data easier and parse properly instead of the hacky
// stuff I was doing before
// v1_0: First version that was very hard to use and relied on all values to be strings
// ---------------------------------------------------------------------------------------------------------------------
// User Structures:
//
// STRUCTURE _JSON_Object
// {
// INTEGER Count // Number of Key:Value pairs in this object
// _KVPair KV[_JSON_MaxPairs] // The actual Key:Value data
// STRUCTURE _KVPair
// {
// CHAR K[_JSON_KeySize] // Key name
// CHAR V[_JSON_ValueSize] // Value data
// INTEGER nType // Value type
// }
// }
//
// STRUCTURE _JSON_Array
// {
// INTEGER Count // Number of values stored in array
// _Value List[_JSON_ArraySize] // Value storage
// STRUCTURE _Value
// {
// CHAR Value[_JSON_ValueSize] // Actual value
// INTEGER nType // Value type
// }
// }
// ---------------------------------------------------------------------------------------------------------------------
// Functions:
// DEFINE_FUNCTION CHAR[] _JSON_TrimWhiteSpace (CHAR cInput[])
// DEFINE_FUNCTION INTEGER JSON_FindObjectInString (CHAR cInput[],CHAR cJSON_String[])
// DEFINE_FUNCTION INTEGER JSON_ParseObject (CHAR cInput[],_JSON_Object jObject)
// DEFINE_FUNCTION INTEGER JSON_Validate (CHAR cInput[],CHAR cCleaned[])
// DEFINE_FUNCTION INTEGER JSON_ParseValidObject (CHAR cInput[],_JSON_Object jObject)
// DEFINE_FUNCTION INTEGER JSON_ParseArray (CHAR cInput[],_JSON_Array jReturn)
// DEFINE_FUNCTION INTEGER JSON_HasKey (_JSON_Object jObject,CHAR cKeyName[])
// DEFINE_FUNCTION CHAR[] JSON_GetValue (_JSON_Object jObject,CHAR cKeyName[])
// DEFINE_FUNCTION SINTEGER JSON_SetValue (_JSON_Object jObject,CHAR cKeyName[],CHAR cValue[],INTEGER nType)
// DEFINE_FUNCTION SINTEGER JSON_DeleteKey (_JSON_Object jObject,CHAR cKeyName[])
// DEFINE_FUNCTION INTEGER JSON_GetType (_JSON_Object jObject,CHAR cKeyName[])
// DEFINE_FUNCTION CHAR[] JSON_ObjectToString (_JSON_Object jObject)
// DEFINE_FUNCTION CHAR[] JSON_ArrayToString (_JSON_Array jArray)
// DEFINE_FUNCTION INTEGER JSON_ClearObject (_JSON_Object jObject)
// DEFINE_FUNCTION CHAR[] NumArrayToJSON (SLONG nArray[],INTEGER nLength)
// DEFINE_FUNCTION CHAR[] StringArrayToJSON (CHAR cArray[][],INTEGER nLength)
// ---------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------
// JSON Constants
// ---------------------------------------------------------------------------------------------------------------------
DEFINE_CONSTANT
CHAR JSON_WhiteSpace[] = { $20, $0D, $0A, $09 } // Space literal, CR, LF, Tab
// ---------------------------------------------------------------------------------------------------------------------
// JSON Object size constants
// ---------------------------------------------------------------------------------------------------------------------
INTEGER _JSON_InputSize = 20000 // Size of buffer for JSON string
INTEGER _JSON_MaxPairs = 50 // Max number of Key:Value pairs in the _JSON_Object Structure; adjust this if you get OutOfMemory error
INTEGER _JSON_KeySize = 50 // String size for all Keys
INTEGER _JSON_ValueSize = 2000 // String size for all Values
INTEGER _JSON_ArraySize = 64 // Max number of values in _JSON_Array
// ---------------------------------------------------------------------------------------------------------------------
// Data type constants
// ---------------------------------------------------------------------------------------------------------------------
INTEGER _JSON_IsObject = 1
INTEGER _JSON_IsString = 2
INTEGER _JSON_IsNumber = 3
INTEGER _JSON_IsArray = 4
INTEGER _JSON_IsBool = 5
INTEGER _JSON_IsNull = 6
CHAR _JSON_Types[][10] = {
'Object',
'String',
'Number',
'Array',
'Bool',
'Null'
}
// ---------------------------------------------------------------------------------------------------------------------
// Error/Status codes
// ---------------------------------------------------------------------------------------------------------------------
SINTEGER _JSON_Success = 0
SINTEGER _JSON_KeyNotFound = -1 // GetValue, DeleteKey
SINTEGER _JSON_OutOfMemory = -2 // SetValue
SINTEGER _JSON_InvalidValue = -3 // SetValue
// ---------------------------------------------------------------------------------------------------------------------
// JSON Structures
// ---------------------------------------------------------------------------------------------------------------------
DEFINE_TYPE
STRUCTURE _Value
{
CHAR Value[_JSON_ValueSize] // Actual value
INTEGER nType // Value type
}
STRUCTURE _JSON_Array
{
INTEGER Count // Number of values stored in array
_Value List[_JSON_ArraySize] // Value storage
}
// Basic Key:Value pair
STRUCTURE _KVPair
{
CHAR K[_JSON_KeySize] // Key name
CHAR V[_JSON_ValueSize] // Value data
INTEGER nType // Value type
}
// The actual object that will be returned
STRUCTURE _JSON_Object
{
INTEGER Count // Number of Key:Value pairs in this object
_KVPair KV[_JSON_MaxPairs] // The actual Key:Value data
}
// ---------------------------------------------------------------------------------------------------------------------
// JSON Parser Variables
// ---------------------------------------------------------------------------------------------------------------------
DEFINE_VARIABLE
VOLATILE INTEGER nJSONDebug = 0
// ---------------------------------------------------------------------------------------------------------------------
// Helper Functions
// ---------------------------------------------------------------------------------------------------------------------
DEFINE_FUNCTION CHAR[_JSON_InputSize] _JSON_TrimWhiteSpace(CHAR cInput[])
// ---------------------------------------------------------------------------------------------------------------------
// FUNCTION: _JSON_TrimWhiteSpace
// PURPOSE: Special version of _TrimWhiteSpace with bigger return array size
// Trims all whitespace characters from the beginning and end of string
//
// EXAMPLE: cReturn = _JSON_TrimWhiteSpace(' Hello ')
// RETURNS: 'Hello'
// ---------------------------------------------------------------------------------------------------------------------
{
STACK_VAR CHAR cTempInput[_JSON_InputSize]
cTempInput = cInput
WHILE(FIND_STRING(JSON_WhiteSpace,LEFT_STRING(cTempInput,1),1))
{
cTempInput = RIGHT_STRING(cTempInput,LENGTH_STRING(cTempInput)-1) // Trim first byte
}
WHILE(FIND_STRING(JSON_WhiteSpace,RIGHT_STRING(cTempInput,1),1))
{
cTempInput = LEFT_STRING(cTempInput,LENGTH_STRING(cTempInput)-1) // Trim last byte
}
RETURN cTempInput
}
// ---------------------------------------------------------------------------------------------------------------------
// JSON Functions
// ---------------------------------------------------------------------------------------------------------------------
DEFINE_FUNCTION INTEGER JSON_ParseObject (CHAR cInput[],_JSON_Object jObject)
// ---------------------------------------------------------------------------------------------------------------------
// FUNCTION: JSON_ParseObject
// PURPOSE: Replaces the old JSON_ParseObject; acts as a wrapper for the Validate and Parse functions so that we can
// use JSON_ParseValidObject without having to run the validation routine
//
// EXAMPLE: nResult = JSON_ParseObject(cInput,jObject)
// RETURNS: 1 if valid; cJSON_String is updated with JSON string if found
// 0 if not valid; cJSON_String is updated with error
// ---------------------------------------------------------------------------------------------------------------------
{
STACK_VAR INTEGER nReturn // Return value from JSON_Validate
STACK_VAR CHAR cTempInput[_JSON_InputSize] // Copy cInput so we're not modifying the input parameter
STACK_VAR CHAR cValidated[_JSON_InputSize] // The validated JSON string or error
cTempInput = cInput
nReturn = JSON_Validate(cTempInput,cValidated) // Check the string to make sure it's valid
IF (!nReturn) // The JSON was not valid; return the error
{
SEND_STRING 0,"'[JSON] Validator returned the following error: ',cValidated"
RETURN 0
}
ELSE // The JSON was valid, parse it
{
nReturn = JSON_ParseValidObject(cValidated,jObject)
RETURN nReturn
}
}
DEFINE_FUNCTION INTEGER JSON_FindObjectInString(CHAR cInput[],CHAR cJSON_String[])
// ---------------------------------------------------------------------------------------------------------------------
// FUNCTION: JSON_FindObjectInString
// PURPOSE: Traverses a string input to a) verify there is an object in the string and b) return the first valid JSON
// string found.
//
// EXAMPLE: nResult = JSON_FindObjectInString(cDeviceBuffer,cJSON_String)
// RETURNS: 1 if valid; cJSON_String is updated with JSON string if found
// 0 if not valid; cJSON_String is updated with error
// ---------------------------------------------------------------------------------------------------------------------
{
STACK_VAR CHAR cTempInput[_JSON_InputSize] // Temporary string storage
STACK_VAR CHAR cTempOutput[_JSON_InputSize] // Temporary string storage
STACK_VAR INTEGER F1 // Loop over bytes in string
STACK_VAR INTEGER nFind // Find location
STACK_VAR INTEGER bInKey // Currently in the key
STACK_VAR INTEGER bInValue // Currently in the value
STACK_VAR INTEGER bEndOfValue // Value should have ended
STACK_VAR INTEGER bInObj // Currently in an object
STACK_VAR INTEGER bInArray // Currently in an array
STACK_VAR INTEGER BrObj // Current number of open object brackets
STACK_VAR INTEGER BrArray // Current number of open array brackets
cTempInput = _JSON_TrimWhiteSpace(cInput)
F1 = 1
WHILE (F1 <= LENGTH_STRING(cTempInput))
{
SWITCH (cTempInput[F1])
{
CASE $0D:
CASE $0A:
CASE $20:
CASE $09:
{
// Ignore, we don't want this in our validated output
}
CASE '"':
{
nFind = FIND_STRING(cTempInput,'"',F1+1)
IF (nFind)
{
cTempOutput = "cTempOutput,MID_STRING(cTempInput,F1,nFind-(F1-1))"
F1 = nFind
}
ELSE
{
cJSON_String = "'ERROR at position ',ITOA(F1),': No string terminator'"
RETURN 0
}
}
CASE ':':
{
cTempOutput = "cTempOutput,cTempInput[F1]"
IF (bInKey) // A : should only occur between the key and value in a pair. If it happens anywhere else not in a string, it's invalid
{
bInKey = 0
bInValue = 1
}
ELSE
{
cJSON_String = "'ERROR at position ',ITOA(F1),': Colon in value or invalid position'"
RETURN 0
}
}
CASE ',': // A , should only occur between key:value pairs to separate them, or inside an array to separate values. If it happens anywhere else not in a string, it's invalid
{
cTempOutput = "cTempOutput,cTempInput[F1]"
IF (bInArray) // This occured inside an array which is valid
{
// This is fine, ignore it
}
ELSE IF (bInValue) // This occurred at the end of the value, transition back to looking for key
{
bInValue = 0
bInKey = 1
}
ELSE
{
cJSON_String = "'ERROR at position ',ITOA(F1),': Comma only valid between key:value or array values.'"
RETURN 0
}
}
CASE '{': // Start of object
{
cTempOutput = "cTempOutput,cTempInput[F1]"
BrObj++
bInObj = 1
bInArray = 0
bInKey = 1
bInValue = 0
}
CASE '}': // End of object
{
cTempOutput = "cTempOutput,cTempInput[F1]"
IF (BrObj) // Make sure we've entered an object before this point; if not, it's not valid JSON
{
BrObj--
bInObj = 0
}
ELSE
{
cJSON_String = "'ERROR at position ',ITOA(F1),': End of object unexpected.'"
RETURN 0
}
}
CASE '[': // Start of array
{
cTempOutput = "cTempOutput,cTempInput[F1]"
IF (bInValue) // Make sure we're looking at the value of the pair; if not, it's not valid JSON
{
BrArray++
bInArray = 1
}
ELSE
{
cJSON_String = "'ERROR at position ',ITOA(F1),': Start of array only valid in value.'"
RETURN 0
}
}
CASE ']':
{
cTempOutput = "cTempOutput,cTempInput[F1]"
IF (BrArray) // Make sure we've entered an array before this point; if not, it's not valid JSON
{
BrArray--
bInArray = 0
}
ELSE
{
cJSON_String = "'ERROR at position ',ITOA(F1),': End of array unexpected.'"
RETURN 0
}
}
CASE 'T': // True
CASE 't': // true
{
IF (bInValue)
{
IF (LOWER_STRING(MID_STRING(cTempInput,F1,4)) == 'true')
{
cTempOutput = "cTempOutput,'true'"
F1 = F1 + 3
}
ELSE
{
cJSON_String = "'ERROR at position ',ITOA(F1),': Unexpected character t.'"
RETURN 0
}
}
ELSE
{
cJSON_String = "'ERROR at position ',ITOA(F1),': Unexpected character t.'"
RETURN 0
}
}
CASE 'F': // False
CASE 'f': // false
{
IF (bInValue)
{
IF (LOWER_STRING(MID_STRING(cTempInput,F1,5)) == 'false')
{
cTempOutput = "cTempOutput,'false'"
F1 = F1 + 4
}
ELSE
{
cJSON_String = "'ERROR at position ',ITOA(F1),': Unexpected character f.'"
RETURN 0
}
}
ELSE
{
cJSON_String = "'ERROR at position ',ITOA(F1),': Unexpected character f.'"
RETURN 0
}
}
CASE 'N': // Null
CASE 'n': // null
{
IF (bInValue)
{
IF (LOWER_STRING(MID_STRING(cTempInput,F1,4)) == 'null')
{
cTempOutput = "cTempOutput,'null'"
F1 = F1 + 3
}
ELSE
{
cJSON_String = "'ERROR at position ',ITOA(F1),': Unexpected character n.'"
RETURN 0
}
}
ELSE
{
cJSON_String = "'ERROR at position ',ITOA(F1),': Unexpected character n.'"
RETURN 0
}
}
CASE '-': // Number Value
CASE '1': // Number value
CASE '2': // Number value
CASE '3': // Number value
CASE '4': // Number value
CASE '5': // Number value
CASE '6': // Number value
CASE '7': // Number value
CASE '8': // Number value
CASE '9': // Number value
CASE '0': // Number value
{
cTempOutput = "cTempOutput,cTempInput[F1]"
IF (!bInValue) // Only time a number outside a string is valid is in the value, also in an array which is in the value
{
cJSON_String = "'ERROR at position ',ITOA(F1),': Unexpected integer in key.'"
RETURN 0
}
}
CASE '.': // A period is only valid outside a string in a number value
{
cTempOutput = "cTempOutput,cTempInput[F1]"
IF (!bInValue) // Only time a period outside a string is valid is in the value when it's a number
{
cJSON_String = "'ERROR at position ',ITOA(F1),': Unexpected period in key.'"
RETURN 0
}
}
DEFAULT:
{
cJSON_String = "'ERROR at position ',ITOA(F1),': Unexpected character.'"
RETURN 0
}
}
IF (!BrObj && !BrArray)
{
cJSON_String = cTempOutput
cInput = RIGHT_STRING(cTempInput,LENGTH_STRING(cTempInput) - F1)
RETURN 1
}
ELSE
{
F1++
}
}
cJSON_String = "'End of string. No valid JSON strings found.'"
RETURN 0
}
DEFINE_FUNCTION INTEGER JSON_Validate(CHAR cInput[],CHAR cCleaned[])
// ---------------------------------------------------------------------------------------------------------------------
// FUNCTION: JSON_Validate
// PURPOSE: Parses cInput to validate that it is a valid JSON structure; this also cleans out all unnecessary
// whitespace characters and returns the cleaned string in the cCleaned parameter
//
// EXAMPLE: nResult = JSON_Validate(cDeviceBuffer,cValidated)
// RETURNS: 1 if valid, 0 if not; cValidated is updated with compacted text
// ---------------------------------------------------------------------------------------------------------------------
{
STACK_VAR CHAR cTempInput[_JSON_InputSize] // Temporary string storage
STACK_VAR CHAR cTempOutput[_JSON_InputSize] // Temporary string storage
STACK_VAR INTEGER F1 // Loop over bytes in string
STACK_VAR INTEGER nFind // Find location
STACK_VAR INTEGER bInKey // Currently in the key
STACK_VAR INTEGER bInValue // Currently in the value
STACK_VAR INTEGER bEndOfValue // Value should have ended
STACK_VAR INTEGER bInObj // Currently in an object
STACK_VAR INTEGER bInArray // Currently in an array
STACK_VAR INTEGER BrObj // Current number of open object brackets
STACK_VAR INTEGER BrArray // Current number of open array brackets
cTempInput = _JSON_TrimWhiteSpace(cInput)
F1 = 1
WHILE (F1 <= LENGTH_STRING(cTempInput))
{
SWITCH (cTempInput[F1])
{
CASE $0D:
CASE $0A:
CASE $20:
CASE $09:
{
// Ignore, we don't want this in our validated output
}
CASE '"':
{
nFind = FIND_STRING(cTempInput,'"',F1+1)
IF (nFind)
{
cTempOutput = "cTempOutput,MID_STRING(cTempInput,F1,nFind-(F1-1))"
F1 = nFind
}
ELSE
{
cCleaned = "'ERROR at position ',ITOA(F1),': No string terminator'"
RETURN 0
}
}
CASE ':':
{
cTempOutput = "cTempOutput,cTempInput[F1]"
IF (bInKey) // A : should only occur between the key and value in a pair. If it happens anywhere else not in a string, it's invalid
{
bInKey = 0
bInValue = 1
}
ELSE
{
cCleaned = "'ERROR at position ',ITOA(F1),': Colon in value or invalid position'"
RETURN 0
}
}
CASE ',': // A , should only occur between key:value pairs to separate them, or inside an array to separate values. If it happens anywhere else not in a string, it's invalid
{
cTempOutput = "cTempOutput,cTempInput[F1]"
IF (bInArray) // This occured inside an array which is valid
{
// This is fine, ignore it
}
ELSE IF (bInValue) // This occurred at the end of the value, transition back to looking for key
{
bInValue = 0
bInKey = 1
}
ELSE
{
cCleaned = "'ERROR at position ',ITOA(F1),': Comma only valid between key:value or array values.'"
RETURN 0
}
}
CASE '{': // Start of object
{
cTempOutput = "cTempOutput,cTempInput[F1]"
BrObj++
bInObj = 1
bInArray = 0
bInKey = 1
bInValue = 0
}
CASE '}': // End of object
{
cTempOutput = "cTempOutput,cTempInput[F1]"
IF (BrObj) // Make sure we've entered an object before this point; if not, it's not valid JSON
{
BrObj--
bInObj = 0
}
ELSE
{
cCleaned = "'ERROR at position ',ITOA(F1),': End of object unexpected.'"
RETURN 0
}
}
CASE '[': // Start of array
{
cTempOutput = "cTempOutput,cTempInput[F1]"
IF (bInValue) // Make sure we're looking at the value of the pair; if not, it's not valid JSON
{
BrArray++
bInArray = 1
}
ELSE
{
cCleaned = "'ERROR at position ',ITOA(F1),': Start of array only valid in value.'"
RETURN 0
}
}
CASE ']':
{
cTempOutput = "cTempOutput,cTempInput[F1]"
IF (BrArray) // Make sure we've entered an array before this point; if not, it's not valid JSON
{
BrArray--
bInArray = 0
}
ELSE
{
cCleaned = "'ERROR at position ',ITOA(F1),': End of array unexpected.'"
RETURN 0
}
}
CASE 'T': // True
CASE 't': // true
{
IF (bInValue)
{
IF (LOWER_STRING(MID_STRING(cTempInput,F1,4)) == 'true')
{
cTempOutput = "cTempOutput,'true'"
F1 = F1 + 3
}
ELSE
{
cCleaned = "'ERROR at position ',ITOA(F1),': Unexpected character t.'"
RETURN 0
}
}
ELSE
{
cCleaned = "'ERROR at position ',ITOA(F1),': Unexpected character t.'"
RETURN 0
}
}
CASE 'F': // False
CASE 'f': // false
{
IF (bInValue)
{
IF (LOWER_STRING(MID_STRING(cTempInput,F1,5)) == 'false')
{
cTempOutput = "cTempOutput,'false'"
F1 = F1 + 4
}
ELSE
{
cCleaned = "'ERROR at position ',ITOA(F1),': Unexpected character f.'"
RETURN 0
}
}
ELSE
{
cCleaned = "'ERROR at position ',ITOA(F1),': Unexpected character f.'"
RETURN 0
}
}
CASE 'N': // Null
CASE 'n': // null
{
IF (bInValue)
{
IF (LOWER_STRING(MID_STRING(cTempInput,F1,4)) == 'null')
{
cTempOutput = "cTempOutput,'null'"
F1 = F1 + 3
}
ELSE
{
cCleaned = "'ERROR at position ',ITOA(F1),': Unexpected character n.'"
RETURN 0
}
}
ELSE
{
cCleaned = "'ERROR at position ',ITOA(F1),': Unexpected character n.'"
RETURN 0
}
}
CASE '-': // Number Value
CASE '1': // Number value
CASE '2': // Number value
CASE '3': // Number value
CASE '4': // Number value
CASE '5': // Number value
CASE '6': // Number value
CASE '7': // Number value
CASE '8': // Number value
CASE '9': // Number value
CASE '0': // Number value
{
cTempOutput = "cTempOutput,cTempInput[F1]"
IF (!bInValue) // Only time a number outside a string is valid is in the value, also in an array which is in the value
{
cCleaned = "'ERROR at position ',ITOA(F1),': Unexpected integer in key.'"
RETURN 0
}
}
CASE '.': // A period is only valid outside a string in a number value
{
cTempOutput = "cTempOutput,cTempInput[F1]"
IF (!bInValue) // Only time a period outside a string is valid is in the value when it's a number
{
cCleaned = "'ERROR at position ',ITOA(F1),': Unexpected period in key.'"
RETURN 0
}
}
DEFAULT:
{
cCleaned = "'ERROR at position ',ITOA(F1),': Unexpected character.'"
RETURN 0
}
}
F1++
}
IF (BrObj || BrArray)
{
cCleaned = "'ERROR at position ',ITOA(F1),': Unexpected end of JSON data.'"
RETURN 0
}
ELSE
{
cCleaned = cTempOutput
RETURN 1
}
}
DEFINE_FUNCTION INTEGER JSON_ParseValidObject(CHAR cValidJSON[],_JSON_Object jObject)
// ---------------------------------------------------------------------------------------------------------------------
// FUNCTION: JSON_ParseValidObject
// PURPOSE: Parses cValidJSON and puts the Key:Value pairs in the _JSON_Object that is passed in the second parameter
//
// EXAMPLE: nResult = JSON_ParseObject(cDeviceBuffer,jObject)
// RETURNS: number of key:value pairs parsed; jObject has the parsed data
// ---------------------------------------------------------------------------------------------------------------------
{
STACK_VAR _JSON_Object jReturn // Local storage of JSON data
STACK_VAR CHAR cTempInput[_JSON_InputSize] // Temporary string storage
STACK_VAR CHAR cValidated[_JSON_InputSize] // Validated JSON data
STACK_VAR CHAR cTemp[5000] // Temporary string storage
STACK_VAR INTEGER nReturn // Return from the validator
STACK_VAR INTEGER F1 // Loop over bytes in string
STACK_VAR INTEGER nFind // Find location
STACK_VAR INTEGER bInKey // Currently in the key
STACK_VAR INTEGER bInValue // Currently in the value
STACK_VAR INTEGER bEndOfValue // Value should have ended
STACK_VAR INTEGER bInObj // Currently in an object
STACK_VAR INTEGER bInChildObj // Currently in a child object
STACK_VAR INTEGER bInArray // Currently in an array
STACK_VAR INTEGER BrObj // Current number of open object brackets
STACK_VAR INTEGER BrChildObj // Current number of open child object brackets
STACK_VAR INTEGER nChildObjStart // Start of the current child object
STACK_VAR INTEGER BrArray // Current number of open array brackets
STACK_VAR INTEGER BrArrayStart // Start of the current array
STACK_VAR INTEGER nCurrentKV // Current KV pair we're processing
cValidated = cValidJSON
IF (jObject.Count)
{
JSON_ClearObject(jObject)
}
nCurrentKV = 1
F1 = 1
WHILE (F1 <= LENGTH_STRING(cValidated)) // Loop through string and look at almost every character
{
SWITCH (cValidated[F1])
{
CASE '"':
{
IF (!bInChildObj && !bInArray) // Looking for strings in either the Key position, or single string values in the Value position
{
nFind = FIND_STRING(cValidated,'"',F1+1)
cTemp = MID_STRING(cValidated,F1+1,nFind-(F1+1))
IF (bInKey)
{
IF (LENGTH_STRING(jReturn.KV[nCurrentKV].K)) // If we've already set a key name in the current index, increment the index; this is where we move to the next K:V pair
{
nCurrentKV++
}
jReturn.KV[nCurrentKV].K = cTemp
}
ELSE IF (bInValue && !bInArray && !bInChildObj)
{
jReturn.KV[nCurrentKV].V = cTemp
jReturn.KV[nCurrentKV].nType = _JSON_IsString
}
F1 = nFind
}
}
CASE ':':
{
IF (!bInChildObj && !bInArray) // Child
{
IF (bInKey) // A : should only occur between the key and value in a pair. If it happens anywhere else not in a string, it's invalid
{
bInKey = 0
bInValue = 1
}
ELSE
{
RETURN 0
}
}
}
CASE ',': // A , should only occur between key:value pairs to separate them, or inside an array to separate values. If it happens anywhere else not in a string, it's invalid
{
IF (!bInChildObj && !bInArray)
{
bInValue = 0
bInKey = 1
}
}
CASE '{': // Start of object
{
IF (nJSONDebug) SEND_STRING 0,"'[JSON] I see a { at ',ITOA(F1),' (bInArray: ',ITOA(bInArray),', bInChildObj: ',ITOA(bInChildObj),')'"
IF (!bInArray)
{
IF (bInChildObj)
{
BrChildObj++
}
ELSE
{
IF (bInValue)
{
BrChildObj++ // This will cause all data to get copied to the value of the current key until we reach the end of the current object
nChildObjStart = F1
bInChildObj = 1
}
ELSE
{
BrObj++
BrChildObj = 0
bInObj = 1
bInArray = 0
bInKey = 1
bInValue = 0
}
}
}
}
CASE '}': // End of object
{
IF (nJSONDebug) SEND_STRING 0,"'[JSON] I see a } at ',ITOA(F1),' (bInArray: ',ITOA(bInArray),', bInChildObj: ',ITOA(bInChildObj),')'"
IF (!bInArray)
{
IF (bInChildObj)
{
BrChildObj --
IF (BrChildObj == 0) // This is the end of the child object
{
nFind = F1
jReturn.KV[nCurrentKV].nType = _JSON_IsObject
jReturn.KV[nCurrentKV].V = MID_STRING(cValidated,nChildObjStart,nFind-(nChildObjStart-1))
nChildObjStart = 0
bInChildObj = 0
}
}
ELSE IF (BrObj) // Make sure we've entered a object before this point; if not, it's not valid JSON
{
BrObj--
bInObj = 0
}
}
}
CASE '[': // Start of array
{
IF (nJSONDebug) SEND_STRING 0,"'[JSON] I see a [ at ',ITOA(F1),' (bInArray: ',ITOA(bInArray),', bInChildObj: ',ITOA(bInChildObj),')'"
IF (!bInChildObj)
{
BrArray++
bInArray = 1
IF (BrArray == 1) // Outer array
{
BrArrayStart = F1
}
IF (nJSONDebug) SEND_STRING 0,"'[JSON] Array start at ',ITOA(F1),': BrArray: ',ITOA(BrArray),', BrArrayStart: ',ITOA(BrArrayStart)"
}
}
CASE ']': // End of Array
{
IF (nJSONDebug) SEND_STRING 0,"'[JSON] I see a ] at ',ITOA(F1),' (bInArray: ',ITOA(bInArray),', bInChildObj: ',ITOA(bInChildObj),')'"
IF (!bInChildObj)
{
BrArray--
IF (nJSONDebug) SEND_STRING 0,"'[JSON] Array stop at ',ITOA(F1),': BrArray: ',ITOA(BrArray),', BrArrayStart: ',ITOA(BrArrayStart)"
IF (BrArray == 0)
{
nFind = F1
jReturn.KV[nCurrentKV].V = MID_STRING(cValidated,BrArrayStart,nFind-(BrArrayStart-1))
jReturn.KV[nCurrentKV].nType = _JSON_IsArray
BrArrayStart = 0
bInArray = 0
}
}
}
CASE 't': // true
{
IF (!bInChildObj && !bInArray)
{
jReturn.KV[nCurrentKV].nType = _JSON_IsBool
jReturn.KV[nCurrentKV].V = 'true'
F1 = F1 + 3
}
}
CASE 'f': // false
{
IF (!bInChildObj && !bInArray)
{
jReturn.KV[nCurrentKV].nType = _JSON_IsBool
jReturn.KV[nCurrentKV].V = 'false'
F1 = F1 + 4
}
}
CASE 'n': // null
{
IF (!bInChildObj && !bInArray)
{
jReturn.KV[nCurrentKV].nType = _JSON_IsNull
jReturn.KV[nCurrentKV].V = 'null'
F1 = F1 + 3
}
}
CASE '-': // Number value
CASE '1': // Number value
CASE '2': // Number value
CASE '3': // Number value
CASE '4': // Number value
CASE '5': // Number value
CASE '6': // Number value
CASE '7': // Number value
CASE '8': // Number value
CASE '9': // Number value
CASE '0': // Number value
{
IF (!bInChildObj && !bInArray && bInValue)
{
nFind = FIND_STRING(cValidated,',',F1) // Find end of number by finding the end of the K:V pair
IF (!nFind)
{
nFind = LENGTH_STRING(cValidated) // Object should end with a } so don't grab that
}
cTemp = MID_STRING(cValidated,F1,nFind-F1)
jReturn.KV[nCurrentKV].nType = _JSON_IsNumber
jReturn.KV[nCurrentKV].V = cTemp
F1 = nFind
bInValue = 0
bInKey = 1
}
}
}
F1++
}
IF (nJSONDebug) SEND_STRING 0,"'[JSON] ',ITOA(F1),' bytes parsed.'"
jReturn.Count = nCurrentKV
jObject = jReturn
RETURN nCurrentKV
}
DEFINE_FUNCTION INTEGER JSON_ParseArray(CHAR cInput[],_JSON_Array jReturn)
// ---------------------------------------------------------------------------------------------------------------------
// FUNCTION: JSON_ParseArray
// PURPOSE: Parses a JSON array string and returns an array object of the values found
// Must be formatted like JSON: [element,element,element]
//
// EXAMPLE: cResult = JSON_ParseArray(cJSONArray,jParsedArray)
// RETURNS: return is number of values in array; jParsedArray is an array object of the values
// ---------------------------------------------------------------------------------------------------------------------
{
STACK_VAR INTEGER F1 // Normal FOR loop var
STACK_VAR INTEGER nFind // Temp search variable
STACK_VAR CHAR cTemp[_JSON_ValueSize] // Temporary string
STACK_VAR CHAR cTempInput[_JSON_ValueSize] // Trimmed copy of the input array
STACK_VAR _JSON_Array jTempArray // Temporary array to store the values
STACK_VAR INTEGER nValueCount // Number of values found so far in array
STACK_VAR INTEGER bInObj // Currently in an object
STACK_VAR INTEGER bInChildObj // Currently in a child object
STACK_VAR INTEGER bInArray // Currently in an array
STACK_VAR INTEGER BrObj // Current number of open object brackets
STACK_VAR INTEGER BrChildObj // Current number of open child object brackets