JSON.pm

filter: Statementstmt 100% 41 / 41 Branchbran 62.5% 5 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 100% 3 / 3 totaltotal 95.2% 60 / 63 CC 5 SCAR 5.1
CC
5
Cov
94%
load
3.0
from_json
2.0
codec
2.0
CRAP 1.7
« JSON/GeometryCollection.pm ↑ Summary JSON/Role/ToJson.pm »
1 package Geo::JSON;
2  
3 7 our $VERSION = '0.007';
4  
5 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 7 ▶use JSON qw/ decode_json /;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use List::Util qw/ first /;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 7 ▶use constant GEOMETRY_OBJECTS => [
Subroutine BEGIN: called CC=1 SCAR=0.0
13 qw/ Point MultiPoint LineString MultiLineString Polygon MultiPolygon GeometryCollection /
14 7 ];
15 7 ▶use constant GEOJSON_OBJECTS => [ #
Subroutine BEGIN: called CC=1 SCAR=0.0
16 7 @{ +GEOMETRY_OBJECTS }, qw/ Feature FeatureCollection /
17 7 ];
18  
19 7 our $json = JSON->new->utf8->convert_blessed(1);
20  
21 32 ▶sub from_json {
Subroutine from_json: called CC=2 SCAR=7.0
Pod documented
22 32 my ( $class, $json ) = @_;
23  
24 32 my $data = decode_json($json);
25  
26 32 ▶ croak "from_json requires a JSON object (hashref)"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" 0 32 32
27 unless ref $data eq 'HASH';
28  
29 32 return $class->load($data);
30 }
31  
32 42 ▶sub load {
Subroutine load: called CC=3 SCAR=11.0
Pod documented
33 42 my ( $class, $data ) = @_;
34  
35 my $type = delete $data->{type}
36 42 ▶ or croak "Invalid JSON data: no type specified";
Branch: 1 branch
BranchTrueFalseTotal
my $type = delete $data->{"type"} or croak("Invalid JSON data: no type specified") 42 0 42
37  
38 42 my $geo_json_class = 'Geo::JSON::' . $type;
39  
40 croak "Invalid type '$type'"
41 42 ▶ unless first { $type eq $_ } @{ +GEOJSON_OBJECTS };
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless &first(sub { $type eq $_; } , @{["Point","MultiPoint","LineString","MultiLineString","Polygon","MultiPolygon","GeometryCollection","Feature","FeatureCollection"];}) 1 41 42
42  
43 41 eval "require $geo_json_class";
44  
45 41 return $geo_json_class->new($data);
46 }
47  
48 22 ▶sub codec {
Subroutine codec: called CC=2 SCAR=7.0
Pod documented
49 22 my $class = shift;
50  
51 22 my $orig = $json;
52 22 ▶ $json = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 22 22
53  
54 22 return $orig;
55 }
56  
57 7 1;
58  
59 __END__
« JSON/GeometryCollection.pm ↑ Summary JSON/Role/ToJson.pm »