stcarrez / ada-util

Ada Utility Library - Composing streams, processes, logs, serialization, encoders and more
Apache License 2.0
69 stars 14 forks source link

Struggling to see how to create json with fields #13

Open AwesomeBiscuitBadger opened 3 years ago

AwesomeBiscuitBadger commented 3 years ago

Hi,

I have been trying to use json.adb in the samples directory to write my own serializers, and I have a couple of issues:

  1. Proxy functions with vector mappers -- can't get this to work. I have a record where one of the items is a vector and I can't work out what should go in the proxy field as the vector mappers don't have a Set_Member.
  2. Producing a json string from a vector of records of the form {"field1": [{"a": 1,"b":2}, {"a: 5, "b":6}]}. For some reason I can't get the output string to have the field names in, just lists the field content. What am I doing wrong there?

    
    procedure Print (P : in My_Type.Cursor);
      procedure Print (P : in My_Element);
    
      procedure Print (P : in My_Element) is
      begin
     Ada.Text_IO.Put_Line ("A       : " & P.A);
     Ada.Text_IO.Put_Line ("B : " & P.B);
      end Print;
    
      procedure Print (P : in My_Type.Cursor) is
      begin
         Print (My_Type.Element (P));
      end Print;

` declare Buffer : aliased Util.Streams.Buffered.Output_Buffer_Stream; Print : aliased Util.Streams.Texts.Print_Stream; Output : Util.Serialize.IO.JSON.Output_Stream; begin -- Convert to external facing type for conversion to json out to HTTP My_Type.Set_Type (To => List, From => Internal_Element); Buffer.Initialize (Size => 10000); Print.Initialize (Buffer'Unchecked_Access); Output.Initialize (Print'Unchecked_Access); Ada.Text_Io.Put_Line("Second Marker"); Output.Write ("{""list"":"); Internal_Vectors_Mapping.Write (Output, List); Output.Write ("}");

        Ada.Text_IO.Put_Line ("IO:");
        Ada.Text_IO.Put_Line (Util.Streams.Texts.To_String (Print));
     end;
  end;` 

Obviously there is a chance I'm doing this all wrong, in a fundamental way. I want to serialize my type to output it to json and then out to other executables, at which point it'll be deserialized and brought back into the internal code.