2014-07-04 14:34:15 +03:00
|
|
|
#include "json-to-value.hh"
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
|
|
|
|
static void skipWhitespace(const char * & s)
|
|
|
|
{
|
|
|
|
while (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r') s++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static string parseJSONString(const char * & s)
|
|
|
|
{
|
|
|
|
string res;
|
|
|
|
if (*s++ != '"') throw JSONParseError("expected JSON string");
|
|
|
|
while (*s != '"') {
|
|
|
|
if (!*s) throw JSONParseError("got end-of-string in JSON string");
|
|
|
|
if (*s == '\\') {
|
|
|
|
s++;
|
|
|
|
if (*s == '"') res += '"';
|
|
|
|
else if (*s == '\\') res += '\\';
|
|
|
|
else if (*s == '/') res += '/';
|
|
|
|
else if (*s == 'b') res += '\b';
|
|
|
|
else if (*s == 'f') res += '\f';
|
|
|
|
else if (*s == 'n') res += '\n';
|
|
|
|
else if (*s == 'r') res += '\r';
|
|
|
|
else if (*s == 't') res += '\t';
|
|
|
|
else if (*s == 'u') throw JSONParseError("\\u characters in JSON strings are currently not supported");
|
|
|
|
else throw JSONParseError("invalid escaped character in JSON string");
|
|
|
|
s++;
|
|
|
|
} else
|
|
|
|
res += *s++;
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void parseJSON(EvalState & state, const char * & s, Value & v)
|
|
|
|
{
|
|
|
|
skipWhitespace(s);
|
|
|
|
|
|
|
|
if (!*s) throw JSONParseError("expected JSON value");
|
|
|
|
|
|
|
|
if (*s == '[') {
|
|
|
|
s++;
|
|
|
|
ValueVector values;
|
|
|
|
values.reserve(128);
|
|
|
|
skipWhitespace(s);
|
|
|
|
while (1) {
|
|
|
|
if (values.empty() && *s == ']') break;
|
|
|
|
Value * v2 = state.allocValue();
|
|
|
|
parseJSON(state, s, *v2);
|
|
|
|
values.push_back(v2);
|
|
|
|
skipWhitespace(s);
|
|
|
|
if (*s == ']') break;
|
2017-07-30 14:27:57 +03:00
|
|
|
if (*s != ',') throw JSONParseError("expected ',' or ']' after JSON array element");
|
2014-07-04 14:34:15 +03:00
|
|
|
s++;
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
state.mkList(v, values.size());
|
|
|
|
for (size_t n = 0; n < values.size(); ++n)
|
2015-07-23 23:05:09 +03:00
|
|
|
v.listElems()[n] = values[n];
|
2014-07-04 14:34:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (*s == '{') {
|
|
|
|
s++;
|
2014-09-19 17:49:41 +03:00
|
|
|
ValueMap attrs;
|
2014-07-04 14:34:15 +03:00
|
|
|
while (1) {
|
|
|
|
skipWhitespace(s);
|
2014-09-19 17:49:41 +03:00
|
|
|
if (attrs.empty() && *s == '}') break;
|
2014-07-04 14:34:15 +03:00
|
|
|
string name = parseJSONString(s);
|
|
|
|
skipWhitespace(s);
|
2017-07-30 14:27:57 +03:00
|
|
|
if (*s != ':') throw JSONParseError("expected ':' in JSON object");
|
2014-07-04 14:34:15 +03:00
|
|
|
s++;
|
|
|
|
Value * v2 = state.allocValue();
|
|
|
|
parseJSON(state, s, *v2);
|
2014-09-19 17:49:41 +03:00
|
|
|
attrs[state.symbols.create(name)] = v2;
|
2014-07-04 14:34:15 +03:00
|
|
|
skipWhitespace(s);
|
|
|
|
if (*s == '}') break;
|
2017-07-30 14:27:57 +03:00
|
|
|
if (*s != ',') throw JSONParseError("expected ',' or '}' after JSON member");
|
2014-07-04 14:34:15 +03:00
|
|
|
s++;
|
|
|
|
}
|
2014-09-19 17:49:41 +03:00
|
|
|
state.mkAttrs(v, attrs.size());
|
|
|
|
for (auto & i : attrs)
|
|
|
|
v.attrs->push_back(Attr(i.first, i.second));
|
2014-07-04 14:34:15 +03:00
|
|
|
v.attrs->sort();
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (*s == '"') {
|
|
|
|
mkString(v, parseJSONString(s));
|
|
|
|
}
|
|
|
|
|
2016-01-05 01:40:40 +02:00
|
|
|
else if (isdigit(*s) || *s == '-' || *s == '.' ) {
|
|
|
|
// Buffer into a string first, then use built-in C++ conversions
|
|
|
|
std::string tmp_number;
|
|
|
|
ValueType number_type = tInt;
|
|
|
|
|
|
|
|
while (isdigit(*s) || *s == '-' || *s == '.' || *s == 'e' || *s == 'E') {
|
|
|
|
if (*s == '.' || *s == 'e' || *s == 'E')
|
|
|
|
number_type = tFloat;
|
2016-02-15 15:46:23 +02:00
|
|
|
tmp_number += *s++;
|
2016-01-05 01:40:40 +02:00
|
|
|
}
|
|
|
|
|
2017-12-15 02:09:45 +02:00
|
|
|
try {
|
|
|
|
if (number_type == tFloat)
|
|
|
|
mkFloat(v, stod(tmp_number));
|
|
|
|
else
|
2018-05-27 01:43:46 +03:00
|
|
|
mkInt(v, stol(tmp_number));
|
2019-09-22 22:29:33 +03:00
|
|
|
} catch (std::invalid_argument & e) {
|
2017-12-15 02:09:45 +02:00
|
|
|
throw JSONParseError("invalid JSON number");
|
2019-09-22 22:29:33 +03:00
|
|
|
} catch (std::out_of_range & e) {
|
2017-12-15 02:09:45 +02:00
|
|
|
throw JSONParseError("out-of-range JSON number");
|
|
|
|
}
|
2014-07-04 14:34:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (strncmp(s, "true", 4) == 0) {
|
|
|
|
s += 4;
|
|
|
|
mkBool(v, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (strncmp(s, "false", 5) == 0) {
|
|
|
|
s += 5;
|
|
|
|
mkBool(v, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (strncmp(s, "null", 4) == 0) {
|
|
|
|
s += 4;
|
|
|
|
mkNull(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
else throw JSONParseError("unrecognised JSON value");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void parseJSON(EvalState & state, const string & s_, Value & v)
|
|
|
|
{
|
|
|
|
const char * s = s_.c_str();
|
|
|
|
parseJSON(state, s, v);
|
|
|
|
skipWhitespace(s);
|
|
|
|
if (*s) throw JSONParseError(format("expected end-of-string while parsing JSON value: %1%") % s);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|