aboutsummaryrefslogtreecommitdiffstats
path: root/lib/WWW/FairViewer/ParseJSON.pm
blob: d03b17a6de569b285a0c9c2a83b4c88f56e7be77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package WWW::FairViewer::ParseJSON;

use utf8;
use 5.014;
use warnings;

=head1 NAME

WWW::FairViewer::ParseJSON - Parse JSON content.

=head1 SYNOPSIS

    use WWW::FairViewer::ParseJSON;
    my $obj = WWW::FairViewer::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

Jesus, C<< <echo aGVja3llbEBoeXBlcmJvbGEuaW5mbw== | base64 -d> >>


=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc WWW::FairViewer::ParseJSON


=head1 LICENSE AND COPYRIGHT

Copyright 2013-2015 Trizen.
Copyright 2020 Jesus E.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see L<https://www.gnu.org/licenses/>.

=cut

1;    # End of WWW::FairViewer::ParseJSON