line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# This file is part of Config-Model |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# This software is Copyright (c) 2005-2022 by Dominique Dumont. |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
# This is free software, licensed under: |
7
|
|
|
|
|
|
|
# |
8
|
|
|
|
|
|
|
# The GNU Lesser General Public License, Version 2.1, February 1999 |
9
|
|
|
|
|
|
|
# |
10
|
|
|
|
|
|
|
package Config::Model::Backend::Json 2.153; # TRIAL |
11
|
|
|
|
|
|
|
|
12
|
1
|
|
|
1
|
|
7
|
use Carp; |
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
116
|
|
13
|
1
|
|
|
1
|
|
12
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
21
|
|
14
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
34
|
|
15
|
1
|
|
|
1
|
|
7
|
use Config::Model::Exception; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
21
|
|
16
|
1
|
|
|
1
|
|
5
|
use File::Path; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
52
|
|
17
|
1
|
|
|
1
|
|
7
|
use Log::Log4perl qw(get_logger :levels); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
12
|
|
18
|
|
|
|
|
|
|
|
19
|
1
|
|
|
1
|
|
186
|
use base qw/Config::Model::Backend::Any/; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
109
|
|
20
|
1
|
|
|
1
|
|
6
|
use JSON; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
8
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
my $logger = get_logger("Backend::Json"); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub read { |
25
|
2
|
|
|
2
|
1
|
8
|
my $self = shift; |
26
|
2
|
|
|
|
|
20
|
my %args = @_; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
# args is: |
29
|
|
|
|
|
|
|
# object => $obj, # Config::Model::Node object |
30
|
|
|
|
|
|
|
# root => './my_test', # fake root directory, userd for tests |
31
|
|
|
|
|
|
|
# config_dir => /etc/foo', # absolute path |
32
|
|
|
|
|
|
|
# file => 'foo.conf', # file name |
33
|
|
|
|
|
|
|
# file_path => './my_test/etc/foo/foo.conf' |
34
|
|
|
|
|
|
|
# check => yes|no|skip |
35
|
|
|
|
|
|
|
|
36
|
2
|
50
|
|
|
|
14
|
return 0 unless defined $args{file_path}->exists; # no file to read |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# load Json file |
39
|
2
|
|
|
|
|
65
|
my $json = $args{file_path}->slurp_utf8; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
# convert to perl data |
42
|
2
|
|
|
|
|
474
|
my $perl_data = decode_json $json ; |
43
|
2
|
50
|
|
|
|
9
|
if ( not defined $perl_data ) { |
44
|
0
|
|
|
|
|
0
|
$logger->warn("No data found in Json file $args{file_path}"); |
45
|
0
|
|
|
|
|
0
|
return 1; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# load perl data in tree |
49
|
2
|
|
50
|
|
|
32
|
$self->{node}->load_data( data => $perl_data, check => $args{check} || 'yes' ); |
50
|
2
|
|
|
|
|
16
|
return 1; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub write { |
54
|
1
|
|
|
1
|
1
|
3
|
my $self = shift; |
55
|
1
|
|
|
|
|
6
|
my %args = @_; |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
# args is: |
58
|
|
|
|
|
|
|
# object => $obj, # Config::Model::Node object |
59
|
|
|
|
|
|
|
# root => './my_test', # fake root directory, userd for tests |
60
|
|
|
|
|
|
|
# config_dir => /etc/foo', # absolute path |
61
|
|
|
|
|
|
|
# file => 'foo.conf', # file name |
62
|
|
|
|
|
|
|
# file_path => './my_test/etc/foo/foo.conf' |
63
|
|
|
|
|
|
|
# check => yes|no|skip |
64
|
|
|
|
|
|
|
|
65
|
1
|
|
|
|
|
18
|
my $perl_data = $self->{node}->dump_as_data( full_dump => $args{full_dump} ); |
66
|
1
|
|
|
|
|
12
|
my $json = to_json( $perl_data, { pretty => 1 } ); |
67
|
|
|
|
|
|
|
|
68
|
1
|
|
|
|
|
90
|
$args{file_path}->spew_utf8($json); |
69
|
|
|
|
|
|
|
|
70
|
1
|
|
|
|
|
880
|
return 1; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
1; |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
# ABSTRACT: Read and write config as a JSON data structure |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
__END__ |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=pod |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=encoding UTF-8 |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=head1 NAME |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
Config::Model::Backend::Json - Read and write config as a JSON data structure |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head1 VERSION |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
version 2.153 |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=head1 SYNOPSIS |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
use Config::Model ; |
94
|
|
|
|
|
|
|
use Data::Dumper ; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
# define configuration tree object |
97
|
|
|
|
|
|
|
my $model = Config::Model->new ; |
98
|
|
|
|
|
|
|
$model ->create_config_class ( |
99
|
|
|
|
|
|
|
name => "MyClass", |
100
|
|
|
|
|
|
|
element => [ |
101
|
|
|
|
|
|
|
[qw/foo bar/] => { |
102
|
|
|
|
|
|
|
type => 'leaf', |
103
|
|
|
|
|
|
|
value_type => 'string' |
104
|
|
|
|
|
|
|
}, |
105
|
|
|
|
|
|
|
baz => { |
106
|
|
|
|
|
|
|
type => 'hash', |
107
|
|
|
|
|
|
|
index_type => 'string' , |
108
|
|
|
|
|
|
|
cargo => { |
109
|
|
|
|
|
|
|
type => 'leaf', |
110
|
|
|
|
|
|
|
value_type => 'string', |
111
|
|
|
|
|
|
|
}, |
112
|
|
|
|
|
|
|
}, |
113
|
|
|
|
|
|
|
], |
114
|
|
|
|
|
|
|
rw_config => { |
115
|
|
|
|
|
|
|
backend => 'Json' , |
116
|
|
|
|
|
|
|
config_dir => '/tmp', |
117
|
|
|
|
|
|
|
file => 'foo.json', |
118
|
|
|
|
|
|
|
auto_create => 1, |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
) ; |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
my $inst = $model->instance(root_class_name => 'MyClass' ); |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
my $root = $inst->config_root ; |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
my $steps = 'foo=yada bar="bla bla" baz:en=hello |
127
|
|
|
|
|
|
|
baz:fr=bonjour baz:hr="dobar dan"'; |
128
|
|
|
|
|
|
|
$root->load( steps => $steps ) ; |
129
|
|
|
|
|
|
|
$inst->write_back ; |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
Now, C</tmp/foo.yml> contains: |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
{ |
134
|
|
|
|
|
|
|
"bar" : "bla bla", |
135
|
|
|
|
|
|
|
"foo" : "yada", |
136
|
|
|
|
|
|
|
"baz" : { |
137
|
|
|
|
|
|
|
"hr" : "dobar dan", |
138
|
|
|
|
|
|
|
"en" : "hello", |
139
|
|
|
|
|
|
|
"fr" : "bonjour" |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=head1 DESCRIPTION |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
This module is used directly by L<Config::Model> to read or write the |
146
|
|
|
|
|
|
|
content of a configuration tree written with Json syntax in |
147
|
|
|
|
|
|
|
C<Config::Model> configuration tree. |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
Note that undefined values are skipped for list element. I.e. if a |
150
|
|
|
|
|
|
|
list element contains C<('a',undef,'b')>, the data structure only |
151
|
|
|
|
|
|
|
contains C<'a','b'>. |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=head1 CONSTRUCTOR |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=head2 new |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
Parameters: C<< ( node => $node_obj, name => 'Json' ) >> |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
Inherited from L<Config::Model::Backend::Any>. The constructor is |
160
|
|
|
|
|
|
|
called by L<Config::Model::BackendMgr>. |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=head2 read |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
Of all parameters passed to this read call-back, only C<file_path> is |
165
|
|
|
|
|
|
|
used. This parameter must be a L<Path::Tiny>. |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
When a file is read, C<read> returns 1. |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=head2 write |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
Of all parameters passed to this write call-back, only C<file_path> is |
172
|
|
|
|
|
|
|
used. This parameter must be L<Path::Tiny> object. |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
C<write> returns 1. |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=head1 AUTHOR |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
Dominique Dumont, (ddumont at cpan dot org) |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=head1 SEE ALSO |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
L<Config::Model>, |
183
|
|
|
|
|
|
|
L<Config::Model::BackendMgr>, |
184
|
|
|
|
|
|
|
L<Config::Model::Backend::Any>, |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=head1 AUTHOR |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
Dominique Dumont |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
This software is Copyright (c) 2005-2022 by Dominique Dumont. |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
This is free software, licensed under: |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
The GNU Lesser General Public License, Version 2.1, February 1999 |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=cut |