File Coverage

blib/lib/EntityModel/Definition/JSON.pm
Criterion Covered Total %
statement 16 35 45.7
branch 0 10 0.0
condition n/a
subroutine 4 7 57.1
pod 5 5 100.0
total 25 57 43.8


line stmt bran cond sub pod time code
1             package EntityModel::Definition::JSON;
2             {
3             $EntityModel::Definition::JSON::VERSION = '0.102';
4             }
5             use EntityModel::Class {
6 3         34 _isa => [qw{EntityModel::Definition}],
7 3     3   1533 };
  3         7  
8              
9             =head1 NAME
10              
11             EntityModel::Definition::JSON - definition support for L
12              
13             =head1 VERSION
14              
15             version 0.102
16              
17             =head1 SYNOPSIS
18              
19             See L.
20              
21             =head1 DESCRIPTION
22              
23             See L.
24              
25             =head1 METHODS
26              
27             =cut
28              
29 3     3   4642 use JSON::XS;
  3         17678  
  3         1665  
30              
31             =head2 load_file
32              
33             =cut
34              
35             sub load_file {
36 0     0 1 0 my $self = shift;
37 0         0 my $path = shift;
38              
39 0 0       0 open my $fh, '<:encoding(utf-8)', $path or die "Failed to open $path - $!";
40 0         0 my $string = do { local $/; <$fh> };
  0         0  
  0         0  
41 0 0       0 close $fh or die "Failed to close $path - $!";
42 0         0 return $self->parse($string);
43             }
44              
45             =head2 load_string
46              
47             =cut
48              
49             sub load_string {
50 2     2 1 5 my $self = shift;
51 2         4 my $string = shift;
52 2         7 logDebug("Load string [%s]", $string);
53 2         54 return $self->parse($string);
54             }
55              
56             =head2 save_file
57              
58             Write output to a file.
59              
60             =cut
61              
62             sub save_file {
63 0     0 1 0 my $self = shift;
64 0         0 my %args = @_;
65 0 0       0 my $path = delete $args{path} or die "No path provided";
66              
67 0         0 my $data = JSON::XS->new->encode($args{structure});
68 0 0       0 open my $fh, '>:encoding(utf-8)', $path or die "Failed to open $path - $!";
69 0         0 $fh->print($data);
70 0 0       0 close $fh or die "Failed to close $path - $!";
71 0         0 return $self;
72             }
73              
74             =head2 save_string
75              
76             Return output as a scalar.
77              
78             =cut
79              
80             sub save_string {
81 0     0 1 0 my $self = shift;
82 0         0 my %args = @_;
83              
84 0         0 return JSON::XS->new->pretty->utf8->encode($self->structure_from_model($args{model}));
85             }
86              
87             =head2 parse
88              
89             Create and parse the L object.
90              
91             =cut
92              
93             sub parse {
94 2     2 1 4 my $self = shift;
95 2         4 my $string = shift;
96 2         21 my $json = JSON::XS->new;
97 2         47 my $def = $json->decode($string);
98 2         7 logDebug($def);
99 2         53 return $def;
100             }
101              
102             1;
103              
104             __END__