mu: support json output directly

Allow for dumping json directly from the Sexp structures, so we don't
need any external libs (i.e. json-glib) anymore.
This commit is contained in:
Dirk-Jan C. Binnema
2020-10-26 18:39:56 +02:00
parent f2e87ea2d4
commit d2aa1f91b0
10 changed files with 93 additions and 66 deletions

View File

@ -179,7 +179,7 @@ Sexp::make_parse (const std::string& expr)
std::string
Sexp::to_string () const
Sexp::to_sexp_string () const
{
std::stringstream sstrm;
@ -188,7 +188,7 @@ Sexp::to_string () const
sstrm << '(';
bool first{true};
for (auto&& child : list()) {
sstrm << (first ? "" : " ") << child.to_string();
sstrm << (first ? "" : " ") << child.to_sexp_string();
first = false;
}
sstrm << ')';
@ -206,3 +206,55 @@ Sexp::to_string () const
return sstrm.str();
}
std::string
Sexp::to_json_string () const
{
std::stringstream sstrm;
switch (type()) {
case Type::List: {
// property-lists become JSON objects
if (is_prop_list()) {
sstrm << "{";
auto it{list().begin()};
bool first{true};
while (it != list().end()) {
sstrm << (first?"":",") << quote(it->value()) << ":";
++it;
sstrm << it->to_json_string();
++it;
first = false;
}
sstrm << "}";
} else { // other lists become arrays.
sstrm << '[';
bool first{true};
for (auto&& child : list()) {
sstrm << (first ? "" : ", ") << child.to_json_string();
first = false;
}
sstrm << ']';
}
break;
}
case Type::String:
sstrm << quote(value());
break;
case Type::Symbol:
if (is_nil())
sstrm << "false";
else if (is_t())
sstrm << "true";
else
sstrm << quote(value());
break;
case Type::Number:
case Type::Empty:
default:
sstrm << value();
}
return sstrm.str();
}