lix/src/libexpr/value-to-json.cc

110 lines
3 KiB
C++
Raw Normal View History

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