diff options
Diffstat (limited to 'third_party/nix/src/libexpr')
-rw-r--r-- | third_party/nix/src/libexpr/json-to-value.cc | 47 |
1 files changed, 24 insertions, 23 deletions
diff --git a/third_party/nix/src/libexpr/json-to-value.cc b/third_party/nix/src/libexpr/json-to-value.cc index 368bc9f5b575..66912aa084e6 100644 --- a/third_party/nix/src/libexpr/json-to-value.cc +++ b/third_party/nix/src/libexpr/json-to-value.cc @@ -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_integer(number_integer_t val) override { + return handle_value(mkInt, val); + } - bool number_unsigned(number_unsigned_t val) { + bool number_unsigned(number_unsigned_t val) override { return handle_value(mkInt, val); } - bool number_float(number_float_t val, const string_t& s) { + 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()); } }; |