wl_lib_0.1.3_1c94dc7c/src/wl-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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
with Ada.Strings.Fixed;
with Ada.Strings.Unbounded;
with Ada.Tags;
with Ada.Text_IO;

package body WL.Json is

   type Null_Json_Value is
     new Atomic_Json_Value with null record;

   overriding function Serialize
     (Value : Null_Json_Value)
      return String;

   type String_Json_Value is
     new Atomic_Json_Value with
      record
         Text : Ada.Strings.Unbounded.Unbounded_String;
      end record;

   overriding function Serialize
     (Value : String_Json_Value)
      return String;

   overriding function Image
     (Value : String_Json_Value)
      return String
   is (Ada.Strings.Unbounded.To_String (Value.Text));

   type Integer_Json_Value is
     new Atomic_Json_Value with
      record
         Value : Integer;
      end record;

   overriding function Serialize
     (Value : Integer_Json_Value)
      return String;

   type Float_Json_Value is
     new Atomic_Json_Value with
      record
         Value : Float;
      end record;

   overriding function Serialize
     (Value : Float_Json_Value)
      return String;

   type Boolean_Json_Value is
     new Atomic_Json_Value with
      record
         Value : Boolean;
      end record;

   overriding function Serialize
     (Value : Boolean_Json_Value)
      return String;

   function To_Safe_String (Text : String) return String;

   function Parse_String
     (S : String)
      return Json_Value'Class
      is separate;

   ------------
   -- Append --
   ------------

   procedure Append
     (To : in out Json_Array'Class; Value : Json_Value'Class) is
   begin
      To.Vector.Append (Value);
   end Append;

   -------------------
   -- Boolean_Value --
   -------------------

   function Boolean_Value (Bool : Boolean) return Json_Value'Class is
   begin
      return Result : constant Boolean_Json_Value :=
        Boolean_Json_Value'
          (Value => Bool);
   end Boolean_Value;

   ----------
   -- Copy --
   ----------

   procedure Copy
     (To   : in out Json_Object'Class;
      From : Json_Object'Class)
   is
   begin
      for Position in From.Properties.Iterate loop
         To.Set_Property
           (Json_Value_Maps.Key (Position),
            Json_Value_Maps.Element (Position));
      end loop;
   end Copy;

   -----------------
   -- Float_Value --
   -----------------

   function Float_Value (F : Float) return Json_Value'Class is
   begin
      return Result : constant Float_Json_Value := Float_Json_Value'
        (Value => F);
   end Float_Value;

   -----------------
   -- Deserialize --
   -----------------

   function Deserialize
     (Text : String)
      return Json_Value'Class
   is
   begin
      return Parse_String (Text);
   end Deserialize;

   ------------------
   -- Get_Property --
   ------------------

   function Get_Property
     (Value : Json_Value;
      Name  : String)
      return Json_Value'Class
   is
   begin
      if Name = "_toString" then
         return String_Value (Json_Value'Class (Value).Image);
      else
         return Null_Value;
      end if;
   end Get_Property;

   ------------------
   -- Get_Property --
   ------------------

   overriding function Get_Property
     (Object : Json_Object;
      Name   : String)
      return Json_Value'Class
   is
   begin
      if Object.Properties.Contains (Name) then
         return Object.Properties (Name);
      else
         return Json_Value (Object).Get_Property (Name);
      end if;
   end Get_Property;

   ------------------
   -- Get_Property --
   ------------------

   function Get_Property
     (Value  : Json_Value'Class;
      Name   : String)
      return String
   is
   begin
      return Value.Get_Property (Name).Image;
   end Get_Property;

   -------------------
   -- Integer_Value --
   -------------------

   function Integer_Value (Int : Integer) return Json_Value'Class is
   begin
      return Result : constant Integer_Json_Value := Integer_Json_Value'
        (Value => Int);
   end Integer_Value;

   -------------
   -- Is_Null --
   -------------

   function Is_Null (Value : Json_Value'Class) return Boolean is
   begin
      return Value in Null_Json_Value'Class;
   end Is_Null;

   ----------------
   -- Null_Value --
   ----------------

   function Null_Value return Json_Value'Class is
   begin
      return Result : Null_Json_Value;
   end Null_Value;

   ---------------
   -- Serialize --
   ---------------

   overriding function Serialize (Value : Json_Object) return String is
      use Ada.Strings.Unbounded;
      Result : Unbounded_String;
   begin
      for Position in Value.Properties.Iterate loop
         if Result /= Null_Unbounded_String then
            Result := Result & ",";
         end if;
         Result := Result & """" & Json_Value_Maps.Key (Position)
           & """:" & Json_Value_Maps.Element (Position).Serialize;
      end loop;
      return "{" & To_String (Result) & "}";
   end Serialize;

   ---------------
   -- Serialize --
   ---------------

   overriding function Serialize (Value : Json_Array) return String is
      use Ada.Strings.Unbounded;
      Result : Unbounded_String;
   begin
      for Item of Value.Vector loop
         if Result /= Null_Unbounded_String then
            Result := Result & ",";
         end if;
         Result := Result & Item.Serialize;
      end loop;
      return "[" & To_String (Result) & "]";
   end Serialize;

   overriding function Serialize
     (Value : Null_Json_Value)
      return String
   is ("null");

   overriding function Serialize
     (Value : String_Json_Value)
      return String
   is (""""
       & To_Safe_String (Ada.Strings.Unbounded.To_String (Value.Text))
       & """");

   overriding function Serialize
     (Value : Integer_Json_Value)
      return String
   is (Ada.Strings.Fixed.Trim (Value.Value'Image, Ada.Strings.Left));

   overriding function Serialize
     (Value : Float_Json_Value)
      return String
   is (Ada.Strings.Fixed.Trim (Value.Value'Image, Ada.Strings.Left));

   overriding function Serialize
     (Value : Boolean_Json_Value)
      return String
   is (if Value.Value then "true" else "false");

   ----------
   -- Save --
   ----------

   procedure Save
     (Value : Json_Value'Class;
      Path  : String)
   is
      use Ada.Text_IO;
      File : File_Type;

      procedure Write_Indented
        (This   : Json_Value'Class;
         Indent : Positive_Count);

      procedure Write_String
        (Text : String);

      --------------------
      -- Write_Indented --
      --------------------

      procedure Write_Indented
        (This   : Json_Value'Class;
         Indent : Positive_Count)
      is
         Child_Indent : constant Positive_Count := Indent + 4;
      begin
         if This in Atomic_Json_Value'Class then
            Put (This.Serialize);
         elsif This in Json_Array'Class then
            Put_Line ("[");
            declare
               First : Boolean := True;
            begin
               for Value of Json_Array (This).Vector loop
                  if First then
                     First := False;
                  else
                     Put_Line (",");
                  end if;
                  Set_Col (Child_Indent);
                  Write_Indented (Value, Child_Indent);
               end loop;
            end;
            New_Line;
            Set_Col (Indent);
            Put ("]");
         elsif This in Json_Object'Class then
            Put_Line ("{");
            declare
               First : Boolean := True;
            begin
               for Position in Json_Object (This).Properties.Iterate loop
                  if First then
                     First := False;
                  else
                     Put_Line (",");
                  end if;
                  Set_Col (Child_Indent);
                  Write_String (Json_Value_Maps.Key (Position));
                  Put (": ");
                  Write_Indented
                    (Json_Value_Maps.Element (Position), Child_Indent);
               end loop;
            end;
            New_Line;
            Set_Col (Indent);
            Put ("}");
         else
            raise Program_Error with
              "unknown json value type: "
              & Ada.Tags.External_Tag (This'Tag);
         end if;
      end Write_Indented;

      ------------------
      -- Write_String --
      ------------------

      procedure Write_String
        (Text : String)
      is
         Image : String (1 .. Text'Length * 2);
         Last  : Natural := 0;
      begin
         for Ch of Text loop
            Last := Last + 1;
            if Ch in '"' | '\' then
               Image (Last) := '\';
               Last := Last + 1;
            end if;
            Image (Last) := Ch;
         end loop;
         Put ('"' & Image (1 .. Last) & '"');
      end Write_String;

   begin
      Create (File, Out_File, Path);
      Set_Output (File);
      Write_Indented (Value, 1);
      Set_Output (Standard_Output);
      Close (File);
   end Save;

   ------------------
   -- Set_Property --
   ------------------

   procedure Set_Property
     (Object : in out Json_Object'Class;
      Name   : String;
      Value  : Json_Value'Class)
   is
   begin
      if Object.Properties.Contains (Name) then
         Object.Properties.Replace (Name, Value);
      else
         Object.Properties.Insert (Name, Value);
      end if;
   end Set_Property;

   ------------------
   -- Set_Property --
   ------------------

   procedure Set_Property
     (Object : in out Json_Object'Class;
      Name   : String;
      Value  : String)
   is
   begin
      Object.Set_Property (Name, String_Value (Value));
   end Set_Property;

   ------------------
   -- Set_Property --
   ------------------

   procedure Set_Property
     (Object : in out Json_Object'Class;
      Name   : String;
      Value  : Integer)
   is
   begin
      Object.Set_Property (Name, Integer_Value (Value));
   end Set_Property;

   ------------------
   -- Set_Property --
   ------------------

   procedure Set_Property
     (Object : in out Json_Object'Class;
      Name   : String;
      Value  : Float)
   is
   begin
      Object.Set_Property (Name, Float_Value (Value));
   end Set_Property;

   ------------------
   -- Set_Property --
   ------------------

   procedure Set_Property
     (Object : in out Json_Object'Class;
      Name   : String;
      Value  : Boolean)
   is
   begin
      Object.Set_Property (Name, Boolean_Value (Value));
   end Set_Property;

   ------------------
   -- String_Value --
   ------------------

   function String_Value (Text : String) return Json_Value'Class is
   begin
      return Result : constant String_Json_Value := String_Json_Value'
        (Text =>
           Ada.Strings.Unbounded.To_Unbounded_String
             (Text));
   end String_Value;

   --------------------
   -- To_Safe_String --
   --------------------

   function To_Safe_String (Text : String) return String is
   begin
      for I in Text'Range loop
         if Character'Pos (Text (I)) = 10 then
            return Text (Text'First .. I - 1)
              & "\n"
              & To_Safe_String (Text (I + 1 .. Text'Last));
         elsif Text (I) = '"' then
            return Text (Text'First .. I - 1)
              & "\"
              & Text (I)
              & To_Safe_String (Text (I + 1 .. Text'Last));
         end if;
      end loop;
      return Text;
   end To_Safe_String;

end WL.Json;