simple_components_4.42.0_21c00530/test_components/test_json.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
--                                                                    --
--  procedure                       Copyright (c)  Dmitry A. Kazakov  --
--     Test_Parser_JSON                            Luebeck            --
--  Test program                                   Autumn, 2019       --
--                                                                    --
--                                Last revision :  09:21 06 Oct 2019  --
--                                                                    --
--  This  library  is  free software; you can redistribute it and/or  --
--  modify it under the terms of  the  GNU  Library  General  Public  --
--  License  as  published  by  the Free Software Foundation; either  --
--  version 2 of the License, or (at your option) any later version.  --
--                                                                    --
--  As a special exception, if other files instantiate generics from  --
--  this unit, or you link this unit with other files to produce  an  --
--  executable, this unit does not by  itself  cause  the  resulting  --
--  executable to be covered by the GNU General Public License. This  --
--  exception  does not however invalidate any other reasons why the  --
--  executable file might be covered by the GNU Public License.       --
--                                                                    --
--  This  library is distributed in the hope that it will be useful,  --
--  but WITHOUT ANY WARRANTY; without even the implied  warranty  of  --
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU  --
--  Library General Public License for more details.                  --
--                                                                    --
--  You  should  have  received  a  copy  of the GNU Library General  --
--  Public  License  along  with  this library; if not, write to the  --
--  Free Software Foundation, Inc.,  675  Mass  Ave,  Cambridge,  MA  --
--  02139, USA.                                                       --
--____________________________________________________________________--

with Ada.Calendar;                        use Ada.Calendar;
with Ada.Characters.Latin_1;              use Ada.Characters.Latin_1;
with Ada.Exceptions;                      use Ada.Exceptions;
with Ada.Text_IO;                         use Ada.Text_IO;
with Strings_Edit.Streams;                use Strings_Edit.Streams;

with Parsers.JSON.Multiline_Source;
with Parsers.JSON.String_Source;
with Parsers.Multiline_Source.Stream_IO;
with Parsers.Multiline_Source.Text_IO;
with Parsers.String_Source;
with Stack_Storage;
with Strings_Edit.Floats;
with Strings_Edit.Streams;
-- with GNAT.Exception_Traces;

procedure Test_JSON is
   use Parsers.JSON;
   CRLF  : constant String := Character'Val (13) & Character'Val (10);
   Arena : aliased Stack_Storage.Pool (200, 512);
begin
-- GNAT.Exception_Traces.Trace_On (GNAT.Exception_Traces.Every_Raise);
   declare
      use Parsers.JSON.String_Source;
      use Parsers.String_Source;
      Text  : aliased String := "123";
      Input : aliased Source (Text'Access);
      Data  : constant JSON_Value := Parse (Input'Access, Arena'Access);
   begin
      Put_Line ("Parsed:" & Image (Data));
   end;
   declare
      use Parsers.JSON.String_Source;
      use Parsers.String_Source;
      Text : aliased String :=
             "{"                         &
                """id"": 1,"             &
                """name"": ""Foo"","     &
                """price"": 123,"        &
                """tags"": ["            &
                   """Bar"","            &
                   """Eek"""             &
                   "],"                  &
                """stock"": {"           &
                   """warehouse"": 300," &
                   """retail"": 20"      &
                   "}"                   &
             "}";
      Input : aliased Source (Text'Access);
      Data  : constant JSON_Value := Parse (Input'Access, Arena'Access);
   begin
      Put_Line ("Parsed:" & Image (Data));
   end;
   Stack_Storage.Deallocate_All (Arena);
   declare
      use Parsers.JSON.String_Source;
      use Parsers.String_Source;
      Text  : aliased String := "{ ""face"": ""\uD83D\uDE02"" }";
      Input : aliased Source (Text'Access);
      Data  : constant JSON_Value := Parse (Input'Access, Arena'Access);
   begin
      Put_Line ("Parsed:" & Image (Data, True));
   end;
   Stack_Storage.Deallocate_All (Arena);
   declare
      use Parsers.JSON.Multiline_Source;
      use Parsers.Multiline_Source.Stream_IO;
      use Strings_Edit.Streams;
      Text  : constant String :=
              "{"                                          & CRLF &
              "  ""first name"": ""John"","                & CRLF &
              "  ""last name"": ""Smith"","                & CRLF &
              "  ""age"": 25,"                             & CRLF &
              "  ""address"": {"                           & CRLF &
              "    ""street address"": ""21 2nd Street""," & CRLF &
              "    ""city"": ""New York"","                & CRLF &
              "    ""state"": ""NY"","                     & CRLF &
              "    ""postal code"": ""10021"""             & CRLF &
              "  },"                                       & CRLF &
              "  ""phone numbers"": ["                     & CRLF &
              "    {"                                      & CRLF &
              "      ""type"": ""home"","                  & CRLF &
              "      ""number"": ""212 555-1234"""         & CRLF &
              "    },"                                     & CRLF &
              "    {"                                      & CRLF &
              "      ""type"": ""fax"","                   & CRLF &
              "      ""number"": ""646 555-4567"""         & CRLF &
              "    }"                                      & CRLF &
              "  ],"                                       & CRLF &
              "  ""sex"": {"                               & CRLF &
              "    ""type"": ""male"""                     & CRLF &
              "  }"                                        & CRLF &
              "}";
      Content : aliased String_Stream (500);
   begin
      Set (Content, Text); -- Note, content is set before Input declared
      declare
         Input : aliased Source (Content'Access);
      begin
         declare
            Data : constant JSON_Value :=
                            Parse (Input'Access, Arena'Access);
         begin
            Put_Line ("Parsed:" & Image (Data, True));
         end;
      end;
   end;
   Stack_Storage.Deallocate_All (Arena);
   declare
      use Parsers.JSON.Multiline_Source;
      use Parsers.Multiline_Source.Text_IO;
      File : aliased File_Type;
   begin
      Create (File, Out_File, "test.json");
      Put_Line (File, "[");
      Put_Line (File, "  {");
      Put_Line (File, "     ""precision"": ""zip"",");
      Put_Line (File, "     ""Latitude"":  37.7668,");
      Put_Line (File, "     ""Longitude"": -122.3959,");
      Put_Line (File, "     ""Address"":   """",");
      Put_Line (File, "     ""City"":      ""SAN FRANCISCO"",");
      Put_Line (File, "     ""State"":     ""CA"",");
      Put_Line (File, "     ""Zip"":       ""94107"",");
      Put_Line (File, "     ""Country"":   ""US""");
      Put_Line (File, "  },");
      Put_Line (File, "  {");
      Put_Line (File, "     ""precision"": ""zip"",");
      Put_Line (File, "     ""Latitude"":  37.371991,");
      Put_Line (File, "     ""Longitude"": -122.026020,");
      Put_Line (File, "     ""Address"":   ""C:\\Program Files"",");
      Put_Line (File, "     ""City"":      ""SUNNYVALE"",");
      Put_Line (File, "     ""State"":     ""CA"",");
      Put_Line (File, "     ""Zip"":       ""94085"",");
      Put_Line (File, "     ""Country"":   ""US""");
      Put_Line (File, "  }");
      Put_Line (File, "]");
      Close (File);
      Open (File, In_File, "test.json");
      declare
         Input : aliased Source (File'Access);
         Data  : constant JSON_Value :=
                          Parse (Input'Access, Arena'Access);
      begin
         Put_Line ("Parsed:" & Image (Data, True));
      end;
      Close (File);
   end;
--     declare
--        use Parsers.JSON.Multiline_Source;
--        use Parsers.Multiline_Source.Text_IO;
--        use Strings_Edit.Floats;
--        File  : aliased File_Type;
--        Start : Time;
--     begin
--        Open (File, In_File, "c:/temp/1.json");
--        Start := Clock;
--        declare
--           Input : aliased Source (File'Access);
--           Data  : constant JSON_Value :=
--                            Parse (Input'Access, Arena'Access);
--        begin
--           Put_Line
--           (  "Parsed: "
--           &  Image (Float (Clock - Start), AbsSmall => -2)
--           &  "s"
--           );
--        end;
--        Close (File);
--     end;
exception
   when Error : others =>
      Put_Line ("Error: " & Exception_Information (Error));
end Test_JSON;