2014-07-04 14:34:15 +03:00
|
|
|
#include "json-to-value.hh"
|
|
|
|
|
2020-01-09 18:38:27 +02:00
|
|
|
#include <variant>
|
|
|
|
#include <nlohmann/json.hpp>
|
2014-07-04 14:34:15 +03:00
|
|
|
|
2020-01-09 18:38:27 +02:00
|
|
|
using json = nlohmann::json;
|
2020-01-09 23:46:41 +02:00
|
|
|
using std::unique_ptr;
|
2014-07-04 14:34:15 +03:00
|
|
|
|
2020-01-09 18:38:27 +02:00
|
|
|
namespace nix {
|
2014-07-04 14:34:15 +03:00
|
|
|
|
2020-01-09 18:38:27 +02:00
|
|
|
// for more information, refer to
|
|
|
|
// https://github.com/nlohmann/json/blob/master/include/nlohmann/detail/input/json_sax.hpp
|
|
|
|
class JSONSax : nlohmann::json_sax<json> {
|
|
|
|
class JSONState {
|
|
|
|
protected:
|
2020-01-09 23:46:41 +02:00
|
|
|
unique_ptr<JSONState> parent;
|
2020-01-09 18:38:27 +02:00
|
|
|
Value * v;
|
|
|
|
public:
|
2020-01-09 23:46:41 +02:00
|
|
|
virtual unique_ptr<JSONState> resolve(EvalState &)
|
2020-01-09 18:38:27 +02:00
|
|
|
{
|
|
|
|
throw std::logic_error("tried to close toplevel json parser state");
|
|
|
|
};
|
2020-01-09 23:46:41 +02:00
|
|
|
explicit JSONState(unique_ptr<JSONState>&& p) : parent(std::move(p)), v(nullptr) {};
|
2020-01-09 18:38:27 +02:00
|
|
|
explicit JSONState(Value* v) : v(v) {};
|
|
|
|
JSONState(JSONState& p) = delete;
|
|
|
|
Value& value(EvalState & state)
|
|
|
|
{
|
|
|
|
if (v == nullptr)
|
|
|
|
v = state.allocValue();
|
|
|
|
return *v;
|
|
|
|
};
|
|
|
|
virtual ~JSONState() {};
|
|
|
|
virtual void add() {};
|
|
|
|
};
|
|
|
|
|
|
|
|
class JSONObjectState : public JSONState {
|
|
|
|
using JSONState::JSONState;
|
|
|
|
ValueMap attrs = ValueMap();
|
2020-01-09 23:46:41 +02:00
|
|
|
virtual unique_ptr<JSONState> resolve(EvalState & state) override
|
2020-01-09 18:38:27 +02:00
|
|
|
{
|
|
|
|
Value& v = parent->value(state);
|
|
|
|
state.mkAttrs(v, attrs.size());
|
|
|
|
for (auto & i : attrs)
|
|
|
|
v.attrs->push_back(Attr(i.first, i.second));
|
2020-01-09 23:46:41 +02:00
|
|
|
return std::move(parent);
|
2020-01-09 18:38:27 +02:00
|
|
|
}
|
|
|
|
virtual void add() override { v = nullptr; };
|
|
|
|
public:
|
|
|
|
void key(string_t& name, EvalState & state)
|
|
|
|
{
|
|
|
|
attrs[state.symbols.create(name)] = &value(state);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class JSONListState : public JSONState {
|
|
|
|
ValueVector values = ValueVector();
|
2020-01-09 23:46:41 +02:00
|
|
|
virtual unique_ptr<JSONState> resolve(EvalState & state) override
|
2020-01-09 18:38:27 +02:00
|
|
|
{
|
|
|
|
Value& v = parent->value(state);
|
|
|
|
state.mkList(v, values.size());
|
|
|
|
for (size_t n = 0; n < values.size(); ++n) {
|
|
|
|
v.listElems()[n] = values[n];
|
|
|
|
}
|
2020-01-09 23:46:41 +02:00
|
|
|
return std::move(parent);
|
2020-01-09 18:38:27 +02:00
|
|
|
}
|
|
|
|
virtual void add() override {
|
|
|
|
values.push_back(v);
|
|
|
|
v = nullptr;
|
|
|
|
};
|
|
|
|
public:
|
2020-01-09 23:46:41 +02:00
|
|
|
JSONListState(unique_ptr<JSONState>&& p, std::size_t reserve) : JSONState(std::move(p))
|
2020-01-09 18:38:27 +02:00
|
|
|
{
|
|
|
|
values.reserve(reserve);
|
|
|
|
}
|
|
|
|
};
|
2014-07-04 14:34:15 +03:00
|
|
|
|
2020-01-09 18:38:27 +02:00
|
|
|
EvalState & state;
|
2020-01-09 23:46:41 +02:00
|
|
|
unique_ptr<JSONState> rs;
|
2020-01-07 01:06:49 +02:00
|
|
|
|
2020-01-09 18:38:27 +02:00
|
|
|
template<typename T, typename... Args> inline bool handle_value(T f, Args... args)
|
2020-01-07 01:06:49 +02:00
|
|
|
{
|
2020-01-09 18:38:27 +02:00
|
|
|
f(rs->value(state), args...);
|
|
|
|
rs->add();
|
|
|
|
return true;
|
2020-01-07 01:06:49 +02:00
|
|
|
}
|
|
|
|
|
2020-01-09 18:38:27 +02:00
|
|
|
public:
|
|
|
|
JSONSax(EvalState & state, Value & v) : state(state), rs(new JSONState(&v)) {};
|
2020-01-07 01:06:49 +02:00
|
|
|
|
2020-01-09 18:38:27 +02:00
|
|
|
bool null()
|
2020-01-07 01:06:49 +02:00
|
|
|
{
|
2020-01-09 18:38:27 +02:00
|
|
|
return handle_value(mkNull);
|
2020-01-07 01:06:49 +02:00
|
|
|
}
|
2020-01-09 18:38:27 +02:00
|
|
|
|
|
|
|
bool boolean(bool val)
|
2020-01-07 01:06:49 +02:00
|
|
|
{
|
2020-01-09 18:38:27 +02:00
|
|
|
return handle_value(mkBool, val);
|
2020-01-07 01:06:49 +02:00
|
|
|
}
|
2020-01-09 18:38:27 +02:00
|
|
|
|
|
|
|
bool number_integer(number_integer_t val)
|
2020-01-07 01:06:49 +02:00
|
|
|
{
|
2020-01-09 18:38:27 +02:00
|
|
|
return handle_value(mkInt, val);
|
2020-01-07 01:06:49 +02:00
|
|
|
}
|
2020-01-09 18:38:27 +02:00
|
|
|
|
|
|
|
bool number_unsigned(number_unsigned_t val)
|
2020-01-07 01:06:49 +02:00
|
|
|
{
|
2020-01-09 18:38:27 +02:00
|
|
|
return handle_value(mkInt, val);
|
2020-01-07 01:06:49 +02:00
|
|
|
}
|
|
|
|
|
2020-01-09 18:38:27 +02:00
|
|
|
bool number_float(number_float_t val, const string_t& s)
|
|
|
|
{
|
|
|
|
return handle_value(mkFloat, val);
|
2014-07-04 14:34:15 +03:00
|
|
|
}
|
|
|
|
|
2020-01-09 18:38:27 +02:00
|
|
|
bool string(string_t& val)
|
|
|
|
{
|
|
|
|
return handle_value<void(Value&, const char*)>(mkString, val.c_str());
|
2014-07-04 14:34:15 +03:00
|
|
|
}
|
|
|
|
|
2020-01-09 18:38:27 +02:00
|
|
|
bool start_object(std::size_t len)
|
|
|
|
{
|
2020-01-09 23:46:41 +02:00
|
|
|
rs = std::make_unique<JSONObjectState>(std::move(rs));
|
2020-01-09 18:38:27 +02:00
|
|
|
return true;
|
2014-07-04 14:34:15 +03:00
|
|
|
}
|
|
|
|
|
2020-01-09 18:38:27 +02:00
|
|
|
bool key(string_t& name)
|
|
|
|
{
|
2020-01-09 23:46:41 +02:00
|
|
|
dynamic_cast<JSONObjectState*>(rs.get())->key(name, state);
|
2020-01-09 18:38:27 +02:00
|
|
|
return true;
|
2014-07-04 14:34:15 +03:00
|
|
|
}
|
|
|
|
|
2020-01-09 18:38:27 +02:00
|
|
|
bool end_object() {
|
2020-01-09 23:46:41 +02:00
|
|
|
rs = rs->resolve(state);
|
2020-01-09 18:38:27 +02:00
|
|
|
rs->add();
|
|
|
|
return true;
|
2014-07-04 14:34:15 +03:00
|
|
|
}
|
|
|
|
|
2020-01-09 18:38:27 +02:00
|
|
|
bool end_array() {
|
|
|
|
return end_object();
|
2014-07-04 14:34:15 +03:00
|
|
|
}
|
|
|
|
|
2020-01-09 18:38:27 +02:00
|
|
|
bool start_array(size_t len) {
|
2020-01-09 23:46:41 +02:00
|
|
|
rs = std::make_unique<JSONListState>(std::move(rs),
|
|
|
|
len != std::numeric_limits<size_t>::max() ? len : 128);
|
2020-01-09 18:38:27 +02:00
|
|
|
return true;
|
2014-07-04 14:34:15 +03:00
|
|
|
}
|
|
|
|
|
2020-01-09 18:38:27 +02:00
|
|
|
bool parse_error(std::size_t, const std::string&, const nlohmann::detail::exception& ex) {
|
|
|
|
throw JSONParseError(ex.what());
|
2014-07-04 14:34:15 +03:00
|
|
|
}
|
2020-01-09 18:38:27 +02:00
|
|
|
};
|
2014-07-04 14:34:15 +03:00
|
|
|
|
|
|
|
void parseJSON(EvalState & state, const string & s_, Value & v)
|
|
|
|
{
|
2020-01-09 18:38:27 +02:00
|
|
|
JSONSax parser(state, v);
|
|
|
|
bool res = json::sax_parse(s_, &parser);
|
|
|
|
if (!res)
|
|
|
|
throw JSONParseError("Invalid JSON Value");
|
2014-07-04 14:34:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|