Json in Ada

Our program demonstrates JSON encoding and decoding in Ada, including handling of basic data types and custom structures.

with Ada.Text_IO;
with GNATCOLL.JSON;

procedure JSON_Example is
   use Ada.Text_IO;
   use GNATCOLL.JSON;

   type Response1 is record
      Page   : Integer;
      Fruits : JSON_Array;
   end record;

   type Response2 is record
      Page   : Integer;
      Fruits : JSON_Array;
   end record;

   procedure Print_JSON (J : JSON_Value) is
   begin
      Put_Line (Write (J));
   end Print_JSON;

   J : JSON_Value;
begin
   -- Encoding basic data types to JSON
   J := Create (True);
   Print_JSON (J);

   J := Create (1);
   Print_JSON (J);

   J := Create (2.34);
   Print_JSON (J);

   J := Create ("gopher");
   Print_JSON (J);

   -- Encoding slices and maps
   declare
      Arr : JSON_Array;
   begin
      Append (Arr, "apple");
      Append (Arr, "peach");
      Append (Arr, "pear");
      J := Create (Arr);
      Print_JSON (J);
   end;

   declare
      Obj : JSON_Value := Create_Object;
   begin
      Obj.Set_Field ("apple", Create (5));
      Obj.Set_Field ("lettuce", Create (7));
      Print_JSON (Obj);
   end;

   -- Encoding custom data types
   declare
      Res1 : Response1;
      Res1_JSON : JSON_Value := Create_Object;
   begin
      Res1.Page := 1;
      Append (Res1.Fruits, "apple");
      Append (Res1.Fruits, "peach");
      Append (Res1.Fruits, "pear");
      Res1_JSON.Set_Field ("Page", Create (Res1.Page));
      Res1_JSON.Set_Field ("Fruits", Create (Res1.Fruits));
      Print_JSON (Res1_JSON);
   end;

   -- Decoding JSON data
   declare
      Data : constant String := "{""num"":6.13,""strs"":[""a"",""b""]}";
      J : constant JSON_Value := Read (Data);
   begin
      Put_Line ("num:" & J.Get ("num").Image);
      Put_Line ("first string:" & J.Get ("strs").Get (1).Image);
   end;

   -- Decoding into custom types
   declare
      Data : constant String := "{""page"": 1, ""fruits"": [""apple"", ""peach""]}";
      J : constant JSON_Value := Read (Data);
      Res : Response2;
   begin
      Res.Page := J.Get ("page");
      Res.Fruits := J.Get ("fruits");
      Put_Line ("Page:" & Res.Page'Image);
      Put_Line ("First fruit:" & Get (Res.Fruits, 1).Image);
   end;
end JSON_Example;

This Ada program demonstrates JSON encoding and decoding using the GNATCOLL.JSON library. Here’s a breakdown of what it does:

  1. We start by encoding basic data types (boolean, integer, float, string) to JSON.

  2. We then show how to encode array-like structures (using JSON_Array) and map-like structures (using JSON_Value as an object).

  3. We demonstrate encoding a custom record type (Response1) to JSON.

  4. For decoding, we first parse a JSON string into a JSON_Value and extract specific fields.

  5. Finally, we show how to decode JSON data into a custom record type (Response2).

Note that Ada doesn’t have built-in JSON support like Go, so we’re using the GNATCOLL.JSON library. The concepts are similar, but the syntax and exact methods differ.

To run this program, you would need to have GNAT (GNU Ada) installed and the GNATCOLL library available. Compile and run it with:

$ gnatmake json_example.adb
$ ./json_example

This will output the JSON representations and the decoded values.

Remember that error handling in Ada is typically done through exceptions, which are not shown in this example for brevity. In a real-world application, you would want to add appropriate exception handling.