added JSON export format:

Starting with version 1.4 JSON export format will be supported but
not be enabled by default. However a deprecation notice will appear if
a user still uses YAML format. YAML support will be removed in version
1.5.

Fixes https://github.com/TLINDEN/note/issues/10.
This commit is contained in:
2023-08-13 19:05:29 +02:00
parent 1b842625ed
commit 37e73950da
6 changed files with 431 additions and 344 deletions

711
Changelog

File diff suppressed because it is too large Load Diff

9
README
View File

@@ -1,4 +1,4 @@
note 1.3.26 by T.v.Dein (09/02/2015)
note 1.4.0 by T.v.Dein (13/08/2023)
=======================================
Introduction
@@ -104,6 +104,11 @@ You need the following things:
you want to use the auto-completion and history functionality.
o Config::General if you want to use the NOTEDB::general
backend.
o YAML is needed to create backups using -D. Please note,
that this format is deprecated starting with 1.4.0. The
Support will be removed in 1.5.0. Please switch to JSON
format as soon as possible, either by using the -j
commandline option or the UseJSON configuration value.
Installation
@@ -207,4 +212,4 @@ and I'll add you.
Last changed
============
09/02/2015
13/08/2023

View File

@@ -1 +1 @@
1.3.26
1.4.0

View File

@@ -1,7 +1,7 @@
#!/usr/bin/perl
#
# note - console notes management with database and encryption support.
# Copyright (C) 1999-2017 T.v.Dein (see README for details!)
# Copyright (C) 1999-2023 T.v.Dein (see README for details!)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
@@ -38,6 +38,7 @@ use Getopt::Long;
use FileHandle;
use File::Spec;
use YAML;
use JSON::PP;
#
@@ -71,6 +72,7 @@ my (
$opt_, $opt_i, $opt_r, $opt_e, $opt_d, $opt_enc,
$opt_s, $opt_t, $opt_T, $opt_l, $opt_L, $opt_c,
$opt_D, $opt_I, $opt_o, $opt_h, $opt_n, $opt_v,
$opt_j,
#
# set from commandline (or interactive)
@@ -133,7 +135,8 @@ my (
'printlines' => 0,
'cache' => 0,
'preferrededitor' => '',
'motd' => ''
'motd' => '',
'usejson' => 0, # will be the default in the future
);
# these are not customizable at runtime!
@@ -141,7 +144,7 @@ $hardparams = "(readonly|maxlen|dbdriver|useencryption|cryptmethod)";
$CONF = File::Spec->catfile($ENV{HOME}, ".noterc");
$USER = getlogin || getpwuid($<); chomp $USER;
$TOPIC = 1;
$VERSION = "1.3.26";
$VERSION = "1.4.0";
$CurDepth = 1; # the current depth inside the topic "directory" structure...
$maxlen = "auto";
$timelen = 22;
@@ -214,6 +217,7 @@ else {
"list|l:s" => \$opt_l, # string, optional
"longlist|L:s" => \$opt_L, # string, optional
"dump|Dump|D:s" => \$opt_D, # string, optional
"json|j" => \$opt_j, # bool, optional
"import|Import|I:s" => \$opt_I, # string, optional
"overwrite|o!" => \$opt_o, # no arg
"help|h|?!" => \$opt_h, # no arg
@@ -276,6 +280,10 @@ else {
else {
$dump_file = "-"; # use STDIN
}
if (defined $opt_j) {
$conf{usejson} = 1; # force JSON
}
}
elsif (defined $opt_I) {
$mode = "import";
@@ -923,6 +931,7 @@ sub edit {
sub dump {
my(%res, $num, $DUMP);
# $dump_file
if ($dump_file eq "-") {
$DUMP = *STDOUT;
@@ -931,8 +940,11 @@ sub dump {
open (DUMPFILE, ">$dump_file") or die "could not open $dump_file\n";
$DUMP = *DUMPFILE;
}
select $DUMP;
%res = $db->get_all();
# FIXME: prepare hashing in NOTEDB class
foreach $num (sort { $a <=> $b } keys %res) {
print STDOUT "dumping note number $num to $dump_file\n" if($dump_file ne "-");
@@ -947,7 +959,17 @@ sub dump {
my $date = $res{$num}->{date};
$res{$num} = { body => $body, title => $title, path => $path, date => $date};
}
if($conf{usejson}) {
my $json = JSON::PP->new->utf8->pretty;
print $json->encode(\%res);
}
else {
warn "Deprecation notice: YAML export format will not be supported in the future!
Enable JSON using the UseJSON config parameter or the -j commandline parameter!";
print Dump(\%res);
}
close(DUMPFILE);
select STDOUT;
}
@@ -964,9 +986,16 @@ sub import {
$DUMP = *DUMPFILE;
}
my $yaml = join '', <$DUMP>;
my $serialized = join '', <$DUMP>;
my $res = Load($yaml);
my $res;
if($serialized =~ /^\{/) {
$res = decode_json($serialized);
}
else {
$res = Load($serialized);
}
foreach my $number (keys %{$res}) {
my $note;

View File

@@ -283,6 +283,12 @@ AddTicket = NO
motd =
#
# Enable JSON formatted backups. The option will be removed
# in version 1.5 and become the only available option.
# In earlier versions: if unset or set to NO, YAML will be used.
UseJSON = YES
#
#
# That's all about it for now.

View File

@@ -77,6 +77,10 @@ the specified ones.
Dumps all notes to the textfile <file>. If <file> is a "-" it will
be printed out to standard output (STDOUT).
=item I<-j --json>
Use JSON format for exports created using -D. The importer determines
the format to be used automatically.
=item I<-I, --Import file | ->
@@ -534,6 +538,6 @@ T.v.Dein <tlinden@cpan.org>
=head1 VERSION
1.3.26
1.4.0
=cut