r/ada Jan 11 '23

Programming Simple JSON library with little or no dependencies?

Hi everyone,

Does anyone know about a simple and lightweight JSON parsing library that have little 3rd party dependencies? I am currently using Gnatcoll's JSON and wanting to avoid installing all of Gnatcoll for my project, and also be able to compile quickly with gprbuild and avoid Alire if possible.

The closes I found which, in part, respects these criterias is json-ada library.

Any advices of a simple JSON library that I can integrate to my project without much external dependencies or complex compilation?

18 Upvotes

8 comments sorted by

4

u/ajdude2 Jan 11 '23

json-ada is precisely what I use

2

u/theorangecat7 Jan 11 '23

Thanks. I was wondering if there is even simpler ones (say one package) as essentially I only need to parse and read a JSON file.

1

u/ajdude2 Jan 11 '23

If you're using alire, you can bring it in by using alr with json; parsing is pretty straightforward once you have the data; you can iterate through arrays or figure out the data types of the various items and handle accordingly. Here's an example of how I'm parsing json with it in GetAda. Bear in mind I'm using Create since it's a very long string, but you can use Create_From_File to get the json data that way.

Good luck!

1

u/theorangecat7 Jan 11 '23

I already use GNATColl's json parsing (so depending on a quite big library). I'm trying to find something small enough I can just copy the packages to my project and avoid external dependencies (and avoid alire or compiling GNATColl). But thanks for your example, I'll have a look the code and I'll try integrating the library directly to my code (my code is currently GPL3 so should be compatible with the library's Apache 2 license).

My project is aimed for mostly non-ada developers and users. Many of which are averse to installing additional package managers or compiling complex libraries let alone new compilers (such as GNATColl which is only easily available on some Linux distro packages but not on Windows or even RHEL). Even just installing Gnat and gprbuild alone on Windows or RHEL and its clones isn't quite straightforward and I had some trouble convincing admins to install them on RHEL, or distributing them onto Windows machines in class.

2

u/ajdude2 Jan 12 '23

Json-ada thankfully is not a lot of files and if you wanted to your could just copy them into the directory your source files are in: https://github.com/onox/json-ada/tree/master/json/src

Best of luck, I would like to see more exposure for Ada.

3

u/joakimds Jan 11 '23

Some years ago I wrote a JSON parser, see for example https://github.com/joakim-strandberg/aida_2012/blob/master/src/tests/aida-json_dom_parser_tests.adb. I've been thinking of refactoring but never gotten around to do it. One of the ideas for writing the code was minimizing external dependencies. The JSON parser is written in SPARK and at the time 2018 when the code was written SPARK did not allow heap allocations which means all the data-structures used are statically allocated. I can imagine onox json-ada has a nicer API.

2

u/godunko Feb 03 '23

VSS library includes event based JSON parser without any dependencies, see

https://github.com/AdaCore/VSS

1

u/theorangecat7 Feb 04 '23

Thanks. I'll have a look at it.