-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprunt-json.adb
More file actions
1700 lines (1387 loc) · 53 KB
/
prunt-json.adb
File metadata and controls
1700 lines (1387 loc) · 53 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
------------------------------------------------------------------------------
-- --
-- This file is based on code from GNATCOLL modified for Prunt. --
-- --
-- G N A T C O L L --
-- --
-- Copyright (C) 2011-2024, AdaCore --
-- Copyright (C) 2025, Liam Powell --
-- --
-- This library is free software; you can redistribute it and/or modify it --
-- under terms of the GNU General Public License as published by the Free --
-- Software Foundation; either version 3, or (at your option) any later --
-- version. This library is distributed in the hope that it will be useful, --
-- but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- --
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. --
-- --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception, --
-- version 3.1, as published by the Free Software Foundation. --
-- --
-- You should have received a copy of the GNU General Public License and --
-- a copy of the GCC Runtime Library Exception along with this program; --
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
-- <http://www.gnu.org/licenses/>. --
-- --
------------------------------------------------------------------------------
with Ada.Characters.Handling; use Ada.Characters.Handling;
with Ada.Containers; use Ada.Containers;
with Ada.Exceptions;
with Ada.Text_IO;
with Ada.Unchecked_Deallocation;
with GNATCOLL.Atomic; use GNATCOLL.Atomic;
with Interfaces; use Interfaces;
with VSS.Characters;
with VSS.Characters.Latin;
with VSS.Strings.Conversions; use VSS.Strings.Conversions;
with VSS.Strings.Cursors.Iterators.Characters;
use type VSS.Characters.Virtual_Character;
package body Prunt.JSON is
pragma Extensions_Allowed (On);
procedure Free is new Ada.Unchecked_Deallocation (JSON_Array_Internal, JSON_Array_Access);
procedure Free is new Ada.Unchecked_Deallocation (JSON_Object_Internal, JSON_Object_Access);
procedure Free is new Ada.Unchecked_Deallocation (JSON_String_Internal, JSON_String_Access);
procedure Free is new Ada.Unchecked_Deallocation (JSON_Parser_States, JSON_Parser_States_Access);
procedure Write (Item : JSON_Value; Compact : Boolean; Indent : Natural; Ret : in out Virtual_String);
-- Auxiliary write function
------------
-- Append --
------------
procedure Append (Arr : JSON_Value; Item : JSON_Value) is
begin
Append (Arr.Data.Arr_Value.Arr, Item);
end Append;
--------------
-- Is_Empty --
--------------
function Is_Empty (Val : JSON_Value) return Boolean is
begin
case Val.Kind is
when JSON_Null_Type =>
return True;
when JSON_Array_Type =>
return Val.Data.Arr_Value.Arr.Vals.Is_Empty;
when JSON_Object_Type =>
return Val.Data.Obj_Value.Vals.Is_Empty;
when others =>
return False;
end case;
end Is_Empty;
-----------------
-- Array_First --
-----------------
function Array_First (Arr : JSON_Array) return Positive is
pragma Unreferenced (Arr);
begin
return 1;
end Array_First;
----------------
-- Array_Next --
----------------
function Array_Next (Arr : JSON_Array; Index : Positive) return Positive is
pragma Unreferenced (Arr);
begin
return Index + 1;
end Array_Next;
-----------------------
-- Array_Has_Element --
-----------------------
function Array_Has_Element (Arr : JSON_Array; Index : Positive) return Boolean is
begin
return Index <= Length (Arr);
end Array_Has_Element;
-------------------
-- Array_Element --
-------------------
function Array_Element (Arr : JSON_Array; Index : Positive) return JSON_Value is
begin
return Get (Arr, Index);
end Array_Element;
--------------------------
-- Format_Parsing_Error --
--------------------------
function Format_Parsing_Error (Error : Parsing_Error) return Virtual_String is
L : constant String := Error.Line'Img;
C : constant String := Error.Column'Img;
begin
return
(To_Virtual_String (L (L'First + 1 .. L'Last))
& ":"
& To_Virtual_String (C (C'First + 1 .. C'Last))
& ": "
& Error.Message);
end Format_Parsing_Error;
function Parse_Next (Self : in out JSON_Parser; Data : in out GNATCOLL.Buffer.Reader) return JSON_Parser_Event is
use GNATCOLL.Buffer;
Result : JSON_Parser_Event;
CC : Character;
Offset : Integer;
procedure Skip_Whitespaces
with Inline_Always => True;
procedure Parse_Number
with Inline_Always => True;
procedure Expect_Value
with Inline_Always => True;
procedure Append_To_State (State : JSON_Parser_State)
with Inline_Always => True;
procedure Append_To_State (State : JSON_Parser_State) is
begin
Self.State_Current := Self.State_Current + 1;
if Self.State_Current > Self.State.all'Last then
declare
New_State : constant JSON_Parser_States_Access := new JSON_Parser_States (1 .. Self.State.all'Last * 2);
begin
New_State.all (1 .. Self.State.all'Last) := Self.State.all;
Free (Self.State);
Self.State := New_State;
end;
end if;
Self.State (Self.State_Current) := State;
end Append_To_State;
procedure Skip_Whitespaces is
Offset : Integer;
begin
Offset := Window_Offset (Data);
while Next (Data, CC, Offset) loop
if CC > ' ' then
exit;
end if;
case CC is
when ' ' | ASCII.HT | ASCII.LF | ASCII.CR =>
null;
when others =>
exit;
end case;
end loop;
Set_Window_Offset (Data, Offset);
Release (Data);
end Skip_Whitespaces;
procedure Parse_Number is
Has_Char : Boolean;
begin
Offset := Window_Offset (Data);
Result.First := Current_Position (Data);
-- If minus sign is present we expect afterward a number
if CC = '-' then
if not Next (Data, CC, Offset) or else CC not in '0' .. '9' then
raise Invalid_JSON_Stream with "not a valid number";
end if;
end if;
-- At this stage, by construction CC is in '0' .. '9'
if CC in '1' .. '9' then
loop
Has_Char := Next (Data, CC, Offset);
exit when not Has_Char or else CC not in '0' .. '9';
end loop;
else
Has_Char := Next (Data, CC, Offset);
end if;
if Has_Char and then CC = '.' then
-- We have a fractional part
Result.Kind := NUMBER_VALUE;
Has_Char := Next (Data, CC, Offset);
if not Has_Char or else CC not in '0' .. '9' then
raise Invalid_JSON_Stream with "not a valid number";
end if;
loop
Has_Char := Next (Data, CC, Offset);
exit when not Has_Char or else CC not in '0' .. '9';
end loop;
end if;
if Has_Char and then (CC = 'e' or else CC = 'E') then
-- We have an exponential part
Result.Kind := NUMBER_VALUE;
Has_Char := Next (Data, CC, Offset);
if not Has_Char then
raise Invalid_JSON_Stream with "not a valid number";
end if;
if CC = '+' or else CC = '-' then
Has_Char := Next (Data, CC, Offset);
end if;
if not Has_Char or else CC not in '0' .. '9' then
raise Invalid_JSON_Stream with "not a valid number";
end if;
loop
Has_Char := Next (Data, CC, Offset);
exit when not Has_Char or else CC not in '0' .. '9';
end loop;
end if;
if Has_Char then
Set_Window_Offset (Data, Offset - 1);
Result.Last := Current_Position (Data);
else
Set_Window_Offset (Data, Offset);
Result.Last := Current_Position (Data);
end if;
end Parse_Number;
procedure Expect_Value is
begin
-- Skip initial white spaces
Skip_Whitespaces;
if Is_End_Of_Data (Data) then
Result.Kind := DOC_END;
return;
end if;
case Current_Char (Data) is
when '{' =>
-- This is an object
Result.Kind := OBJECT_START;
return;
when '}' =>
Result.Kind := OBJECT_END;
return;
when '[' =>
-- This is an array
Result.Kind := ARRAY_START;
when ']' =>
Result.Kind := ARRAY_END;
return;
when ':' =>
Result.Kind := NAME_SEP;
return;
when ',' =>
Result.Kind := VALUE_SEP;
return;
when 't' =>
-- This is true
Result.Kind := TRUE_VALUE;
if not GNATCOLL.Buffer.Check (Data, "rue") then
raise Invalid_JSON_Stream with "invalid token";
end if;
when 'f' =>
-- This is false
Result.Kind := FALSE_VALUE;
if not GNATCOLL.Buffer.Check (Data, "alse") then
raise Invalid_JSON_Stream with "invalid token";
end if;
when 'n' =>
-- This is null
Result.Kind := NULL_VALUE;
if not GNATCOLL.Buffer.Check (Data, "ull") then
raise Invalid_JSON_Stream with "invalid token";
end if;
when '"' =>
Result.Kind := STRING_VALUE;
declare
Offset : Integer;
begin
Offset := Window_Offset (Data);
Result.First := Current_Position (Data);
-- This is a string
while Next (Data, CC, Offset) loop
case CC is
when '"' =>
Set_Window_Offset (Data, Offset);
Result.Last := Current_Position (Data);
return;
when ASCII.NUL .. ASCII.US =>
raise Invalid_JSON_Stream with "control character not allowed in string";
when '\' =>
if not Next (Data, CC, Offset) then
raise Invalid_JSON_Stream with "non terminated string";
end if;
case CC is
when '\' | '/' | '"' | 'b' | 'f' | 'n' | 'r' | 't' =>
-- This is a single character escape sequence
null;
when 'u' =>
for Idx in 1 .. 4 loop
if not Next (Data, CC, Offset) then
raise Invalid_JSON_Stream with "invalid unicode escape sequence";
elsif CC not in 'a' .. 'f' | 'A' .. 'F' | '0' .. '9' then
raise Invalid_JSON_Stream with "invalid unicode escape sequence";
end if;
end loop;
when others =>
raise Invalid_JSON_Stream with "unknown case";
end case;
when others =>
null;
end case;
end loop;
end;
raise Invalid_JSON_Stream with "non terminated string";
when '-' | '0' .. '9' =>
-- This is a number
Result.Kind := INTEGER_VALUE;
Parse_Number;
when others =>
raise Invalid_JSON_Stream with "unexpected character '" & Current_Char (Data) & "'";
end case;
end Expect_Value;
Scan_Next : Boolean := True;
begin
if Self.State_Current = 0 then
Self.State := new JSON_Parser_States (1 .. 32);
Self.State (1) := EXPECT_VALUE;
Self.State_Current := 1;
end if;
Offset := Window_Offset (Data);
-- Release previous data from the buffer
Release (Data);
-- Read next token
Expect_Value;
if Self.State (Self.State_Current) = EXPECT_OBJECT_NAME_SEP then
if Result.Kind = NAME_SEP then
Self.State (Self.State_Current) := EXPECT_OBJECT_VALUE;
Expect_Value;
else
raise Invalid_JSON_Stream with "colon expected";
end if;
elsif Self.State (Self.State_Current) = EXPECT_OBJECT_SEP then
if Result.Kind = VALUE_SEP then
Self.State (Self.State_Current) := EXPECT_OBJECT_KEY;
Expect_Value;
elsif Result.Kind = OBJECT_END then
Self.State_Current := Self.State_Current - 1;
Scan_Next := False;
else
raise Invalid_JSON_Stream with "coma or } expected";
end if;
elsif Self.State (Self.State_Current) = EXPECT_ARRAY_SEP then
if Result.Kind = VALUE_SEP then
Self.State (Self.State_Current) := EXPECT_ARRAY_VALUE;
Expect_Value;
elsif Result.Kind = ARRAY_END then
Self.State_Current := Self.State_Current - 1;
Scan_Next := False;
else
raise Invalid_JSON_Stream with "coma or ] expected";
end if;
end if;
if Scan_Next then
case Self.State (Self.State_Current) is
when EXPECT_VALUE =>
if Result.Kind not in STRING_VALUE .. OBJECT_START then
raise Invalid_JSON_Stream with "value expected (got " & Result.Kind'Img & ")";
end if;
Self.State (Self.State_Current) := EXPECT_DOC_END;
when EXPECT_OBJECT_KEY =>
if Result.Kind = STRING_VALUE then
Self.State (Self.State_Current) := EXPECT_OBJECT_NAME_SEP;
else
raise Invalid_JSON_Stream with "string expected";
end if;
when EXPECT_OBJECT_FIRST_KEY =>
if Result.Kind = STRING_VALUE then
Self.State (Self.State_Current) := EXPECT_OBJECT_NAME_SEP;
elsif Result.Kind = OBJECT_END then
Self.State_Current := Self.State_Current - 1;
else
raise Invalid_JSON_Stream with "string expected";
end if;
when EXPECT_OBJECT_VALUE =>
if Result.Kind not in STRING_VALUE .. OBJECT_START then
raise Invalid_JSON_Stream with "json valud expected";
end if;
Self.State (Self.State_Current) := EXPECT_OBJECT_SEP;
when EXPECT_ARRAY_FIRST_VALUE =>
if Result.Kind = ARRAY_END then
Self.State_Current := Self.State_Current - 1;
elsif Result.Kind not in STRING_VALUE .. OBJECT_START then
raise Invalid_JSON_Stream with "array element expected";
else
Self.State (Self.State_Current) := EXPECT_ARRAY_SEP;
end if;
when EXPECT_ARRAY_VALUE =>
if Result.Kind not in STRING_VALUE .. OBJECT_START then
raise Invalid_JSON_Stream with "array element expected";
else
Self.State (Self.State_Current) := EXPECT_ARRAY_SEP;
end if;
when EXPECT_DOC_END =>
if Result.Kind /= DOC_END then
raise Invalid_JSON_Stream with "end of doc expected";
end if;
when others =>
raise Invalid_JSON_Stream
with "unknown state " & JSON_Parser_State'Image (Self.State (Self.State_Current));
end case;
end if;
-- Handle value beginning of object or array
if Result.Kind = OBJECT_START then
Append_To_State (EXPECT_OBJECT_FIRST_KEY);
end if;
if Result.Kind = ARRAY_START then
Append_To_State (EXPECT_ARRAY_FIRST_VALUE);
end if;
return Result;
end Parse_Next;
----------
-- Read --
----------
function Read (Strm : String) return Read_Result;
-- Parse the UTF-8 JSON document in Strm and return the value or parsing error.
function Read (Strm : String; Filename : String := "<data>") return JSON_Value;
-- Parse the UTF-8 JSON document in Strm and raise Invalid_JSON_Stream on parsing errors.
function Read (Strm : String) return Read_Result is
use GNATCOLL.Buffer;
Data : Reader := Open_String (Strm);
begin
return Read (Data);
end Read;
function Read (Strm : String; Filename : String := "<data>") return JSON_Value is
Result : Read_Result;
begin
declare
use GNATCOLL.Buffer;
Data : Reader := Open_String (Strm);
begin
Result := Read (Data);
end;
if Result.Success then
return Result.Value;
else
Ada.Text_IO.New_Line;
if Filename = "" then
Ada.Text_IO.Put ("<stream>:");
else
Ada.Text_IO.Put (Filename & ":");
end if;
Ada.Text_IO.Put_Line (To_UTF_8_String (Format_Parsing_Error (Result.Error)));
raise Invalid_JSON_Stream with To_UTF_8_String (Result.Error.Message);
end if;
end Read;
function Read (Strm : Virtual_String; Filename : String := "<data>") return JSON_Value is
begin
return Read (To_UTF_8_String (Strm), Filename);
end Read;
function Read (Strm : Virtual_String) return Read_Result is
begin
return Read (To_UTF_8_String (Strm));
end Read;
function Read (Data : in out GNATCOLL.Buffer.Reader) return Read_Result is
use GNATCOLL.Buffer;
Parser : JSON_Parser;
Event : JSON_Parser_Event;
Result : JSON_Value;
Cursor : JSON_Value;
Tmp : JSON_Value;
Current_Key : Virtual_String;
type Read_State is record
Expect_Key : Boolean;
Cursor : JSON_Value;
end record;
type Read_State_Array is array (Integer range <>) of Read_State;
type Read_State_Array_Access is access Read_State_Array;
procedure Free is new Ada.Unchecked_Deallocation (Read_State_Array, Read_State_Array_Access);
Read_States : Read_State_Array_Access := new Read_State_Array (1 .. 32);
Read_States_Last : Integer := 0;
begin
loop
Event := Parse_Next (Parser, Data);
exit when Event.Kind = DOC_END;
if Event.Kind /= OBJECT_END and then Read_States_Last > 0 and then Read_States (Read_States_Last).Expect_Key
then
Current_Key := To_Virtual_String (Token (Data, Event.First, Event.Last));
Event := Parse_Next (Parser, Data);
end if;
case Event.Kind is
when OBJECT_START =>
Tmp := Create_Object;
when ARRAY_START =>
Tmp := Create (Empty_Array);
when STRING_VALUE =>
declare
Str : constant String := Token (Data, Event.First, Event.Last);
begin
Tmp := Create (Un_Escape_String (To_Virtual_String (Str)));
end;
when INTEGER_VALUE =>
-- Constraint_Error in Ada might occur for distinct reasons.
-- try to limit the scope of the Constraint_Error exception
-- handler to errors caused by an int too big to fit into a
-- Long_Long_Integer.
declare
Int_Value : Long_Long_Integer;
Failed : Boolean := False;
begin
begin
Int_Value := Long_Long_Integer'Value (Token (Data, Event.First, Event.Last));
exception
when Constraint_Error =>
Failed := True;
end;
if Failed then
Tmp := Create (Long_Float'Value (Token (Data, Event.First, Event.Last)));
else
Tmp := Create (Int_Value);
end if;
end;
when NUMBER_VALUE =>
Tmp := Create (Long_Float'Value (Token (Data, Event.First, Event.Last)));
when TRUE_VALUE =>
Tmp := Create (True);
when FALSE_VALUE =>
Tmp := Create (False);
when NULL_VALUE =>
Tmp := Create;
when others =>
null;
end case;
if Event.Kind = OBJECT_END then
Read_States_Last := Read_States_Last - 1;
if Read_States_Last > 0 then
Cursor := Read_States (Read_States_Last).Cursor;
end if;
elsif Event.Kind = ARRAY_END then
Read_States_Last := Read_States_Last - 1;
if Read_States_Last > 0 then
Cursor := Read_States (Read_States_Last).Cursor;
end if;
else
if Read_States_Last > 0 then
if Read_States (Read_States_Last).Expect_Key then
Set_Field (Cursor, Un_Escape_String (Current_Key), Tmp);
else
Append (Cursor, Tmp);
end if;
else
Result := Tmp;
end if;
if Event.Kind = OBJECT_START or else Event.Kind = ARRAY_START then
Read_States_Last := Read_States_Last + 1;
if Read_States_Last > Read_States'Last then
declare
Tmp : constant Read_State_Array_Access := new Read_State_Array (1 .. Read_States'Last * 2);
begin
Tmp.all (1 .. Read_States.all'Last) := Read_States.all;
Free (Read_States);
Read_States := Tmp;
end;
end if;
Read_States (Read_States_Last) := (Event.Kind = OBJECT_START, Tmp);
Cursor := Tmp;
end if;
end if;
end loop;
Free (Read_States);
return (Success => True, Value => Result);
exception
when E : Invalid_JSON_Stream =>
Free (Read_States);
declare
Line, Column : Integer;
begin
Data.Current_Text_Position (Line, Column);
return
(Success => False, Error => (Line, Column, To_Virtual_String (Ada.Exceptions.Exception_Message (E))));
end;
end Read;
---------------
-- Read_File --
---------------
function Read_File (Path : String) return Read_Result is
use GNATCOLL.Buffer;
Data : Reader := Open (Path);
begin
return Read (Data);
end Read_File;
-----------
-- Write --
-----------
procedure Write (Item : JSON_Value; Compact : Boolean; Indent : Natural; Ret : in out Virtual_String) is
procedure Do_Indent (Val : Natural);
-- Adds whitespace characters to Ret corresponding to the indentation
-- level.
---------------
-- Do_Indent --
---------------
procedure Do_Indent (Val : Natural) is
begin
if Compact then
return;
end if;
for I in 1 .. 2 * Val loop
Ret.Append (' ');
end loop;
end Do_Indent;
begin
case Item.Kind is
when JSON_Null_Type =>
Ret.Append ("null");
when JSON_Boolean_Type =>
if Item.Data.Bool_Value then
Ret.Append ("true");
else
Ret.Append ("false");
end if;
when JSON_Int_Type =>
declare
S : constant String := Item.Data.Int_Value'Img;
begin
if S (S'First) = ' ' then
Ret.Append (To_Virtual_String (S (S'First + 1 .. S'Last)));
else
Ret.Append (To_Virtual_String (S));
end if;
end;
when JSON_Float_Type =>
declare
S : constant String := Item.Data.Flt_Value'Img;
begin
if S (S'First) = ' ' then
Ret.Append (To_Virtual_String (S (S'First + 1 .. S'Last)));
else
Ret.Append (To_Virtual_String (S));
end if;
end;
when JSON_String_Type =>
Ret.Append (Escape_String (Item.Data.Str_Value.Str));
when JSON_Array_Type =>
Ret.Append ('[');
if not Compact then
Ret.Append (VSS.Characters.Latin.Line_Feed);
end if;
for J in Item.Data.Arr_Value.Arr.Vals.First_Index .. Item.Data.Arr_Value.Arr.Vals.Last_Index loop
Do_Indent (Indent + 1);
Write (Item.Data.Arr_Value.Arr.Vals.Element (J), Compact, Indent + 1, Ret);
if J < Item.Data.Arr_Value.Arr.Vals.Last_Index then
Ret.Append (",");
end if;
if not Compact then
Ret.Append (VSS.Characters.Latin.Line_Feed);
end if;
end loop;
Do_Indent (Indent);
Ret.Append (']');
when JSON_Object_Type =>
declare
use Object_Items_Pkg;
J : Object_Items_Pkg.Cursor := Item.Data.Obj_Value.Vals.First;
begin
Ret.Append ('{');
if not Compact then
Ret.Append (VSS.Characters.Latin.Line_Feed);
end if;
while Has_Element (J) loop
Do_Indent (Indent + 1);
Ret.Append (Escape_String (Key (J)));
Ret.Append (':');
if not Compact then
Ret.Append (' ');
end if;
Write (Element (J), Compact, Indent + 1, Ret);
Next (J);
if Has_Element (J) then
Ret.Append (",");
end if;
if not Compact then
Ret.Append (VSS.Characters.Latin.Line_Feed);
end if;
end loop;
Do_Indent (Indent);
Ret.Append ('}');
end;
end case;
end Write;
-----------
-- Write --
-----------
function Write (Item : JSON_Value; Compact : Boolean := True) return Virtual_String is
Ret : Virtual_String;
begin
Write (Item, Compact, 0, Ret);
return Ret;
end Write;
------------
-- Length --
------------
function Length (Arr : JSON_Array) return Natural is
begin
return Natural (Arr.Vals.Length);
end Length;
--------------
-- Is_Empty --
--------------
function Is_Empty (Arr : JSON_Array) return Boolean is
begin
return Arr.Vals.Is_Empty;
end Is_Empty;
---------
-- Get --
---------
function Get (Arr : JSON_Array; Index : Positive) return JSON_Value is
begin
return Arr.Vals.Element (Index);
end Get;
-----------------
-- Set_Element --
-----------------
procedure Set_Element (Arr : in out JSON_Array; Index : Positive; Item : JSON_Value) is
begin
Arr.Vals.Replace_Element (Index, Item);
end Set_Element;
----------
-- Sort --
----------
procedure Sort (Arr : in out JSON_Array; Less : access function (Left, Right : JSON_Value) return Boolean) is
package Sorting is new Vect_Pkg.Generic_Sorting ("<" => Less.all);
begin
Sorting.Sort (Arr.Vals);
end Sort;
procedure Sort (Val : in out JSON_Value; Less : access function (Left, Right : JSON_Value) return Boolean) is
-- package Sorting is new Object_Items_Pkg.Generic_Sorting ("<");
begin
case Val.Kind is
when JSON_Array_Type =>
Sort (Val.Data.Arr_Value.Arr, Less);
when JSON_Object_Type =>
-- Sorting.Sort (Val.Data.Obj_Value.Vals);
null;
when others =>
null;
end case;
end Sort;
------------
-- Append --
------------
procedure Append (Arr : in out JSON_Array; Val : JSON_Value) is
begin
Arr.Vals.Append (Val);
end Append;
-------------
-- Prepend --
-------------
procedure Prepend (Arr : in out JSON_Array; Val : JSON_Value) is
begin
Arr.Vals.Prepend (Val);
end Prepend;
---------
-- "&" --
---------
function "&" (Arr : JSON_Array; Value : JSON_Value) return JSON_Array is
Result : JSON_Array := Arr;
begin
Append (Result, Value);
return Result;
end "&";
function "&" (Value1, Value2 : JSON_Value) return JSON_Array is
Result : JSON_Array;
begin
Append (Result, Value1);
Append (Result, Value2);
return Result;
end "&";
-----------
-- Clear --
-----------
procedure Clear (Arr : in out JSON_Array) is
begin
Arr.Vals.Clear;
end Clear;
------------
-- Adjust --
------------
overriding
procedure Adjust (Obj : in out JSON_Value) is
begin
case Obj.Data.Kind is
when JSON_String_Type =>
if Obj.Data.Str_Value /= null then
Increment (Obj.Data.Str_Value.Cnt);
end if;
when JSON_Array_Type =>
if Obj.Data.Arr_Value /= null then
Increment (Obj.Data.Arr_Value.Cnt);
end if;
when JSON_Object_Type =>
if Obj.Data.Obj_Value /= null then
Increment (Obj.Data.Obj_Value.Cnt);
end if;
when others =>
null;
end case;
end Adjust;
--------------
-- Finalize --
--------------
overriding
procedure Finalize (Self : in out JSON_Parser) is
begin
if Self.State /= null then
Free (Self.State);
end if;
end Finalize;
overriding
procedure Finalize (Obj : in out JSON_Value) is
begin
case Obj.Data.Kind is
when JSON_String_Type =>
if Obj.Data.Str_Value /= null and then Decrement (Obj.Data.Str_Value.Cnt) then
Free (Obj.Data.Str_Value);
end if;
when JSON_Array_Type =>
declare
Arr : JSON_Array_Access := Obj.Data.Arr_Value;
begin
Obj.Data.Arr_Value := null;
if Arr /= null and then Decrement (Arr.Cnt) then
Free (Arr);