wiki:League/JSON/Streams

Using JSON as stream format

Starting from 0.7 Matreshka supports use of JSON format with Ada's 'Read/'Write/'Input/'Output attributes. Matreshka provides JSON stream package and user need to redefine attributes for own types. Matreshka's Universal_String type supports JSON streams. There are few additional packages to help to develop user defined input/output attributes:

User defined input/output attributes

Below is example how user can define own stream attributes to be able to save/restore objects using JSON stream.

with Ada.Streams;

with League.Strings;
        
package Demo is

   type Rect is record
      F : Boolean;
      X : Integer;
      Y : Integer;
      N : League.Strings.Universal_String;
   end record;
        
   procedure Read
    (S : access Ada.Streams.Root_Stream_Type'Class;
     V : out Rect);

   procedure Write
    (S : access Ada.Streams.Root_Stream_Type'Class;
     V : Rect);
        
   for Rect'Read use Read;
   for Rect'Write use Write;

end Demo;
with League.JSON.Streams.Boolean_Stream_Operations;
with League.JSON.Streams.Integer_Stream_Operations;

package body Demo is

   ----------
   -- Read --
   ----------

   procedure Read
    (S : access Ada.Streams.Root_Stream_Type'Class;
     V : out Rect) is
   begin
      if S.all in League.JSON.Streams.JSON_Stream'Class then
         declare
            JS : League.JSON.Streams.JSON_Stream'Class
              renames League.JSON.Streams.JSON_Stream'Class (S.all);

         begin
            JS.Start_Object;
            JS.Key (League.Strings.To_Universal_String ("f"));
            League.JSON.Streams.Boolean_Stream_Operations.Read (JS, V.F);
            JS.Key (League.Strings.To_Universal_String ("x"));
            League.JSON.Streams.Integer_Stream_Operations.Read (JS, V.X);
            JS.Key (League.Strings.To_Universal_String ("y"));
            League.JSON.Streams.Integer_Stream_Operations.Read (JS, V.Y);
            JS.Key (League.Strings.To_Universal_String ("n"));
            League.Strings.Universal_String'Read (JS'Access, V.N);
            JS.End_Object;
         end;

      else
         Boolean'Read (S, V.F);
         Integer'Read (S, V.X);
         Integer'Read (S, V.Y);
         League.Strings.Universal_String'Read (S, V.N);
      end if;
   end Read;

   -----------
   -- Write --
   -----------

   procedure Write
    (S : access Ada.Streams.Root_Stream_Type'Class;
     V : Rect) is
   begin
      if S.all in League.JSON.Streams.JSON_Stream'Class then
         declare
            JS : League.JSON.Streams.JSON_Stream'Class
              renames League.JSON.Streams.JSON_Stream'Class (S.all);

         begin
            JS.Start_Object;
            JS.Key (League.Strings.To_Universal_String ("f"));
            League.JSON.Streams.Boolean_Stream_Operations.Write (JS, V.F);
            JS.Key (League.Strings.To_Universal_String ("x"));
            League.JSON.Streams.Integer_Stream_Operations.Write (JS, V.X);
            JS.Key (League.Strings.To_Universal_String ("y"));
            League.JSON.Streams.Integer_Stream_Operations.Write (JS, V.Y);
            JS.Key (League.Strings.To_Universal_String ("n"));
            League.Strings.Universal_String'Write (JS'Access, V.N);
            JS.End_Object;
         end;

      else
         Boolean'Write (S, V.F);
         Integer'Write (S, V.X);
         Integer'Write (S, V.Y);
         League.Strings.Universal_String'Write (S, V.N);
      end if;
   end Write;

end Demo;

To serialize object of Rect you need to create JSON stream object and serialize Rect object with 'Write attribute to this stream. To restore object use need to use JSON stream and 'Read attribute.

with Ada.Wide_Wide_Text_IO;

with League.JSON.Streams;

procedure Demo.Driver is
   Stream   : aliased League.JSON.Streams.JSON_Stream;
   In_Data  : Demo.Rect := (True, 1, 2, League.Strings.To_Universal_String ("str"));
   Out_Data : Demo.Rect;

begin
   Demo.Rect'Write (Stream'Access, In_Data);
   Ada.Wide_Wide_Text_IO.Put_Line
    (Stream.Get_JSON_Document.To_JSON.To_Wide_Wide_String);

   Stream.Set_JSON_Document (Stream.Get_JSON_Document);
   Demo.Rect'Read (Stream'Access, Out_Data);
   Ada.Wide_Wide_Text_IO.Put_Line
    (Boolean'Wide_Wide_Image (Out_Data.F)
       & Integer'Wide_Wide_Image (Out_Data.X)
       & Integer'Wide_Wide_Image (Out_Data.Y)
       & " """
       & Out_Data.N.To_Wide_Wide_String
       & '"');
end Demo.Driver;
Last modified 7 years ago Last modified on Oct 22, 2014, 9:46:34 AM
Note: See TracWiki for help on using the wiki.