Skip to content

Commit d48695f

Browse files
committed
Implement JSON array concatenation
Issue: eng/gpr/gpr-issues#739
1 parent 0cfb9a6 commit d48695f

3 files changed

Lines changed: 45 additions & 0 deletions

File tree

core/src/gnatcoll-json.adb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,13 @@ package body GNATCOLL.JSON is
953953
Arr.Vals.Append (Val);
954954
end Append;
955955

956+
procedure Append (Arr : in out JSON_Array; Val : JSON_Array) is
957+
begin
958+
for Value of Val.Vals loop
959+
Arr.Vals.Append (Value);
960+
end loop;
961+
end Append;
962+
956963
-------------
957964
-- Prepend --
958965
-------------
@@ -962,6 +969,13 @@ package body GNATCOLL.JSON is
962969
Arr.Vals.Prepend (Val);
963970
end Prepend;
964971

972+
procedure Prepend (Arr : in out JSON_Array; Val : JSON_Array) is
973+
begin
974+
for Value of reverse Val.Vals loop
975+
Arr.Vals.Prepend (Value);
976+
end loop;
977+
end Prepend;
978+
965979
---------
966980
-- "&" --
967981
---------

core/src/gnatcoll-json.ads

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,15 @@ package GNATCOLL.JSON is
219219
procedure Append (Arr : in out JSON_Array; Val : JSON_Value);
220220
-- Append Val as a new element at the end of the Arr array
221221

222+
procedure Append (Arr : in out JSON_Array; Val : JSON_Array);
223+
-- Append all values of Val at the end of the Arr array
224+
222225
procedure Prepend (Arr : in out JSON_Array; Val : JSON_Value);
223226
-- Insert Val as the first element of the Arr array
224227

228+
procedure Prepend (Arr : in out JSON_Array; Val : JSON_Array);
229+
-- Insert all values of Val at the beginning of the Arr array
230+
225231
procedure Clear (Arr : in out JSON_Array);
226232
-- Remove all elements in Arr
227233

testsuite/core/tests/json/api/test.adb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,11 @@ function Test return Integer is
8282

8383
Int_0 : JSON_Value := Create (Integer'(0));
8484
Int_1 : constant JSON_Value := Create (Integer'(1));
85+
Int_2 : constant JSON_Value := Create (Integer'(2));
86+
Int_3 : constant JSON_Value := Create (Integer'(3));
87+
8588
Hello_World : constant JSON_Value := Create ("Hello world!");
89+
Hi_World : constant JSON_Value := Create ("Hi world!");
8690

8791
Float_0 : constant JSON_Value := Create (Float'(0.0));
8892
Float_1 : constant JSON_Value := Create (Float'(1.0));
@@ -99,6 +103,7 @@ begin
99103

100104
declare
101105
Arr : JSON_Array := Int_0 & Int_1 & Hello_World;
106+
Arr2 : constant JSON_Array := Int_2 & Int_3 & Hi_World;
102107
Empty : constant JSON_Array := Empty_Array;
103108
begin
104109
-- Inspect an empty array
@@ -143,6 +148,26 @@ begin
143148
Clear (Arr);
144149
A.Assert (Length (Arr) = 0);
145150

151+
Arr := Hello_World & Int_0 & Int_1;
152+
Append (Arr, Arr2);
153+
A.Assert (Length (Arr) = 6);
154+
A.Assert (Get (Arr, 1) = Hello_World);
155+
A.Assert (Get (Arr, 2) = Int_0);
156+
A.Assert (Get (Arr, 3) = Int_1);
157+
A.Assert (Get (Arr, 4) = Int_2);
158+
A.Assert (Get (Arr, 5) = Int_3);
159+
A.Assert (Get (Arr, 6) = Hi_World);
160+
161+
Arr := Hello_World & Int_0 & Int_1;
162+
Prepend (Arr, Arr2);
163+
A.Assert (Length (Arr) = 6);
164+
A.Assert (Get (Arr, 1) = Int_2);
165+
A.Assert (Get (Arr, 2) = Int_3);
166+
A.Assert (Get (Arr, 3) = Hi_World);
167+
A.Assert (Get (Arr, 4) = Hello_World);
168+
A.Assert (Get (Arr, 5) = Int_0);
169+
A.Assert (Get (Arr, 6) = Int_1);
170+
146171
Arr := Hello_World & Int_0 & Int_1 & Int_0;
147172
Sort (Arr, Less'Access);
148173
Check_Image (Create (Arr), "[0,0,1,""Hello world!""]");

0 commit comments

Comments
 (0)