• Stars
    star
    35
  • Rank 750,836 (Top 15 %)
  • Language
    Ada
  • License
    Apache License 2.0
  • Created over 8 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An Ada 2012 library for parsing JSON

Build status Test status Alire json License GitHub release IRC Gitter chat

json-ada

An Ada 2012 library for parsing JSON (RFC 7159). It supports Ada 2012's iterator and indexing syntax. The RFC does not support comments, thus this library does not support it either. If your JSON data contains comments, you should minify the data so that comments are removed.

Unicode may not be supported yet.

Usage

with Ada.Command_Line;
with Ada.Text_IO;

with JSON.Parsers;
with JSON.Types;

procedure Example is
   package Types   is new JSON.Types (Long_Integer, Long_Float);
   package Parsers is new JSON.Parsers (Types);

   Parser : Parsers.Parser := Parsers.Create_From_File (Ada.Command_Line.Argument (1));
   Value  : constant Types.JSON_Value := Parser.Parse;

   use Types;
begin
   --  The data type of a Value can be retrieved with `Value.Kind`. The value
   --  can be one of:
   --
   --  Null_Kind, Boolean_Kind, Integer_Kind, Float_Kind, String_Kind,
   --  Array_Kind, or Object_Kind.
   case Value.Kind is
      when Array_Kind | Object_Kind =>
         --  Query the length of a JSON array or object with the function `Length`
         Ada.Text_IO.Put_Line (Value.Length'Image);

         --  A JSON object provides the function `Contains`
         if Value.Kind = Object_Kind and then Value.Contains ("foo") then
            --  To get an element of a JSON array or object write `Value.Get (Index_Or_Key)`
            --  or use Ada 2012's indexing syntax `Value (Index_Or_Key)`.
            --
            --  If Value is a JSON object, you can call `Get_Array_Or_Empty`,
            --  `Get_Object_Or_Empty`, or `Get` (with an additional parameter containing
            --  the default value).
            Ada.Text_IO.Put_Line (Value ("foo").Kind'Image);
         end if;

         --  Iterate over a JSON array or object by using the
         --  Ada 2012 iterator syntax `for Element_Or_Key of Value`. The type is a
         --  JSON_Value and represents an element of the array or the key of an object.
         for Element of Value loop
            --  To get the image of JSON_Value (to serialize it), use function `Image`:
            Ada.Text_IO.Put_Line (Element.Image);
         end loop;
      when Float_Kind =>
         declare
            --  Get the value (String, generic Integer_Type or Float_Type, or Boolean)
            --  of a Value by calling `Value.Value`. An Invalid_Type_Error
            --  exception will be raised if Value has a different type than the type
            --  of the variable in which you try to store the result.
            Data : constant Long_Float := Value.Value;
         begin
            Ada.Text_IO.Put_Line (Data'Image);
         end;
      when others =>
         null;
   end case;
end Example;

Optional generic parameters of JSON.Types:

  • Maximum_Number_Length (default is 30): Maximum length in characters of numbers

Optional generic parameters of JSON.Parsers:

  • Default_Maximum_Depth (default is 10)

  • Check_Duplicate_Keys (default is False): Check for duplicate keys when parsing. Parsing large JSON texts will be slower if enabled. If disabled then the Get operation will return the value of the first key that matches.

A parser can be created by calling Create_From_File or Create. The actual parameter of Create can be some text as a String or an access to a Ada.Streams.Stream_Element_Array containing the text. In case an access to a Stream_Element_Array is given, then the Parser takes no ownership of the pointer and you must deallocate the array yourself.

The default maximum nesting depth can be overriden with the optional second parameter Maximum_Depth of function Create.

After parsing, elements of arrays and objects are stored in the Parser object, while strings remain stored in the String or Stream_Element_Array. Therefore, the Parser object must not go out of scope before any JSON_Value goes out of scope.

Dependencies

In order to build the library, you need to have:

  • An Ada 2012 compiler

  • Alire

Optional dependencies:

  • lcov to generate a coverage report for unit tests

  • make

Using the library

Use the library in your crates as follows:

$ alr with json

Tools

The JSON pretty printer can be run as follows:

$ alr run -q --args=path/to/file.json

Tests

The project contains a set of unit tests. Use make tests to build and run the unit tests. A coverage report can be generated with make coverage:

$ make tests
$ make coverage

Contributing

Read the contributing guidelines if you want to add a bugfix or an improvement.

License

The Ada code and unit tests are licensed under the Apache License 2.0. The first line of each Ada file should contain an SPDX license identifier tag that refers to this license:

SPDX-License-Identifier: Apache-2.0

More Repositories

1

orka

The OpenGL 4.6 Rendering Kernel in Ada 2012
Ada
52
star
2

OpenRTI

Mirror of OpenRTI on SourceForge. Do not create PR's. Instead send patches or git pull commands to the mailing list on SF.
C++
51
star
3

inotify-ada

An Ada 2012 library for monitoring filesystem events using Linux' inotify API
Ada
8
star
4

emojis

An Ada 2012 library to replace names between colons with emojis
Ada
7
star
5

xoshiro

Ada/SPARK port of the xoshiro128++ and xoshiro256++ pseudo-random number generators
Ada
7
star
6

wayland-ada

Ada 2012 bindings for Wayland
Ada
6
star
7

dcf-ada

An Ada 2012 library for document container files
Ada
5
star
8

orka-demo

Demo project using Orka 3D engine
Ada
4
star
9

evdev-ada

An Ada 2012 library to read input events and use force-feedback using Linux' evdev API
Ada
4
star
10

canberra-ada

Ada 2012 bindings for libcanberra
Ada
4
star
11

awt

Ada Window Toolkit, a library for managing input devices and windows that can display 3D graphics
3
star
12

weechat-ada

Ada 2012 library for WeeChat plug-ins
Ada
3
star
13

vim-ada

Modified Vim script for Ada 2012
Vim Script
2
star
14

weechat-emoji

A WeeChat plug-in written in Ada 2012 πŸ₯° that displays emoji πŸ₯³
Ada
2
star
15

spoon

An Ada 2012 library for posix_spawn() to spawn processes without a fork().
Ada
2
star
16

opus-ada

Ada 2012 bindings for the Opus audio codec.
Ada
2
star
17

xdg-base-dir

Ada 2012 library implementing the XDG Base Directory Specification
Ada
1
star
18

goblin-terminal

A mutated clone of GNOME Terminal
Python
1
star
19

weechat-canberra

A WeeChat plug-in written in Ada 2012 that plays sounds using libcanberra
Ada
1
star