-
Notifications
You must be signed in to change notification settings - Fork 15
/
json_parser.pkb
1028 lines (921 loc) · 31.9 KB
/
json_parser.pkb
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
CREATE OR REPLACE
PACKAGE BODY json_parser IS
----------------------------------------------------------
-- PRIVATE EXCEPTIONS
----------------------------------------------------------
----------------------------------------------------------
-- PRIVATE TYPES
----------------------------------------------------------
----------------------------------------------------------
-- PRIVATE CONSTANTS
----------------------------------------------------------
----------------------------------------------------------
-- PRIVATE VARIABLES
----------------------------------------------------------
decimalpoint VARCHAR2(1 CHAR) := '.';
----------------------------------------------------------
-- LOCAL MODULES
----------------------------------------------------------
FUNCTION next_char(indx NUMBER, s IN OUT NOCOPY json_src) RETURN VARCHAR2;
FUNCTION next_char2(indx NUMBER, s IN OUT NOCOPY json_src, amount NUMBER DEFAULT 1) RETURN VARCHAR2;
FUNCTION prepareClob(buf IN CLOB) RETURN json_src;
FUNCTION prepareVARCHAR2(buf IN VARCHAR2) RETURN json_src;
FUNCTION lexer(jsrc IN OUT NOCOPY json_src) RETURN lTokens;
PROCEDURE parseMem(tokens lTokens, indx IN OUT PLS_INTEGER, mem_name VARCHAR2, mem_indx NUMBER, theParentID IN OUT BINARY_INTEGER, theLastID IN OUT BINARY_INTEGER, theNodes IN OUT NOCOPY jsonNodes);
----------------------------------------------------------
-- GLOBAL MODULES
----------------------------------------------------------
----------------------------------------------------------
-- updateDecimalPoint (private)
--
PROCEDURE updateDecimalPoint
IS
BEGIN
SELECT SUBSTR(VALUE, 1, 1) INTO decimalpoint FROM nls_session_parameters WHERE parameter = 'NLS_NUMERIC_CHARACTERS';
END updateDecimalPoint;
----------------------------------------------------------
-- next_char (private)
-- type json_src is record (len number, offset number, src varchar2(10), s_clob clob);
--
FUNCTION next_char(indx NUMBER, s IN OUT NOCOPY json_src) RETURN VARCHAR2
IS
BEGIN
IF (indx > s.len) THEN
RETURN NULL;
END IF;
-- right offset?
IF (indx > 4000 + s.offset OR indx < s.offset) THEN
-- load right offset
s.offset := indx - (indx MOD 4000);
s.src := dbms_lob.substr(s.s_clob, 4000, s.offset + 1);
END IF;
-- read from s.src
RETURN substr(s.src, indx - s.offset, 1);
END next_char;
----------------------------------------------------------
-- next_char2 (private)
--
FUNCTION next_char2(indx NUMBER, s IN OUT NOCOPY json_src, amount NUMBER DEFAULT 1) RETURN VARCHAR2
IS
buf VARCHAR2(32767) := '';
BEGIN
FOR I IN 1 .. amount LOOP
buf := buf || next_char(indx-1+i,s);
END LOOP;
RETURN buf;
END next_char2;
----------------------------------------------------------
-- prepareClob (private)
--
function prepareClob(buf CLOB) RETURN json_src
IS
temp json_src;
BEGIN
temp.s_clob := buf;
temp.offset := 0;
temp.src := dbms_lob.substr(buf, 4000, temp.offset + 1);
temp.len := dbms_lob.getlength(buf);
RETURN temp;
END prepareClob;
----------------------------------------------------------
-- prepareVarchar2 (private)
--
FUNCTION prepareVarchar2(buf VARCHAR2) RETURN json_src
IS
temp json_src;
begin
temp.s_clob := buf;
temp.offset := 0;
temp.src := substr(buf, 1, 4000);
temp.len := length(buf);
RETURN temp;
END prepareVarchar2;
----------------------------------------------------------
-- debug (private)
--
PROCEDURE debug(text VARCHAR2)
IS
BEGIN
--dbms_output.put_line(text);
NULL;
END debug;
--
-- START SCANNER
-- *************
----------------------------------------------------------
-- s_error (private)
--
PROCEDURE s_error(text VARCHAR2, line NUMBER, col NUMBER)
IS
BEGIN
raise_application_error(-20100, 'JSON Scanner exception @ line: '||line||' column: '||col||' - '||text);
END s_error;
----------------------------------------------------------
-- s_error (private)
--
PROCEDURE s_error(text VARCHAR2, tok rToken)
IS
BEGIN
raise_application_error(-20100, 'JSON Scanner exception @ line: '||tok.line||' column: '||tok.col||' - '||text);
END s_error;
----------------------------------------------------------
-- mt (private)
--
FUNCTION mt(t VARCHAR2, l PLS_INTEGER, c PLS_INTEGER, d VARCHAR2) RETURN rToken
IS
token rToken;
BEGIN
token.type_name := t;
token.line := l;
token.col := c;
token.data := d;
RETURN token;
END mt;
----------------------------------------------------------
-- lexNumber (private)
--
FUNCTION lexNumber(jsrc IN OUT NOCOPY json_src, tok IN OUT NOCOPY rToken, indx IN OUT NOCOPY PLS_INTEGER) RETURN PLS_INTEGER
IS
numbuf varchar2(4000) := '';
buf varchar2(4);
checkLoop boolean;
BEGIN
buf := next_char(indx, jsrc);
if(buf = '-') then numbuf := '-'; indx := indx + 1; end if;
buf := next_char(indx, jsrc);
--0 or [1-9]([0-9])*
if(buf = '0') then
numbuf := numbuf || '0'; indx := indx + 1;
buf := next_char(indx, jsrc);
elsif(buf >= '1' and buf <= '9') then
numbuf := numbuf || buf; indx := indx + 1;
--read digits
buf := next_char(indx, jsrc);
while(buf >= '0' and buf <= '9') loop
numbuf := numbuf || buf; indx := indx + 1;
buf := next_char(indx, jsrc);
end loop;
end if;
--fraction
if(buf = '.') then
numbuf := numbuf || buf; indx := indx + 1;
buf := next_char(indx, jsrc);
checkLoop := FALSE;
while(buf >= '0' and buf <= '9') loop
checkLoop := TRUE;
numbuf := numbuf || buf; indx := indx + 1;
buf := next_char(indx, jsrc);
end loop;
if(not checkLoop) then
s_error('Expected: digits in fraction', tok);
end if;
end if;
--exp part
if(buf in ('e', 'E')) then
numbuf := numbuf || buf; indx := indx + 1;
buf := next_char(indx, jsrc);
if(buf = '+' or buf = '-') then
numbuf := numbuf || buf; indx := indx + 1;
buf := next_char(indx, jsrc);
end if;
checkLoop := FALSE;
while(buf >= '0' and buf <= '9') loop
checkLoop := TRUE;
numbuf := numbuf || buf; indx := indx + 1;
buf := next_char(indx, jsrc);
end loop;
if(not checkLoop) then
s_error('Expected: digits in exp', tok);
end if;
end if;
tok.data := numbuf;
return indx;
END lexNumber;
----------------------------------------------------------
-- lexName (private)
--
-- [a-zA-Z]([a-zA-Z0-9])*
--
FUNCTION lexName(jsrc IN OUT NOCOPY json_src, tok IN OUT NOCOPY rToken, indx IN OUT NOCOPY PLS_INTEGER) RETURN PLS_INTEGER
IS
varbuf varchar2(32767) := '';
buf varchar(4);
num number;
BEGIN
buf := next_char(indx, jsrc);
while(REGEXP_LIKE(buf, '^[[:alnum:]\_]$', 'i')) loop
varbuf := varbuf || buf;
indx := indx + 1;
buf := next_char(indx, jsrc);
if (buf is null) then
goto retname;
--debug('Premature string ending');
end if;
end loop;
<<retname>>
--could check for reserved keywords here
--debug(varbuf);
tok.data := varbuf;
return indx-1;
END lexName;
----------------------------------------------------------
-- updateClob (private)
--
PROCEDURE updateClob(v_extended IN OUT NOCOPY CLOB, v_str VARCHAR2)
IS
BEGIN
dbms_lob.writeappend(v_extended, LENGTH(v_str), v_str);
END updateClob;
----------------------------------------------------------
-- lexString (private)
--
FUNCTION lexString(jsrc IN OUT NOCOPY json_src, tok IN OUT NOCOPY rToken, indx IN OUT NOCOPY PLS_INTEGER, endChar CHAR) RETURN PLS_INTEGER
IS
v_extended clob := null; v_count number := 0;
varbuf varchar2(32767) := '';
buf varchar(4);
wrong boolean;
begin
indx := indx +1;
buf := next_char(indx, jsrc);
while(buf != endChar) loop
--clob control
if(v_count > 8191) then --crazy oracle error (16383 is the highest working length with unistr - 8192 choosen to be safe)
if(v_extended is null) then
v_extended := empty_clob();
dbms_lob.createtemporary(v_extended, true);
end if;
updateClob(v_extended, unistr(varbuf));
varbuf := ''; v_count := 0;
end if;
if(buf = Chr(13) or buf = CHR(9) or buf = CHR(10)) then
s_error('Control characters not allowed (CHR(9),CHR(10)CHR(13))', tok);
end if;
if(buf = '\') then
--varbuf := varbuf || buf;
indx := indx + 1;
buf := next_char(indx, jsrc);
case
when buf in ('\') then
varbuf := varbuf || buf || buf; v_count := v_count + 2;
indx := indx + 1;
buf := next_char(indx, jsrc);
when buf in ('"', '/') then
varbuf := varbuf || buf; v_count := v_count + 1;
indx := indx + 1;
buf := next_char(indx, jsrc);
when buf = '''' then
if(json_strict = false) then
varbuf := varbuf || buf; v_count := v_count + 1;
indx := indx + 1;
buf := next_char(indx, jsrc);
else
s_error('strictmode - expected: " \ / b f n r t u ', tok);
end if;
when buf in ('b', 'f', 'n', 'r', 't') then
--backspace b = U+0008
--formfeed f = U+000C
--newline n = U+000A
--carret r = U+000D
--tabulator t = U+0009
case buf
when 'b' then varbuf := varbuf || chr(8);
when 'f' then varbuf := varbuf || chr(12);
when 'n' then varbuf := varbuf || chr(10);
when 'r' then varbuf := varbuf || chr(13);
when 't' then varbuf := varbuf || chr(9);
end case;
--varbuf := varbuf || buf;
v_count := v_count + 1;
indx := indx + 1;
buf := next_char(indx, jsrc);
when buf = 'u' then
--four hexidecimal chars
declare
four varchar2(4);
begin
four := next_char2(indx+1, jsrc, 4);
wrong := FALSE;
if(upper(substr(four, 1,1)) not in ('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','a','b','c','d','e','f')) then wrong := TRUE; end if;
if(upper(substr(four, 2,1)) not in ('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','a','b','c','d','e','f')) then wrong := TRUE; end if;
if(upper(substr(four, 3,1)) not in ('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','a','b','c','d','e','f')) then wrong := TRUE; end if;
if(upper(substr(four, 4,1)) not in ('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','a','b','c','d','e','f')) then wrong := TRUE; end if;
if(wrong) then
s_error('expected: " \u([0-9][A-F]){4}', tok);
end if;
-- varbuf := varbuf || buf || four;
varbuf := varbuf || '\'||four;--chr(to_number(four,'XXXX'));
v_count := v_count + 5;
indx := indx + 5;
buf := next_char(indx, jsrc);
end;
else
s_error('expected: " \ / b f n r t u ', tok);
end case;
else
varbuf := varbuf || buf; v_count := v_count + 1;
indx := indx + 1;
buf := next_char(indx, jsrc);
end if;
end loop;
if (buf is null) then
s_error('string ending not found', tok);
--debug('Premature string ending');
end if;
--debug(varbuf);
--dbms_output.put_line(varbuf);
if(v_extended is not null) then
updateClob(v_extended, unistr(varbuf));
tok.data_overflow := v_extended;
tok.data := dbms_lob.substr(v_extended, 1, 32767);
else
tok.data := unistr(varbuf);
end if;
return indx;
end lexString;
----------------------------------------------------------
-- lexer (private)
--
-- scanner tokens:
-- '{', '}', ',', ':', '[', ']', STRING, NUMBER, TRUE, FALSE, NULL
--
FUNCTION lexer(jsrc IN OUT NOCOPY json_src) RETURN lTokens
IS
tokens lTokens;
indx pls_integer := 1;
tok_indx pls_integer := 1;
buf varchar2(4);
lin_no number := 1;
col_no number := 0;
BEGIN
while (indx <= jsrc.len) loop
--read into buf
buf := next_char(indx, jsrc);
col_no := col_no + 1;
--convert to switch case
case
when buf = '{' then tokens(tok_indx) := mt('{', lin_no, col_no, null); tok_indx := tok_indx + 1;
when buf = '}' then tokens(tok_indx) := mt('}', lin_no, col_no, null); tok_indx := tok_indx + 1;
when buf = ',' then tokens(tok_indx) := mt(',', lin_no, col_no, null); tok_indx := tok_indx + 1;
when buf = ':' then tokens(tok_indx) := mt(':', lin_no, col_no, null); tok_indx := tok_indx + 1;
when buf = '[' then tokens(tok_indx) := mt('[', lin_no, col_no, null); tok_indx := tok_indx + 1;
when buf = ']' then tokens(tok_indx) := mt(']', lin_no, col_no, null); tok_indx := tok_indx + 1;
when buf = 't' then
if(next_char2(indx, jsrc, 4) != 'true') then
if(json_strict = false and REGEXP_LIKE(buf, '^[[:alpha:]]$', 'i')) then
tokens(tok_indx) := mt('STRING', lin_no, col_no, null);
indx := lexName(jsrc, tokens(tok_indx), indx);
col_no := col_no + length(tokens(tok_indx).data) + 1;
tok_indx := tok_indx + 1;
else
s_error('Expected: ''true''', lin_no, col_no);
end if;
else
tokens(tok_indx) := mt('TRUE', lin_no, col_no, null); tok_indx := tok_indx + 1;
indx := indx + 3;
col_no := col_no + 3;
end if;
when buf = 'n' then
if(next_char2(indx, jsrc, 4) != 'null') then
if(json_strict = false and REGEXP_LIKE(buf, '^[[:alpha:]]$', 'i')) then
tokens(tok_indx) := mt('STRING', lin_no, col_no, null);
indx := lexName(jsrc, tokens(tok_indx), indx);
col_no := col_no + length(tokens(tok_indx).data) + 1;
tok_indx := tok_indx + 1;
else
s_error('Expected: ''null''', lin_no, col_no);
end if;
else
tokens(tok_indx) := mt('NULL', lin_no, col_no, null); tok_indx := tok_indx + 1;
indx := indx + 3;
col_no := col_no + 3;
end if;
when buf = 'f' then
if(next_char2(indx, jsrc, 5) != 'false') then
if(json_strict = false and REGEXP_LIKE(buf, '^[[:alpha:]]$', 'i')) then
tokens(tok_indx) := mt('STRING', lin_no, col_no, null);
indx := lexName(jsrc, tokens(tok_indx), indx);
col_no := col_no + length(tokens(tok_indx).data) + 1;
tok_indx := tok_indx + 1;
else
s_error('Expected: ''false''', lin_no, col_no);
end if;
else
tokens(tok_indx) := mt('FALSE', lin_no, col_no, null); tok_indx := tok_indx + 1;
indx := indx + 4;
col_no := col_no + 4;
end if;
/* -- 9 = TAB, 10 = \n, 13 = \r (Linux = \n, Windows = \r\n, Mac = \r */
when (buf = Chr(10)) then --linux newlines
lin_no := lin_no + 1;
col_no := 0;
when (buf = Chr(13)) then --Windows or Mac way
lin_no := lin_no + 1;
col_no := 0;
if(jsrc.len >= indx +1) then -- better safe than sorry
buf := next_char(indx+1, jsrc);
if(buf = Chr(10)) then --\r\n
indx := indx + 1;
end if;
end if;
when (buf = CHR(9)) then null; --tabbing
when (buf in ('-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9')) then --number
tokens(tok_indx) := mt('NUMBER', lin_no, col_no, null);
indx := lexNumber(jsrc, tokens(tok_indx), indx)-1;
col_no := col_no + length(tokens(tok_indx).data);
tok_indx := tok_indx + 1;
when buf = '"' then --number
tokens(tok_indx) := mt('STRING', lin_no, col_no, null);
indx := lexString(jsrc, tokens(tok_indx), indx, '"');
col_no := col_no + length(tokens(tok_indx).data) + 1;
tok_indx := tok_indx + 1;
when buf = '''' and json_strict = false then --number
tokens(tok_indx) := mt('STRING', lin_no, col_no, null);
indx := lexString(jsrc, tokens(tok_indx), indx, '''');
col_no := col_no + length(tokens(tok_indx).data) + 1; --hovsa her
tok_indx := tok_indx + 1;
when json_strict = false and REGEXP_LIKE(buf, '^[[:alpha:]]$', 'i') then
tokens(tok_indx) := mt('STRING', lin_no, col_no, null);
indx := lexName(jsrc, tokens(tok_indx), indx);
if(tokens(tok_indx).data_overflow is not null) then
col_no := col_no + dbms_lob.getlength(tokens(tok_indx).data_overflow) + 1;
else
col_no := col_no + length(tokens(tok_indx).data) + 1;
end if;
tok_indx := tok_indx + 1;
when json_strict = false and buf||next_char(indx+1, jsrc) = '/*' then --strip comments
declare
saveindx number := indx;
un_esc clob;
begin
indx := indx + 1;
loop
indx := indx + 1;
buf := next_char(indx, jsrc)||next_char(indx+1, jsrc);
exit when buf = '*/';
exit when buf is null;
end loop;
if(indx = saveindx+2) then
--enter unescaped mode
--dbms_output.put_line('Entering unescaped mode');
un_esc := empty_clob();
dbms_lob.createtemporary(un_esc, true);
indx := indx + 1;
loop
indx := indx + 1;
buf := next_char(indx, jsrc)||next_char(indx+1, jsrc)||next_char(indx+2, jsrc)||next_char(indx+3, jsrc);
exit when buf = '/**/';
if buf is null then
s_error('Unexpected sequence /**/ to end unescaped data: '||buf, lin_no, col_no);
end if;
buf := next_char(indx, jsrc);
dbms_lob.writeappend(un_esc, length(buf), buf);
end loop;
tokens(tok_indx) := mt('ESTRING', lin_no, col_no, null);
tokens(tok_indx).data_overflow := un_esc;
col_no := col_no + dbms_lob.getlength(un_esc) + 1; --note: line count won't work properly
tok_indx := tok_indx + 1;
indx := indx + 2;
end if;
indx := indx + 1;
end;
when buf = ' ' then null; --space
else
s_error('Unexpected char: '||buf, lin_no, col_no);
end case;
indx := indx + 1;
end loop;
return tokens;
END lexer;
--
-- END SCANNER
-- ***********
--
-- START PARSER
-- ************
----------------------------------------------------------
-- p_error (private)
--
PROCEDURE p_error(text VARCHAR2, tok rToken)
IS
BEGIN
raise_application_error(-20101, 'JSON Parser exception @ line: '||tok.line||' column: '||tok.col||' - '||text);
END p_error;
----------------------------------------------------------
-- parseObj (private)
--
PROCEDURE parseObj(tokens lTokens, indx IN OUT NOCOPY PLS_INTEGER, theParentID IN OUT BINARY_INTEGER, theLastID IN OUT BINARY_INTEGER, theNodes IN OUT NOCOPY jsonNodes)
IS
TYPE memmap IS TABLE OF NUMBER INDEX BY VARCHAR2(4000); -- i've read somewhere that this is not possible - but it is!
mymap memmap;
nullelemfound BOOLEAN := FALSE;
--yyy obj json;
obj jsonObject := jsonObject();
tok rToken;
mem_name VARCHAR(4000);
--yyy arr json_value_array := json_value_array();
arr jsonNodes := jsonNodes();
BEGIN
debug('parseObj - begin');
-- what to expect?
WHILE (indx <= tokens.count) LOOP
tok := tokens(indx);
debug('parseObj - type: '||tok.type_name);
CASE tok.type_name
WHEN 'STRING' THEN
-- member
mem_name := substr(tok.data, 1, 4000);
BEGIN
IF (mem_name IS NULL) THEN
IF (nullelemfound) THEN
p_error('Duplicate empty member: ', tok);
ELSE
nullelemfound := TRUE;
END IF;
ELSIF (mymap(mem_name) IS NOT NULL) THEN
p_error('Duplicate member name: '||mem_name, tok);
END IF;
EXCEPTION
WHEN no_data_found THEN
mymap(mem_name) := 1;
END;
indx := indx + 1;
IF (indx > tokens.count) THEN
p_error('Unexpected end of input', tok);
END IF;
tok := tokens(indx);
indx := indx + 1;
IF (indx > tokens.count) THEN
p_error('Unexpected end of input', tok);
END IF;
IF (tok.type_name = ':') THEN
debug('parseObj - : found');
parseMem(tokens, indx, mem_name, 0, theParentID, theLastID, theNodes);
ELSE
p_error('Expected '':''', tok);
END IF;
--move indx forward if ',' is found
IF (indx > tokens.count) THEN
p_error('Unexpected end of input', tok);
END IF;
tok := tokens(indx);
IF (tok.type_name = ',') THEN
debug('found ,');
indx := indx + 1;
tok := tokens(indx);
IF (tok.type_name = '}') THEN --premature exit
p_error('Premature exit in json object', tok);
END IF;
ELSIF (tok.type_name != '}') THEN
p_error('A comma seperator is probably missing', tok);
END IF;
WHEN '}' THEN
debug('parseObj - } found: returning '||arr.COUNT||' theNodes');
RETURN;
ELSE
p_error('Expected string or }', tok);
END CASE;
END LOOP;
p_error('} not found', tokens(indx-1));
END parseObj;
----------------------------------------------------------
-- parseArr (private)
--
PROCEDURE parseArr(tokens lTokens, indx IN OUT NOCOPY PLS_INTEGER, theParentID IN OUT BINARY_INTEGER, theLastID IN OUT BINARY_INTEGER, theNodes IN OUT NOCOPY jsonNodes)
IS
aNodeID BINARY_INTEGER;
aLastID BINARY_INTEGER;
tok rToken;
aNode jsonNode := jsonNode();
BEGIN
IF (indx > tokens.count) THEN
p_error('more elements in array was excepted', tok);
END IF;
tok := tokens(indx);
WHILE (tok.type_name != ']') LOOP
CASE tok.type_name
WHEN 'TRUE' THEN
debug('parseArr - type: '||tok.type_name||' indx: '||indx);
aNode := jsonNode(NULL, TRUE);
aNode.par := theParentID;
aNodeID := json_utils.addNode(theNodes=>theNodes, theNode=>aNode);
IF (theLastID IS NOT NULL) THEN
theNodes(theLastID).nex := aNodeID;
END IF;
theLastID := aNodeID;
WHEN 'FALSE' THEN
debug('parseArr - type: '||tok.type_name||' indx: '||indx);
aNode := jsonNode(NULL, FALSE);
aNode.par := theParentID;
aNodeID := json_utils.addNode(theNodes=>theNodes, theNode=>aNode);
IF (theLastID IS NOT NULL) THEN
theNodes(theLastID).nex := aNodeID;
END IF;
theLastID := aNodeID;
WHEN 'NULL' THEN
debug('parseArr - type: '||tok.type_name||' indx: '||indx);
aNode := jsonNode(NULL);
aNode.par := theParentID;
aNodeID := json_utils.addNode(theNodes=>theNodes, theNode=>aNode);
IF (theLastID IS NOT NULL) THEN
theNodes(theLastID).nex := aNodeID;
END IF;
theLastID := aNodeID;
WHEN 'STRING' THEN
debug('parseArr - type: '||tok.type_name||' indx: '||indx||' value: '||CASE WHEN tok.data_overflow IS NOT NULL THEN tok.data_overflow ELSE tok.data END);
aNode := jsonNode(NULL, CASE WHEN tok.data_overflow IS NOT NULL THEN tok.data_overflow ELSE tok.data END);
aNode.par := theParentID;
aNodeID := json_utils.addNode(theNodes=>theNodes, theNode=>aNode);
IF (theLastID IS NOT NULL) THEN
theNodes(theLastID).nex := aNodeID;
END IF;
theLastID := aNodeID;
WHEN 'ESTRING' THEN
debug('parseArr - type: '||tok.type_name||' indx: '||indx||' value: '||tok.data_overflow);
aNode := jsonNode(NULL, tok.data_overflow);
aNode.par := theParentID;
aNodeID := json_utils.addNode(theNodes=>theNodes, theNode=>aNode);
IF (theLastID IS NOT NULL) THEN
theNodes(theLastID).nex := aNodeID;
END IF;
theLastID := aNodeID;
WHEN 'NUMBER' THEN
debug('parseArr - type: '||tok.type_name||' indx: '||indx||' value: '||TO_NUMBER(REPLACE(tok.data, '.', decimalpoint)));
aNode := jsonNode(NULL, TO_NUMBER(REPLACE(tok.data, '.', decimalpoint)));
aNode.par := theParentID;
aNodeID := json_utils.addNode(theNodes=>theNodes, theNode=>aNode);
IF (theLastID IS NOT NULL) THEN
theNodes(theLastID).nex := aNodeID;
END IF;
theLastID := aNodeID;
WHEN '{' THEN
debug('parseArr - type: '||tok.type_name||' indx: '||indx);
aNode := jsonNode();
aNode.typ := 'O';
aNode.sub := theNodes.COUNT + 2;
aNodeID := json_utils.addNode(theNodes=>theNodes, theNode=>aNode);
IF (theLastID IS NOT NULL) THEN
theNodes(theLastID).nex := aNodeID;
END IF;
theLastID := aNodeID;
theParentID := aNodeID;
indx := indx + 1;
aLastID := theLastID;
theLastID := NULL;
parseObj(tokens, indx, theParentID, theLastID, theNodes);
-- if "theLastID" did not change, we must be dealing with an empty object that does actually not have any sub-nodes
IF (theLastID IS NULL) THEN
theNodes(aLastID).sub := NULL;
END IF;
theLastID := aLastID;
aLastID := NULL;
theParentID := NULL;
WHEN '[' THEN
debug('parseArr - type: '||tok.type_name||' indx: '||indx);
aNode := jsonNode();
aNode.typ := 'A';
aNode.sub := theNodes.COUNT + 2;
aNodeID := json_utils.addNode(theNodes=>theNodes, theNode=>aNode);
IF (theLastID IS NOT NULL) THEN
theNodes(theLastID).nex := aNodeID;
END IF;
theLastID := aNodeID;
theParentID := aNodeID;
indx := indx + 1;
aLastID := theLastID;
theLastID := NULL;
parseArr(tokens, indx, theParentID, theLastID, theNodes);
-- if "theLastID" did not change, we must be dealing with an empty array that does actually not have any sub-nodes
IF (theLastID IS NULL) THEN
theNodes(aLastID).sub := NULL;
END IF;
theLastID := aLastID;
aLastID := NULL;
theParentID := NULL;
ELSE
p_error('Expected a value', tok);
END CASE;
indx := indx + 1;
IF (indx > tokens.count) THEN
p_error('] not found', tok);
END IF;
tok := tokens(indx);
IF (tok.type_name = ',') THEN --advance
indx := indx + 1;
IF (indx > tokens.count) THEN
p_error('more elements in array was excepted', tok);
END IF;
tok := tokens(indx);
IF (tok.type_name = ']') THEN --premature exit
p_error('Premature exit in array', tok);
END IF;
ELSIF (tok.type_name != ']') THEN --error
p_error('Expected , or ]', tok);
END IF;
END LOOP;
end parseArr;
----------------------------------------------------------
-- parseMem (private)
--
PROCEDURE parseMem(tokens lTokens, indx IN OUT PLS_INTEGER, mem_name VARCHAR2, mem_indx NUMBER, theParentID IN OUT BINARY_INTEGER, theLastID IN OUT BINARY_INTEGER, theNodes IN OUT NOCOPY jsonNodes)
IS
tok rToken;
aNodeID BINARY_INTEGER;
aLastID BINARY_INTEGER;
aNode jsonNode := jsonNode();
BEGIN
tok := tokens(indx);
CASE tok.type_name
WHEN 'TRUE' THEN
debug('parseMem - type: '||tok.type_name||' name: '||mem_name);
aNode := jsonNode(mem_name, TRUE);
aNode.par := theParentID;
aNodeID := json_utils.addNode(theNodes=>theNodes, theNode=>aNode);
IF (theLastID IS NOT NULL) THEN
theNodes(theLastID).nex := aNodeID;
END IF;
theLastID := aNodeID;
WHEN 'FALSE' THEN
debug('parseMem - type: '||tok.type_name||' name: '||mem_name);
aNode := jsonNode(mem_name, FALSE);
aNode.par := theParentID;
aNodeID := json_utils.addNode(theNodes=>theNodes, theNode=>aNode);
IF (theLastID IS NOT NULL) THEN
theNodes(theLastID).nex := aNodeID;
END IF;
theLastID := aNodeID;
WHEN 'NULL' THEN
debug('parseMem - type: '||tok.type_name||' name: '||mem_name);
aNode := jsonNode(mem_name);
aNode.par := theParentID;
aNodeID := json_utils.addNode(theNodes=>theNodes, theNode=>aNode);
IF (theLastID IS NOT NULL) THEN
theNodes(theLastID).nex := aNodeID;
END IF;
theLastID := aNodeID;
WHEN 'STRING' THEN
debug('parseMem - type: '||tok.type_name||' name: '||mem_name||' value: '||CASE WHEN tok.data_overflow IS NOT NULL THEN tok.data_overflow ELSE tok.data END);
aNode := jsonNode(mem_name, CASE WHEN tok.data_overflow IS NOT NULL THEN tok.data_overflow ELSE tok.data END);
aNode.par := theParentID;
aNodeID := json_utils.addNode(theNodes=>theNodes, theNode=>aNode);
IF (theLastID IS NOT NULL) THEN
theNodes(theLastID).nex := aNodeID;
END IF;
theLastID := aNodeID;
WHEN 'ESTRING' THEN
debug('parseMem - type: '||tok.type_name||' name: '||mem_name||' value: '||tok.data_overflow);
aNode := jsonNode(mem_name, tok.data_overflow);
aNode.par := theParentID;
aNodeID := json_utils.addNode(theNodes=>theNodes, theNode=>aNode);
IF (theLastID IS NOT NULL) THEN
theNodes(theLastID).nex := aNodeID;
END IF;
theLastID := aNodeID;
WHEN 'NUMBER' THEN
debug('parseMem - type: '||tok.type_name||' name: '||mem_name||' value: '||TO_NUMBER(REPLACE(tok.data, '.', decimalpoint)));
aNode := jsonNode(mem_name, TO_NUMBER(REPLACE(tok.data, '.', decimalpoint)));
aNode.par := theParentID;
aNodeID := json_utils.addNode(theNodes=>theNodes, theNode=>aNode);
IF (theLastID IS NOT NULL) THEN
theNodes(theLastID).nex := aNodeID;
END IF;
theLastID := aNodeID;
WHEN '{' THEN
debug('parseMem - type: '||tok.type_name||' name: '||mem_name);
aNode := jsonNode();
aNode.typ := 'O';
aNode.nam := mem_name;
aNode.sub := theNodes.COUNT + 2;
aNodeID := json_utils.addNode(theNodes=>theNodes, theNode=>aNode);
IF (theLastID IS NOT NULL) THEN
theNodes(theLastID).nex := aNodeID;
END IF;
theLastID := aNodeID;
theParentID := aNodeID;
indx := indx + 1;
aLastID := theLastID;
theLastID := NULL;
parseObj(tokens, indx, theParentID, theLastID, theNodes);
-- if "theLastID" did not change, we must be dealing with an empty object that does actually not have any sub-nodes
IF (theLastID IS NULL) THEN
theNodes(aLastID).sub := NULL;
END IF;
theLastID := aLastID;
aLastID := NULL;
theParentID := NULL;
WHEN '[' THEN
debug('parseMem - type: '||tok.type_name||' name: '||mem_name);
aNode := jsonNode();
aNode.typ := 'A';
aNode.nam := mem_name;
aNode.sub := theNodes.COUNT + 2;
aNodeID := json_utils.addNode(theNodes=>theNodes, theNode=>aNode);
IF (theLastID IS NOT NULL) THEN
theNodes(theLastID).nex := aNodeID;
END IF;
theLastID := aNodeID;
theParentID := aNodeID;
indx := indx + 1;
aLastID := theLastID;
theLastID := NULL;
parseArr(tokens, indx, theParentID, theLastID, theNodes);
-- if "theLastID" did not change, we must be dealing with an empty array that does actually not have any sub-nodes
IF (theLastID IS NULL) THEN
theNodes(aLastID).sub := NULL;
END IF;
theLastID := aLastID;
aLastID := NULL;
theParentID := NULL;
ELSE
p_error('Found '||tok.type_name, tok);
END CASE;
indx := indx + 1;
END parseMem;
----------------------------------------------------------
-- parse_list
--
FUNCTION parse_list(str CLOB) RETURN jsonNodes
IS
tokens lTokens;
--yyy obj json_list;
obj jsonNodes := jsonNodes();
indx PLS_INTEGER := 1;
jsrc json_src;
BEGIN
debug('parse_list');
updateDecimalPoint();
jsrc := prepareClob(str);
tokens := lexer(jsrc);
IF (tokens(indx).type_name = '[') THEN
indx := indx + 1;
--yyy obj := parseArr(tokens, indx);
ELSE
raise_application_error(-20101, 'JSON List Parser exception - no [ start found');
END IF;
IF (tokens.count != indx) THEN
p_error('] should end the JSON List object', tokens(indx));
END IF;
RETURN obj;
END parse_list;
----------------------------------------------------------
-- parser
--
FUNCTION parser(str CLOB) RETURN jsonNodes
IS
tokens lTokens;
obj jsonNodes := jsonNodes();
indx PLS_INTEGER := 1;
jsrc json_src;
i BINARY_INTEGER;
aParentID BINARY_INTEGER := NULL;
aLastID BINARY_INTEGER := NULL;
BEGIN
updateDecimalPoint();
jsrc := prepareClob(str);
tokens := lexer(jsrc);
-- dump tokens
/*
dbms_output.put_line('----------LEXER-S----------');
i := tokens.FIRST;
WHILE (i IS NOT NULL) LOOP
dbms_output.put_line(i||'. type=('||tokens(i).type_name||') type=('||tokens(i).data||') type=('||tokens(i).data_overflow||')');
i := tokens.NEXT(i);
END LOOP;
dbms_output.put_line('----------LEXER-E----------');
*/
IF (tokens(indx).type_name = '{') THEN
indx := indx + 1;
--yyy obj := parseObj(tokens, indx);
parseObj(tokens, indx, aParentID, aLastID, obj);
ELSE
raise_application_error(-20101, 'JSON Parser exception - no { start found');
END IF;
IF (tokens.count != indx) THEN
p_error('} should end the JSON object', tokens(indx));
END IF;
RETURN obj;