2013-11-19 00:33:06 +01:00
|
|
|
#include "value-to-json.hh"
|
2016-08-26 18:55:55 +02:00
|
|
|
#include "json.hh"
|
2013-11-19 00:03:11 +01:00
|
|
|
#include "eval-inline.hh"
|
|
|
|
#include "util.hh"
|
|
|
|
|
|
|
|
#include <cstdlib>
|
2014-09-30 00:41:18 +02:00
|
|
|
#include <iomanip>
|
2013-11-19 00:03:11 +01:00
|
|
|
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
void printValueAsJSON(EvalState & state, bool strict,
|
2016-08-26 18:55:55 +02:00
|
|
|
Value & v, JSONPlaceholder & out, PathSet & context)
|
2013-11-19 00:03:11 +01:00
|
|
|
{
|
|
|
|
checkInterrupt();
|
|
|
|
|
|
|
|
if (strict) state.forceValue(v);
|
|
|
|
|
|
|
|
switch (v.type) {
|
|
|
|
|
|
|
|
case tInt:
|
2016-08-26 18:55:55 +02:00
|
|
|
out.write(v.integer);
|
2013-11-19 00:03:11 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case tBool:
|
2016-08-26 18:55:55 +02:00
|
|
|
out.write(v.boolean);
|
2013-11-19 00:03:11 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case tString:
|
|
|
|
copyContext(v, context);
|
2016-08-26 18:55:55 +02:00
|
|
|
out.write(v.string.s);
|
2013-11-19 00:03:11 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case tPath:
|
2016-08-26 18:55:55 +02:00
|
|
|
out.write(state.copyPathToStore(context, v.path));
|
2013-11-19 00:03:11 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case tNull:
|
2016-08-26 18:55:55 +02:00
|
|
|
out.write(nullptr);
|
2013-11-19 00:03:11 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case tAttrs: {
|
|
|
|
Bindings::iterator i = v.attrs->find(state.sOutPath);
|
|
|
|
if (i == v.attrs->end()) {
|
2016-08-26 18:55:55 +02:00
|
|
|
auto obj(out.object());
|
2013-11-19 00:03:11 +01:00
|
|
|
StringSet names;
|
2015-07-17 19:24:28 +02:00
|
|
|
for (auto & j : *v.attrs)
|
|
|
|
names.insert(j.name);
|
|
|
|
for (auto & j : names) {
|
|
|
|
Attr & a(*v.attrs->find(state.symbols.create(j)));
|
2016-08-26 18:55:55 +02:00
|
|
|
auto placeholder(obj.placeholder(j));
|
|
|
|
printValueAsJSON(state, strict, *a.value, placeholder, context);
|
2013-11-19 00:03:11 +01:00
|
|
|
}
|
|
|
|
} else
|
2016-08-26 18:55:55 +02:00
|
|
|
printValueAsJSON(state, strict, *i->value, out, context);
|
2013-11-19 00:03:11 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-07-23 22:05:09 +02:00
|
|
|
case tList1: case tList2: case tListN: {
|
2016-08-26 18:55:55 +02:00
|
|
|
auto list(out.list());
|
2015-07-23 22:05:09 +02:00
|
|
|
for (unsigned int n = 0; n < v.listSize(); ++n) {
|
2016-08-26 18:55:55 +02:00
|
|
|
auto placeholder(list.placeholder());
|
|
|
|
printValueAsJSON(state, strict, *v.listElems()[n], placeholder, context);
|
2013-11-19 00:03:11 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-07-17 19:24:28 +02:00
|
|
|
case tExternal:
|
2016-08-26 18:55:55 +02:00
|
|
|
v.external->printValueAsJSON(state, strict, out, context);
|
2014-11-30 19:16:19 +01:00
|
|
|
break;
|
|
|
|
|
2016-01-05 00:40:40 +01:00
|
|
|
case tFloat:
|
2016-08-26 18:55:55 +02:00
|
|
|
out.write(v.fpoint);
|
2016-01-05 00:40:40 +01:00
|
|
|
break;
|
|
|
|
|
2013-11-19 00:03:11 +01:00
|
|
|
default:
|
|
|
|
throw TypeError(format("cannot convert %1% to JSON") % showType(v));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-26 18:55:55 +02:00
|
|
|
void printValueAsJSON(EvalState & state, bool strict,
|
|
|
|
Value & v, std::ostream & str, PathSet & context)
|
|
|
|
{
|
|
|
|
JSONPlaceholder out(str);
|
|
|
|
printValueAsJSON(state, strict, v, out, context);
|
|
|
|
}
|
2013-11-19 00:03:11 +01:00
|
|
|
|
2014-11-30 19:16:19 +01:00
|
|
|
void ExternalValueBase::printValueAsJSON(EvalState & state, bool strict,
|
2016-08-26 18:55:55 +02:00
|
|
|
JSONPlaceholder & out, PathSet & context) const
|
2014-11-30 19:16:19 +01:00
|
|
|
{
|
|
|
|
throw TypeError(format("cannot convert %1% to JSON") % showType());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-19 00:03:11 +01:00
|
|
|
}
|