Implement new JSON parser for redbean

This commit is contained in:
Justine Tunney 2022-07-09 11:44:19 -07:00
parent d37536bd4b
commit 2189877856
12 changed files with 338 additions and 21 deletions

View file

@ -673,6 +673,21 @@ FUNCTIONS
URIs that do things like embed a PNG file in a web page. See
encodebase64.c.
ParseJson(input:str)
├─→ value:*
├─→ true [if useoutput]
└─→ nil, error:str
Turns JSON string into a Lua data structure.
This is a very permissive parser. That means it should always
parse correctly formatted JSON correctly. However it will not
complain if the `input` string is weirdly formatted. There is
currently no validation performed, other than what we need to
ensure security. For example `{3=4}` will decode as `{[3]=4}`
even though that structure won't round-trip with `EncodeJson`
since redbean won't generate invalid JSON (see Postel's Law).
EncodeJson(value[,options:table])
├─→ json:str
├─→ true [if useoutput]

222
tool/net/ljson.c Normal file
View file

@ -0,0 +1,222 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2022 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/errno.h"
#include "libc/limits.h"
#include "libc/math.h"
#include "libc/str/str.h"
#include "libc/str/tpenc.h"
#include "third_party/lua/lauxlib.h"
#include "third_party/lua/lua.h"
struct Rc {
int t;
const char *p;
};
static int Accumulate(int x, int c, int d) {
if (!__builtin_mul_overflow(x, 10, &x) &&
!__builtin_add_overflow(x, (c - '0') * d, &x)) {
return x;
} else {
errno = ERANGE;
if (d > 0) {
return INT_MAX;
} else {
return INT_MIN;
}
}
}
static struct Rc Parse(struct lua_State *L, const char *p, const char *e) {
uint64_t w;
struct Rc r;
luaL_Buffer b;
int A, B, C, D;
int c, d, i, j, x, y, z;
for (d = +1; p < e;) {
switch ((c = *p++ & 255)) {
case '-':
d = -1;
break;
case ']':
case '}':
return (struct Rc){0, p};
case '[':
lua_newtable(L);
i = 0;
do {
r = Parse(L, p, e);
p = r.p;
if (r.t) {
lua_rawseti(L, -2, i++ + 1);
}
} while (r.t);
return (struct Rc){1, p};
case '{':
lua_newtable(L);
i = 0;
do {
r = Parse(L, p, e);
p = r.p;
if (r.t) {
r = Parse(L, p, e);
p = r.p;
if (!r.t) {
lua_pushnil(L);
}
lua_settable(L, -3);
}
} while (r.t);
return (struct Rc){1, p};
case '"':
luaL_buffinit(L, &b);
while (p < e) {
switch ((c = *p++ & 255)) {
default:
AddChar:
luaL_addchar(&b, c);
break;
case '\\':
if (p < e) {
switch ((c = *p++ & 255)) {
case '"':
case '/':
case '\\':
default:
goto AddChar;
case 'b':
c = '\b';
goto AddChar;
case 'f':
c = '\f';
goto AddChar;
case 'n':
c = '\n';
goto AddChar;
case 'r':
c = '\r';
goto AddChar;
case 't':
c = '\t';
goto AddChar;
case 'u':
if (p + 4 <= e && //
(A = kHexToInt[p[0] & 255]) != -1 && //
(B = kHexToInt[p[1] & 255]) != -1 && //
(C = kHexToInt[p[2] & 255]) != -1 && //
(D = kHexToInt[p[3] & 255]) != -1) {
p += 4;
c = A << 12 | B << 8 | C << 4 | D;
w = tpenc(c);
do {
luaL_addchar(&b, w & 255);
} while ((w >>= 8));
break;
} else {
goto AddChar;
}
}
}
break;
case '"':
goto FinishString;
}
}
FinishString:
luaL_pushresult(&b);
return (struct Rc){1, p};
case '0' ... '9':
for (x = (c - '0') * d; p < e; ++p) {
c = *p & 255;
if (isdigit(c)) {
x = Accumulate(x, c, d);
} else if (c == '.') {
++p;
goto Fraction;
} else if (c == 'e' || c == 'E') {
++p;
j = 0;
y = 0;
goto Exponent;
} else {
break;
}
}
lua_pushinteger(L, x);
return (struct Rc){1, p};
Fraction:
for (j = y = 0; p < e; ++p) {
c = *p & 255;
if (isdigit(c)) {
--j;
y = Accumulate(y, c, d);
} else if (c == 'e' || c == 'E') {
++p;
goto Exponent;
} else {
break;
}
}
lua_pushnumber(L, x + y * exp10(j));
return (struct Rc){1, p};
Exponent:
d = +1;
for (z = 0; p < e; ++p) {
c = *p & 255;
if (isdigit(c)) {
z = Accumulate(z, c, d);
} else if (c == '-') {
d = -1;
} else if (c == '+') {
d = +1;
} else {
break;
}
}
lua_pushnumber(L, (x + y * exp10(j)) * exp10(z));
return (struct Rc){1, p};
case ',':
case ':':
case ' ':
case '\n':
case '\r':
case '\t':
default:
break;
}
}
return (struct Rc){0, p};
}
/**
* Parses JSON data structure string into a Lua data structure.
*/
int ParseJson(struct lua_State *L, const char *p, size_t n) {
if (n == -1) n = p ? strlen(p) : 0;
return Parse(L, p, p + n).t;
}

11
tool/net/ljson.h Normal file
View file

@ -0,0 +1,11 @@
#ifndef COSMOPOLITAN_TOOL_NET_LJSON_H_
#define COSMOPOLITAN_TOOL_NET_LJSON_H_
#include "third_party/lua/lauxlib.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
int ParseJson(struct lua_State *, const char *, size_t);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_TOOL_NET_LJSON_H_ */

View file

@ -94,6 +94,7 @@ o/$(MODE)/tool/net/redbean.com.dbg: \
o/$(MODE)/tool/net/redbean.o \
o/$(MODE)/tool/net/lfuncs.o \
o/$(MODE)/tool/net/lre.o \
o/$(MODE)/tool/net/ljson.o \
o/$(MODE)/tool/net/lmaxmind.o \
o/$(MODE)/tool/net/lsqlite3.o \
o/$(MODE)/tool/net/largon2.o \
@ -212,6 +213,7 @@ o/$(MODE)/tool/net/redbean-demo.com.dbg: \
o/$(MODE)/tool/net/redbean.o \
o/$(MODE)/tool/net/lfuncs.o \
o/$(MODE)/tool/net/lre.o \
o/$(MODE)/tool/net/ljson.o \
o/$(MODE)/tool/net/lmaxmind.o \
o/$(MODE)/tool/net/lsqlite3.o \
o/$(MODE)/tool/net/largon2.o \
@ -328,6 +330,7 @@ o/$(MODE)/tool/net/redbean-unsecure.com.dbg: \
o/$(MODE)/tool/net/redbean-unsecure.o \
o/$(MODE)/tool/net/lfuncs.o \
o/$(MODE)/tool/net/lre.o \
o/$(MODE)/tool/net/ljson.o \
o/$(MODE)/tool/net/lmaxmind.o \
o/$(MODE)/tool/net/lsqlite3.o \
o/$(MODE)/tool/net/largon2.o \

View file

@ -112,6 +112,7 @@
#include "tool/args/args.h"
#include "tool/build/lib/case.h"
#include "tool/net/lfuncs.h"
#include "tool/net/ljson.h"
#include "tool/net/luacheck.h"
#include "tool/net/sandbox.h"
@ -4258,6 +4259,13 @@ static int LuaEncodeLua(lua_State *L) {
return LuaEncodeSmth(L, LuaEncodeLuaData);
}
static int LuaParseJson(lua_State *L) {
size_t n;
const char *p;
p = luaL_checklstring(L, 1, &n);
return ParseJson(L, p, n);
}
static int LuaGetUrl(lua_State *L) {
char *p;
size_t n;
@ -5150,6 +5158,7 @@ static const luaL_Reg kLuaFuncs[] = {
{"ParseHost", LuaParseHost}, //
{"ParseHttpDateTime", LuaParseHttpDateTime}, //
{"ParseIp", LuaParseIp}, //
{"ParseJson", LuaParseJson}, //
{"ParseParams", LuaParseParams}, //
{"ParseUrl", LuaParseUrl}, //
{"Popcnt", LuaPopcnt}, //