| 1 | package Dancer2::Serializer::JSON; | ||||||||||||
| 2 | # ABSTRACT: Serializer for handling JSON data | ||||||||||||
| 3 | 28 | $Dancer2::Serializer::JSON::VERSION = '2.2.1'; | |||||||||||
| 4 | 28 | ▶ | use Moo; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 28 | ▶ | use Ref::Util qw< is_plain_arrayref is_plain_hashref >; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 28 | ▶ | use JSON::MaybeXS (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 28 | ▶ | use Encode qw(decode FB_CROAK); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 28 | ▶ | use Scalar::Util 'blessed'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 28 | with 'Dancer2::Core::Role::Serializer'; | |||||||||||
| 11 | |||||||||||||
| 12 | 33 | ▶ | has '+content_type' => ( default => sub {'application/json'} ); | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | |||||||||||||
| 14 | # helpers | ||||||||||||
| 15 | 10 | ▶ | sub from_json { __PACKAGE__->deserialize(@_) } | ||||||||||
|
Subroutine
from_json: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 16 | |||||||||||||
| 17 | 37 | ▶ | sub to_json { __PACKAGE__->serialize(@_) } | ||||||||||
|
Subroutine
to_json: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 18 | |||||||||||||
| 19 | sub decode_json { | ||||||||||||
| 20 | 2 | ▶ | my ( $entity ) = @_; | ||||||||||
|
Subroutine
decode_json: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 21 | |||||||||||||
| 22 | 2 | JSON::MaybeXS::decode_json($entity); | |||||||||||
| 23 | } | ||||||||||||
| 24 | |||||||||||||
| 25 | sub encode_json { | ||||||||||||
| 26 | 2 | ▶ | my ( $entity ) = @_; | ||||||||||
|
Subroutine
encode_json: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 27 | |||||||||||||
| 28 | 2 | JSON::MaybeXS::encode_json(_ensure_characters($entity)); | |||||||||||
| 29 | } | ||||||||||||
| 30 | |||||||||||||
| 31 | # class definition | ||||||||||||
| 32 | sub serialize { | ||||||||||||
| 33 | 95 | ▶ | my ( $self, $entity, $options ) = @_; | ||||||||||
|
Subroutine
serialize: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||
| 34 | |||||||||||||
| 35 | 95 | ▶ | my $config = blessed $self ? $self->config : {}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 36 | 95 | my $strict_utf8 = $config->{strict_utf8}; | |||||||||||
| 37 | 95 | ▶ | $options ||= {}; | ||||||||||
|
Condition100%2/2
$options ||= {}
MC/DC100%1/1
$options ||= {}
$options
Truth table100%2/2
A
A= $options
| |||||||||||||
| 38 | |||||||||||||
| 39 | 95 | foreach (keys %$config) { | |||||||||||
| 40 | 71 | ▶ | $options->{$_} = $config->{$_} unless exists $options->{$_}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 41 | } | ||||||||||||
| 42 | |||||||||||||
| 43 | 95 | $options->{utf8} = 1; | |||||||||||
| 44 | exists $options->{strict_utf8} | ||||||||||||
| 45 | 95 | ▶ | and $strict_utf8 = delete $options->{strict_utf8}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 46 | 95 | $entity = _ensure_characters( $entity, $strict_utf8, $self ); | |||||||||||
| 47 | 94 | JSON::MaybeXS->new($options)->encode($entity); | |||||||||||
| 48 | } | ||||||||||||
| 49 | |||||||||||||
| 50 | sub deserialize { | ||||||||||||
| 51 | 32 | ▶ | my ( $self, $entity, $options ) = @_; | ||||||||||
|
Subroutine
deserialize: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||
| 52 | |||||||||||||
| 53 | 32 | ▶ | $options ||= {}; | ||||||||||
|
Condition50%1/2
$options ||= {}
MC/DC0%0/1
$options ||= {}
$options
Truth table50%1/2
A
A= $options
| |||||||||||||
| 54 | 32 | $options->{utf8} = 1; | |||||||||||
| 55 | 32 | delete $options->{strict_utf8}; | |||||||||||
| 56 | 32 | JSON::MaybeXS->new($options)->decode($entity); | |||||||||||
| 57 | } | ||||||||||||
| 58 | |||||||||||||
| 59 | 28 | my $HAS_UNICODE_UTF8 = eval { require Unicode::UTF8; 1; }; | |||||||||||
| 60 | |||||||||||||
| 61 | sub _valid_utf8 { | ||||||||||||
| 62 | 2 | ▶ | my ($bytes) = @_; | ||||||||||
|
Subroutine
_valid_utf8: called CC=2 SCAR=10.1
| |||||||||||||
| 63 | 2 | ▶ | return Unicode::UTF8::valid_utf8($bytes) if $HAS_UNICODE_UTF8; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 64 | 0 | return eval { decode( 'UTF-8', $bytes, FB_CROAK ); 1 }; | |||||||||||
| 65 | } | ||||||||||||
| 66 | |||||||||||||
| 67 | sub _decode_utf8 { | ||||||||||||
| 68 | 0 | ▶ | my ($bytes) = @_; | ||||||||||
|
Subroutine
_decode_utf8: not called CC=2 SCAR=17.9
| |||||||||||||
| 69 | 0 | ▶ | return Unicode::UTF8::decode_utf8($bytes) if $HAS_UNICODE_UTF8; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 70 | 0 | return decode( 'UTF-8', $bytes ); | |||||||||||
| 71 | } | ||||||||||||
| 72 | |||||||||||||
| 73 | sub _ensure_characters { | ||||||||||||
| 74 | 291 | ▶ | my ( $entity, $strict_utf8, $self ) = @_; | ||||||||||
|
Subroutine
_ensure_characters: called CC=8 SCAR=20.9
| |||||||||||||
| 75 | |||||||||||||
| 76 | 291 | ▶ | return $entity if !defined $entity; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 77 | 290 | ▶ | return _ensure_scalar( $entity, $strict_utf8, $self ) if !ref $entity; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 78 | |||||||||||||
| 79 | 123 | ▶ | if ( is_plain_arrayref($entity) ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 80 | 17 | for my $i ( 0 .. $#{$entity} ) { | |||||||||||
| 81 | 42 | $entity->[$i] = _ensure_characters( $entity->[$i], $strict_utf8, $self ); | |||||||||||
| 82 | } | ||||||||||||
| 83 | 17 | return $entity; | |||||||||||
| 84 | } | ||||||||||||
| 85 | |||||||||||||
| 86 | 106 | ▶ | if ( is_plain_hashref($entity) ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 87 | 105 | my %ret; | |||||||||||
| 88 | 105 | for my $key ( keys %{$entity} ) { | |||||||||||
| 89 | 152 | my $value = $entity->{$key}; | |||||||||||
| 90 | 152 | my $decoded_key = _ensure_scalar( $key, $strict_utf8, $self ); | |||||||||||
| 91 | 152 | my $decoded_value = | |||||||||||
| 92 | _ensure_characters( $value, $strict_utf8, $self ); | ||||||||||||
| 93 | |||||||||||||
| 94 | 151 | ▶ | if ( $decoded_key ne $key ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 95 | 0 | delete $entity->{$key}; | |||||||||||
| 96 | 0 | $ret{$decoded_key} = $decoded_value; | |||||||||||
| 97 | } else { | ||||||||||||
| 98 | 151 | $ret{$key} = $decoded_value; | |||||||||||
| 99 | } | ||||||||||||
| 100 | } | ||||||||||||
| 101 | 104 | return \%ret; | |||||||||||
| 102 | } | ||||||||||||
| 103 | |||||||||||||
| 104 | 1 | return $entity; | |||||||||||
| 105 | } | ||||||||||||
| 106 | |||||||||||||
| 107 | sub _ensure_scalar { | ||||||||||||
| 108 | 319 | ▶ | my ( $value, $strict_utf8, $self ) = @_; | ||||||||||
|
Subroutine
_ensure_scalar: called CC=4 SCAR=13.9
| |||||||||||||
| 109 | |||||||||||||
| 110 | 319 | ▶ | return $value if utf8::is_utf8($value); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 111 | 308 | ▶ | return $value if $value !~ /[\x80-\xFF]/; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 112 | 2 | ▶ | return _decode_utf8($value) if _valid_utf8($value); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 113 | |||||||||||||
| 114 | 2 | _invalid_utf8( $strict_utf8, $self ); | |||||||||||
| 115 | 1 | return $value; | |||||||||||
| 116 | } | ||||||||||||
| 117 | |||||||||||||
| 118 | sub _invalid_utf8 { | ||||||||||||
| 119 | 2 | ▶ | my ( $strict_utf8, $self ) = @_; | ||||||||||
|
Subroutine
_invalid_utf8: called CC=3 SCAR=11.2
| |||||||||||||
| 120 | 2 | my $msg = 'Invalid UTF-8 in JSON data'; | |||||||||||
| 121 | |||||||||||||
| 122 | 2 | ▶ | $strict_utf8 | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 123 | and die "$msg\n"; | ||||||||||||
| 124 | |||||||||||||
| 125 | 1 | ▶ | if ( blessed($self) ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 126 | 1 | $self->log_cb->( warning => "$msg; leaving bytes unchanged" ); | |||||||||||
| 127 | } else { | ||||||||||||
| 128 | 0 | warn "$msg; leaving bytes unchanged\n"; | |||||||||||
| 129 | } | ||||||||||||
| 130 | } | ||||||||||||
| 131 | |||||||||||||
| 132 | 28 | 1; | |||||||||||
| 133 | |||||||||||||
| 134 | __END__ | ||||||||||||