Differences

This shows you the differences between two versions of the page.

Link to this comparison view

handling_json_using_gnatcoll [2011/12/19 19:10]
krc moved
handling_json_using_gnatcoll [2011/12/23 06:34] (current)
thomaslocke
Line 1: Line 1:
 ===== Introduction ===== ===== Introduction =====
-The [http://​json.org/​ JSON] data format has grown to become quite popular in these past years. It is lightweight,​ fairly easy to both read and write and a lot less "​bloated"​ than it's heavy XML cousin. Granted there'​s a lot of things you can do with XML which just isn't possible with JSON, but a lot of those things are rarely used, and if all you're looking for is a light data-interchange format, then JSON might just be what you need.+The [[http://​json.org/​|JSON]] data format has grown to become quite popular in these past years. It is lightweight,​ fairly easy to both read and write and a lot less "​bloated"​ than it's heavy XML cousin. Granted there'​s a lot of things you can do with XML which just isn't possible with JSON, but a lot of those things are rarely used, and if all you're looking for is a light data-interchange format, then JSON might just be what you need.
  
 And luckily Ada can handle JSON just fine. And luckily Ada can handle JSON just fine.
  
-On this page you'll find an example on how to handle JSON reading and writing using the [http://​libre.adacore.com/​libre/​tools/​gnat-component-collection/​ GNATColl] package from AdaCore. We're going to build a very simple JSON object that contains some company related information and we're going to read a JSON object from a file and output its fields using an iterator.+On this page you'll find an example on how to handle JSON reading and writing using the [[http://​libre.adacore.com/​libre/​tools/​gnat-component-collection/​|GNATColl]] package from AdaCore. We're going to build a very simple JSON object that contains some company related information and we're going to read a JSON object from a file and output its fields using an iterator.
  
 Obviously this program depends on you having GNATColl installed on your system. The GNATColl SVN URL is available in the JSON_Test README file. See the GNATColl documentation for how to install it. Obviously this program depends on you having GNATColl installed on your system. The GNATColl SVN URL is available in the JSON_Test README file. See the GNATColl documentation for how to install it.
Line 11: Line 11:
 You can clone the JSON_Test program from this Git repository: You can clone the JSON_Test program from this Git repository:
  
- git://​github.com/​ThomasLocke/​JSON_test.git+<​code>​git://​github.com/​ThomasLocke/​JSON_test.git</​code>​
  
 The commands to clone, compile and execute JSON_test are: The commands to clone, compile and execute JSON_test are:
  
- $ git clone git://​github.com/​ThomasLocke/​JSON_test.git +<code bash> 
- $ cd JSON_test +$ git clone git://​github.com/​ThomasLocke/​JSON_test.git 
- $ make +$ cd JSON_test 
- $ cd exe/ +$ make 
- $ ./json_test+$ cd exe/ 
 +$ ./json_test 
 +</​code>​
  
-If you don't feel like using Git, the code can be copied from below. I will of course do my best to make sure that the code on this page matches the code in the Git repository, but since this is a Wiki where anybody can make changes, I cannot guarantee the safety of running the code from this page, so use it with caution. I '''​strongly''' ​recommend cloning from the Git repository.+If you don't feel like using Git, the code can be copied from below. I will of course do my best to make sure that the code on this page matches the code in the Git repository, but since this is a Wiki where anybody can make changes, I cannot guarantee the safety of running the code from this page, so use it with caution. I //strongly// recommend cloning from the Git repository.
  
 ==== Some GNATCOLL.JSON Basics ==== ==== Some GNATCOLL.JSON Basics ====
 When reading and writing JSON objects using GNATCOLL.JSON,​ you will mostly be interacting with the following types: When reading and writing JSON objects using GNATCOLL.JSON,​ you will mostly be interacting with the following types:
  
-<pre lang="ada">+<code ada>
 type JSON_Value_Type is type JSON_Value_Type is
   (JSON_Null_Type,​   (JSON_Null_Type,​
Line 40: Line 42:
 type JSON_Value is tagged private; type JSON_Value is tagged private;
 type JSON_Array is private; type JSON_Array is private;
-</pre>+</code>
  
-The <tt>JSON_Value_Type</tt> ​lists the possible types of data you can add to or read from a JSON object. The <tt>UTF8_String</tt> ​type is used whenever you're dealing with string content. The <tt>JSON_Value</tt> ​type holds the actual JSON fields and data and it can even hold other <tt>JSON_Value</tt> ​objects. The <tt>JSON_Array</tt> ​type is used when you need to add arrays of data to a <tt>JSON_Value</tt> ​object.+The //JSON_Value_Type/​lists the possible types of data you can add to or read from a JSON object. The //UTF8_String/​type is used whenever you're dealing with string content. The //JSON_Value/type holds the actual JSON fields and data and it can even hold other //JSON_Value/objects. The //JSON_Array/type is used when you need to add arrays of data to a //JSON_Value/object.
  
 So, how would we go about creating and outputting a JSON object like this one: So, how would we go about creating and outputting a JSON object like this one:
  
-<pre lang="javascript">+<code javascript>​
 {"​foo":"​bar"​} {"​foo":"​bar"​}
-</pre>+</code>
  
 The Ada programming needed to create and output that JSON object is only a handful of lines in total: The Ada programming needed to create and output that JSON object is only a handful of lines in total:
  
-<pre lang="ada">+<code ada>
 with Ada.Text_IO;​ with Ada.Text_IO;​
 with GNATCOLL.JSON;​ with GNATCOLL.JSON;​
Line 67: Line 69:
    ​Put_Line (Write (Foo_Bar_JSON));​    ​Put_Line (Write (Foo_Bar_JSON));​
 end FooBar; end FooBar;
-</pre>+</code>
  
-And that is all! First we initialize the empty <tt>FooBar</tt> <tt>JSON_Value</tt> ​object, then we call <tt>Set_Field</tt> ​to add a field (<tt>foo</tt>) and a value (<tt>bar</tt>) to it and finally we call <tt>Write</tt> ​to turn the <tt>JSON_Value</tt> ​object into a string. ​+And that is all! First we initialize the empty ''​FooBar''​ //​JSON_Value/​object, then we call //Set_Field/to add a field (//foo//) and a value (//bar//) to it and finally we call //Write/to turn the //JSON_Value/object into a string. ​
  
 What if we wanted to add some very important persons to a JSON array? What if we wanted to add some very important persons to a JSON array?
  
-<pre lang="ada">+<code ada>
 with Ada.Text_IO;​ with Ada.Text_IO;​
 with GNATCOLL.JSON;​ with GNATCOLL.JSON;​
Line 100: Line 102:
    ​Put_Line (Write (Foo_Bar_JSON));​    ​Put_Line (Write (Foo_Bar_JSON));​
 end FooBar; end FooBar;
-</pre>+</code>
  
 And then we get: And then we get:
  
-<pre lang="javascript">+<code javascript>​
 {"​vips":​["​Arthur",​ "​Lancelot"​],​ "​foo":"​bar"​} {"​vips":​["​Arthur",​ "​Lancelot"​],​ "​foo":"​bar"​}
-</pre>+</code>
  
-Reading data from the JSON object is done using a handful of <tt>Get</tt> ​functions:+Reading data from the JSON object is done using a handful of //Get/functions:
  
-<pre lang="ada">+<code ada>
 Put_Line (Get (Val   => FooBar_JSON,​ Put_Line (Get (Val   => FooBar_JSON,​
                Field => "​foo"​));​                Field => "​foo"​));​
-</pre>+</code>
  
 Which obviously outputs "​bar"​. ​ Which obviously outputs "​bar"​. ​
  
-There are also functions available to get the length of a JSON array and to return the value at a given index of an array. If you need to iterate all fields in a JSON object, you can use the <tt>Map_JSON_Object</tt> ​procedure. And lastly you can read a string JSON object and turn it into a <tt>JSON_Value</tt>+There are also functions available to get the length of a JSON array and to return the value at a given index of an array. If you need to iterate all fields in a JSON object, you can use the //Map_JSON_Object/​procedure. And lastly you can read a string JSON object and turn it into a //JSON_Value//
  
 Most of these things are shown in the JSON_Test program below, so without further ado, lets move on to that. Most of these things are shown in the JSON_Test program below, so without further ado, lets move on to that.
Line 124: Line 126:
 This is the main program file. There'​s not a whole lot JSON action going on here. Most of that is happening in the Helpers package. This is the main program file. There'​s not a whole lot JSON action going on here. Most of that is happening in the Helpers package.
  
-<pre lang="ada">+<code ada>
 ------------------------------------------------------------------------------- -------------------------------------------------------------------------------
 --                                                                           -- --                                                                           --
Line 225: Line 227:
    ​New_Line;​    ​New_Line;​
 end JSON_Test; end JSON_Test;
-</pre>+</code>
  
 ==== src/​helpers.ads ==== ==== src/​helpers.ads ====
 It is in this package the actual building and iteration of the JSON object happens. It is in this package the actual building and iteration of the JSON object happens.
  
-<pre lang="ada">+<code ada>
 ------------------------------------------------------------------------------- -------------------------------------------------------------------------------
 --                                                                           -- --                                                                           --
Line 297: Line 299:
        
 end Helpers; end Helpers;
-</pre>+</code>
  
 ==== src/​helpers.adb ==== ==== src/​helpers.adb ====
 Simplicity at work! Using GNATCOLL.JSON it's a complete no-brainer to build JSON objects. Simplicity at work! Using GNATCOLL.JSON it's a complete no-brainer to build JSON objects.
  
-<pre lang="ada">+<code ada>
 ------------------------------------------------------------------------------- -------------------------------------------------------------------------------
 --                                                                           -- --                                                                           --
Line 546: Line 548:
  
 end Helpers; end Helpers;
-</pre>+</code>
  
 ==== exe/​test.json ==== ==== exe/​test.json ====
 This is the test JSON object we'll read from file and then output using the <​tt>​Map_JSON_Object</​tt>​ procedure. This is the test JSON object we'll read from file and then output using the <​tt>​Map_JSON_Object</​tt>​ procedure.
  
-<pre lang="javascript">+<code javascript>​
 {"​widget":​ { {"​widget":​ {
     "​debug":​ true,     "​debug":​ true,
Line 578: Line 580:
     }     }
 }} }}
-</pre>+</code>
  
 ====  json_test.gpr ==== ====  json_test.gpr ====
-This file set up various compile options. It informs the compiler of the location of the source files and where to put the object and the executable files. Note that you can build the JSON_Test program using debug settings. This is simply done by calling ​<tt>make debug</tt> ​instead of plain <tt>make</tt>.+This file set up various compile options. It informs the compiler of the location of the source files and where to put the object and the executable files. Note that you can build the JSON_Test program using debug settings. This is simply done by calling ​//make debug/instead of plain //make//.
  
-<pre lang="ada">+<code ada>
 ------------------------------------------------------------------------------- -------------------------------------------------------------------------------
 --                                                                           -- --                                                                           --
Line 660: Line 662:
  
 end JSON_Test; end JSON_Test;
-</pre>+</code>
  
 ==== makefile ==== ==== makefile ====
- 
 Ultra simple makefile. Ultra simple makefile.
  
-<pre lang="make">+<code make>
 ###############################################################################​ ###############################################################################​
 #                                                                             # #                                                                             #
Line 696: Line 697:
  gnatclean -P json_test  gnatclean -P json_test
  BUILDTYPE=Debug gnatclean -P json_test  BUILDTYPE=Debug gnatclean -P json_test
-</pre>+</code>
  
 ==== Program Output ==== ==== Program Output ====
 When running the JSON_Test program, you should get output similar to this: When running the JSON_Test program, you should get output similar to this:
  
-<pre>+<code json>
 --> Company_JSON Start <-- --> Company_JSON Start <--
 { {
Line 776: Line 777:
 onMouseUp(string):​sun1.opacity = (sun1.opacity / 100) * 90; onMouseUp(string):​sun1.opacity = (sun1.opacity / 100) * 90;
 --> Test_JSON End <-- --> Test_JSON End <--
-</pre>+</code>
  
-It might not look exactly like above, but the contents should be the same. You can validate the <tt>Company_JSON</tt> ​object at [http://​jsonlint.com/​ jsonlint.com].+It might not look exactly like above, but the contents should be the same. You can validate the //Company_JSON/​object at [[http://​jsonlint.com/​ jsonlint.com]].
  
 I hope you've enjoyed this short venture into the world of Ada and JSON. Feel free to make changes to this page if you find any mistakes or if you feel something is unclear. I hope you've enjoyed this short venture into the world of Ada and JSON. Feel free to make changes to this page if you find any mistakes or if you feel something is unclear.

Navigation