6 use Exporter
qw(import);
7 our @EXPORT_OK = qw(parse_json parse_json_aur write_json);
8 our $VERSION = 'unstable';
12 AUR::Json - Perl interface to AurJson
16 use AUR::Json qw(parse_json_aur write_json);
19 my @results = parse_json_aur($json);
20 my $object = parse_json($json);
21 my $str = write_json($object);
25 This module provides Perl aur(1) scripts a coherent way to deal with
26 AurJson responses. In particular, parse_json_aur() returns an array of
27 package results for variable AUR inputs (both from AurJson and
30 If JSON::XS is available, this module will use it for JSON
31 parsing. JSON::PP shipped with Perl is used as a fallback.
33 TODO: The interface is in its early stages and is prone to change in
34 later versions. Possible additions include AUR types for common use
35 with aur-format(1) and aur-search(1).
39 Alad Wenter <https://github.com/AladW/aurutils>
45 # Fallback to slower perl-based JSON parsing
46 if (eval { require JSON
::XS
; 1 }) {
47 $aur_json = JSON
::XS
->new;
51 $aur_json = JSON
::PP
->new;
60 my $obj = $aur_json->incr_parse($str)
61 or die __PACKAGE__
. ": expected JSON object or array at beginning of string";
62 $aur_json->incr_reset();
67 =head2 parse_json_aur()
73 my $obj = parse_json
($str);
75 # Possible AUR responses:
76 # - JSON arrays: REST (suggests), metadata archives (packages-meta-v1.json)
77 # - JSON hashes, `results` array: REST (info, search)
78 # - JSON hashes: `repo-parse` (JSONL)
79 if (ref($obj) eq 'HASH' and defined($obj->{'results'})) {
80 my $error = $obj->{'error'};
82 if (defined($error)) {
83 say STDERR __PACKAGE__
. ': response error (' . $error . ')';
86 return @
{$obj->{'results'}};
88 elsif (ref($obj) eq 'HASH') {
91 elsif (ref($obj) eq 'ARRAY') {
95 say STDERR __PACKAGE__
. ": not an array or hash";
106 my $str = $aur_json->canonical()->encode($obj);
110 # vim: set et sw=4 sts=4 ft=perl: