File Coverage

blib/lib/Regexp/Pattern/JSON.pm
Criterion Covered Total %
statement 2 2 100.0
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 3 3 100.0


line stmt bran cond sub pod time code
1             package Regexp::Pattern::JSON;
2              
3             our $DATE = '2018-09-12'; # DATE
4             our $VERSION = '0.004'; # VERSION
5              
6 1     1   66481 use 5.010001;
  1         12  
7              
8             our %RE;
9              
10             $RE{number} = {
11             summary => 'Match a JSON number literal',
12             pat => qr{(?:
13             (
14             -?
15             (?: 0 | [1-9][0-9]* )
16             (?: \. [0-9]+ )?
17             (?: [eE] [-+]? [0-9]+ )?
18             )
19             )}x,
20             examples => [
21             {str=>'12', matches=>1},
22             {str=>'-34', matches=>1},
23             {str=>'1.23', matches=>1},
24             {str=>'-1.23e2', matches=>1},
25             ],
26             };
27              
28             $RE{string} = {
29             summary => 'Match a JSON string literal',
30             pat => qr{(?:
31             "
32             (?:
33             [^\\"]+
34             |
35             \\ [0-7]{1,3}
36             |
37             \\ x [0-9A-Fa-f]{1,2}
38             |
39             \\ ["\\/bfnrt]
40             #|
41             # \\ u [0-9a-fA-f]{4}
42             )*
43             "
44             )}xms,
45             examples => [
46             {str=>q(""), matches=>1},
47             {str=>q(''), matches=>0, summary=>"Single quotes are not string delimiters"},
48             {str=>q("\\n"), matches=>1},
49             {str=>q("contains \\" double quote"), matches=>1},
50             ],
51             };
52              
53             our $define = qr{
54              
55             (?(DEFINE)
56              
57             (?
58             \{\s*
59             (?:
60             (?&KV)
61             \s*
62             (?:,\s* (?&KV))*
63             )?
64             \s*
65             \}
66             )
67              
68             (?
69             (?&STRING)
70             \s*
71             (?::\s* (?&VALUE))
72             )
73              
74             (?
75             \[\s*
76             (?:
77             (?&VALUE)
78             (?:\s*,\s* (?&VALUE))*
79             )?
80             \s*
81             \]
82             )
83              
84             (?
85             \s*
86             (?:
87             (?&STRING)
88             |
89             (?&NUMBER)
90             |
91             (?&OBJECT)
92             |
93             (?&ARRAY)
94             |
95             true
96             |
97             false
98             |
99             null
100             )
101             \s*
102             )
103              
104             (? $RE{string}{pat})
105              
106             (? $RE{number}{pat})
107              
108             ) # DEFINE
109              
110             }xms;
111              
112             $RE{array} = {
113             summary => 'Match a JSON array',
114             pat => qr{(?:
115             (?&ARRAY)
116             $define
117             )}xms,
118             examples => [
119             {str=>q([]), matches=>1},
120             {str=>q([1, true, "abc"]), matches=>1},
121             {str=>q([1), matches=>0, summary=>"Missing closing bracket"},
122             ],
123             };
124              
125             $RE{object} = {
126             summary => 'Match a JSON object (a.k.a. hash/dictionary)',
127             pat => qr{(?:
128             (?&OBJECT)
129             $define
130             )}xms,
131             examples => [
132             {str=>q({}), matches=>1},
133             {str=>q({"a":1}), matches=>1},
134             {str=>q({"a":1), matches=>0, summary=>"Missing closing curly bracket"},
135             {str=>q({a: 1}), matches=>0, summary=>"Unquoted key"},
136             ],
137             };
138              
139             $RE{value} = {
140             summary => 'Match a JSON value',
141             pat => qr{(?:
142             (?&VALUE)
143             $define
144             )}xms,
145             examples => [
146             {str=>q(true), matches=>1},
147             {str=>q([]), matches=>1},
148             {str=>q({}), matches=>1},
149             {str=>q(-1), matches=>1},
150             {str=>q(""), matches=>1},
151             ],
152             };
153              
154             1;
155             # ABSTRACT: Regexp patterns to match JSON
156              
157             __END__