| JSON-Util documentation | Contained in the JSON-Util distribution. |
JSON::Util - easy and portable encode/decode of JSON
use JSON::Util;
$data = JSON::Util->decode('{"bar": "foo"}');
$data = JSON::Util->decode('some.json');
JSON::Util->encode({ 'foo' => 'bar' }, 'someother.json');
$data = JSON::Util->decode(['..', 'folder', some.json]);
JSON::Util->encode([123,321], ['..', 'folder', someother.json]);
print JSON::Util->encode([987,789]), "\n";
print JSON::Util->encode({987 => 789}), "\n";
my $json = JSON::Util->new(pretty => 0, convert_blessed => 1);
print $json->encode([ $object, $object2 ]);
# with file locking
$data = JSON::Util->decode(['..', 'folder', some.json], { 'LOCK_SH' => 1 });
$data = JSON::Util->decode(['..', 'folder', some.json], { 'LOCK_SH' => 1, LOCK_NB => 1 });
JSON::Util->encode([123,321], ['..', 'folder', someother.json], { 'LOCK_EX' => 1 });
JSON::Util->encode([123,321], ['..', 'folder', someother.json], { 'LOCK_EX' => 1, LOCK_NB => 1 });
Object constructor. Needed only when the default_json configuration
needs to be changed. Any key/value passed as parameter will be called on
<JSON::XS-new()>> as <$json-$key($value)>>.
Returns <JSON::XS-new()>> with:
'utf8' => 1,
'pretty' => 1,
'convert_blessed' => 1,
Returns current JSON::XS object.
Return ref with decoded $what. See IO::Any for $where and $opt
description.
Returns encoded $data. If $where is passed then then the result is
written there. See IO::Any for $where and $opt description.
Jozef Kutej, <jkutej at cpan.org>
Please report any bugs or feature requests to bug-json-util at rt.cpan.org, or through
the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=JSON-Util. I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
You can find documentation for this module with the perldoc command.
perldoc JSON::Util
You can also look for information at:
Copyright 2009 Jozef Kutej, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
| JSON-Util documentation | Contained in the JSON-Util distribution. |
package JSON::Util;
use warnings; use strict; our $VERSION = '0.05'; use 5.010; use feature 'state'; use Scalar::Util 'blessed'; use IO::Any; use Carp 'croak'; use JSON::XS;
sub new { my $class = shift; my %options = ( 'utf8' => 1, 'pretty' => 1, 'convert_blessed' => 1, @_ ); my $self = bless \%options, __PACKAGE__; my $json = JSON::XS->new(); while (my ($option, $value) = each %options) { $json->$option($value); } $self->{'json'} = $json; return $self; }
sub default_json { my $class = shift; state $json = $class->new->{'json'}; return $json; }
sub json { return (blessed $_[0] ? $_[0]->{'json'} : $_[0]->default_json); }
sub decode { my $self = shift; my $what = shift; my $opt = shift; croak 'too many arguments' if @_; my $data = eval { $self->json->decode(IO::Any->slurp($what, $opt)) }; my $error = $@; $error =~ s/\n$//; croak $error if $@; return $data; }
sub encode { my $self = shift; my $data = shift; # with one argument just do json encode return $self->json->encode($data) if (@_ == 0); my $where = shift; my $opt = shift; croak 'too many arguments' if @_; return IO::Any->spew($where, $self->json->encode($data), $opt); } 1; __END__
1; # End of JSON::Util