are_1.4.0_a458cb9e/ada-util/samples/jsonread.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
with Ada.Text_IO;
with Ada.Command_Line;
with Util.Serialize.IO.JSON;
with Util.Beans.Objects;
with Util.Beans.Objects.Iterators;
procedure Jsonread is

   package UBO renames Util.Beans.Objects;
   procedure Dump (Object : in UBO.Object;
                   Indent : in Natural);

   procedure Dump (Object : in UBO.Object;
                   Indent : in Natural) is
      Iter : UBO.Iterators.Iterator := UBO.Iterators.First (Object);
      Pos  : Natural := 0;
   begin
      while UBO.Iterators.Has_Element (Iter) loop
         Ada.Text_IO.Set_Col (Ada.Text_IO.Count (Indent + 1));
         if UBO.Iterators.Has_Key (Iter) then
            Ada.Text_IO.Put (UBO.Iterators.Key (Iter) & "=");
         else
            Pos := Pos + 1;
            Ada.Text_IO.Put (Positive'Image (Pos) & "=");
         end if;
         declare
            Item : constant UBO.Object := UBO.Iterators.Element (Iter);
         begin
            if UBO.To_Bean (Item) /= null then
               Dump (Item, Indent + 1);
            else
               Ada.Text_IO.Put_Line (UBO.To_String (Item));
            end if;
         end;
         UBO.Iterators.Next (Iter);
      end loop;
   end Dump;

   Root   : UBO.Object;
begin
   if Ada.Command_Line.Argument_Count = 0 then
      Ada.Text_IO.Put_Line ("Usage: jsonread path");
      return;
   end if;

   for I in 1 .. Ada.Command_Line.Argument_Count loop
      Root := Util.Serialize.IO.JSON.Read (Ada.Command_Line.Argument (I));
      Dump (Root, 0);
   end loop;

end Jsonread;