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:

League.JSON.Streams.Boolean_Stream_Operations​ to input/output of boolean values

League.JSON.Streams.Generic_Integer_Stream_Operations​ generic to input/output values of user defined integer types. There are two instantiations of this package is provided: League.JSON.Streams.Integer_Stream_Operations​ to input/output values of Integer type League.JSON.Streams.Long_Integer_Stream_Operations​ to input/output values of Long_Integer type

League.JSON.Streams.Generic_Float_Stream_Operations​ generic to input/output values of user defined integer types. There are two instantiations of this package is provided: League.JSON.Streams.Float_Stream_Operations​ to input/output values of Float type League.JSON.Streams.Long_Float_Stream_Operations​ to input/output values of Long_Float type



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.