2013-11-19 01:33:06 +02:00
|
|
|
#include "value-to-json.hh"
|
2016-08-26 19:55:55 +03:00
|
|
|
#include "json.hh"
|
2013-11-19 01:03:11 +02:00
|
|
|
#include "eval-inline.hh"
|
|
|
|
#include "util.hh"
|
|
|
|
|
|
|
|
#include <cstdlib>
|
2014-09-30 01:41:18 +03:00
|
|
|
#include <iomanip>
|
2013-11-19 01:03:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
void printValueAsJSON(EvalState & state, bool strict,
|
2016-08-26 19:55:55 +03:00
|
|
|
Value & v, JSONPlaceholder & out, PathSet & context)
|
2013-11-19 01:03:11 +02:00
|
|
|
{
|
|
|
|
checkInterrupt();
|
|
|
|
|
|
|
|
if (strict) state.forceValue(v);
|
|
|
|
|
2020-12-17 15:45:45 +02:00
|
|
|
switch (v.type()) {
|
2013-11-19 01:03:11 +02:00
|
|
|
|
2020-12-12 03:09:10 +02:00
|
|
|
case nInt:
|
2016-08-26 19:55:55 +03:00
|
|
|
out.write(v.integer);
|
2013-11-19 01:03:11 +02:00
|
|
|
break;
|
|
|
|
|
2020-12-12 03:09:10 +02:00
|
|
|
case nBool:
|
2016-08-26 19:55:55 +03:00
|
|
|
out.write(v.boolean);
|
2013-11-19 01:03:11 +02:00
|
|
|
break;
|
|
|
|
|
2020-12-12 03:09:10 +02:00
|
|
|
case nString:
|
2013-11-19 01:03:11 +02:00
|
|
|
copyContext(v, context);
|
2016-08-26 19:55:55 +03:00
|
|
|
out.write(v.string.s);
|
2013-11-19 01:03:11 +02:00
|
|
|
break;
|
|
|
|
|
2020-12-12 03:09:10 +02:00
|
|
|
case nPath:
|
2016-08-26 19:55:55 +03:00
|
|
|
out.write(state.copyPathToStore(context, v.path));
|
2013-11-19 01:03:11 +02:00
|
|
|
break;
|
|
|
|
|
2020-12-12 03:09:10 +02:00
|
|
|
case nNull:
|
2016-08-26 19:55:55 +03:00
|
|
|
out.write(nullptr);
|
2013-11-19 01:03:11 +02:00
|
|
|
break;
|
|
|
|
|
2020-12-12 03:09:10 +02:00
|
|
|
case nAttrs: {
|
2019-10-27 11:15:51 +02:00
|
|
|
auto maybeString = state.tryAttrsToString(noPos, v, context, false, false);
|
|
|
|
if (maybeString) {
|
|
|
|
out.write(*maybeString);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
auto i = v.attrs->find(state.sOutPath);
|
2013-11-19 01:03:11 +02:00
|
|
|
if (i == v.attrs->end()) {
|
2016-08-26 19:55:55 +03:00
|
|
|
auto obj(out.object());
|
2013-11-19 01:03:11 +02:00
|
|
|
StringSet names;
|
2015-07-17 20:24:28 +03: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 19:55:55 +03:00
|
|
|
auto placeholder(obj.placeholder(j));
|
|
|
|
printValueAsJSON(state, strict, *a.value, placeholder, context);
|
2013-11-19 01:03:11 +02:00
|
|
|
}
|
|
|
|
} else
|
2016-08-26 19:55:55 +03:00
|
|
|
printValueAsJSON(state, strict, *i->value, out, context);
|
2013-11-19 01:03:11 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-12-12 03:09:10 +02:00
|
|
|
case nList: {
|
2016-08-26 19:55:55 +03:00
|
|
|
auto list(out.list());
|
2015-07-23 23:05:09 +03:00
|
|
|
for (unsigned int n = 0; n < v.listSize(); ++n) {
|
2016-08-26 19:55:55 +03:00
|
|
|
auto placeholder(list.placeholder());
|
|
|
|
printValueAsJSON(state, strict, *v.listElems()[n], placeholder, context);
|
2013-11-19 01:03:11 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-12-12 03:09:10 +02:00
|
|
|
case nExternal:
|
2016-08-26 19:55:55 +03:00
|
|
|
v.external->printValueAsJSON(state, strict, out, context);
|
2014-11-30 20:16:19 +02:00
|
|
|
break;
|
|
|
|
|
2020-12-12 03:09:10 +02:00
|
|
|
case nFloat:
|
2016-08-26 19:55:55 +03:00
|
|
|
out.write(v.fpoint);
|
2016-01-05 01:40:40 +02:00
|
|
|
break;
|
|
|
|
|
2020-12-12 03:09:10 +02:00
|
|
|
case nThunk:
|
|
|
|
throw TypeError("cannot convert %1% to JSON", showType(v));
|
|
|
|
|
|
|
|
case nFunction:
|
2020-04-22 02:07:07 +03:00
|
|
|
throw TypeError("cannot convert %1% to JSON", showType(v));
|
2013-11-19 01:03:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-26 19:55:55 +03: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 01:03:11 +02:00
|
|
|
|
2014-11-30 20:16:19 +02:00
|
|
|
void ExternalValueBase::printValueAsJSON(EvalState & state, bool strict,
|
2016-08-26 19:55:55 +03:00
|
|
|
JSONPlaceholder & out, PathSet & context) const
|
2014-11-30 20:16:19 +02:00
|
|
|
{
|
2020-04-22 02:07:07 +03:00
|
|
|
throw TypeError("cannot convert %1% to JSON", showType());
|
2014-11-30 20:16:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-19 01:03:11 +02:00
|
|
|
}
|