| 1 | package Catmandu::Importer::JSON; | ||||||||||
| 2 | |||||||||||
| 3 | 4 | ▶ | use Catmandu::Sane; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 1 | our $VERSION = '1.0201_02'; | |||||||||
| 6 | |||||||||||
| 7 | 4 | ▶ | use Cpanel::JSON::XS (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 4 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 4 | ▶ | use namespace::clean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 1 | with 'Catmandu::Importer'; | |||||||||
| 12 | |||||||||||
| 13 | 5 | ▶ | has line_delimited => (is => 'ro', default => sub {0}); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 1 | has json => (is => 'lazy'); | |||||||||
| 15 | |||||||||||
| 16 | sub _build_json { | ||||||||||
| 17 | 5 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
_build_json: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 5 | Cpanel::JSON::XS->new->utf8($self->encoding eq ':raw'); | |||||||||
| 19 | } | ||||||||||
| 20 | |||||||||||
| 21 | 5 | ▶ | sub _build_encoding {':raw'} | ||||||||
|
Subroutine
_build_encoding: called CC=1 SCAR=0.0
| |||||||||||
| 22 | |||||||||||
| 23 | sub generator { | ||||||||||
| 24 | 5 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
generator: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||
| 25 | |||||||||||
| 26 | 5 | ▶ | if ($self->line_delimited) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | return sub { | ||||||||||
| 28 | state $json = $self->json; | ||||||||||
| 29 | state $fh = $self->fh; | ||||||||||
| 30 | if (defined(my $line = <$fh>)) { | ||||||||||
| 31 | return $json->decode($line); | ||||||||||
| 32 | } | ||||||||||
| 33 | return; | ||||||||||
| 34 | 0 | }; | |||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | # switch to slower incremental parser | ||||||||||
| 38 | sub { | ||||||||||
| 39 | state $json = $self->json; | ||||||||||
| 40 | state $fh = $self->fh; | ||||||||||
| 41 | |||||||||||
| 42 | for (;;) { | ||||||||||
| 43 | my $res = sysread($fh, my $buf, 512); | ||||||||||
| 44 | $res // Catmandu::Error->throw($!); | ||||||||||
| 45 | $json->incr_parse($buf); # void context, so no parsing | ||||||||||
| 46 | $json->incr_text =~ s/^[^{]+//; | ||||||||||
| 47 | return if $json->incr_text =~ /^$/; | ||||||||||
| 48 | last if $json->incr_text =~ /^{/; | ||||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | # read data until we get a single json object | ||||||||||
| 52 | for (;;) { | ||||||||||
| 53 | if (my $data = $json->incr_parse) { | ||||||||||
| 54 | return $data; | ||||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | my $res = sysread($fh, my $buf, 512); | ||||||||||
| 58 | $res // Catmandu::Error->throw($!); | ||||||||||
| 59 | $res | ||||||||||
| 60 | || Catmandu::Error->throw( | ||||||||||
| 61 | "JSON syntax error: unexpected end of object"); | ||||||||||
| 62 | $json->incr_parse($buf); | ||||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | return; | ||||||||||
| 66 | |||||||||||
| 67 | 5 | }; | |||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | 1 | 1; | |||||||||
| 71 | |||||||||||
| 72 | __END__ | ||||||||||
| 73 | |||||||||||
| 74 | =pod | ||||||||||
| 75 | |||||||||||
| 76 | =head1 NAME | ||||||||||
| 77 | |||||||||||
| 78 | Catmandu::Importer::JSON - Package that imports JSON data | ||||||||||
| 79 | |||||||||||
| 80 | =head1 SYNOPSIS | ||||||||||
| 81 | |||||||||||
| 82 | # From the command line | ||||||||||
| 83 | |||||||||||
| 84 | $ catmandu convert JSON to YAML < data.json | ||||||||||
| 85 | |||||||||||
| 86 | # or for faster newline delimited input | ||||||||||
| 87 | |||||||||||
| 88 | $ catmandu convert JSON --line_delimited 1 to YAML < data.json | ||||||||||
| 89 | |||||||||||
| 90 | # In a Perl script | ||||||||||
| 91 | |||||||||||
| 92 | use Catmandu; | ||||||||||
| 93 | |||||||||||
| 94 | my $importer = Catmandu->importer('JSON', file => "/foo/bar.json"); | ||||||||||
| 95 | |||||||||||
| 96 | my $n = $importer->each(sub { | ||||||||||
| 97 | my $hashref = $_[0]; | ||||||||||
| 98 | # ... | ||||||||||
| 99 | }); | ||||||||||
| 100 | |||||||||||
| 101 | =head1 DESCRIPTION | ||||||||||
| 102 | |||||||||||
| 103 | This package imports JSON data. The parser is quite liberal in the input | ||||||||||
| 104 | it accepts. You can use the C<line_delimited> option to parse "newline | ||||||||||
| 105 | delimited JSON" faster: | ||||||||||
| 106 | |||||||||||
| 107 | { "recordno": 1, "name": "Alpha" } | ||||||||||
| 108 | { "recordno": 2, "name": "Beta" } | ||||||||||
| 109 | { "recordno": 3, "name": "Gamma" } | ||||||||||
| 110 | |||||||||||
| 111 | =head1 CONFIGURATION | ||||||||||
| 112 | |||||||||||
| 113 | =over | ||||||||||
| 114 | |||||||||||
| 115 | =item file | ||||||||||
| 116 | |||||||||||
| 117 | Read input from a local file given by its path. Alternatively a scalar | ||||||||||
| 118 | reference can be passed to read from a string. | ||||||||||
| 119 | |||||||||||
| 120 | =item fh | ||||||||||
| 121 | |||||||||||
| 122 | Read input from an L<IO::Handle>. If not specified, L<Catmandu::Util::io> is used to | ||||||||||
| 123 | create the input stream from the C<file> argument or by using STDIN. | ||||||||||
| 124 | |||||||||||
| 125 | =item encoding | ||||||||||
| 126 | |||||||||||
| 127 | Binmode of the input stream C<fh>. Set to C<:utf8> by default. | ||||||||||
| 128 | |||||||||||
| 129 | =item fix | ||||||||||
| 130 | |||||||||||
| 131 | An ARRAY of one or more fixes or file scripts to be applied to imported items. | ||||||||||
| 132 | |||||||||||
| 133 | =item line_delimited | ||||||||||
| 134 | |||||||||||
| 135 | Read line-delimited JSON with a faster, non-incremental parser. | ||||||||||
| 136 | |||||||||||
| 137 | =back | ||||||||||
| 138 | |||||||||||
| 139 | =head1 METHODS | ||||||||||
| 140 | |||||||||||
| 141 | Every L<Catmandu::Importer> is a L<Catmandu::Iterable> all its methods are | ||||||||||
| 142 | inherited. The methods are not idempotent: JSON streams can only be read once. | ||||||||||
| 143 | |||||||||||
| 144 | =head1 SEE ALSO | ||||||||||
| 145 | |||||||||||
| 146 | L<Catmandu::Exporter::JSON> | ||||||||||
| 147 | |||||||||||
| 148 | =cut | ||||||||||