lib/FP/JSON.pm

filter: Statementstmt 94.4% 85 / 90 Branchbran 75.0% 15 / 20 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 26 / 26 Podpod 0.0% 0 / 6 totaltotal 89.1% 131 / 147 CC 12 SCAR 10.7
CC
12
Cov
91%
_to_json
9.1
scalar_to_json
4.0
fieldname_to_mint
2.0
CRAP 2.9
« lib/FP/Interface.pm ↑ Summary lib/FP/TransparentLazy.pm »
1 #
2 # Copyright (c) 2021-2022 Christian Jaeger, copying@christianjaeger.ch
3 #
4 # This is free software, offered under either the same terms as perl 5
5 # or the terms of the Artistic License version 2 or the terms of the
6 # MIT License (Expat version). See the file COPYING.md that came
7 # bundled with this file.
8 #
9  
10 =head1 NAME
11  
12 FP::JSON
13  
14 =head1 SYNOPSIS
15  
16 1 ▶ use FP::JSON qw(to_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 my $settings = {
19 output_format => "JSON", # or "Mint"
20 auto_numbers => 1,
21 auto_integers => 0,
22 # converter => sub ($obj) { ... convert to accepted data .. },
23 # pretty => 1, # extra re-parsing step at the end
24 };
25  
26 use FP::List;
27 is to_json([10, list(20,30), {40=> "foo", bar=> 50}], $settings),
28 '[
29 10,
30 [
31 20,
32 30
33 ],
34 {
35 "40": "foo",
36 "bar": 50
37 }
38 ]';
39  
40 $settings->{pretty} = 1;
41 is to_json([10, list(20,30), {40=> "foo", bar=> 50}], $settings),
42 '[
43 10,
44 [
45 20,
46 30
47 ],
48 {
49 "40" : "foo",
50 "bar" : 50
51 }
52 ]
53 ';
54  
55 =head1 DESCRIPTION
56  
57 Currently just provides `to_json` to turn some kinds of data into a
58 JSON or Mint language string. This module will need some work for more
59 serious use.
60  
61 This somewhat consciously is not implemented as a class--nonetheless,
62 the $settings argument to `to_json` is basically $self. Still, isn't
63 it neat how few changes one needs to do from procedural code this way
64 (Ok, all 3 functions now take the settings, though), and there's no
65 need to use a constructor, just bare "data", which is quite en vogue
66 (again) today (e.g. in Clojure, Elixir).
67  
68 =head1 NOTE
69  
70 This is alpha software! Read the status section in the package README
71 or on the L<website|http://functional-perl.org/>.
72  
73 =cut
74  
75 package FP::JSON;
76 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
77 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 6 ▶use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
79 6 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
80 6 ▶use Exporter "import";
Subroutine BEGIN: called CC=1 SCAR=0.0
81  
82 6 our @EXPORT = qw();
83 6 our @EXPORT_OK = qw(to_json);
84 6 our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]);
85  
86 6 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
87 6 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
88 6 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
89 6 ▶use FP::PureArray qw(purearray array_to_purearray);
Subroutine BEGIN: called CC=1 SCAR=0.0
90 6 ▶use Chj::TEST;
Subroutine BEGIN: called CC=1 SCAR=0.0
91 6 ▶use FP::Show;
Subroutine BEGIN: called CC=1 SCAR=0.0
92  
93 29 ▶sub fieldname_to_json($str) {
Subroutine fieldname_to_json: called CC=1 SCAR=0.0
Pod undocumented
94 29 '"' . $str . '"' # XXXX
95 }
96  
97 9 ▶sub fieldname_to_mint($str) {
Subroutine fieldname_to_mint: called CC=2 SCAR=7.1
Pod undocumented
98 10 my $s = lcfirst($str);
99 10 $s =~ s/^\s+//;
100 9 $s =~ s/\s+\z//;
101 9 $s =~ s/[^\w\d_]/_/sg; # XX would mint support e.g. "-" ?
102 9 ▶ if ($s =~ m/^\d/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /^\d/) 0 9 9
103 0 $s = "_$s"; # prepend an underscore if fieldname starts with a
104 # digit; XX needed?
105 }
106 $s
107 9 }
108  
109 1 ▶TEST { fieldname_to_mint "Quadrat 'o'Hara" } 'quadrat__o_Hara';
Subroutine __ANON__: called CC=1 SCAR=0.0
110  
111 6 our $output_formats = {
112 JSON =>
113 { hashmap_pair_op => ": ", fieldname_convert => \&fieldname_to_json, },
114 Mint =>
115 { hashmap_pair_op => " = ", fieldname_convert => \&fieldname_to_mint, },
116 };
117  
118 6 my $json = JSON->new->allow_nonref;
119  
120 46 ▶sub scalar_to_json ($val, $settings) {
Subroutine scalar_to_json: called CC=4 SCAR=13.9
Pod undocumented
121 46 ▶ if ($settings->{auto_numbers} and looks_like_number $val) {
Branch: 1 branch
BranchTrueFalseTotal
if ($settings->{'auto_numbers'} and &looks_like_number($val)) { } 18 28 46
Condition100%3/3
$settings->{"auto_numbers"} and &looks_like_number($val)
!ll&&!rl&&r
181018
MC/DC100%2/2
$settings->{"auto_numbers"} and &looks_like_number($val)
$settings->{"auto_numbers"} &looks_like_number($val)
Truth table100%3/3
A and B
A= $settings->{"auto_numbers"}B= &looks_like_number($val)
ABresult
0X0
100
111
122  
123 # XX *assumes* that mint has the same number syntax as Perl
124 18 ▶ $val =~ s/\.0*\z// if $settings->{auto_integers};
Branch: 1 branch
BranchTrueFalseTotal
if $settings->{"auto_integers"} 5 13 18
125 18 $val
126 } else {
127  
128 # XX mint may *not* use all of the same syntax as JSON. For
129 # now assume it does:
130 28 $json->encode($val)
131 }
132 }
133  
134 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
135 1 my $s = { auto_numbers => 0, auto_integers => 0 };
136 1 scalar_to_json "152.00", $s
137 }
138 6 "\"152.00\"";
139 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
140 1 my $s = { auto_numbers => 1, auto_integers => 0 };
141 1 scalar_to_json "152.00", $s
142 }
143 6 "152.00";
144 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
145 1 my $s = { auto_numbers => 1, auto_integers => 1 };
146 1 scalar_to_json "152.00", $s
147 }
148 6 "152";
149 1 ▶TEST { scalar_to_json "foo bar", {} } "\"foo bar\"";
Subroutine __ANON__: called CC=1 SCAR=0.0
150  
151 #run_tests __PACKAGE__
152  
153 10 ▶sub hashmap_to_json ($hashmap, $settings) {
Subroutine hashmap_to_json: called CC=1 SCAR=0.0
Pod undocumented
154 10 my $output_format = $output_formats->{ $settings->{output_format} };
155 10 my $fieldname_convert = $output_format->{fieldname_convert};
156 10 my $hashmap_pair_op = $output_format->{hashmap_pair_op};
157 "{\n" . purearray(sort keys %$hashmap)->map(
158 36 ▶ sub ($title) {
Subroutine __ANON__: called CC=1 SCAR=0.0
159 36 my $value = $hashmap->{$title};
160 36 $fieldname_convert->($title)
161 . $hashmap_pair_op
162 . _to_json($value, $settings)
163 }
164 10 )->strings_join(",\n")
165 . "\n}"
166 }
167  
168 4 ▶sub sequence_to_json ($l, $settings) {
Subroutine sequence_to_json: called CC=1 SCAR=0.0
Pod undocumented
169 "[\n"
170 4 ▶ . $l->map(sub($v) { _to_json($v, $settings) })->strings_join(",\n")
Subroutine __ANON__: called CC=1 SCAR=0.0
171 . "\n]"
172 }
173  
174 56 ▶sub _to_json ($value, $settings) {
Subroutine _to_json: called CC=7 SCAR=22.0
175 56 ▶ if (defined(my $class = blessed $value)) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $class = builtin::blessed($value))) { } 2 54 56
elsif (my $r = ref $value) { } 12 42 54
176 2 ▶ if ($value->isa("FP::Abstract::Sequence")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value->isa('FP::Abstract::Sequence')) { } 2 0 2
177 2 return sequence_to_json($value->purearray, $settings)
178 } else {
179 0 ▶ if (defined(my $c = $settings->{converter})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $c = $settings->{'converter'})) { } 0 0 0
180 0 return _to_json($c->($value), $settings) # XX tail
181 } else {
182 0 die "to_json: don't know how to map this: " . show($value)
183 }
184 }
185 } elsif (my $r = ref($value)) {
186 12 ▶ if ($r eq "ARRAY") {
Branch: 2 branches
BranchTrueFalseTotal
if ($r eq 'ARRAY') { } 2 10 12
elsif ($r eq 'HASH') { } 10 0 10
187 2 return sequence_to_json(array_to_purearray($value), $settings)
188 } elsif ($r eq "HASH") {
189 10 return hashmap_to_json($value, $settings)
190 }
191 } else {
192 42 return scalar_to_json($value, $settings)
193 }
194 0 die "bug"
195 }
196  
197 10 ▶sub to_json ($value, $settings) {
Subroutine to_json: called CC=2 SCAR=6.9
Pod undocumented
198 10 ▶ if ($settings->{pretty}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($settings->{'pretty'}) { } 1 9 10
199  
200 # Our non-pretty variant is sorted, thus enable canonical
201 # here, too.
202 1 my $json = JSON->new->allow_nonref->pretty->canonical;
203 1 $json->encode($json->decode(_to_json($value, $settings)))
204 } else {
205 9 _to_json($value, $settings)
206 }
207 }
208  
209 6 1
« lib/FP/Interface.pm ↑ Summary lib/FP/TransparentLazy.pm »