| 1 | package JSON::Util; | ||||||||||
| 2 | |||||||||||
| 3 | =head1 NAME | ||||||||||
| 4 | |||||||||||
| 5 | JSON::Util - easy and portable encode/decode of JSON | ||||||||||
| 6 | |||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||
| 8 | |||||||||||
| 9 | use JSON::Util; | ||||||||||
| 10 | $data = JSON::Util->decode('{"bar": "foo"}'); | ||||||||||
| 11 | $data = JSON::Util->decode('some.json'); | ||||||||||
| 12 | JSON::Util->encode({ 'foo' => 'bar' }, 'someother.json'); | ||||||||||
| 13 | |||||||||||
| 14 | $data = JSON::Util->decode(['..', 'folder', some.json]); | ||||||||||
| 15 | JSON::Util->encode([123,321], ['..', 'folder', someother.json]); | ||||||||||
| 16 | |||||||||||
| 17 | print JSON::Util->encode([987,789]), "\n"; | ||||||||||
| 18 | print JSON::Util->encode({987 => 789}), "\n"; | ||||||||||
| 19 | |||||||||||
| 20 | my $json = JSON::Util->new(pretty => 0, convert_blessed => 1); | ||||||||||
| 21 | print $json->encode([ $object, $object2 ]); | ||||||||||
| 22 | |||||||||||
| 23 | # with file locking | ||||||||||
| 24 | $data = JSON::Util->decode(['..', 'folder', some.json], { 'LOCK_SH' => 1 }); | ||||||||||
| 25 | $data = JSON::Util->decode(['..', 'folder', some.json], { 'LOCK_SH' => 1, LOCK_NB => 1 }); | ||||||||||
| 26 | JSON::Util->encode([123,321], ['..', 'folder', someother.json], { 'LOCK_EX' => 1 }); | ||||||||||
| 27 | JSON::Util->encode([123,321], ['..', 'folder', someother.json], { 'LOCK_EX' => 1, LOCK_NB => 1 }); | ||||||||||
| 28 | |||||||||||
| 29 | =head1 DESCRIPTION | ||||||||||
| 30 | |||||||||||
| 31 | =cut | ||||||||||
| 32 | |||||||||||
| 33 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 34 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 35 | |||||||||||
| 36 | 2 | our $VERSION = '0.06'; | |||||||||
| 37 | |||||||||||
| 38 | 2 | ▶ | use 5.010; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 39 | 2 | ▶ | use feature 'state'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 40 | |||||||||||
| 41 | 2 | ▶ | use Scalar::Util 'blessed'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 42 | 2 | ▶ | use IO::Any; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 43 | 2 | ▶ | use Carp 'croak'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 44 | 2 | ▶ | use JSON::MaybeXS; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 45 | |||||||||||
| 46 | =head1 METHODS | ||||||||||
| 47 | |||||||||||
| 48 | =head2 new() | ||||||||||
| 49 | |||||||||||
| 50 | Object constructor. Needed only when the L</default_json> configuration | ||||||||||
| 51 | needs to be changed. Any key/value passed as parameter will be called on | ||||||||||
| 52 | C<<JSON::MaybeXS->new()>> as C<<$json->$key($value)>>. | ||||||||||
| 53 | |||||||||||
| 54 | =cut | ||||||||||
| 55 | |||||||||||
| 56 | 2 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 57 | 2 | my $class = shift; | |||||||||
| 58 | 2 | my %options = ( | |||||||||
| 59 | 'utf8' => 1, | ||||||||||
| 60 | 'pretty' => 1, | ||||||||||
| 61 | 'convert_blessed' => 1, | ||||||||||
| 62 | @_ | ||||||||||
| 63 | ); | ||||||||||
| 64 | |||||||||||
| 65 | 2 | my $self = bless \%options, __PACKAGE__; | |||||||||
| 66 | |||||||||||
| 67 | 2 | my $json = JSON::MaybeXS->new(); | |||||||||
| 68 | 2 | ▶ | while (my ($option, $value) = each %options) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 6 | $json->$option($value); | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | 2 | $self->{'json'} = $json; | |||||||||
| 73 | |||||||||||
| 74 | 2 | return $self; | |||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | =head2 default_json | ||||||||||
| 78 | |||||||||||
| 79 | Returns C<<JSON::MaybeXS->new()>> with: | ||||||||||
| 80 | |||||||||||
| 81 | 'utf8' => 1, | ||||||||||
| 82 | 'pretty' => 1, | ||||||||||
| 83 | 'convert_blessed' => 1, | ||||||||||
| 84 | |||||||||||
| 85 | =cut | ||||||||||
| 86 | |||||||||||
| 87 | 5 | ▶ | sub default_json { | ||||||||
|
Subroutine
default_json: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 88 | 5 | my $class = shift; | |||||||||
| 89 | 5 | state $json = $class->new->{'json'}; | |||||||||
| 90 | 5 | return $json; | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | =head2 json | ||||||||||
| 94 | |||||||||||
| 95 | Returns current L<JSON::MaybeXS> object. | ||||||||||
| 96 | |||||||||||
| 97 | =cut | ||||||||||
| 98 | |||||||||||
| 99 | 7 | ▶ | sub json { | ||||||||
|
Subroutine
json: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 100 | 7 | ▶ | return (blessed $_[0] ? $_[0]->{'json'} : $_[0]->default_json); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | =head2 decode($what, [$opt]) | ||||||||||
| 104 | |||||||||||
| 105 | Return ref with decoded C<$what>. See L<IO::Any> for C<$where> and C<$opt> | ||||||||||
| 106 | description. | ||||||||||
| 107 | |||||||||||
| 108 | =cut | ||||||||||
| 109 | |||||||||||
| 110 | 3 | ▶ | sub decode { | ||||||||
|
Subroutine
decode: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||
| 111 | 3 | my $self = shift; | |||||||||
| 112 | 3 | my $what = shift; | |||||||||
| 113 | 3 | my $opt = shift; | |||||||||
| 114 | 3 | ▶ | croak 'too many arguments' | ||||||||
|
Branch: 1 branch
| |||||||||||
| 115 | if @_; | ||||||||||
| 116 | |||||||||||
| 117 | 3 | my $data = eval { $self->json->decode(IO::Any->slurp($what, $opt)) }; | |||||||||
| 118 | 3 | my $error = $@; $error =~ s/\n$//; | |||||||||
| 119 | 3 | ▶ | croak $error if $@; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 120 | |||||||||||
| 121 | 3 | return $data; | |||||||||
| 122 | } | ||||||||||
| 123 | |||||||||||
| 124 | =head2 encode($data, [$where], [$opt]) | ||||||||||
| 125 | |||||||||||
| 126 | Returns encoded C<$data>. If C<$where> is passed then the result is | ||||||||||
| 127 | written there. See L<IO::Any> for C<$where> and C<$opt> description. | ||||||||||
| 128 | |||||||||||
| 129 | =cut | ||||||||||
| 130 | |||||||||||
| 131 | 4 | ▶ | sub encode { | ||||||||
|
Subroutine
encode: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 132 | 4 | my $self = shift; | |||||||||
| 133 | 4 | my $data = shift; | |||||||||
| 134 | |||||||||||
| 135 | # with one argument just do json encode | ||||||||||
| 136 | 4 | ▶ | return $self->json->encode($data) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 137 | if (@_ == 0); | ||||||||||
| 138 | |||||||||||
| 139 | 2 | my $where = shift; | |||||||||
| 140 | 2 | my $opt = shift; | |||||||||
| 141 | 2 | ▶ | croak 'too many arguments' | ||||||||
|
Branch: 1 branch
| |||||||||||
| 142 | if @_; | ||||||||||
| 143 | |||||||||||
| 144 | 2 | return IO::Any->spew($where, $self->json->encode($data), $opt); | |||||||||
| 145 | } | ||||||||||
| 146 | |||||||||||
| 147 | 2 | 1; | |||||||||
| 148 | |||||||||||
| 149 | |||||||||||
| 150 | __END__ | ||||||||||