| 1 | 113 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 2 | 113 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | |||||||||||||
| 4 | package Jifty::JSON; | ||||||||||||
| 5 | |||||||||||||
| 6 | 113 | ▶ | use base 'Exporter'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 113 | our @EXPORT_OK = qw/jsonToObj objToJson decode_json encode_json/; | |||||||||||
| 8 | |||||||||||||
| 9 | 113 | ▶ | use Carp qw//; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 113 | ▶ | use JSON qw/ -support_by_pp -no_export /; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | |||||||||||||
| 12 | =head1 NAME | ||||||||||||
| 13 | |||||||||||||
| 14 | Jifty::JSON -- Wrapper around L<JSON> | ||||||||||||
| 15 | |||||||||||||
| 16 | =head1 SYNOPSIS | ||||||||||||
| 17 | |||||||||||||
| 18 | use Jifty::JSON qw/decode_json encode_json/; | ||||||||||||
| 19 | |||||||||||||
| 20 | my $obj = decode_json(q! { "x": "1", "y": "2", "z": "3" } !); | ||||||||||||
| 21 | my $json = encode_json($obj); | ||||||||||||
| 22 | |||||||||||||
| 23 | =head1 DESCRIPTION | ||||||||||||
| 24 | |||||||||||||
| 25 | Provides a thin wrapper around the L<JSON> 2.xx library, which provides a | ||||||||||||
| 26 | frontend for L<JSON::XS> and L<JSON::PP>. | ||||||||||||
| 27 | |||||||||||||
| 28 | This module used to wrap L<JSON::Syck> and L<JSON> 1.xx with special-casing for | ||||||||||||
| 29 | outputting JSON with single quoted values. Single quotes make it easy to | ||||||||||||
| 30 | simply plop JSON into HTML attributes but are in violation of the JSON spec | ||||||||||||
| 31 | which mandates only double quoted strings. | ||||||||||||
| 32 | |||||||||||||
| 33 | The old behavior is now unsupported and it is recommended that you simply HTML | ||||||||||||
| 34 | escape your entire blob of JSON if you are sticking it in an HTML attribute. | ||||||||||||
| 35 | You can use L<Jifty-E<gt>web-E<gt>escape()|Jifty::Web/escape> to properly | ||||||||||||
| 36 | escape problematic characters for HTML. | ||||||||||||
| 37 | |||||||||||||
| 38 | =head1 FUNCTIONS | ||||||||||||
| 39 | |||||||||||||
| 40 | =head2 decode_json JSON, [ARGUMENT HASHREF] | ||||||||||||
| 41 | |||||||||||||
| 42 | =head2 encode_json JSON, [ARGUMENT HASHREF] | ||||||||||||
| 43 | |||||||||||||
| 44 | These functions are just like L<JSON>'s, except that you can pass options to | ||||||||||||
| 45 | them like you can with L<JSON>'s C<from_json> and C<to_json> functions. | ||||||||||||
| 46 | |||||||||||||
| 47 | By default they encode/decode using UTF8 (like L<JSON>'s functions of the same | ||||||||||||
| 48 | name), but you can turn that off by passing C<utf8 =E<gt> 0> in the | ||||||||||||
| 49 | options. The L<allow_nonref|JSON/allow_nonref> flag is also enabled for | ||||||||||||
| 50 | backwards compatibility with earlier versions of this module. It allows | ||||||||||||
| 51 | encoding/decoding of values that are not references. | ||||||||||||
| 52 | |||||||||||||
| 53 | L<JSON> is imported with the C<-support_by_pp> flag in order to support all | ||||||||||||
| 54 | options that L<JSON::PP> provides when using L<JSON::XS> as the backend. If | ||||||||||||
| 55 | you are concerned with speed, be careful what options you specify as it may | ||||||||||||
| 56 | cause the pure Perl backend to be used. Read L<JSON/JSON::PP SUPPORT METHODS> | ||||||||||||
| 57 | for more information. | ||||||||||||
| 58 | |||||||||||||
| 59 | =cut | ||||||||||||
| 60 | |||||||||||||
| 61 | 0 | ▶ | sub decode_json { | ||||||||||
|
Subroutine
decode_json: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 62 | 0 | ▶ | JSON::from_json( $_[0], { utf8 => 1, allow_nonref => 1, %{$_[1] || {}} } ); | ||||||||||
|
Condition0%0/2
$_[1] || {}
MC/DC0%0/1
$_[1] || {}
$_[1]
Truth table0%0/2
A
A= $_[1]
| |||||||||||||
| 63 | } | ||||||||||||
| 64 | |||||||||||||
| 65 | 0 | ▶ | sub encode_json { | ||||||||||
|
Subroutine
encode_json: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 66 | 0 | ▶ | JSON::to_json( $_[0], { utf8 => 1, allow_nonref => 1, %{$_[1] || {}} } ); | ||||||||||
|
Condition0%0/2
$_[1] || {}
MC/DC0%0/1
$_[1] || {}
$_[1]
Truth table0%0/2
A
A= $_[1]
| |||||||||||||
| 67 | } | ||||||||||||
| 68 | |||||||||||||
| 69 | =head1 DEPRECATED FUNCTIONS | ||||||||||||
| 70 | |||||||||||||
| 71 | =head2 jsonToObj JSON, [ARGUMENTS] | ||||||||||||
| 72 | |||||||||||||
| 73 | =head2 objToJson JSON, [ARGUMENTS] | ||||||||||||
| 74 | |||||||||||||
| 75 | These functions are deprecated and provided for backwards compatibility. They | ||||||||||||
| 76 | wrap the appropriate function above, but L<Carp/croak> if you try to set the | ||||||||||||
| 77 | C<singlequote> option. | ||||||||||||
| 78 | |||||||||||||
| 79 | =cut | ||||||||||||
| 80 | |||||||||||||
| 81 | 0 | ▶ | sub jsonToObj { | ||||||||||
|
Subroutine
jsonToObj: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 82 | 0 | ▶ | my $args = $_[1] || {}; | ||||||||||
|
Condition0%0/2
$_[1] || {}
MC/DC0%0/1
$_[1] || {}
$_[1]
Truth table0%0/2
A
A= $_[1]
| |||||||||||||
| 83 | Carp::croak("Attempted to set 'singlequote' option, but it is no longer supported.". | ||||||||||||
| 84 | " You may need to HTML escape the resulting JSON.". | ||||||||||||
| 85 | " Please read the POD of Jifty::JSON and fix your code.") | ||||||||||||
| 86 | 0 | ▶ | if exists $args->{'singlequote'}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 87 | 0 | decode_json( @_ ); | |||||||||||
| 88 | } | ||||||||||||
| 89 | |||||||||||||
| 90 | 0 | ▶ | sub objToJson { | ||||||||||
|
Subroutine
objToJson: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 91 | 0 | ▶ | my $args = $_[1] || {}; | ||||||||||
|
Condition0%0/2
$_[1] || {}
MC/DC0%0/1
$_[1] || {}
$_[1]
Truth table0%0/2
A
A= $_[1]
| |||||||||||||
| 92 | Carp::croak("Attempted to set 'singlequote' option, but it is no longer supported.". | ||||||||||||
| 93 | " You may need to HTML escape the resulting JSON.". | ||||||||||||
| 94 | " Please read the POD of Jifty::JSON and fix your code.") | ||||||||||||
| 95 | 0 | ▶ | if exists $args->{'singlequote'}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 96 | 0 | encode_json( @_ ); | |||||||||||
| 97 | } | ||||||||||||
| 98 | |||||||||||||
| 99 | =head1 LICENSE | ||||||||||||
| 100 | |||||||||||||
| 101 | Jifty is Copyright 2005-2010 Best Practical Solutions, LLC. | ||||||||||||
| 102 | Jifty is distributed under the same terms as Perl itself. | ||||||||||||
| 103 | |||||||||||||
| 104 | =cut | ||||||||||||
| 105 | |||||||||||||
| 106 | 113 | 1; | |||||||||||