blib/lib/JSON/WithComments.pm

filter: Statementstmt 80.7% 42 / 52 Branchbran 30.0% 3 / 10 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 3 totaltotal 68.6% 57 / 83 SCAR 24.1
CC
9
Cov
70%
import
19.8
comment_style
3.2
get_comment_style
2.0
CRAP 11.2
↑ Summary
1 ###############################################################################
2 #
3 # This file copyright © 2017 by Randy J. Ray, all rights reserved
4 #
5 # See "LICENSE AND COPYRIGHT" in the POD for terms.
6 #
7 ###############################################################################
8 #
9 # Description: Simple support for comments in JSON content.
10 #
11 # Functions: import
12 # comment_style
13 # get_comment_style
14 # decode
15 #
16 # Libraries: JSON
17 #
18 # Global Consts: %PATTERNS
19 #
20 # Environment: None
21 #
22 ###############################################################################
23  
24 package JSON::WithComments;
25  
26 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use base qw(JSON);
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 1 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 1 our $VERSION = '0.002'; # VERSION
34  
35 # These regular expressions are adapted from Regexp::Common::comment.
36  
37 # The length of the regexp for JS multi-line comments triggers this:
38 ## no critic(RegularExpressions::RequireExtendedFormatting)
39 1 my $JS_SINGLE = qr{(?://)(?:[^\n]*)};
40 1 my $JS_MULTI = qr{(?:\/[*])(?:(?:[^*]+|[*](?!\/))*)(?:[*]\/)};
41 1 my $PERL = qr{(?:#)(?:[^\n]*)};
42 1 my %PATTERNS = (
43 perl => qr{(?<!\\)($PERL)},
44 javascript => qr{(?<!\\)($JS_SINGLE|$JS_MULTI)},
45 );
46  
47 # This is the comment-style that will be used if/when an object has not
48 # specified a style. It can be changed in import() with -default_comment_style.
49 # This is also the style that will be used by decode_json.
50 1 my $default_comment_style = 'javascript';
51  
52 # This table is used in lieu of per-object hashkeys, as the object is not a
53 # hashref when the JSON::XS backend is in use.
54 1 my %comment_style;
55  
56 sub import {
57 1 ▶ my ($class, @imports) = @_;
Subroutine import: called CC=6 SCAR=29.9
58  
59 1 my ($index, $style);
60 1 for my $idx (0 .. $#imports) {
61 0 ▶ if ($imports[$idx] eq '-default_comment_style') {
Branch: 1 branch
BranchTrueFalseTotal
if ($imports[$idx] eq "-default_comment_style") 0 0 0
62 0 $index = $idx;
63 0 $style = $imports[$idx + 1];
64 0 last;
65 }
66 }
67 1 ▶ if (defined $index) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $index) 0 1 1
68 0 ▶ $style ||= '(undef)';
Condition0%0/2
$style ||= "(undef)"
l!l
00
MC/DC0%0/1
$style ||= "(undef)"
$style
Truth table0%0/2
A
A= $style
Aresult
11
00
69 0 ▶ if (! $PATTERNS{$style}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($PATTERNS{$style}) 0 0 0
70 0 Carp::croak "Unknown comment style '$style' given as default";
71 }
72 0 $default_comment_style = $style;
73 0 splice @imports, $index, 2;
74 }
75  
76 1 return $class->SUPER::import(@imports);
77 }
78  
79 sub comment_style {
80 1 ▶ my ($self, $value) = @_;
Subroutine comment_style: called CC=3 SCAR=11.8
Pod undocumented
81  
82 1 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 1 0 1
83 1 ▶ if (! $PATTERNS{$value}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($PATTERNS{$value}) 0 1 1
84 0 Carp::croak "Unknown comment_style ($value)";
85 }
86 1 $comment_style{"$self"} = $value;
87 }
88  
89 1 return $self;
90 }
91  
92 sub get_comment_style {
93 4 ▶ my $self = shift;
Subroutine get_comment_style: called CC=2 SCAR=7.1
Pod undocumented
94  
95 4 ▶ return $comment_style{"$self"} || $default_comment_style;
Condition66%2/3
$comment_style{"$self"} || $default_comment_style
l!l&&r!l&&!r
220
MC/DC0%0/2
$comment_style{"$self"} || $default_comment_style
$comment_style{"$self"} $default_comment_style
Truth table66%2/3
A || B
A= $comment_style{"$self"}B= $default_comment_style
ABresult
1X1
011
000
96 }
97  
98 sub decode {
99 2 ▶ my ($self, $text) = @_;
Subroutine decode: called CC=1 SCAR=0.0
Pod undocumented
100  
101 2 my $comment_re = $PATTERNS{$self->get_comment_style};
102 # The JSON module reports errors using the character-offset within the
103 # string as a whole. So rather than deleting comments, replace them with a
104 # string of spaces of the same length. This should mean that any reported
105 # character offsets in the JSON data will still be correct.
106 2 $text =~ s/$comment_re/q{ } x length($1)/ge;
107  
108 2 return $self->SUPER::decode($text);
109 }
110  
111 sub DESTROY {
112 1 ▶ my $self = shift;
Subroutine DESTROY: called CC=1 SCAR=0.0
113  
114 1 delete $comment_style{"$self"};
115  
116 1 return;
117 }
118  
119 1 1;
120  
121 __END__
↑ Summary