| 1 | package Data::Record::Serialize::Encode::json; | ||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||
| 3 | # ABSTRACT: encoded a record as JSON | ||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||
| 5 | 2 | ▶ | use v5.12; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 6 | 2 | ▶ | use strict; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 7 | 2 | ▶ | use warnings; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||
| 9 | 2 | ▶ | use Data::Record::Serialize::Error { errors => ['json_backend'] }, -all; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 10 | 2 | ▶ | use Module::Version; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||
| 12 | 2 | ▶ | use Moo::Role; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||
| 14 | 2 | our $VERSION = '2.05'; | |||||||||||||||||||||||
| 15 | 2 | our $JSON; | |||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||
| 17 | 2 | ▶ | BEGIN { | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=4 SCAR=18.6
| |||||||||||||||||||||||||
| 18 | 2 | my $Cpanel_JSON_XS_VERSION = 3.0236; | |||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||
| 20 | # Module::Version doesn't load the code, so avoids loading | ||||||||||||||||||||||||
| 21 | # Cpanel::JSON::XS's version of JSON::PP::Boolean which prevents | ||||||||||||||||||||||||
| 22 | # loading the version provided by JSON::PP if Cpanel::JSON::XS is | ||||||||||||||||||||||||
| 23 | # too old. Symbol::delete_package could be used to remove | ||||||||||||||||||||||||
| 24 | # Cpanel::JSON::XS's version, but it's better not to load it in | ||||||||||||||||||||||||
| 25 | # the first place. | ||||||||||||||||||||||||
| 26 | 2 | $JSON = do { | |||||||||||||||||||||||
| 27 | 2 | ▶ | if ( ( Module::Version::get_version( 'Cpanel::JSON::XS' ) // 0 >= $Cpanel_JSON_XS_VERSION ) ) { | ||||||||||||||||||||||
|
Branch: 2 branches
Condition50%1/2
Module::Version::get_version("Cpanel::JSON::XS") // 0 >= $Cpanel_JSON_XS_VERSION
MC/DC0%0/1
Module::Version::get_version("Cpanel::JSON::XS") // 0 >= $Cpanel_JSON_XS_VERSION
Module::Version::get_version("Cpanel::JSON::XS")
Truth table50%1/2
A
A= Module::Version::get_version("Cpanel::JSON::XS")
| |||||||||||||||||||||||||
| 28 | 2 | require Cpanel::JSON::XS; | |||||||||||||||||||||||
| 29 | 2 | 'Cpanel::JSON::XS'; | |||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||
| 31 | 0 | elsif ( eval { require JSON::PP; 1; } ) { | |||||||||||||||||||||||
| 32 | 0 | 'JSON::PP'; | |||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||
| 34 | else { | ||||||||||||||||||||||||
| 35 | 0 | error( | |||||||||||||||||||||||
| 36 | 'json_backend', | ||||||||||||||||||||||||
| 37 | q{can't find either Cpanel::JSON::XS (>= $Cpanel_JSON_XS_VERSION) or JSON::PP. Please install one of them.}, | ||||||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||
| 40 | }; | ||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||
| 44 | 2 | ▶ | use constant ENCODER_OPTIONS => qw( | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 45 | ascii latin1 utf8 pretty indent space_before space_after canonical | ||||||||||||||||||||||||
| 46 | allow_blessed convert_blessed | ||||||||||||||||||||||||
| 47 | 2 | ); | |||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||
| 49 | 2 | ▶ | use namespace::clean; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||
| 52 | 2 | has [ grep { $_ ne 'utf8' } ENCODER_OPTIONS ] => ( is => 'ro', default => !!0 ); | |||||||||||||||||||||||
| 53 | 2 | has utf8 => ( is => 'ro', default => !!1 ); | |||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||
| 55 | 2 | has _encoder => ( | |||||||||||||||||||||||
| 56 | is => 'rwp', | ||||||||||||||||||||||||
| 57 | init_arg => undef, | ||||||||||||||||||||||||
| 58 | clearer => 1, | ||||||||||||||||||||||||
| 59 | predicate => 1, | ||||||||||||||||||||||||
| 60 | ); | ||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||
| 62 | 2 | has '+numify' => ( is => 'ro', default => 1 ); | |||||||||||||||||||||||
| 63 | 2 | has '+stringify' => ( is => 'ro', default => 1 ); | |||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||
| 65 | 24 | ▶ | sub _needs_eol { 1 } | ||||||||||||||||||||||
|
Subroutine
_needs_eol: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||
| 75 | 1 | ▶ | sub to_bool { $_[1] ? \1 : \0 } | ||||||||||||||||||||||
|
Subroutine
to_bool: called CC=2 SCAR=7.6
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||
| 82 | 23 | ▶ | sub setup { | ||||||||||||||||||||||
|
Subroutine
setup: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||
| 84 | 23 | my $self = shift; | |||||||||||||||||||||||
| 85 | 23 | ▶ | return if $self->_has_encoder; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||
| 87 | 23 | my $json = $JSON->new; | |||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||
| 89 | 23 | for my $option ( ENCODER_OPTIONS ) { | |||||||||||||||||||||||
| 90 | 230 | ▶ | next unless $self->$option; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 91 | 41 | $json = $json->$option( $self->$option ); | |||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||
| 94 | 23 | $self->_set__encoder( $json ); | |||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||
| 103 | 24 | ▶ | sub encode { $_[0]->_encoder->encode( $_[1] ) } | ||||||||||||||||||||||
|
Subroutine
encode: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||
| 105 | 2 | with 'Data::Record::Serialize::Role::Encode'; | |||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||
| 107 | 2 | 1; | |||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||
| 109 | # | ||||||||||||||||||||||||
| 110 | # This file is part of Data-Record-Serialize | ||||||||||||||||||||||||
| 111 | # | ||||||||||||||||||||||||
| 112 | # This software is Copyright (c) 2017 by Smithsonian Astrophysical Observatory. | ||||||||||||||||||||||||
| 113 | # | ||||||||||||||||||||||||
| 114 | # This is free software, licensed under: | ||||||||||||||||||||||||
| 115 | # | ||||||||||||||||||||||||
| 116 | # The GNU General Public License, Version 3, June 2007 | ||||||||||||||||||||||||
| 117 | # | ||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||
| 119 | __END__ | ||||||||||||||||||||||||