| 1 |
-- Test program. Read a valid toml-test compatible JSON description on the |
| 2 |
-- standard input and emit a corresponding TOML document on the standard |
| 3 |
-- output. |
| 4 |
|
| 5 |
with Ada.Containers.Generic_Array_Sort; |
| 6 |
with Ada.Strings.Unbounded; |
| 7 |
with Ada.Text_IO; |
| 8 |
|
| 9 |
with GNATCOLL.JSON; |
| 10 |
|
| 11 |
with TOML; |
| 12 |
with TOML.Generic_Dump; |
| 13 |
|
| 14 |
procedure Ada_TOML_Encode is |
| 15 |
|
| 16 |
use type Ada.Strings.Unbounded.Unbounded_String; |
| 17 |
use all type GNATCOLL.JSON.JSON_Value_Type; |
| 18 |
|
| 19 |
package US renames Ada.Strings.Unbounded; |
| 20 |
package IO renames Ada.Text_IO; |
| 21 |
package J renames GNATCOLL.JSON; |
| 22 |
|
| 23 |
type Stdout_Stream is null record; |
| 24 |
|
| 25 |
procedure Put (Stream : in out Stdout_Stream; Bytes : String); |
| 26 |
-- Callback for TOML.Generic_Dump |
| 27 |
|
| 28 |
function Interpret (Desc : J.JSON_Value) return TOML.TOML_Value; |
| 29 |
-- Interpret the given toml-test compatible JSON description (Value) and |
| 30 |
-- return the corresponding TOML value. |
| 31 |
|
| 32 |
type String_Array is array (Positive range <>) of US.Unbounded_String; |
| 33 |
|
| 34 |
procedure Sort_Strings is new Ada.Containers.Generic_Array_Sort |
| 35 |
(Index_Type => Positive, |
| 36 |
Element_Type => US.Unbounded_String, |
| 37 |
Array_Type => String_Array, |
| 38 |
"<" => US."<"); |
| 39 |
|
| 40 |
function Sorted_Keys (Desc : J.JSON_Value) return String_Array |
| 41 |
with Pre => Desc.Kind = JSON_Object_Type; |
| 42 |
-- Return a sorted array for all keys in the Desc object |
| 43 |
|
| 44 |
--------- |
| 45 |
-- Put -- |
| 46 |
--------- |
| 47 |
|
| 48 |
procedure Put (Stream : in out Stdout_Stream; Bytes : String) is |
| 49 |
pragma Unreferenced (Stream); |
| 50 |
begin |
| 51 |
IO.Put (Bytes); |
| 52 |
end Put; |
| 53 |
|
| 54 |
----------------- |
| 55 |
-- Sorted_Keys -- |
| 56 |
----------------- |
| 57 |
|
| 58 |
function Sorted_Keys (Desc : J.JSON_Value) return String_Array is |
| 59 |
Count : Natural := 0; |
| 60 |
|
| 61 |
procedure Count_CB |
| 62 |
(Dummy_Name : J.UTF8_String; Dummy_Value : J.JSON_Value); |
| 63 |
|
| 64 |
-------------- |
| 65 |
-- Count_CB -- |
| 66 |
-------------- |
| 67 |
|
| 68 |
procedure Count_CB |
| 69 |
(Dummy_Name : J.UTF8_String; Dummy_Value : J.JSON_Value) is |
| 70 |
begin |
| 71 |
Count := Count + 1; |
| 72 |
end Count_CB; |
| 73 |
begin |
| 74 |
Desc.Map_JSON_Object (Count_CB'Access); |
| 75 |
|
| 76 |
return Result : String_Array (1 .. Count) do |
| 77 |
declare |
| 78 |
I : Positive := Result'First; |
| 79 |
|
| 80 |
procedure Read_Entry |
| 81 |
(Name : J.UTF8_String; Dummy_Value : J.JSON_Value); |
| 82 |
|
| 83 |
---------------- |
| 84 |
-- Read_Entry -- |
| 85 |
---------------- |
| 86 |
|
| 87 |
procedure Read_Entry |
| 88 |
(Name : J.UTF8_String; Dummy_Value : J.JSON_Value) is |
| 89 |
begin |
| 90 |
Result (I) := US.To_Unbounded_String (Name); |
| 91 |
I := I + 1; |
| 92 |
end Read_Entry; |
| 93 |
begin |
| 94 |
Desc.Map_JSON_Object (Read_Entry'Access); |
| 95 |
Sort_Strings (Result); |
| 96 |
end; |
| 97 |
end return; |
| 98 |
end Sorted_Keys; |
| 99 |
|
| 100 |
--------------- |
| 101 |
-- Interpret -- |
| 102 |
--------------- |
| 103 |
|
| 104 |
function Interpret (Desc : J.JSON_Value) return TOML.TOML_Value is |
| 105 |
|
| 106 |
Time_Base_Length : constant := 8; |
| 107 |
Time_Milli_Length : constant := 4; |
| 108 |
Date_Length : constant := 10; |
| 109 |
Local_Datetime_Base_Length : constant := |
| 110 |
Date_Length + 1 + Time_Base_Length; |
| 111 |
Offset_Datetime_Base_Length : constant := Local_Datetime_Base_Length + 1; |
| 112 |
Offset_Full_Length : constant := 6; |
| 113 |
|
| 114 |
function Decode_Offset_Datetime |
| 115 |
(S : String) return TOML.Any_Offset_Datetime; |
| 116 |
function Decode_Local_Datetime |
| 117 |
(S : String) return TOML.Any_Local_Datetime; |
| 118 |
function Decode_Date (S : String) return TOML.Any_Local_Date; |
| 119 |
function Decode_Time (S : String) return TOML.Any_Local_Time; |
| 120 |
|
| 121 |
---------------------------- |
| 122 |
-- Decode_Offset_Datetime -- |
| 123 |
---------------------------- |
| 124 |
|
| 125 |
function Decode_Offset_Datetime |
| 126 |
(S : String) return TOML.Any_Offset_Datetime |
| 127 |
is |
| 128 |
use type TOML.Any_Local_Offset; |
| 129 |
|
| 130 |
pragma Assert (S'Length >= Offset_Datetime_Base_Length); |
| 131 |
|
| 132 |
Offset : TOML.Any_Local_Offset; |
| 133 |
Unknown_Offset : Boolean; |
| 134 |
|
| 135 |
I : constant Positive := S'First; |
| 136 |
Last : Positive := S'Last; |
| 137 |
begin |
| 138 |
if S (Last) = 'Z' then |
| 139 |
Last := Last - 1; |
| 140 |
Offset := 0; |
| 141 |
Unknown_Offset := False; |
| 142 |
else |
| 143 |
declare |
| 144 |
pragma Assert (S (Last - 2) = ':'); |
| 145 |
|
| 146 |
Offset_Sign : Character renames S (Last - 5); |
| 147 |
Hour_Offset : String renames S (Last - 4 .. Last - 3); |
| 148 |
Minute_Offset : String renames S (Last - 1 .. Last); |
| 149 |
begin |
| 150 |
Offset := 60 * TOML.Any_Local_Offset'Value (Hour_Offset) |
| 151 |
+ TOML.Any_Local_Offset'Value (Minute_Offset); |
| 152 |
case Offset_Sign is |
| 153 |
when '-' => Offset := -Offset; |
| 154 |
when '+' => null; |
| 155 |
when others => raise Program_Error; |
| 156 |
end case; |
| 157 |
Unknown_Offset := Offset = 0 and then Offset_Sign = '-'; |
| 158 |
|
| 159 |
Last := Last - Offset_Full_Length; |
| 160 |
end; |
| 161 |
end if; |
| 162 |
|
| 163 |
declare |
| 164 |
Local_Datetime : constant TOML.Any_Local_Datetime := |
| 165 |
Decode_Local_Datetime (S (I .. Last)); |
| 166 |
begin |
| 167 |
return (Local_Datetime, Offset, Unknown_Offset); |
| 168 |
end; |
| 169 |
end Decode_Offset_Datetime; |
| 170 |
|
| 171 |
--------------------------- |
| 172 |
-- Decode_Local_Datetime -- |
| 173 |
--------------------------- |
| 174 |
|
| 175 |
function Decode_Local_Datetime |
| 176 |
(S : String) return TOML.Any_Local_Datetime |
| 177 |
is |
| 178 |
I : constant Positive := S'First; |
| 179 |
|
| 180 |
pragma Assert (S'Length >= Local_Datetime_Base_Length); |
| 181 |
pragma Assert (S (I + Date_Length) = 'T'); |
| 182 |
|
| 183 |
Date : constant TOML.Any_Local_Date := |
| 184 |
Decode_Date (S (I .. I + Date_Length - 1)); |
| 185 |
Time : constant TOML.Any_Local_Time := |
| 186 |
Decode_Time (S (I + Date_Length + 1 .. S'Last)); |
| 187 |
begin |
| 188 |
return (Date, Time); |
| 189 |
end Decode_Local_Datetime; |
| 190 |
|
| 191 |
----------------- |
| 192 |
-- Decode_Date -- |
| 193 |
----------------- |
| 194 |
|
| 195 |
function Decode_Date (S : String) return TOML.Any_Local_Date is |
| 196 |
I : constant Positive := S'First; |
| 197 |
|
| 198 |
pragma Assert (S'Length = Date_Length); |
| 199 |
pragma Assert (S (I + 4) = '-'); |
| 200 |
pragma Assert (S (I + 7) = '-'); |
| 201 |
|
| 202 |
Year : String renames S (I + 0 .. I + 3); |
| 203 |
Month : String renames S (I + 5 .. I + 6); |
| 204 |
Day : String renames S (I + 8 .. I + 9); |
| 205 |
begin |
| 206 |
return (TOML.Any_Year'Value (Year), |
| 207 |
TOML.Any_Month'Value (Month), |
| 208 |
TOML.Any_Day'Value (Day)); |
| 209 |
end Decode_Date; |
| 210 |
|
| 211 |
----------------- |
| 212 |
-- Decode_Time -- |
| 213 |
----------------- |
| 214 |
|
| 215 |
function Decode_Time (S : String) return TOML.Any_Local_Time is |
| 216 |
I : constant Positive := S'First; |
| 217 |
|
| 218 |
pragma Assert (S'Length in Time_Base_Length |
| 219 |
| Time_Base_Length + Time_Milli_Length); |
| 220 |
pragma Assert (S (I + 2) = ':'); |
| 221 |
pragma Assert (S (I + 5) = ':'); |
| 222 |
|
| 223 |
Hour : String renames S (I + 0 .. I + 1); |
| 224 |
Minute : String renames S (I + 3 .. I + 4); |
| 225 |
Second : String renames S (I + 6 .. I + 7); |
| 226 |
Millisecond : TOML.Any_Millisecond := 0; |
| 227 |
begin |
| 228 |
if S'Length /= Time_Base_Length then |
| 229 |
pragma Assert (S (I + Time_Base_Length) = '.'); |
| 230 |
Millisecond := TOML.Any_Millisecond'Value |
| 231 |
(S (I + Time_Base_Length + 1 .. S'Last)); |
| 232 |
end if; |
| 233 |
return (TOML.Any_Hour'Value (Hour), |
| 234 |
TOML.Any_Minute'Value (Minute), |
| 235 |
TOML.Any_Second'Value (Second), |
| 236 |
Millisecond); |
| 237 |
end Decode_Time; |
| 238 |
|
| 239 |
Result : TOML.TOML_Value; |
| 240 |
begin |
| 241 |
case Desc.Kind is |
| 242 |
when JSON_Object_Type => |
| 243 |
declare |
| 244 |
Keys : constant String_Array := Sorted_Keys (Desc); |
| 245 |
begin |
| 246 |
if Keys'Length = 2 |
| 247 |
and then Keys (1) = US.To_Unbounded_String ("type") |
| 248 |
and then Keys (2) = US.To_Unbounded_String ("value") |
| 249 |
then |
| 250 |
declare |
| 251 |
T : constant String := Desc.Get ("type"); |
| 252 |
V : constant J.JSON_Value := Desc.Get ("value"); |
| 253 |
begin |
| 254 |
if T = "string" then |
| 255 |
declare |
| 256 |
S : constant String := V.Get; |
| 257 |
begin |
| 258 |
Result := TOML.Create_String (S); |
| 259 |
end; |
| 260 |
|
| 261 |
elsif T = "float" then |
| 262 |
declare |
| 263 |
S : constant String := V.Get; |
| 264 |
I : Positive := S'First; |
| 265 |
Positive : Boolean := True; |
| 266 |
Value : TOML.Any_Float; |
| 267 |
begin |
| 268 |
if S (I) = '+' then |
| 269 |
I := I + 1; |
| 270 |
elsif S (I) = '-' then |
| 271 |
Positive := False; |
| 272 |
I := I + 1; |
| 273 |
end if; |
| 274 |
|
| 275 |
if S (I .. S'Last) = "nan" then |
| 276 |
Value := (Kind => TOML.NaN, |
| 277 |
Positive => Positive); |
| 278 |
elsif S (I .. S'Last) = "inf" then |
| 279 |
Value := (Kind => TOML.Infinity, |
| 280 |
Positive => Positive); |
| 281 |
else |
| 282 |
declare |
| 283 |
use type TOML.Valid_Float; |
| 284 |
VF : TOML.Valid_Float := |
| 285 |
TOML.Valid_Float'Value (S (I .. S'Last)); |
| 286 |
begin |
| 287 |
if not Positive then |
| 288 |
VF := -VF; |
| 289 |
end if; |
| 290 |
Value := (Kind => TOML.Regular, Value => VF); |
| 291 |
end; |
| 292 |
end if; |
| 293 |
|
| 294 |
Result := TOML.Create_Float (Value); |
| 295 |
end; |
| 296 |
|
| 297 |
elsif T = "integer" then |
| 298 |
declare |
| 299 |
S : constant String := V.Get; |
| 300 |
begin |
| 301 |
Result := |
| 302 |
TOML.Create_Integer (TOML.Any_Integer'Value (S)); |
| 303 |
end; |
| 304 |
|
| 305 |
elsif T = "bool" then |
| 306 |
declare |
| 307 |
S : constant String := V.Get; |
| 308 |
begin |
| 309 |
Result := TOML.Create_Boolean (Boolean'Value (S)); |
| 310 |
end; |
| 311 |
|
| 312 |
elsif T = "datetime" then |
| 313 |
Result := TOML.Create_Offset_Datetime |
| 314 |
(Decode_Offset_Datetime (V.Get)); |
| 315 |
|
| 316 |
elsif T = "datetime-local" then |
| 317 |
Result := TOML.Create_Local_Datetime |
| 318 |
(Decode_Local_Datetime (V.Get)); |
| 319 |
|
| 320 |
elsif T = "date-local" then |
| 321 |
Result := TOML.Create_Local_Date (Decode_Date (V.Get)); |
| 322 |
|
| 323 |
elsif T = "time-local" then |
| 324 |
Result := TOML.Create_Local_Time (Decode_Time (V.Get)); |
| 325 |
|
| 326 |
elsif T = "array" then |
| 327 |
Result := Interpret (V); |
| 328 |
|
| 329 |
else |
| 330 |
raise Program_Error with "unhandled value type: " & T; |
| 331 |
end if; |
| 332 |
end; |
| 333 |
|
| 334 |
else |
| 335 |
Result := TOML.Create_Table; |
| 336 |
for K of Keys loop |
| 337 |
declare |
| 338 |
Item : constant TOML.TOML_Value := |
| 339 |
Interpret (Desc.Get (US.To_String (K))); |
| 340 |
begin |
| 341 |
Result.Set (K, Item); |
| 342 |
end; |
| 343 |
end loop; |
| 344 |
end if; |
| 345 |
end; |
| 346 |
|
| 347 |
when JSON_Array_Type => |
| 348 |
declare |
| 349 |
Elements : constant J.JSON_Array := Desc.Get; |
| 350 |
begin |
| 351 |
Result := TOML.Create_Array; |
| 352 |
for I in 1 .. J.Length (Elements) loop |
| 353 |
Result.Append (Interpret (J.Get (Elements, I))); |
| 354 |
end loop; |
| 355 |
end; |
| 356 |
|
| 357 |
when others => |
| 358 |
raise Program_Error; |
| 359 |
end case; |
| 360 |
|
| 361 |
return Result; |
| 362 |
end Interpret; |
| 363 |
|
| 364 |
procedure Dump is new TOML.Generic_Dump (Stdout_Stream, Put); |
| 365 |
|
| 366 |
Input : US.Unbounded_String; |
| 367 |
Description : J.JSON_Value; |
| 368 |
Result : TOML.TOML_Value; |
| 369 |
Stdout : Stdout_Stream := (null record); |
| 370 |
begin |
| 371 |
-- Read the stdin until end of file and store its content in Input |
| 372 |
|
| 373 |
loop |
| 374 |
begin |
| 375 |
declare |
| 376 |
Line : constant String := IO.Get_Line; |
| 377 |
begin |
| 378 |
US.Append (Input, Line); |
| 379 |
end; |
| 380 |
exception |
| 381 |
when IO.End_Error => |
| 382 |
exit; |
| 383 |
end; |
| 384 |
end loop; |
| 385 |
|
| 386 |
-- Decode this input as JSON |
| 387 |
|
| 388 |
Description := J.Read (US.To_String (Input)); |
| 389 |
|
| 390 |
-- Build the TOML document from the JSON description and output it on the |
| 391 |
-- standard output. |
| 392 |
|
| 393 |
Result := Interpret (Description); |
| 394 |
Dump (Stdout, Result); |
| 395 |
end Ada_TOML_Encode; |