Refactor JSON output

This commit is contained in:
Eelco Dolstra 2013-11-19 00:33:06 +01:00
parent 77c13cdf56
commit 5fea98111b
2 changed files with 56 additions and 13 deletions

View file

@ -1,5 +1,4 @@
#include "value-to-xml.hh" #include "value-to-json.hh"
#include "xml-writer.hh"
#include "eval-inline.hh" #include "eval-inline.hh"
#include "util.hh" #include "util.hh"
@ -9,7 +8,7 @@
namespace nix { namespace nix {
static void escapeJSON(std::ostream & str, const string & s) void escapeJSON(std::ostream & str, const string & s)
{ {
str << "\""; str << "\"";
foreach (string::const_iterator, i, s) foreach (string::const_iterator, i, s)
@ -55,32 +54,26 @@ void printValueAsJSON(EvalState & state, bool strict,
case tAttrs: { case tAttrs: {
Bindings::iterator i = v.attrs->find(state.sOutPath); Bindings::iterator i = v.attrs->find(state.sOutPath);
if (i == v.attrs->end()) { if (i == v.attrs->end()) {
str << "{"; JSONObject json(str);
StringSet names; StringSet names;
foreach (Bindings::iterator, i, *v.attrs) foreach (Bindings::iterator, i, *v.attrs)
names.insert(i->name); names.insert(i->name);
bool first = true;
foreach (StringSet::iterator, i, names) { foreach (StringSet::iterator, i, names) {
if (!first) str << ","; else first = false;
Attr & a(*v.attrs->find(state.symbols.create(*i))); Attr & a(*v.attrs->find(state.symbols.create(*i)));
escapeJSON(str, *i); json.attr(*i);
str << ":";
printValueAsJSON(state, strict, *a.value, str, context); printValueAsJSON(state, strict, *a.value, str, context);
} }
str << "}";
} else } else
printValueAsJSON(state, strict, *i->value, str, context); printValueAsJSON(state, strict, *i->value, str, context);
break; break;
} }
case tList: { case tList: {
str << "["; JSONList json(str);
bool first = true;
for (unsigned int n = 0; n < v.list.length; ++n) { for (unsigned int n = 0; n < v.list.length; ++n) {
if (!first) str << ","; else first = false; json.elem();
printValueAsJSON(state, strict, *v.list.elems[n], str, context); printValueAsJSON(state, strict, *v.list.elems[n], str, context);
} }
str << "]";
break; break;
} }

View file

@ -11,4 +11,54 @@ namespace nix {
void printValueAsJSON(EvalState & state, bool strict, void printValueAsJSON(EvalState & state, bool strict,
Value & v, std::ostream & out, PathSet & context); Value & v, std::ostream & out, PathSet & context);
void escapeJSON(std::ostream & str, const string & s);
struct JSONObject
{
std::ostream & str;
bool first;
JSONObject(std::ostream & str) : str(str), first(true)
{
str << "{";
}
~JSONObject()
{
str << "}";
}
void attr(const string & s)
{
if (!first) str << ","; else first = false;
escapeJSON(str, s);
str << ":";
}
void attr(const string & s, const string & t)
{
attr(s);
escapeJSON(str, t);
}
};
struct JSONList
{
std::ostream & str;
bool first;
JSONList(std::ostream & str) : str(str), first(true)
{
str << "[";
}
~JSONList()
{
str << "]";
}
void elem()
{
if (!first) str << ","; else first = false;
}
void elem(const string & s)
{
elem();
escapeJSON(str, s);
}
};
} }