diff options
author | trizen <trizen@protonmail.com> | 2020-02-15 00:05:24 +0200 |
---|---|---|
committer | trizen <trizen@protonmail.com> | 2020-02-15 00:05:24 +0200 |
commit | 8e4ba906343718a4a1c2fbd939a9dbe0a75287f8 (patch) | |
tree | 340f4a502ddefbdfc0a5eec997751b5e19206aa8 /lib/WWW/StrawViewer/ParseJSON.pm | |
parent | 3e92a9d96d2ff6d2718a5a1e0d69dac766f6a141 (diff) | |
download | fair-viewer-8e4ba906343718a4a1c2fbd939a9dbe0a75287f8.tar.lz fair-viewer-8e4ba906343718a4a1c2fbd939a9dbe0a75287f8.tar.xz fair-viewer-8e4ba906343718a4a1c2fbd939a9dbe0a75287f8.zip |
Import files.
Diffstat (limited to 'lib/WWW/StrawViewer/ParseJSON.pm')
-rw-r--r-- | lib/WWW/StrawViewer/ParseJSON.pm | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/lib/WWW/StrawViewer/ParseJSON.pm b/lib/WWW/StrawViewer/ParseJSON.pm new file mode 100644 index 0000000..e2e7d65 --- /dev/null +++ b/lib/WWW/StrawViewer/ParseJSON.pm @@ -0,0 +1,76 @@ +package WWW::StrawViewer::ParseJSON; + +use utf8; +use 5.014; +use warnings; + +=head1 NAME + +WWW::StrawViewer::ParseJSON - Parse JSON content. + +=head1 SYNOPSIS + + use WWW::StrawViewer::ParseJSON; + my $obj = WWW::StrawViewer::ParseJSON->new(%opts); + +=head1 SUBROUTINES/METHODS + +=cut + +=head2 parse_json_string($json_string) + +Parse a JSON string and return a HASH ref. + +=cut + +sub parse_json_string { + my ($self, $json) = @_; + + if (not defined($json) or $json eq '') { + return {}; + } + + require JSON; + my $hash = eval { JSON::decode_json($json) }; + return $@ ? do { warn "[JSON]: $@\n"; {} } : $hash; +} + +=head2 make_json_string($ref) + +Create a JSON string from a HASH or ARRAY ref. + +=cut + +sub make_json_string { + my ($self, $ref) = @_; + + require JSON; + my $str = eval { JSON::encode_json($ref) }; + return $@ ? do { warn "[JSON]: $@\n"; '' } : $str; +} + +=head1 AUTHOR + +Trizen, C<< <echo dHJpemVuQHByb3Rvbm1haWwuY29tCg== | base64 -d> >> + + +=head1 SUPPORT + +You can find documentation for this module with the perldoc command. + + perldoc WWW::StrawViewer::ParseJSON + + +=head1 LICENSE AND COPYRIGHT + +Copyright 2013-2015 Trizen. + +This program is free software; you can redistribute it and/or modify it +under the terms of either: the GNU General Public License as published +by the Free Software Foundation; or the Artistic License. + +See L<http://dev.perl.org/licenses/> for more information. + +=cut + +1; # End of WWW::StrawViewer::ParseJSON |