Getting down and dirty with Ada and JSON

JSON is a pretty neat data-interchange format. It is lightweight and both easy to read and write. It is also ever so slowly becoming the de facto standard for web-applications, because it is extremely easy to use with Javascript. As a matter of fact, JSON is a subset of the object literal notation of JavaScript so it can be used in the language with no muss or fuss.

But how does JSON fare when coupled with Ada?

Pretty good actually. I ventured into the realm of JSON suspecting I would end up having to write my own parser/generator, but as luck would have it, one of my favorite Ada libraries, GNATColl, turned out to have support for JSON in its latest SVN checkout, and even better: It was pretty good!

I started mucking about with the GNATColl.JSON package, and after an initial failure to make it work (due to me not being able to read properly), I made it fly. And it flew well. It is both intuitive to use, and very clean to read. There’s really nothing bad to say about the JSON support in GNATColl, except for perhaps one little thing: It’s still very new, so new in fact that at the time of writing, there’s no entry for it in the GNATColl manual. This is not a huge issue, but it’s worth taking into consideration.

To really drive home the fact that I enjoyed working with the GNATCOLL.JSON package, I wrote a couple of articles and dropped some test code at my GitHub page:

In short: JSON and Ada programming is a good match, so before settling on using XML for your data-interchange needs, perhaps it’s worth taking a look at JSON? It is much nimbler and very easy to work with.

4 thoughts on “Getting down and dirty with Ada and JSON”

  1. Hi Thomas,

    Thank you for posting example. It really helps me to understand GNATColl.JSON. Your Ada-DK Wiki is also useful.

    One thing I am not so sure. As in the output example in your Ada-DK wiki, the sequence of the objects in the output are not the same as when they are created. Let’s take the core company data as an example.

    In procedure Set_Core_Company_Data, the sequence is company_name, vat_no, hours, product and web. But the output shows the order: company_name, web, [addresses], products, hours, [persons] and vat_no.

    Is this a bug in GNATColl.JASON? I can’t find any bug. Perhaps it is the “Ada.Containers.Indefinite_Hashed_Maps” or I have missed something?

    1. I don’t think this is a bug in GNATCOLL.JSON, but rather a conscious decision from the programmers, because a JSON object basically is an unordered collection of name/value pairs.

      If you need some sort of specific sequence of JSON name/value pairs, then you’ll have to do the sorting in the client receiving the JSON.

  2. If Ada supports JSON through GNATCOLL one would expect that the list of libraries sorted by language, included that, but it does not. Someone should contact them for correcting this unfortunate omission, but how? I cannot see any contact information.

    1. Yes, this is unfortunate. JSON is an important enough technology that they probably should’ve mentioned it on this page:

      OTOH AdaCore isn’t exactly known for it’s marketing prowess, and the JSON addition to GNATColl is rather new (compared to the rest of GNATColl), so that might be it.

      I’ve sent them an email asking why they aren’t flaunting JSON like there’s no tomorrow! 🙂

Leave a Reply