| 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
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$settings->{"auto_numbers"} and &looks_like_number($val)
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)
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 176 | 2 | ▶ | if ($value->isa("FP::Abstract::Sequence")) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | 2 | return sequence_to_json($value->purearray, $settings) | |||||||||||||||||||||||||||
| 178 | } else { | ||||||||||||||||||||||||||||
| 179 | 0 | ▶ | if (defined(my $c = $settings->{converter})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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 | |||||||||||||||||||||||||||