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
|
****************************
**JSON**: handling JSON data
****************************
.. index:: json
.. highlight:: ada
JSON is a format often used on the web to communicate between
a server and a browser, or between servers. It plays a similar
role to XML, but is much lighter in terms of size. On the
other hand, it doesn't provide advanced features like validation
which XML provides.
The package **GNATCOLL.JSON** provides an Ada for creating
JSON data, or parse such data that your application receives.
Most JSON data will generally start with an object, on which
attributes can be set. The value for the attributes are also
JSON data.
Here is an example of use::
pragma Ada_05;
with GNATCOLL.JSON; use GNATCOLL.JSON;
with Ada.Text_IO; use Ada.Text_IO;
procedure JSON_Test is
MyObj : JSON_Value := Create_Object;
begin
MyObj.Set_Field ("field1", Create (1));
MyObj.Set_Field ("name", "theName");
-- Now print the value
Put_Line (MyObj.Write);
end JSON_Test;
This example used the Ada05 dot notation to call the primitive
operations, but would also work using the more traditional
prefix notation.
It is also possible to create JSON arrays. These are not
tagged types, so the prefix notation has to be used. Here
is a further example that sets another field in the object
we had before::
declare
MyArr : JSON_Array := Empty_Array;
begin
Append (MyArr, Create (1));
Append (MyArr, Create ("aString"));
MyObj.Set_Field ("vals", MyArr);
end;
GNATColl automatically takes care of memory management, and
all allocated memory is automatically freed when the object
is no longer needed.
|