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
|
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Command_Line;
with Ada.Text_IO;
with GNATCOLL.JSON;
with GNAT.OS_Lib;
procedure Test is
package JSON renames GNATCOLL.JSON;
package IO renames Ada.Text_IO;
function Read_File (Filename : String) return Unbounded_String is
use GNAT.OS_Lib;
F : constant File_Descriptor := Open_Read (Filename, Binary);
Expected_Bytes_Read : Integer;
Bytes_Read : Integer;
begin
Expected_Bytes_Read := Integer (File_Length (F));
declare
Buffer_Str : aliased String (1 .. Expected_Bytes_Read);
begin
Bytes_Read := Read (F, Buffer_Str'Address, Expected_Bytes_Read);
Close (F);
return To_Unbounded_String (Buffer_Str);
end;
end Read_File;
-- Read json filename passed as first argument
Filename : constant String := Ada.Command_Line.Argument(1);
JSON_Data : Unbounded_String := Read_File (Filename);
begin
-- Parse the json
declare
Value : JSON.JSON_Value := JSON.Read (Strm => JSON_Data,
Filename => Filename);
begin
-- Dump JSON back to stdout for validation by the python json
-- parser
declare
New_JSON_Data : constant Unbounded_String :=
JSON.Write (Item => Value, Compact => False);
begin
Ada.Text_IO.Put_Line(To_String(New_JSON_Data));
end;
end;
end Test;
|