Skip to content

Commit db5b3c7

Browse files
committed
Use Pre aspect for some internal procedures in JSON.Types
1 parent 23d77eb commit db5b3c7

File tree

2 files changed

+31
-38
lines changed

2 files changed

+31
-38
lines changed

json/src/json-types.adb

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -263,22 +263,18 @@ package body JSON.Types is
263263

264264
procedure Append (Object : in out JSON_Value; Value : JSON_Value) is
265265
begin
266-
if Object.Kind = Array_Kind then
267-
declare
268-
Length : constant Natural
269-
:= Natural (Object.Allocator.Array_Levels (Object.Depth).Length);
270-
begin
271-
-- Assert that Object is the last array in a particular level
272-
-- so that its elements form a continuous array
273-
pragma Assert (Length = Object.Offset + Object.Length);
274-
end;
275-
276-
Object.Allocator.Array_Levels (Object.Depth).Append
277-
(Array_Value'(Kind => Value.Kind, Value => Value));
278-
Object.Length := Object.Length + 1;
279-
else
280-
raise Invalid_Type_Error with "Value not an array";
281-
end if;
266+
declare
267+
Length : constant Natural
268+
:= Natural (Object.Allocator.Array_Levels (Object.Depth).Length);
269+
begin
270+
-- Assert that Object is the last array in a particular level
271+
-- so that its elements form a continuous array
272+
pragma Assert (Length = Object.Offset + Object.Length);
273+
end;
274+
275+
Object.Allocator.Array_Levels (Object.Depth).Append
276+
(Array_Value'(Kind => Value.Kind, Value => Value));
277+
Object.Length := Object.Length + 1;
282278
end Append;
283279

284280
procedure Insert
@@ -287,27 +283,23 @@ package body JSON.Types is
287283
Value : JSON_Value;
288284
Check_Duplicate_Keys : Boolean) is
289285
begin
290-
if Object.Kind = Object_Kind then
291-
if Check_Duplicate_Keys and then Object.Contains (Key.Value) then
292-
raise Constraint_Error with "JSON object already has key '" & Key.Value & "'";
293-
end if;
294-
295-
declare
296-
Length : constant Natural
297-
:= Natural (Object.Allocator.Object_Levels (Object.Depth).Length);
298-
begin
299-
-- Assert that Object is the last object in a particular level
300-
-- so that its key-value pairs form a continuous array
301-
pragma Assert (Length = Object.Offset + Object.Length);
302-
end;
303-
pragma Assert (Key.Kind = String_Kind);
304-
305-
Object.Allocator.Object_Levels (Object.Depth).Append
306-
(Key_Value_Pair'(Kind => Value.Kind, Key => Key, Element => Value));
307-
Object.Length := Object.Length + 1;
308-
else
309-
raise Invalid_Type_Error with "Value not an object";
286+
if Check_Duplicate_Keys and then Object.Contains (Key.Value) then
287+
raise Constraint_Error with "JSON object already has key '" & Key.Value & "'";
310288
end if;
289+
290+
declare
291+
Length : constant Natural
292+
:= Natural (Object.Allocator.Object_Levels (Object.Depth).Length);
293+
begin
294+
-- Assert that Object is the last object in a particular level
295+
-- so that its key-value pairs form a continuous array
296+
pragma Assert (Length = Object.Offset + Object.Length);
297+
end;
298+
pragma Assert (Key.Kind = String_Kind);
299+
300+
Object.Allocator.Object_Levels (Object.Depth).Append
301+
(Key_Value_Pair'(Kind => Value.Kind, Key => Key, Element => Value));
302+
Object.Length := Object.Length + 1;
311303
end Insert;
312304

313305
-----------------------------------------------------------------------------

json/src/json-types.ads

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,16 @@ package JSON.Types is
9292

9393
-----------------------------------------------------------------------------
9494

95-
procedure Append (Object : in out JSON_Value; Value : JSON_Value);
95+
procedure Append (Object : in out JSON_Value; Value : JSON_Value)
96+
with Pre => Object.Kind = Array_Kind;
9697
-- Internal procedure
9798

9899
procedure Insert
99100
(Object : in out JSON_Value;
100101
Key : JSON_Value;
101102
Value : JSON_Value;
102103
Check_Duplicate_Keys : Boolean)
103-
with Pre => Key.Kind = String_Kind;
104+
with Pre => Object.Kind = Object_Kind and Key.Kind = String_Kind;
104105
-- Internal procedure
105106

106107
-----------------------------------------------------------------------------

0 commit comments

Comments
 (0)