| 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.001'; # 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 | sub import { | ||||||||||||||||||||
| 53 | 1 | ▶ | my ($class, @imports) = @_; | ||||||||||||||||||
|
Subroutine
import: called CC=6 SCAR=29.9
| |||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 1 | my ($index, $style); | |||||||||||||||||||
| 56 | 1 | for my $idx (0 .. $#imports) { | |||||||||||||||||||
| 57 | 0 | ▶ | if ($imports[$idx] eq '-default_comment_style') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 58 | 0 | $index = $idx; | |||||||||||||||||||
| 59 | 0 | $style = $imports[$idx + 1]; | |||||||||||||||||||
| 60 | 0 | last; | |||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | } | ||||||||||||||||||||
| 63 | 1 | ▶ | if (defined $index) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 64 | 0 | ▶ | $style ||= '(undef)'; | ||||||||||||||||||
|
Condition0%0/2
$style ||= "(undef)"
MC/DC0%0/1
$style ||= "(undef)"
$style
Truth table0%0/2
A
A= $style
| |||||||||||||||||||||
| 65 | 0 | ▶ | if (! $PATTERNS{$style}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 66 | 0 | Carp::croak "Unknown comment style '$style' given as default"; | |||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | 0 | $default_comment_style = $style; | |||||||||||||||||||
| 69 | 0 | splice @imports, $index, 2; | |||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 1 | return $class->SUPER::import(@imports); | |||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | sub comment_style { | ||||||||||||||||||||
| 76 | 0 | ▶ | my ($self, $value) = @_; | ||||||||||||||||||
|
Subroutine
comment_style: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 0 | ▶ | if (defined $value) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 79 | 0 | ▶ | if (! $PATTERNS{$value}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 80 | 0 | Carp::croak "Unknown comment_style ($value)"; | |||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | 0 | $self->{comment_style} = $value; | |||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 0 | return $self; | |||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | sub get_comment_style { | ||||||||||||||||||||
| 89 | 1 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
get_comment_style: called CC=2 SCAR=10.5
Pod
documented
| |||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 1 | ▶ | return $self->{comment_style} || $default_comment_style; | ||||||||||||||||||
|
Condition0%0/3
$self->{'comment_style'} || $default_comment_style
MC/DC0%0/2
$self->{'comment_style'} || $default_comment_style
$self->{'comment_style'}
$default_comment_style
Truth table0%0/3
A || B
A= $self->{'comment_style'}B= $default_comment_style
| |||||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | sub decode { | ||||||||||||||||||||
| 95 | 0 | ▶ | my ($self, $text) = @_; | ||||||||||||||||||
|
Subroutine
decode: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 0 | my $comment_re = $PATTERNS{$self->get_comment_style}; | |||||||||||||||||||
| 98 | # The JSON module reports errors using the character-offset within the | ||||||||||||||||||||
| 99 | # string as a whole. So rather than deleting comments, replace them with a | ||||||||||||||||||||
| 100 | # string of spaces of the same length. This should mean that any reported | ||||||||||||||||||||
| 101 | # character offsets in the JSON data will still be correct. | ||||||||||||||||||||
| 102 | 0 | $text =~ s/$comment_re/q{ } x length($1)/ge; | |||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 0 | return $self->SUPER::decode($text); | |||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 1 | 1; | |||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | __END__ | ||||||||||||||||||||