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:17]
krc Whoops
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 14: Line 14:
  
 The commands to clone, compile and execute JSON_test are: The commands to clone, compile and execute JSON_test are:
 +
 <code bash> <code bash>
- $ git clone git://​github.com/​ThomasLocke/​JSON_test.git +$ git clone git://​github.com/​ThomasLocke/​JSON_test.git 
- $ cd JSON_test +$ cd JSON_test 
- $ make +$ make 
- $ cd exe/ +$ cd exe/ 
- $ ./json_test+$ ./json_test
 </​code>​ </​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.
  
Line 42: Line 44:
 </​code>​ </​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:
Line 69: Line 71:
 </​code>​ </​code>​
  
-And that is all! First we initialize the empty ''​FooBar'' ​<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?
Line 108: Line 110:
 </​code>​ </​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:
  
 <code ada> <code ada>
Line 117: Line 119:
 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 581: Line 583:
  
 ====  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//.
  
 <code ada> <code ada>
Line 663: Line 665:
  
 ==== makefile ==== ==== makefile ====
- 
 Ultra simple makefile. Ultra simple makefile.
  
Line 778: Line 779:
 </​code>​ </​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