comp.lang.ada
 help / color / mirror / Atom feed
From: Simon Wright <simon@pushface.org>
Subject: Re: GNATCOLL JSON Parsing
Date: Mon, 03 Dec 2018 18:10:35 +0000
Date: 2018-12-03T18:10:35+00:00	[thread overview]
Message-ID: <lyy3961nxg.fsf@pushface.org> (raw)
In-Reply-To: e4e757c5-275c-4bde-bece-4d7dac71c008@googlegroups.com

eduardsapotski@gmail.com writes:

> Again problem ...
> I'm trying parse such result: https://api.exmo.com/v1/pair_settings/
>
>    function Get_Pair_Settings return Pair_Settings_Vector_Package.Vector is
>       JSON    : Unbounded_String := Null_Unbounded_String;
>       URL_Str : Unbounded_String := To_Unbounded_String("https://api.exmo.com/v1/pair_settings/");
>
>       Current_JSON_Item, JSON_Main_Node : JSON_Value := Create;
>       JSON_Result_Array : JSON_Array := Empty_Array;
>
>       Result  : Pair_Settings_Vector_Package.Vector;
>    begin
>       JSON := To_Unbounded_String(Message_Body(Get(URL => To_String(URL_Str))));
>       JSON_Main_Node := Read(JSON);
>       JSON_Result_Array := Get(JSON_Main_Node);
>
>       if Length(JSON_Result_Array) > 0 then
>          for i in 1 .. Length(JSON_Result_Array) loop
>             Put_Line("123");
>          end loop;
>       end if;
>
>       return Result;
>
>    end;
>
> ...
>
> raised CONSTRAINT_ERROR : gnatcoll-json.adb:1328 discriminant check failed

I finally managed to make this work.

It turns out that the JSON returned from that query (actually, the http:
version) is a single JSON object, where the contents are fields, not
array elements (I may have the nomenclature wrong there).

This code

with Aws;
with Aws.Client;
with Aws.Response;
with Ada.Text_IO; use Ada.Text_IO;
with GNATCOLL.JSON; use GNATCOLL.JSON;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
procedure Test_JSON is
   Aws_Reply         : Aws.Response.Data;
   JSON_Main_Node    : JSON_Value := Create;

   procedure Report_Outer_Field_Name (Name : Utf8_String; Value : JSON_Value);
   procedure Report_Inner_Field_Name (Name : Utf8_String; Value : JSON_Value);

   procedure Report_Outer_Field_Name (Name : Utf8_String; Value : JSON_Value)
   is
   begin
      Put_Line ("field: " & Name);
      Map_JSON_Object (Value, Report_Inner_Field_Name'Access);
   end Report_Outer_Field_Name;

   procedure Report_Inner_Field_Name (Name : Utf8_String; Value : JSON_Value)
   is
   begin
      Put_Line ("    field: " & Name & ", kind: " & Kind (Value)'Image);
   end Report_Inner_Field_Name;

begin
   Aws_Reply := Aws.Client.Get
     (Url          => "http://api.exmo.com/v1/pair_settings");

   JSON_Main_Node := Read
     (Strm     => Unbounded_String'(Aws.Response.Message_Body(Aws_Reply)),
      Filename => "");

   Map_JSON_Object (JSON_Main_Node, Report_Outer_Field_Name'Access);

end Test_JSON;

results in

$ ./test_json 
field: BTC_USD
    field: min_quantity, kind: JSON_STRING_TYPE
    field: max_quantity, kind: JSON_STRING_TYPE
    field: min_price, kind: JSON_STRING_TYPE
    field: max_price, kind: JSON_STRING_TYPE
    field: max_amount, kind: JSON_STRING_TYPE
    field: min_amount, kind: JSON_STRING_TYPE
field: BTC_EUR
    field: min_quantity, kind: JSON_STRING_TYPE
    field: max_quantity, kind: JSON_STRING_TYPE
    field: min_price, kind: JSON_STRING_TYPE
    field: max_price, kind: JSON_STRING_TYPE
    field: max_amount, kind: JSON_STRING_TYPE
    field: min_amount, kind: JSON_STRING_TYPE
...
field: BTG_ETH
    field: min_quantity, kind: JSON_STRING_TYPE
    field: max_quantity, kind: JSON_STRING_TYPE
    field: min_price, kind: JSON_STRING_TYPE
    field: max_price, kind: JSON_STRING_TYPE
    field: max_amount, kind: JSON_STRING_TYPE
    field: min_amount, kind: JSON_STRING_TYPE

Struggles:

As noted several times, this was all a bit mystifying

The default build of GNATCOLL has no debug symbols (both static & shared
libraries)

  parent reply	other threads:[~2018-12-03 18:10 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-06-08  5:52 GNATCOLL JSON Parsing eduardsapotski
2018-06-08  9:35 ` Björn Lundin
2018-06-08 10:40 ` eduardsapotski
2018-06-08 12:00 ` Per Sandberg
2018-06-08 15:19 ` eduardsapotski
2018-06-08 15:26   ` Björn Lundin
2018-12-03 15:02   ` Olivier Henley
2018-12-03 18:06     ` Dmitry A. Kazakov
2018-12-03 18:45       ` Olivier Henley
2018-12-03 20:54         ` Dmitry A. Kazakov
2018-12-03 22:01           ` Olivier Henley
2018-12-03 18:06     ` briot.emmanuel
2018-12-03 19:12       ` Olivier Henley
2018-12-03 19:43         ` briot.emmanuel
2018-12-03 20:48           ` Olivier Henley
2018-12-03 20:56             ` Olivier Henley
2018-12-04  7:25             ` briot.emmanuel
2018-12-04 13:39               ` Olivier Henley
2018-12-04 10:14             ` gautier_niouzes
2018-12-04 13:47               ` Olivier Henley
2018-06-08 15:58 ` eduardsapotski
2018-06-08 16:35 ` eduardsapotski
2018-06-08 17:09   ` Björn Lundin
2018-06-09  4:33   ` gautier_niouzes
2018-11-27  6:22   ` Per Sandberg
2018-06-09  4:37 ` gautier_niouzes
2018-06-09  5:08 ` eduardsapotski
2018-06-09  5:31 ` eduardsapotski
2018-06-12  9:55   ` R Srinivasan
2018-06-09  5:35 ` eduardsapotski
2018-11-26  6:42 ` eduardsapotski
2018-11-26 10:17   ` Björn Lundin
2018-11-26 15:56     ` Simon Wright
2018-11-26 16:11       ` eduardsapotski
2018-11-26 18:01         ` Simon Wright
2018-11-26 20:32           ` Björn Lundin
2018-12-03 18:10   ` Simon Wright [this message]
2018-11-26 10:26 ` eduardsapotski
2018-11-26 10:50 ` eduardsapotski
2018-11-26 16:16 ` eduardsapotski
2018-11-27  1:47 ` eduardsapotski
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox