Perlito5X/JSON.pm untested

filter: Statementstmt 0.0% 0 / 65 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 141 CC 28 SCAR 53.2
CC
28
Cov
0%
_decode_json
272.0
_string_loop
182.0
import
2.0
CRAP 203.8
1 package JSON;
2 use Perlito5::JSON;
3 # use strict;
4  
5 sub import {
6 my $pkg = shift;
7 my $callpkg = caller(0);
8 *{ $callpkg . "::encode_json" } = \&encode_json;
9 *{ $callpkg . "::decode_json" } = \&decode_json;
10 return;
11 }
12  
13 sub encode_json {
14 Perlito5::JSON::ast_dumper($_[0]);
15 }
16  
17 sub _string_loop {
18 my $s = "";
19 while (1) {
20 if ($_[0] =~ /\G"[ \t\r\n]*/gc) {
21 # end-string + spaces
22 return $s;
23 }
24 elsif ($_[0] =~ /\G\\([trnfbu\\\/])/gc) {
25 # escape
26 if ($1 eq 't') { $s .= "\t" }
27 elsif ($1 eq 'r') { $s .= "\r" }
28 elsif ($1 eq 'n') { $s .= "\n" }
29 elsif ($1 eq 'f') { $s .= "\f" }
30 elsif ($1 eq 'b') { $s .= "\b" }
31 elsif ($1 eq '\\') { $s .= "\\" }
32 elsif ($1 eq '/') { $s .= "/" }
33 elsif ($1 eq 'u') {
34 $_[0] =~ /\G([0-9a-fA-F]{4})/gc
35 or die "unexpected end of string while parsing JSON string, at character offset " . pos($_[0]);
36 my $uni = hex($1);
37 if ($uni >= 0xD800 && $uni < 0xDC00) {
38 # surrogate pair
39 $_[0] =~ /\G\\u([0-9a-fA-F]{4})/gc
40 or die "unexpected end of string while parsing JSON string, at character offset " . pos($_[0]);
41 my $hi = $uni;
42 my $lo = hex($1);
43 $uni = 0x10000 + ($hi - 0xD800) * 0x400 + ($lo - 0xDC00);
44 }
45 $s .= chr($uni);
46 }
47 }
48 else {
49 die "unexpected end of string while parsing JSON string, at character offset " . pos($_[0]);
50 }
51 if ($_[0] =~ /\G([^"\\]+)/gc) {
52 $s .= $1;
53 }
54 }
55 }
56  
57 sub decode_json {
58 $_[0] =~ /^[ \t\r\n]*/g; # skip spaces + reset pos()
59 &_decode_json;
60 }
61 sub _decode_json {
62 if ($_[0] =~ /\G"([^"\\]*)/gc) {
63 # string
64 return $1 . &_string_loop;
65 }
66 elsif ($_[0] =~ /\G(-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?)[ \t\r\n]*/gc) {
67 # number + spaces
68 return 0+$1;
69 }
70 elsif ($_[0] =~ /\G\[[ \t\r\n]*/gc) {
71 # array
72 my @r;
73 if ($_[0] =~ /\G\][ \t\r\n]*/gc) {
74 # end-array + spaces
75 return \@r;
76 }
77 while (1) {
78 push @r, &_decode_json;
79 if ($_[0] =~ /\G\][ \t\r\n]*/gc) {
80 # end-array + spaces
81 return \@r;
82 }
83 elsif ($_[0] =~ /\G\,[ \t\r\n]*/gc) {
84 # comma + spaces
85 }
86 else {
87 die ", or ] expected while parsing array, at character offset " . pos($_[0]);
88 }
89 }
90 }
91 elsif ($_[0] =~ /\G\{[ \t\r\n]*/gc) {
92 # object
93 my %r;
94 if ($_[0] =~ /\G\}[ \t\r\n]*/gc) {
95 # end-object
96 return \%r;
97 }
98 while (1) {
99 if ($_[0] !~ /\G"([^"\\]*)/gc) {
100 # not string
101 die "unexpected end of string while parsing JSON string, at character offset " . pos($_[0]);
102 }
103 # string
104 my $index = $1 . &_string_loop;
105  
106 # skip colon + spaces
107 if ($_[0] !~ /\G:[ \t\r\n]*/gc) {
108 # not colon
109 die "unexpected end of string while parsing JSON string, at character offset " . pos($_[0]);
110 }
111 $r{$index} = &_decode_json;
112 if ($_[0] =~ /\G\}[ \t\r\n]*/gc) {
113 # end-object
114 return \%r;
115 }
116 elsif ($_[0] =~ /\G\,[ \t\r\n]*/gc) {
117 # comma + spaces
118 }
119 else {
120 die "unexpected end of string while parsing JSON string, at character offset " . pos($_[0]);
121 }
122 }
123 }
124 elsif ($_[0] =~ /\Gfalse[ \t\r\n]*/gc) {
125 # false
126 return 0; # TODO
127 }
128 elsif ($_[0] =~ /\Gtrue[ \t\r\n]*/gc) {
129 # true
130 return 1; # TODO
131 }
132 elsif ($_[0] =~ /\Gnull[ \t\r\n]*/gc) {
133 # null
134 return undef;
135 }
136 else {
137 die "malformed JSON string, neither tag, array, object, number, string or atom, at character offset " . pos($_[0]);
138 }
139 }
140  
141 1;
142  
143 __END__