line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# $Id$ |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# file::json Brik |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
package Metabrik::File::Json; |
7
|
1
|
|
|
1
|
|
868
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
29
|
|
8
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
26
|
|
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
5
|
use base qw(Metabrik::File::Text); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
551
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub brik_properties { |
13
|
|
|
|
|
|
|
return { |
14
|
0
|
|
|
0
|
1
|
|
revision => '$Revision$', |
15
|
|
|
|
|
|
|
tags => [ qw(unstable) ], |
16
|
|
|
|
|
|
|
author => 'GomoR ', |
17
|
|
|
|
|
|
|
license => 'http://opensource.org/licenses/BSD-3-Clause', |
18
|
|
|
|
|
|
|
attributes => { |
19
|
|
|
|
|
|
|
input => [ qw(file) ], |
20
|
|
|
|
|
|
|
output => [ qw(file) ], |
21
|
|
|
|
|
|
|
encoding => [ qw(utf8|ascii) ], |
22
|
|
|
|
|
|
|
overwrite => [ qw(0|1) ], |
23
|
|
|
|
|
|
|
use_utf8 => [ qw(0|1) ], |
24
|
|
|
|
|
|
|
_sj => [ qw(INTERNAL) ], |
25
|
|
|
|
|
|
|
}, |
26
|
|
|
|
|
|
|
attributes_default => { |
27
|
|
|
|
|
|
|
use_utf8 => 1, |
28
|
|
|
|
|
|
|
overwrite => 1, |
29
|
|
|
|
|
|
|
}, |
30
|
|
|
|
|
|
|
commands => { |
31
|
|
|
|
|
|
|
read => [ qw(input_file|OPTIONAL) ], |
32
|
|
|
|
|
|
|
read_next => [ qw(input_file|OPTIONAL) ], |
33
|
|
|
|
|
|
|
write => [ qw($json_hash output_file|OPTIONAL) ], |
34
|
|
|
|
|
|
|
is_valid => [ qw(input_file|OPTIONAL) ], |
35
|
|
|
|
|
|
|
}, |
36
|
|
|
|
|
|
|
require_modules => { |
37
|
|
|
|
|
|
|
'Metabrik::File::Write' => [ ], |
38
|
|
|
|
|
|
|
'Metabrik::String::Json' => [ ], |
39
|
|
|
|
|
|
|
}, |
40
|
|
|
|
|
|
|
}; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub brik_use_properties { |
44
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
return { |
47
|
0
|
|
0
|
|
|
|
attributes_default => { |
48
|
|
|
|
|
|
|
encoding => defined($self->global) && $self->global->encoding || 'utf8', |
49
|
|
|
|
|
|
|
}, |
50
|
|
|
|
|
|
|
}; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub brik_init { |
54
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
55
|
|
|
|
|
|
|
|
56
|
0
|
0
|
|
|
|
|
my $sj = Metabrik::String::Json->new_from_brik_init($self) or return; |
57
|
0
|
|
|
|
|
|
$self->_sj($sj); |
58
|
|
|
|
|
|
|
|
59
|
0
|
|
|
|
|
|
return $self->SUPER::brik_init; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub read { |
63
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
64
|
0
|
|
|
|
|
|
my ($input) = @_; |
65
|
|
|
|
|
|
|
|
66
|
0
|
|
0
|
|
|
|
$input ||= $self->input; |
67
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('read', $input) or return; |
68
|
|
|
|
|
|
|
|
69
|
0
|
|
|
|
|
|
my $sj = $self->_sj; |
70
|
0
|
|
|
|
|
|
$sj->use_utf8($self->use_utf8); |
71
|
|
|
|
|
|
|
|
72
|
0
|
0
|
|
|
|
|
my $data = $self->SUPER::read($input) or return; |
73
|
|
|
|
|
|
|
|
74
|
0
|
|
|
|
|
|
return $sj->decode($data); |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub read_next { |
78
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
79
|
0
|
|
|
|
|
|
my ($input) = @_; |
80
|
|
|
|
|
|
|
|
81
|
0
|
|
0
|
|
|
|
$input ||= $self->input; |
82
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('read_next', $input) or return; |
83
|
|
|
|
|
|
|
|
84
|
0
|
|
|
|
|
|
my $sj = $self->_sj; |
85
|
0
|
|
|
|
|
|
$sj->use_utf8($self->use_utf8); |
86
|
|
|
|
|
|
|
|
87
|
0
|
0
|
|
|
|
|
my $data = $self->SUPER::read_line($input) or return; |
88
|
|
|
|
|
|
|
|
89
|
0
|
|
|
|
|
|
return $sj->decode($data); |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub write { |
93
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
94
|
0
|
|
|
|
|
|
my ($json_hash, $output) = @_; |
95
|
|
|
|
|
|
|
|
96
|
0
|
|
0
|
|
|
|
$output ||= $self->output; |
97
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('write', $json_hash) or return; |
98
|
0
|
0
|
|
|
|
|
$self->brik_help_run_invalid_arg('write', $json_hash, 'ARRAY', 'HASH') |
99
|
|
|
|
|
|
|
or return; |
100
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('write', $output) or return; |
101
|
|
|
|
|
|
|
|
102
|
0
|
|
|
|
|
|
my $sj = $self->_sj; |
103
|
0
|
|
|
|
|
|
$sj->use_utf8($self->use_utf8); |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
# Always make it an ARRAY |
106
|
0
|
0
|
|
|
|
|
$json_hash = ref($json_hash) eq 'ARRAY' ? $json_hash : [ $json_hash ]; |
107
|
|
|
|
|
|
|
|
108
|
0
|
|
|
|
|
|
my @data = (); |
109
|
0
|
|
|
|
|
|
for my $this (@$json_hash) { |
110
|
0
|
0
|
|
|
|
|
my $data = $sj->encode($this) or next; |
111
|
0
|
|
|
|
|
|
push @data, $data; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
0
|
0
|
|
|
|
|
$self->SUPER::write(\@data, $output) or return; |
115
|
|
|
|
|
|
|
|
116
|
0
|
|
|
|
|
|
return $output; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
sub is_valid { |
120
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
121
|
0
|
|
|
|
|
|
my ($input) = @_; |
122
|
|
|
|
|
|
|
|
123
|
0
|
|
0
|
|
|
|
$input ||= $self->input; |
124
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('is_valid', $input) or return; |
125
|
|
|
|
|
|
|
|
126
|
0
|
0
|
|
|
|
|
my $data = $self->SUPER::read($input) or return; |
127
|
|
|
|
|
|
|
|
128
|
0
|
|
|
|
|
|
my $sj = $self->_sj; |
129
|
0
|
|
|
|
|
|
$sj->use_utf8($self->use_utf8); |
130
|
|
|
|
|
|
|
|
131
|
0
|
|
|
|
|
|
return $sj->is_valid($data); |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
1; |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
__END__ |