mu-find: implement format=json2
Implement a new experimental json2 format for mu-find, which gets rid of the ":" prefixes for fields, i.e., "subject" instead of ":subject". Document it as well.
This commit is contained in:
@ -228,6 +228,13 @@ Sexp::to_json_string(Format fopts) const
|
||||
{
|
||||
std::stringstream sstrm;
|
||||
|
||||
const auto sym_name=[&](const std::string& sym) {
|
||||
if (any_of(fopts & Format::NoColon) && sym[0] == ':')
|
||||
return sym.substr(1); // remove colon
|
||||
else
|
||||
return sym;
|
||||
};
|
||||
|
||||
switch (type()) {
|
||||
case Type::List: {
|
||||
// property-lists become JSON objects
|
||||
@ -237,16 +244,18 @@ Sexp::to_json_string(Format fopts) const
|
||||
bool first{true};
|
||||
while (it != list().end()) {
|
||||
const auto key{it->symbol().name};
|
||||
sstrm << (first ? "" : ",") << quote(key) << ":";
|
||||
sstrm << (first ? "" : ",")
|
||||
<< quote(sym_name(key)) << ":";
|
||||
++it;
|
||||
const auto emacs_tstamp{*it};
|
||||
sstrm << emacs_tstamp.to_json_string();
|
||||
sstrm << emacs_tstamp.to_json_string(fopts);
|
||||
++it;
|
||||
first = false;
|
||||
// special-case: tstamp-fields also get a "unix" value,
|
||||
// which are easier to work with than the "emacs" timestamps
|
||||
if (key == ":date" || key == ":changed")
|
||||
sstrm << "," << quote(key + "-unix") << ":"
|
||||
sstrm << "," << quote(sym_name(key) + "-unix")
|
||||
<< ":"
|
||||
<< unix_tstamp(emacs_tstamp);
|
||||
}
|
||||
sstrm << "}";
|
||||
@ -256,7 +265,7 @@ Sexp::to_json_string(Format fopts) const
|
||||
sstrm << '[';
|
||||
bool first{true};
|
||||
for (auto&& child : list()) {
|
||||
sstrm << (first ? "" : ", ") << child.to_json_string();
|
||||
sstrm << (first ? "" : ", ") << child.to_json_string(fopts);
|
||||
first = false;
|
||||
}
|
||||
sstrm << ']';
|
||||
@ -287,7 +296,6 @@ Sexp::to_json_string(Format fopts) const
|
||||
}
|
||||
|
||||
|
||||
|
||||
Sexp&
|
||||
Sexp::del_prop(const std::string& pname)
|
||||
{
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
/*
|
||||
** Copyright (C) 2020-2023 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
||||
** Copyright (C) 2020-2025 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
||||
**
|
||||
** This program is free software; you can redistribute it and/or modify it
|
||||
** under the terms of the GNU General Public License as published by the
|
||||
@ -264,6 +264,8 @@ struct Sexp {
|
||||
Default = 0, /**< Nothing in particular */
|
||||
SplitList = 1 << 0, /**< Insert newline after list item */
|
||||
TypeInfo = 1 << 1, /**< Show type-info */
|
||||
NoColon = 1 << 2, /**< In the Json output, remove the colon
|
||||
* prefixes for property keys */
|
||||
};
|
||||
|
||||
/**
|
||||
@ -272,7 +274,7 @@ struct Sexp {
|
||||
* @return str
|
||||
*/
|
||||
std::string to_string(Format fopts=Format::Default) const;
|
||||
std::string to_json_string(Format fopts=Format::Default) const;
|
||||
std::string to_json_string(Format fopts) const;
|
||||
|
||||
Sexp& del_prop(const std::string& pname);
|
||||
|
||||
@ -280,7 +282,7 @@ struct Sexp {
|
||||
* Some useful constants
|
||||
*
|
||||
*/
|
||||
static inline const auto nil_sym = Sexp::Symbol{"nil"};
|
||||
static inline const auto nil_sym = Sexp::Symbol{"nil"};
|
||||
static inline const auto t_sym = Sexp::Symbol{"t"};
|
||||
|
||||
protected:
|
||||
|
||||
Reference in New Issue
Block a user