-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson-unbounded_codecs.adb
1800 lines (1464 loc) · 65.7 KB
/
json-unbounded_codecs.adb
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
------------------------------------------------------------------------------
-- --
-- JSON Parser/Constructor --
-- --
-- ------------------------------------------------------------------------ --
-- --
-- Copyright (C) 2021-2022, ANNEXI-STRAYLINE Trans-Human Ltd. --
-- All rights reserved. --
-- --
-- Original Contributors: --
-- * Richard Wai (ANNEXI-STRAYLINE) --
-- --
-- Redistribution and use in source and binary forms, with or without --
-- modification, are permitted provided that the following conditions are --
-- met: --
-- --
-- * Redistributions of source code must retain the above copyright --
-- notice, this list of conditions and the following disclaimer. --
-- --
-- * Redistributions in binary form must reproduce the above copyright --
-- notice, this list of conditions and the following disclaimer in --
-- the documentation and/or other materials provided with the --
-- distribution. --
-- --
-- * Neither the name of the copyright holder nor the names of its --
-- contributors may be used to endorse or promote products derived --
-- from this software without specific prior written permission. --
-- --
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS --
-- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT --
-- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A --
-- PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT --
-- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, --
-- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT --
-- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, --
-- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY --
-- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT --
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE --
-- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. --
-- --
------------------------------------------------------------------------------
with Ada.Real_Time;
with Ada.Exceptions;
with Ada.IO_Exceptions;
with Ada.Strings.Fixed;
with Ada.Strings.Wide_Wide_Fixed;
with Ada.Strings.Wide_Wide_Maps;
with Ada.Strings.UTF_Encoding.Wide_Wide_Strings;
with Ada.Characters.Conversions;
with Ada.Unchecked_Deallocate_Subpool;
with Unicode.UTF8_Stream_Decoder;
with JSON.Standards;
package body JSON.Unbounded_Codecs is
function To_Wide_Wide_String (Item: in String) return Wide_Wide_String
renames Ada.Characters.Conversions.To_Wide_Wide_String;
--
-- Tools
--
-----------------
-- Append_Node --
-----------------
-- Allocates and initializes a new node that is appended to Parent as a
-- child, and returns the reference.
--
-- This process properly initializes the Index, as well as updating the
-- Member/Element counter of the Parent.
--
-- The container is default initialized (JSON_Null), and the callee is
-- reponsible for mutating the Container, and properly initializing it.
function Append_Node (Parent: not null Node) return not null Node
is
New_Node: Node := new (Parent.Codec_Subpool) JSON_Value;
begin
New_Node.Root := (if Parent.Parent = null then Parent else Parent.Root);
New_Node.Parent := Parent;
New_Node.Codec_Subpool := Parent.Codec_Subpool;
case JSON_Structure_Kind (Parent.Container.Kind) is
when JSON_Object =>
if Parent.Container.First_Member = null then
pragma Assert (Parent.Container.Last_Member = null);
Parent.Container.First_Member := New_Node;
Parent.Container.Last_Member := New_Node;
New_Node.Index := 0;
else
pragma Assert (Parent.Container.Last_Member.Next = null);
Parent.Container.Last_Member.Next := New_Node;
New_Node.Prev := Parent.Container.Last_Member;
New_Node.Index := Parent.Container.Last_Member.Index + 1;
Parent.Container.Last_Member := New_Node;
end if;
Slab_Strings.Setup (Target => New_Node.Name,
Subpool => New_Node.Codec_Subpool);
New_Node.Index := Parent.Container.Member_Count;
Parent.Container.Member_Count := Parent.Container.Member_Count + 1;
when JSON_Array =>
if Parent.Container.First_Element = null then
pragma Assert (Parent.Container.Last_Element = null);
Parent.Container.First_Element := New_Node;
Parent.Container.Last_Element := New_Node;
New_Node.Index := 0;
else
pragma Assert (Parent.Container.Last_Element.Next = null);
Parent.Container.Last_Element.Next := New_Node;
New_Node.Prev := Parent.Container.Last_Element;
New_Node.Index := Parent.Container.Last_Element.Index + 1;
Parent.Container.Last_Element := New_Node;
end if;
New_Node.Index := Parent.Container.Element_Count;
Parent.Container.Element_Count
:= Parent.Container.Element_Count + 1;
end case;
return New_Node;
end Append_Node;
---------------
-- Seek_Node --
---------------
-- Seek Node obtains the Node value of a an actual JSON_Value within a Codec.
-- This is primarily used by Codec.Delve and Codec.Constant_Delve to
-- generate the JSON_Mutable_Structure, which contains a reference, without
-- resorting to Unchecked_Access.
--
-- Seek_Node also ensures Value belongs to Codec, and raises Program_Error
-- if it does not.
function Seek_Node (Codec: Unbounded_JSON_Codec;
Value: JSON_Value'Class)
return not null Node with Inline
is begin
if Value.Root /= Codec.Root_Node then
raise Program_Error with "Value is not a member of Codec";
elsif Value.Parent = null then
return Codec.Root_Node;
elsif Value.Prev /= null then
return Value.Prev.Next;
elsif Value.Next /= null then
return Value.Next.Prev;
else
-- This should not possibly fail
case JSON_Structure_Kind (Value.Parent.Container.Kind) is
when JSON_Object =>
pragma Assert
(Value.Parent.Container.Member_Count = 1);
return Value.Parent.Container.First_Member;
when JSON_Array =>
pragma Assert
(Value.Parent.Container.Element_Count = 1);
return Value.Parent.Container.First_Element;
end case;
end if;
end Seek_Node;
--
-- Internal Subsystem Implementations
--
package body Slab_Strings is separate;
package body Node_Hash_Maps is separate;
--
-- JSON_Value
--
----------
-- Kind --
----------
function Kind (Value: JSON_Value) return JSON_Value_Kind is
(Value.Container.Kind);
-----------------
-- Parent_Kind --
-----------------
function Parent_Kind (Value: JSON_Value) return JSON_Structure_Kind is
begin
if Value.Parent = null then
-- This indicates the root object
raise Constraint_Error with
"The Codec Root object has no parent.";
else
return Value.Parent.Container.Kind;
end if;
end;
----------
-- Name --
----------
function Name (Value: JSON_Value) return JSON_String_Value is
begin
-- Explicit check for the precondition, since this could be a common
-- mistake
if Value.Container.Kind /= JSON_Object then
raise Constraint_Error with "Value is not named. Only members of "
& "JSON objects are named.";
end if;
return Slab_Strings.To_JSON_String (Value.Name);
end;
---------------
-- UTF8_Name --
---------------
function UTF8_Name (Value: JSON_Value) return UTF_8_String is
use Ada.Strings.UTF_Encoding.Wide_Wide_Strings;
Wide_Wide_Name: constant JSON_String_Value
:= Slab_Strings.To_JSON_String (Value.Name);
begin
return Encode (Wide_Wide_Name);
end;
-----------
-- Index --
-----------
function Index (Value: JSON_Value) return Natural is (Value.Index);
-----------
-- Value -- (Get)
-----------
function Value (Value: JSON_Value) return Boolean is
(Value.Container.Boolean_Value);
function Value (Value: JSON_Value) return JSON_Integer_Value is
(Value.Container.Integer_Value);
function Value (Value: JSON_Value) return JSON_Float_Value is
(Value.Container.Float_Value);
function Value (Value: JSON_Value) return JSON_String_Value is
(Slab_Strings.To_JSON_String (Value.Container.String_Value));
function UTF8_Value (Value: JSON_Value) return UTF_8_String is
(Ada.Strings.UTF_Encoding.Wide_Wide_Strings.Encode (Value.Value));
-----------
-- Value -- (Set)
-----------
procedure Value (Target: in out JSON_Value;
Value : in Boolean;
Mutate: in Boolean := False)
is begin
if Target.Container.Kind = JSON_Boolean then
Target.Container.Boolean_Value := Value;
elsif Mutate or else Target.Container.Kind = JSON_Null then
-- Full mutation
Target.Container
:= Value_Container'(Kind => JSON_Boolean,
Boolean_Value => Value);
else
raise Constraint_Error with
"Intended value (JSON_INTEGER) does not fit the Kind of the target "
& '(' & JSON_Value_Kind'Image (Target.Container.Kind) & ").";
end if;
end;
----------------------------------------------------------------------
procedure Value (Target: in out JSON_Value;
Value : in JSON_Integer_Value;
Mutate: in Boolean := False)
is begin
if Target.Container.Kind = JSON_Integer then
Target.Container.Integer_Value := Value;
elsif Mutate or else Target.Container.Kind = JSON_Null then
-- Full mutation
Target.Container
:= Value_Container'(Kind => JSON_Integer,
Integer_Value => Value);
else
raise Constraint_Error with
"Intended value (JSON_INTEGER) does not fit the Kind of the target "
& '(' & JSON_Value_Kind'Image (Target.Container.Kind) & ").";
end if;
end;
----------------------------------------------------------------------
procedure Value (Target: in out JSON_Value;
Value : in JSON_Float_Value;
Mutate: in Boolean := False)
is begin
if Target.Container.Kind = JSON_Float then
Target.Container.Float_Value := Value;
elsif Mutate or else Target.Kind = JSON_Null then
-- Full mutation
Target.Container
:= Value_Container'(Kind => JSON_Float,
Float_Value => Value);
else
raise Constraint_Error with
"Intended value (JSON_FLOAT) does not fit the Kind of the target "
& '(' & JSON_Value_Kind'Image (Target.Container.Kind) & ").";
end if;
end;
----------------------------------------------------------------------
procedure Value (Target: in out JSON_Value;
Value : in JSON_String_Value;
Mutate: in Boolean := False)
is
procedure Set_Value with Inline is
use Slab_Strings;
begin
Clear (Target.Container.String_Value);
Append (Target.Container.String_Value, Value);
end;
begin
if Target.Container.Kind = JSON_String then
Set_Value;
elsif Mutate or else Target.Container.Kind = JSON_Null then
-- Full mutation
Target.Container := Value_Container'(Kind => JSON_String,
others => <>);
Slab_Strings.Setup (Target => Target.Container.String_Value,
Subpool => Target.Codec_Subpool);
Set_Value;
else
raise Constraint_Error with
"Intended value (JSON_STRIJNG) does not fit the Kind of the target "
& '(' & JSON_Value_Kind'Image (Target.Container.Kind) & ").";
end if;
end;
-------------
-- Nullify --
-------------
procedure Nullify (Target: in out JSON_Value) is
begin
Target.Container := Value_Container'(Kind => JSON_Null);
end;
--
-- JSON_Constant_Structure / JSON_Mutable_Structure
--
----------
-- Kind --
----------
function Kind (Structure: JSON_Constant_Structure) return
JSON_Structure_Kind is (Structure.Structure_Root.Container.Kind);
function Kind (Structure: JSON_Mutable_Structure) return
JSON_Structure_Kind is (Structure.Structure_Root.Container.Kind);
------------
-- Length --
------------
function Length (Structure: JSON_Constant_Structure) return Natural is
Struct_Actual: JSON_Value renames Structure.Structure_Root.all;
begin
case JSON_Structure_Kind (Struct_Actual.Container.Kind) is
when JSON_Object => return Struct_Actual.Container.Member_Count;
when JSON_Array => return Struct_Actual.Container.Element_Count;
end case;
end;
----------------------------------------------------------------------
function Length (Structure: JSON_Mutable_Structure) return Natural is
(JSON_Constant_Structure'(Structure_Root => Structure.Structure_Root,
Codec_Constant => Structure.Codec_Constant)
.Length);
----------------
-- Has_Member --
----------------
function Has_Member (Structure: JSON_Constant_Structure;
Name : JSON_String_Value)
return Boolean
is
use Node_Hash_Maps;
Struct_Actual: JSON_Value renames Structure.Structure_Root.all;
begin
if Structure.Codec_Constant.Write_Only then
raise Constraint_Error with "Cannot index. Codec is write-only.";
elsif Struct_Actual.Container.Kind /= JSON_Object then
return False;
else
return Lookup_By_Name (Name_Map => Struct_Actual.Container.Name_Map,
Name => Name)
/= null;
end if;
end;
----------------------------------------------------------------------
function Has_Member (Structure: JSON_Mutable_Structure;
Name : JSON_String_Value)
return Boolean
is (JSON_Constant_Structure'(Structure_Root => Structure.Structure_Root,
Codec_Constant => Structure.Codec_Constant)
.Has_Member(Name));
--------------------
-- Array Indexing --
--------------------
function JCS_Reference_Node (Structure: JSON_Constant_Structure;
Index : JSON_Array_Index)
return not null Node
is
Struct_Actual: JSON_Value renames Structure.Structure_Root.all;
begin
if Structure.Codec_Constant.Write_Only then
raise Constraint_Error with "Cannot index. Codec is write-only.";
elsif Struct_Actual.Container.Kind /= JSON_Array then
-- This allows a smart compiler to eliminate remaining discrimint
-- checks
raise Constraint_Error with
"Integer indexing only applies to JSON_Array types";
elsif Index >= Struct_Actual.Container.Element_Count then
raise Constraint_Error with "Index is out of range of JSON_Array";
end if;
return Node_Hash_Maps.Lookup_By_Index
(Index_Map => Struct_Actual.Container.Index_Map,
Index => Index);
-- Null check is enforced here if, somehow, Index is not valid
end JCS_Reference_Node;
----------------------------------------------------------------------
function JCS_Constant_Reference
(Structure: JSON_Constant_Structure; Index: JSON_Array_Index)
return JSON_Value_Constant_Reference
is ((Ref => JCS_Reference_Node (Structure, Index)));
----------------------------------------------------------------------
function JMS_Constant_Reference
(Structure: JSON_Mutable_Structure; Index: JSON_Array_Index)
return JSON_Value_Constant_Reference
is (JSON_Constant_Structure'(Structure_Root => Structure.Structure_Root,
Codec_Constant => Structure.Codec_Constant)
.JCS_Constant_Reference (Index));
----------------------------------------------------------------------
function JMS_Reference (Structure: in out JSON_Mutable_Structure;
Index : in JSON_Array_Index)
return JSON_Value_Reference
is
Constant_Structure: JSON_Constant_Structure :=
(Structure_Root => Structure.Structure_Root,
Codec_Constant => Structure.Codec_Constant);
Target: Node := JCS_Reference_Node (Structure => Constant_Structure,
Index => Index);
begin
return (Ref => Target);
end;
----------------------------
-- Object (Name) Indexing --
----------------------------
function JCS_Reference_Node (Structure: JSON_Constant_Structure;
Name : JSON_String_Value)
return not null Node
is
Struct_Actual: JSON_Value renames Structure.Structure_Root.all;
Hit: Node;
begin
if Structure.Codec_Constant.Write_Only then
raise Constraint_Error with "Cannot index. Codec is write-only.";
elsif Struct_Actual.Container.Kind /= JSON_Object then
raise Constraint_Error with
"Name indexing only applies to JSON_Object types";
end if;
Hit := Node_Hash_Maps.Lookup_By_Name
(Name_Map => Struct_Actual.Container.Name_Map,
Name => Name);
if Hit = null then
raise Constraint_Error with "Object does not contain named member.";
else
return Hit;
end if;
end JCS_Reference_Node;
----------------------------------------------------------------------
function JCS_Constant_Reference
(Structure: JSON_Constant_Structure; Name: JSON_String_Value)
return JSON_Value_Constant_Reference
is ((Ref => JCS_Reference_Node (Structure, Name)));
----------------------------------------------------------------------
function JMS_Constant_Reference
(Structure: JSON_Mutable_Structure; Name: JSON_String_Value)
return JSON_Value_Constant_Reference
is (JSON_Constant_Structure'(Structure_Root => Structure.Structure_Root,
Codec_Constant => Structure.Codec_Constant)
.JCS_Constant_Reference (Name));
----------------------------------------------------------------------
function JMS_Reference (Structure: in out JSON_Mutable_Structure;
Name : in JSON_String_Value)
return JSON_Value_Reference
is
Constant_Structure: JSON_Constant_Structure :=
(Structure_Root => Structure.Structure_Root,
Codec_Constant => Structure.Codec_Constant);
Target: Node := JCS_Reference_Node (Structure => Constant_Structure,
Name => Name);
begin
return (Ref => Target);
end;
---------------
-- Has_Value --
---------------
function Has_Value (Position: JSON_Structure_Cursor) return Boolean is
(Position.Target /= null);
---------------------
-- Cursor Indexing --
---------------------
function JCS_Reference_Node (Structure: JSON_Constant_Structure;
Position : JSON_Structure_Cursor)
return not null Node
is begin
if Position.Target = null then
raise Constraint_Error with "Cursor does not designate a value.";
elsif Position.Target.Parent /= Structure.Structure_Root then
raise Constraint_Error with "Cursor is not from this structure.";
else
return Position.Target;
end if;
end JCS_Reference_Node;
----------------------------------------------------------------------
function JCS_Constant_Reference (Structure: JSON_Constant_Structure;
Position : JSON_Structure_Cursor)
return JSON_Value_Constant_Reference
is ((Ref => JCS_Reference_Node (Structure, Position)));
----------------------------------------------------------------------
function JMS_Constant_Reference (Structure: JSON_Mutable_Structure;
Position : JSON_Structure_Cursor)
return JSON_Value_Constant_Reference
is (JSON_Constant_Structure'(Structure_Root => Structure.Structure_Root,
Codec_Constant => Structure.Codec_Constant)
.JCS_Constant_Reference (Position));
----------------------------------------------------------------------
function JMS_Reference (Structure: in out JSON_Mutable_Structure;
Position : in JSON_Structure_Cursor)
return JSON_Value_Reference
is
Constant_Structure: JSON_Constant_Structure :=
(Structure_Root => Structure.Structure_Root,
Codec_Constant => Structure.Codec_Constant);
Target: Node := JCS_Reference_Node (Structure => Constant_Structure,
Position => Position);
begin
return (Ref => Target);
end;
---------------
-- Iteration --
---------------
type Structure_Iterator is limited
new JSON_Structure_Iterators.Reversible_Iterator with
record
Structure_Root: not null Node;
end record;
overriding
function First (Object: Structure_Iterator) return JSON_Structure_Cursor;
overriding
function Next (Object: Structure_Iterator; Position: JSON_Structure_Cursor)
return JSON_Structure_Cursor;
overriding
function Last (Object: Structure_Iterator) return JSON_Structure_Cursor;
overriding
function Previous (Object : Structure_Iterator;
Position: JSON_Structure_Cursor)
return JSON_Structure_Cursor;
----------------------------------------------------------------------
function JCS_Iterate
(Structure: JSON_Constant_Structure)
return JSON_Structure_Iterators.Reversible_Iterator'Class
is (Structure_Iterator'(Structure_Root => Structure.Structure_Root));
function JMS_Iterate
(Structure: JSON_Mutable_Structure)
return JSON_Structure_Iterators.Reversible_Iterator'Class
is (Structure_Iterator'(Structure_Root => Structure.Structure_Root));
----------------------------------------------------------------------
function First (Object: Structure_Iterator) return JSON_Structure_Cursor is
Struct_Actual: JSON_Value renames Object.Structure_Root.all;
begin
case JSON_Structure_Kind (Struct_Actual.Container.Kind) is
when JSON_Object =>
return (Target => Struct_Actual.Container.First_Member);
when JSON_Array =>
return (Target => Struct_Actual.Container.First_Element);
end case;
end;
----------------------------------------------------------------------
function Last (Object: Structure_Iterator) return JSON_Structure_Cursor is
Struct_Actual: JSON_Value renames Object.Structure_Root.all;
begin
case JSON_Structure_Kind (Struct_Actual.Container.Kind) is
when JSON_Object =>
return (Target => Struct_Actual.Container.Last_Member);
when JSON_Array =>
return (Target => Struct_Actual.Container.Last_Element);
end case;
end;
----------------------------------------------------------------------
function Next (Object: Structure_Iterator; Position: JSON_Structure_Cursor)
return JSON_Structure_Cursor
is begin
if Object.Structure_Root /= Position.Target.Parent then
raise Constraint_Error with
"Cursor does not belong to iterated structure.";
end if;
return (Target => Position.Target.Next);
end Next;
----------------------------------------------------------------------
function Previous (Object : Structure_Iterator;
Position: JSON_Structure_Cursor)
return JSON_Structure_Cursor
is begin
if Object.Structure_Root /= Position.Target.Parent then
raise Constraint_Error with
"Cursor does not belong to iterated structure.";
end if;
return (Target => Position.Target.Prev);
end;
------------------------
-- Structure Building --
------------------------
function Append_Null_Member (Structure: in out JSON_Mutable_Structure;
Name : in JSON_String_Value)
return JSON_Value_Reference
is
Struct_Actual: JSON_Value renames Structure.Structure_Root.all;
New_Node: Node;
begin
if Name'Length = 0 then
raise Constraint_Error with "Names must not be empty";
elsif Struct_Actual.Container.Kind /= JSON_Object then
raise Constraint_Error with "Structure is not an object.";
end if;
New_Node := Append_Node (Structure.Structure_Root);
Slab_Strings.Append (New_Node.Name, Name);
if not Structure.Codec_Constant.Write_Only then
Node_Hash_Maps.Register
(Path_Map => Structure.Codec_Mutable.Path_Map,
Registrant => New_Node);
end if;
return JSON_Value_Reference'(Ref => New_Node);
end Append_Null_Member;
----------------------------------------------------------------------
function Append_Null_Element (Structure: in out JSON_Mutable_Structure)
return JSON_Value_Reference
is
Struct_Actual: JSON_Value renames Structure.Structure_Root.all;
New_Node: Node;
begin
if Struct_Actual.Container.Kind /= JSON_Array then
raise Constraint_Error with "Structure is not an array.";
end if;
New_Node := Append_Node (Structure.Structure_Root);
if not Structure.Codec_Constant.Write_Only then
Node_Hash_Maps.Register
(Path_Map => Structure.Codec_Mutable.Path_Map,
Registrant => New_Node);
end if;
return JSON_Value_Reference'(Ref => New_Node);
end Append_Null_Element;
----------------------------------------------------------------------
function Append_Structural_Member
(Structure: in out JSON_Mutable_Structure;
Name : in JSON_String_Value;
Kind : in JSON_Structure_Kind)
return JSON_Value_Reference
is
New_Struct: JSON_Value_Reference
:= Structure.Append_Null_Member (Name);
begin
case Kind is
when JSON_Object =>
New_Struct.Container := (Kind => JSON_Object, others => <>);
Node_Hash_Maps.Setup (Map => New_Struct.Container.Name_Map,
Subpool => New_Struct.Codec_Subpool);
when JSON_Array =>
New_Struct.Container := (Kind => JSON_Array, others => <>);
Node_Hash_Maps.Setup (Map => New_Struct.Container.Index_Map,
Subpool => New_Struct.Codec_Subpool);
end case;
return (Ref => New_Struct.Ref.all'Unchecked_Access);
-- THIS IS SAFE BECAUSE: Ref points at a Node, which is always allocated
-- dynamically on a subpool owned by the codec. Since
-- JSON_Mutable_Structure is limited, and non-default disciminated
-- objects cannot be assigned except through initialization, it is not
-- possible for any JSON_Value_Reference returned here to end up as a
-- dangling reference. However since we are using an anonymous access
-- type, the compiler doesn't know that.
end;
----------------------------------------------------------------------
function Append_Structural_Element
(Structure: in out JSON_Mutable_Structure;
Kind : in JSON_Structure_Kind)
return JSON_Value_Reference
is
New_Struct: JSON_Value_Reference
:= Structure.Append_Null_Element;
begin
case Kind is
when JSON_Object =>
New_Struct.Container := (Kind => JSON_Object, others => <>);
Node_Hash_Maps.Setup (Map => New_Struct.Container.Name_Map,
Subpool => New_Struct.Codec_Subpool);
when JSON_Array =>
New_Struct.Container := (Kind => JSON_Array, others => <>);
Node_Hash_Maps.Setup (Map => New_Struct.Container.Index_Map,
Subpool => New_Struct.Codec_Subpool);
end case;
return (Ref => New_Struct.Ref.all'Unchecked_Access);
-- THIS IS SAFE BECAUSE: Ref points at a Node, which is always allocated
-- dynamically on a subpool owned by the codec. Since
-- JSON_Mutable_Structure is limited, and non-default disciminated
-- objects cannot be assigned except through initialization, it is not
-- possible for any JSON_Value_Reference returned here to end up as a
-- dangling reference. However since we are using an anonymous access
-- type, the compiler doesn't know that.
end;
--
-- Unbounded_JSON_Codec
--
-----------------
-- Path_Exists --
-----------------
function Path_Exists (Codec: Unbounded_JSON_Codec;
Path : in JSON_String_Value)
return Boolean
is
use Node_Hash_Maps;
begin
if Codec.Write_Only then
raise Constraint_Error with "Cannot index. Codec is write-only.";
end if;
return Lookup_By_Path (Path_Map => Codec.Path_Map,
Path => Path)
/= null;
end;
------------
-- Lookup --
------------
function Lookup (Codec: aliased in out Unbounded_JSON_Codec;
Path : in JSON_String_Value)
return JSON_Value_Reference
is
use Node_Hash_Maps;
begin
if Codec.Write_Only then
raise Constraint_Error with "Cannot index. Codec is write-only.";
end if;
return JSON_Value_Reference'
(Ref => Lookup_By_Path (Path_Map => Codec.Path_Map,
Path => Path));
end;
----------------------------------------------------------------------
function Constant_Lookup (Codec: aliased Unbounded_JSON_Codec;
Path : JSON_String_Value)
return JSON_Value_Constant_Reference
is
use Node_Hash_Maps;
begin
if Codec.Write_Only then
raise Constraint_Error with "Cannot index. Codec is write-only.";
end if;
return JSON_Value_Constant_Reference'
(Ref => Lookup_By_Path (Path_Map => Codec.Path_Map,
Path => Path));
end;
----------
-- Root --
----------
function Root (Codec: aliased in out Unbounded_JSON_Codec)
return JSON_Mutable_Structure'Class
is (JSON_Mutable_Structure'(Structure_Root => Codec.Root_Node,
Codec_Constant => Codec'Unchecked_Access,
Codec_Mutable => Codec'Unchecked_Access));
-- THIS IS SAFE BECAUSE: JSON_Mutable_Structure'Class is a limited type,
-- and so this function must be initializing a new declaration, which
-- ensures Codec exists for at least as long as the returned object.
--
-- If Unchecked_Deallocate is used on Codec, use of 'Access would not be
-- protective anyways.
----------------------------------------------------------------------
function Constant_Root (Codec: aliased Unbounded_JSON_Codec)
return JSON_Constant_Structure'Class
is (JSON_Constant_Structure'(Structure_Root => Codec.Root_Node,
Codec_Constant => Codec'Unchecked_Access));
-- THIS IS SAFE BECAUSE: JSON_Constant_Structure'Class is a limited type,
-- and so this function must be initializing a new declaration, which
-- ensures Codec exists for at least as long as the returned object.
--
-- If Unchecked_Deallocate is used on Codec, use of 'Access would not be
-- protective anyways.
-----------
-- Delve --
-----------
function Delve (Codec : aliased in out Unbounded_JSON_Codec;
Structure: aliased in out JSON_Value'Class)
return JSON_Mutable_Structure'Class
is begin
return JSON_Mutable_Structure'
(Structure_Root => Seek_Node (Codec => Codec,
Value => Structure),
Codec_Constant => Codec'Unchecked_Access,
Codec_Mutable => Codec'Unchecked_Access);
-- THIS IS SAFE BECAUSE: JSON_Mutable_Structure'Class is a limited type,
-- and so this function must be initializing a new declaration, which
-- ensures Codec exists for at least as long as the returned object.
--
-- If Unchecked_Deallocate is used on Codec, use of 'Access would not be
-- protective anyways.
end Delve;
----------------------------------------------------------------------
function Constant_Delve (Codec : aliased Unbounded_JSON_Codec;
Structure: aliased JSON_Value'Class)
return JSON_Constant_Structure'Class
is begin
return JSON_Constant_Structure'
(Structure_Root => Seek_Node (Codec => Codec,
Value => Structure),
Codec_Constant => Codec'Unchecked_Access);
-- THIS IS SAFE BECAUSE: JSON_Constant_Structure'Class is a limited type,
-- and so this function must be initializing a new declaration, which
-- ensures Codec exists for at least as long as the returned object.
--
-- If Unchecked_Deallocate is used on Codec, use of 'Access would not be
-- protective anyways.
end Constant_Delve;
-----------
-- Valid --
-----------
function Valid (Codec: Unbounded_JSON_Codec) return Boolean is
use Parsers;
begin
return Last_Indication (Codec.Parser) /= Invalid;
end;
-------------------------------
-- Invalid_Because_End_Error --
-------------------------------
function Invalid_Because_End_Error (Codec: Unbounded_JSON_Codec)
return Boolean
is (Codec.End_Error);
-------------------
-- Error_Message --
-------------------
procedure Set_Parser_Error (Codec: in out Unbounded_JSON_Codec) is
use Ada.Strings, Ada.Strings.Fixed;
Pos: constant Parsers.Text_Position
:= Parsers.Last_Position (Codec.Parser);
Parser_Error_String: constant String
:= "Invalid JSON: "
&
(if Pos.Overflow then "(Position Overflow):"
else Trim (Positive'Image (Pos.Line), Left) & ':'
& Trim (Natural'Image (Pos.Column), Left) & ':')
& ' '
& Parsers.Invalid_Reason (Codec.Parser);
begin
Slab_Strings.Clear (Codec.Error);
Slab_Strings.Append
(Target => Codec.Error,
Source => To_Wide_Wide_String (Parser_Error_String));
end Set_Parser_Error;
----------------------------------------------------------------------
function Error_Message (Codec: Unbounded_JSON_Codec) return String is
use Ada.Strings.UTF_Encoding.Wide_Wide_Strings;
begin
if Codec.Valid then
raise Constraint_Error with "Error_Message can only be invoked if "
& "the Codec is inValid.";
end if;
return Encode (Slab_Strings.To_JSON_String (Codec.Error));
end Error_Message;