refactor(tvix): JSONSax: Use a RootValue
More #3377. Backported from upstream at 9f46f54de4e55267df492456fc0393f74616366 Change-Id: I11bfca4ec56bd4e45291ce3f98a60f198dff0196 Reviewed-on: https://cl.tvl.fyi/c/depot/+/2140 Tested-by: BuildkiteCI Reviewed-by: andi <andi@notmuch.email>
This commit is contained in:
parent
89f1489916
commit
a30e616efb
1 changed files with 25 additions and 24 deletions
49
third_party/nix/src/libexpr/json-to-value.cc
vendored
49
third_party/nix/src/libexpr/json-to-value.cc
vendored
|
@ -16,22 +16,21 @@ class JSONSax : nlohmann::json_sax<json> {
|
|||
class JSONState {
|
||||
protected:
|
||||
std::unique_ptr<JSONState> parent;
|
||||
Value* v;
|
||||
std::shared_ptr<Value*> v;
|
||||
|
||||
public:
|
||||
virtual std::unique_ptr<JSONState> resolve(EvalState&) {
|
||||
throw std::logic_error("tried to close toplevel json parser state");
|
||||
};
|
||||
explicit JSONState(std::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 {
|
||||
|
@ -63,7 +62,7 @@ class JSONSax : nlohmann::json_sax<json> {
|
|||
return std::move(parent);
|
||||
}
|
||||
void add() override {
|
||||
values.push_back(v);
|
||||
values.push_back(*v);
|
||||
v = nullptr;
|
||||
};
|
||||
|
||||
|
@ -87,50 +86,52 @@ class JSONSax : nlohmann::json_sax<json> {
|
|||
public:
|
||||
JSONSax(EvalState& state, Value& v) : state(state), rs(new JSONState(&v)){};
|
||||
|
||||
bool null() { return handle_value(mkNull); }
|
||||
bool null() override { return handle_value(mkNull); }
|
||||
|
||||
bool boolean(bool val) { return handle_value(mkBool, val); }
|
||||
bool boolean(bool val) override { return handle_value(mkBool, val); }
|
||||
|
||||
bool number_integer(number_integer_t val) { return handle_value(mkInt, val); }
|
||||
|
||||
bool number_unsigned(number_unsigned_t val) {
|
||||
bool number_integer(number_integer_t val) override {
|
||||
return handle_value(mkInt, val);
|
||||
}
|
||||
|
||||
bool number_float(number_float_t val, const string_t& s) {
|
||||
bool number_unsigned(number_unsigned_t val) override {
|
||||
return handle_value(mkInt, val);
|
||||
}
|
||||
|
||||
bool number_float(number_float_t val, const string_t&) override {
|
||||
return handle_value(mkFloat, val);
|
||||
}
|
||||
|
||||
bool string(string_t& val) {
|
||||
bool string(string_t& val) override {
|
||||
return handle_value<void(Value&, const char*)>(mkString, val.c_str());
|
||||
}
|
||||
|
||||
bool start_object(std::size_t len) {
|
||||
bool start_object(std::size_t) override {
|
||||
rs = std::make_unique<JSONObjectState>(std::move(rs));
|
||||
return true;
|
||||
}
|
||||
|
||||
bool key(string_t& name) {
|
||||
bool key(string_t& name) override {
|
||||
dynamic_cast<JSONObjectState*>(rs.get())->key(name, state);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool end_object() {
|
||||
bool end_object() override {
|
||||
rs = rs->resolve(state);
|
||||
rs->add();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool end_array() { return end_object(); }
|
||||
bool end_array() override { return end_object(); }
|
||||
|
||||
bool start_array(size_t len) {
|
||||
bool start_array(size_t len) override {
|
||||
rs = std::make_unique<JSONListState>(
|
||||
std::move(rs), len != std::numeric_limits<size_t>::max() ? len : 128);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool parse_error(std::size_t, const std::string&,
|
||||
const nlohmann::detail::exception& ex) {
|
||||
const nlohmann::detail::exception& ex) override {
|
||||
throw JSONParseError(ex.what());
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue