【发布时间】:2015-03-26 20:56:55
【问题描述】:
我正在尝试使用 JSON 来保存和读取配置文件。我想选择在配置文件中使用 cmets。与常规 Perl cmets 一样,注释行应以井号 # 开头。
读取配置文件没有问题,但是当我想写回磁盘时,所有的cmets都丢失了。例如:
use feature qw(say);
use strict;
use warnings;
use Data::Dump;
use JSON::XS;
my $json = JSON::XS->new->relaxed->pretty->canonical;
my $str = '
{
# Here we assign a value of 1 to a
"a" : 1,
"b" : {
"c" : 3, # and c should be equal to 3
"d" : 4
}
}
';
my $h = $json->decode($str);
#say $str;
#dd $h;
$h->{b}{a} = 2;
my $new_str = $json->encode($h);
say $new_str;
输出是:
{
"a" : "1",
"b" : {
"a" : 2,
"c" : "3",
"d" : "4"
}
}
而预期的输出是:
{
# Here we assign a value of 1 to a
"a" : 1,
"b" : {
"a" : 2,
"c" : 3, # and c should be equal to 3
"d" : 4
}
}
这可以用 JSON 实现吗,还是有其他更适合的配置文件格式?
【问题讨论】: