JSONSax: Use a RootValue

More #3377.
This commit is contained in:
Eelco Dolstra 2020-04-16 17:28:32 +02:00
parent 10e17eaa58
commit 9f46f54de4

View file

@ -4,7 +4,6 @@
#include <nlohmann/json.hpp>
using json = nlohmann::json;
using std::unique_ptr;
namespace nix {
@ -13,30 +12,30 @@ namespace nix {
class JSONSax : nlohmann::json_sax<json> {
class JSONState {
protected:
unique_ptr<JSONState> parent;
Value * v;
std::unique_ptr<JSONState> parent;
RootValue v;
public:
virtual unique_ptr<JSONState> resolve(EvalState &)
virtual std::unique_ptr<JSONState> resolve(EvalState &)
{
throw std::logic_error("tried to close toplevel json parser state");
};
explicit JSONState(unique_ptr<JSONState>&& p) : parent(std::move(p)), v(nullptr) {};
explicit JSONState(Value* v) : v(v) {};
}
explicit JSONState(std::unique_ptr<JSONState> && p) : parent(std::move(p)) {}
explicit JSONState(Value * v) : v(allocRootValue(v)) {}
JSONState(JSONState & p) = delete;
Value & value(EvalState & state)
{
if (v == nullptr)
v = state.allocValue();
return *v;
};
virtual ~JSONState() {};
virtual void add() {};
if (!v)
v = allocRootValue(state.allocValue());
return **v;
}
virtual ~JSONState() {}
virtual void add() {}
};
class JSONObjectState : public JSONState {
using JSONState::JSONState;
ValueMap attrs = ValueMap();
virtual unique_ptr<JSONState> resolve(EvalState & state) override
ValueMap attrs;
std::unique_ptr<JSONState> resolve(EvalState & state) override
{
Value & v = parent->value(state);
state.mkAttrs(v, attrs.size());
@ -44,17 +43,17 @@ class JSONSax : nlohmann::json_sax<json> {
v.attrs->push_back(Attr(i.first, i.second));
return std::move(parent);
}
virtual void add() override { v = nullptr; };
void add() override { v = nullptr; }
public:
void key(string_t & name, EvalState & state)
{
attrs[state.symbols.create(name)] = &value(state);
attrs.insert_or_assign(state.symbols.create(name), &value(state));
}
};
class JSONListState : public JSONState {
ValueVector values = ValueVector();
virtual unique_ptr<JSONState> resolve(EvalState & state) override
ValueVector values;
std::unique_ptr<JSONState> resolve(EvalState & state) override
{
Value & v = parent->value(state);
state.mkList(v, values.size());
@ -63,19 +62,19 @@ class JSONSax : nlohmann::json_sax<json> {
}
return std::move(parent);
}
virtual void add() override {
values.push_back(v);
void add() override {
values.push_back(*v);
v = nullptr;
};
}
public:
JSONListState(unique_ptr<JSONState>&& p, std::size_t reserve) : JSONState(std::move(p))
JSONListState(std::unique_ptr<JSONState> && p, std::size_t reserve) : JSONState(std::move(p))
{
values.reserve(reserve);
}
};
EvalState & state;
unique_ptr<JSONState> rs;
std::unique_ptr<JSONState> rs;
template<typename T, typename... Args> inline bool handle_value(T f, Args... args)
{