3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-10-04 18:38:47 +02:00

Sort JSON keys when saving

This commit is contained in:
Pragmatic Software 2019-06-30 17:00:03 -07:00
parent f7b1551979
commit d87c29a698
3 changed files with 6 additions and 16 deletions

View File

@ -95,7 +95,7 @@ sub save {
$self->{pbot}->{logger}->log("Saving $self->{name} to $filename\n");
my $json = JSON->new;
my $json_text = $json->pretty->utf8->encode($self->{hash});
my $json_text = $json->pretty->canonical->utf8->encode($self->{hash});
open(FILE, "> $filename") or die "Couldn't open $filename: $!\n";
print FILE "$json_text\n";

View File

@ -95,7 +95,7 @@ sub save {
$self->{pbot}->{logger}->log("Saving $self->{name} to $filename\n");
my $json = JSON->new;
my $json_text = $json->pretty->utf8->encode($self->{hash});
my $json_text = $json->pretty->canonical->utf8->encode($self->{hash});
open(FILE, "> $filename") or die "Couldn't open $filename: $!\n";
print FILE "$json_text\n";

View File

@ -162,13 +162,14 @@ sub load_questions {
sub save_questions {
my $self = shift;
my $json = encode_json $self->{questions};
my $json = JSON->new;
my $json_text = $json->pretty->canonical->utf8->encode($self->{questions});
my $filename = exists $self->{loaded_filename} ? $self->{loaded_filename} : $self->{questions_filename};
open my $fh, '>', $filename or do {
$self->{pbot}->{logger}->log("Failed to open Spinach file $filename: $!\n");
return;
};
print $fh "$json\n";
print $fh "$json_text\n";
close $fh;
}
@ -394,18 +395,7 @@ sub spinach_cmd {
}
$question->{$key} = $value;
my $json = encode_json $self->{questions};
my $filename = exists $self->{loaded_filename} ? $self->{loaded_filename} : $self->{questions_filename};
open my $fh, '>', $filename or do {
$self->{pbot}->{logger}->log("Failed to open Spinach file $filename: $!\n");
return;
};
print $fh "$json\n";
close $fh;
$self->load_questions;
$self->save_questions;
return "$nick: Question $id: $key set to $value";
}