File: JSON.pm

package info (click to toggle)
libbadger-perl 0.16-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,400 kB
  • sloc: perl: 11,004; makefile: 9
file content (156 lines) | stat: -rw-r--r-- 3,469 bytes parent folder | download
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#========================================================================
#
# Badger::Codec::JSON
#
# DESCRIPTION
#   Codec module for encoding/decoding Base64
#
# AUTHOR
#   Andy Wardley   <abw@wardley.org>
#
#========================================================================

package Badger::Codec::JSON;

use Badger::Class
    version => 0.01,
    base    => 'Badger::Codec',
    import  => 'class CLASS',
    codecs  => 'utf8';

our ($HAS_CP_JSON_XS, $HAS_JSON_XS, $HAS_JSON);

# Cpanel::JSON::XS is more complete/correct than JSON::XS
eval "require Cpanel::JSON::XS";
$HAS_CP_JSON_XS = $@ ? 0 : 1;

# JSON::XS has bits missing (e.g. allow_bignum)
unless ($HAS_CP_JSON_XS) {
    eval "require JSON::XS";
    $HAS_JSON_XS = $@ ? 0 : 1;
}

# fallback to Perl implementation
unless ($HAS_CP_JSON_XS || $HAS_JSON_XS) {
    eval "require JSON";
    $HAS_JSON = $@ ? 0 : 1;
}

our $MODULE =
    $HAS_CP_JSON_XS ? 'Cpanel::JSON::XS' :
    $HAS_JSON_XS    ? 'JSON::XS' :
    $HAS_JSON       ? 'JSON'     :
    CLASS->error("You don't have JSON, JSON::XS or Cpanel::JSON::XS installed");

our $JSON = $MODULE->new;

sub encode_json {
    $JSON->encode(shift);
}

sub decode_json {
    my $json = shift;
#   $json = encode_utf8($json);
    $JSON->decode($json);
}

sub encode {
    shift;
    goto &encode_json;
}

sub decode {
    shift;
    goto &decode_json;
}

sub encoder {
    \&encode_json;
}

sub decoder {
    \&decode_json;
}

1;


__END__

=head1 NAME

Badger::Codec::JSON - encode/decode data using JSON

=head1 SYNOPSIS

    use Badger::Codec::JSON;
    my $codec   = Badger::Codec::JSON->new();
    my $encoded = $codec->encode({ msg => "Hello World" });
    my $decoded = $codec->decode($encoded);

=head1 DESCRIPTION

This module implements a subclass of L<Badger::Codec> which uses the
L<JSON::XS> or L<JSON> module (whichever you have installed) to encode and
decode data to and from JSON. It is little more than an adapter module
to fit L<JSON> into the L<Badger::Codec> mould.

=head1 METHODS

=head2 encode($data)

Encodes the Perl data in C<$data> to a JSON string. This method is a wrapper
around the internal the L<encode_json()> subroutine.

    $encoded = Badger::Codec::JSON->encode($data);

=head2 decode($json)

Decodes the encoded JSON string in C<$json> back into a Perl data structure.
This method is a wrapper around the internal the L<decode_json()> subroutine.

    $decoded = Badger::Codec::JSON->decode($encoded);

=head2 encoder()

This method returns a reference to the real subroutine that's doing
all the encoding work, i.e. the internal C<encode_json()> subroutine.

=head2 decoder()

This method returns a reference to the real subroutine that's doing
all the decoding work, i.e. the C<decode_json()> subroutine in L<JSON>.

=head1 INTERNAL SUBROUTINES

=head2 encode_json($data)

This is the internal subroutine that encodes the JSON data.  It delegates
to the L<JSON::XS> or L<JSON> module, depending on which you have installed.

=head2 decode_json($json)

This is the internal subroutine that decodes the JSON data.  As per
L<encode_json()>, it delegates the task to the L<JSON::XS> or L<JSON> module.

=head1 AUTHOR

Andy Wardley L<http://wardley.org/>

=head1 COPYRIGHT

Copyright (C) 2005-2009 Andy Wardley. All rights reserved.

=head1 SEE ALSO

L<Badger::Codecs>, L<Badger::Codec>, L<JSON>

=cut

# Local Variables:
# mode: Perl
# perl-indent-level: 4
# indent-tabs-mode: nil
# End:
#
# vim: expandtab shiftwidth=4: