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