-
- function readvalue()
- skipstuff();
- while ch do
- if ch == "{" then
- return readobject();
- elseif ch == "[" then
- return readarray();
- elseif ch == "\"" then
- return readstring();
- elseif ch:match("[%-0-9%.]") then
- return readnumber();
- elseif ch == "n" then
- return readliteral("null", null);
- elseif ch == "t" then
- return readliteral("true", true);
- elseif ch == "f" then
- return readliteral("false", false);
- else
- error("invalid character at value start: "..ch);
- end
- end
- error("eof while reading value");
+ return nil, "false parse failed";
+end
+function _readvalue(json, index)
+ index = _skip_whitespace(json, index);
+ local b = json:byte(index);
+ -- TODO try table lookup instead of if-else?
+ if b == 0x7B then -- "{"
+ return _readobject(json, index);
+ elseif b == 0x5B then -- "["
+ return _readarray(json, index);
+ elseif b == 0x22 then -- "\""
+ return _readstring(json, index);
+ elseif b ~= nil and b >= 0x30 and b <= 0x39 or b == 0x2d then -- "0"-"9" or "-"
+ return _readnumber(json, index);
+ elseif b == 0x6e then -- "n"
+ return _readnull(json, index);
+ elseif b == 0x74 then -- "t"
+ return _readtrue(json, index);
+ elseif b == 0x66 then -- "f"
+ return _readfalse(json, index);
+ else
+ return nil, "value expected";