nix-super/src/libexpr/value-to-json.cc

110 lines
3 KiB
C++
Raw Normal View History

2013-11-19 01:33:06 +02:00
#include "value-to-json.hh"
2013-11-19 01:03:11 +02:00
#include "eval-inline.hh"
#include "util.hh"
#include "store-api.hh"
2013-11-19 01:03:11 +02:00
#include <cstdlib>
#include <iomanip>
#include <nlohmann/json.hpp>
2013-11-19 01:03:11 +02:00
namespace nix {
using json = nlohmann::json;
json printValueAsJSON(EvalState & state, bool strict,
Value & v, const PosIdx pos, PathSet & context, bool copyToStore)
2013-11-19 01:03:11 +02:00
{
checkInterrupt();
if (strict) state.forceValue(v, pos);
2013-11-19 01:03:11 +02:00
json out;
switch (v.type()) {
2013-11-19 01:03:11 +02:00
case nInt:
out = v.integer;
2013-11-19 01:03:11 +02:00
break;
case nBool:
out = v.boolean;
2013-11-19 01:03:11 +02:00
break;
case nString:
2013-11-19 01:03:11 +02:00
copyContext(v, context);
out = v.string.s;
2013-11-19 01:03:11 +02:00
break;
case nPath:
if (copyToStore)
out = state.store->printStorePath(state.copyPathToStore(context, v.path));
else
out = v.path;
2013-11-19 01:03:11 +02:00
break;
case nNull:
2013-11-19 01:03:11 +02:00
break;
case nAttrs: {
auto maybeString = state.tryAttrsToString(pos, v, context, false, false);
2019-10-27 11:15:51 +02:00
if (maybeString) {
out = *maybeString;
2019-10-27 11:15:51 +02:00
break;
}
auto i = v.attrs->find(state.sOutPath);
2013-11-19 01:03:11 +02:00
if (i == v.attrs->end()) {
out = json::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.emplace(state.symbols[j.name]);
2015-07-17 20:24:28 +03:00
for (auto & j : names) {
Attr & a(*v.attrs->find(state.symbols.create(j)));
out[j] = printValueAsJSON(state, strict, *a.value, a.pos, context, copyToStore);
2013-11-19 01:03:11 +02:00
}
} else
return printValueAsJSON(state, strict, *i->value, i->pos, context, copyToStore);
2013-11-19 01:03:11 +02:00
break;
}
case nList: {
out = json::array();
for (auto elem : v.listItems())
out.push_back(printValueAsJSON(state, strict, *elem, pos, context, copyToStore));
2013-11-19 01:03:11 +02:00
break;
}
case nExternal:
return v.external->printValueAsJSON(state, strict, context, copyToStore);
break;
case nFloat:
out = v.fpoint;
break;
case nThunk:
case nFunction:
auto e = TypeError({
.msg = hintfmt("cannot convert %1% to JSON", showType(v)),
.errPos = state.positions[v.determinePos(pos)]
});
e.addTrace(state.positions[pos], hintfmt("message for the trace"));
2022-05-06 17:47:21 +03:00
state.debugThrowLastTrace(e);
throw e;
2013-11-19 01:03:11 +02:00
}
return out;
2013-11-19 01:03:11 +02:00
}
void printValueAsJSON(EvalState & state, bool strict,
Value & v, const PosIdx pos, std::ostream & str, PathSet & context, bool copyToStore)
{
str << printValueAsJSON(state, strict, v, pos, context, copyToStore);
}
2013-11-19 01:03:11 +02:00
json ExternalValueBase::printValueAsJSON(EvalState & state, bool strict,
PathSet & context, bool copyToStore) const
{
2022-05-25 13:32:22 +03:00
state.debugThrowLastTrace(TypeError("cannot convert %1% to JSON", showType()));
}
2013-11-19 01:03:11 +02:00
}