mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-28 21:40:30 +00:00
Further improve json / lua serialization
This commit is contained in:
parent
9e86980191
commit
ee82cee432
10 changed files with 248 additions and 155 deletions
11
third_party/double-conversion/wrapper.cc
vendored
11
third_party/double-conversion/wrapper.cc
vendored
|
@ -18,6 +18,7 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/str/str.h"
|
||||
#include "third_party/double-conversion/double-conversion.h"
|
||||
#include "third_party/double-conversion/double-to-string.h"
|
||||
#include "third_party/double-conversion/wrapper.h"
|
||||
|
||||
namespace double_conversion {
|
||||
|
@ -31,6 +32,16 @@ char* DoubleToEcmascript(char buf[128], double x) {
|
|||
return b.Finalize();
|
||||
}
|
||||
|
||||
char* DoubleToLua(char buf[128], double x) {
|
||||
static const DoubleToStringConverter kDoubleToLua(
|
||||
DoubleToStringConverter::EMIT_TRAILING_DECIMAL_POINT |
|
||||
DoubleToStringConverter::NO_TRAILING_ZERO,
|
||||
"math.huge", "0/0", 'e', -6, 21, 6, 0);
|
||||
StringBuilder b(buf, 128);
|
||||
kDoubleToLua.ToShortest(x, &b);
|
||||
return b.Finalize();
|
||||
}
|
||||
|
||||
double StringToDouble(const char* s, size_t n, int* out_processed) {
|
||||
if (n == -1) n = strlen(s);
|
||||
int flags = StringToDoubleConverter::ALLOW_CASE_INSENSITIVITY |
|
||||
|
|
1
third_party/double-conversion/wrapper.h
vendored
1
third_party/double-conversion/wrapper.h
vendored
|
@ -3,6 +3,7 @@
|
|||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
COSMOPOLITAN_C_START_
|
||||
|
||||
char *DoubleToLua(char[128], double);
|
||||
char *DoubleToEcmascript(char[128], double);
|
||||
double StringToDouble(const char *, size_t, int *);
|
||||
|
||||
|
|
17
third_party/lua/luaencodejsondata.c
vendored
17
third_party/lua/luaencodejsondata.c
vendored
|
@ -99,12 +99,17 @@ static int LuaEncodeJsonDataImpl(lua_State *L, char **buf, int level,
|
|||
// json tables must be arrays or use string keys
|
||||
goto OnError;
|
||||
}
|
||||
RETURN_ON_ERROR(sli = AppendStrList(&sl));
|
||||
RETURN_ON_ERROR(LuaEncodeJsonDataImpl(L, &sl.p[sli], level - 1,
|
||||
numformat, -2, visited));
|
||||
RETURN_ON_ERROR(appendw(&sl.p[sli], ':'));
|
||||
RETURN_ON_ERROR(LuaEncodeJsonDataImpl(L, &sl.p[sli], level - 1,
|
||||
numformat, -1, visited));
|
||||
// the json parser inserts a `__json_object__` into empty
|
||||
// objects, so we don't serialize `{}` as `[]` by mistake
|
||||
// and as such, we should ignore it here, for readability
|
||||
if (strcmp(luaL_checkstring(L, -2), "__json_object__")) {
|
||||
RETURN_ON_ERROR(sli = AppendStrList(&sl));
|
||||
RETURN_ON_ERROR(LuaEncodeJsonDataImpl(L, &sl.p[sli], level - 1,
|
||||
numformat, -2, visited));
|
||||
RETURN_ON_ERROR(appendw(&sl.p[sli], ':'));
|
||||
RETURN_ON_ERROR(LuaEncodeJsonDataImpl(L, &sl.p[sli], level - 1,
|
||||
numformat, -1, visited));
|
||||
}
|
||||
lua_pop(L, 1); // table/-2, key/-1
|
||||
}
|
||||
// stack: table/-1, as the key was popped by lua_next
|
||||
|
|
19
third_party/lua/luaencodeluadata.c
vendored
19
third_party/lua/luaencodeluadata.c
vendored
|
@ -25,6 +25,7 @@
|
|||
#include "libc/stdio/append.internal.h"
|
||||
#include "libc/stdio/strlist.internal.h"
|
||||
#include "libc/x/x.h"
|
||||
#include "third_party/double-conversion/wrapper.h"
|
||||
#include "third_party/lua/cosmo.h"
|
||||
#include "third_party/lua/lauxlib.h"
|
||||
#include "third_party/lua/lctype.h"
|
||||
|
@ -126,22 +127,8 @@ static int LuaEncodeLuaDataImpl(lua_State *L, char **buf, int level,
|
|||
appendd(buf, ibuf,
|
||||
FormatFlex64(ibuf, luaL_checkinteger(L, idx), 2) - ibuf));
|
||||
} else {
|
||||
// TODO(jart): replace this api
|
||||
while (*numformat == '%' || *numformat == '.' ||
|
||||
isdigit(*numformat)) {
|
||||
++numformat;
|
||||
}
|
||||
switch (*numformat) {
|
||||
case 'a':
|
||||
case 'g':
|
||||
case 'f':
|
||||
fmt[4] = *numformat;
|
||||
break;
|
||||
default:
|
||||
// prevent format string hacking
|
||||
goto OnError;
|
||||
}
|
||||
RETURN_ON_ERROR(appendf(buf, fmt, lua_tonumber(L, idx)));
|
||||
RETURN_ON_ERROR(
|
||||
appends(buf, DoubleToLua(ibuf, lua_tonumber(L, idx))));
|
||||
}
|
||||
return 0;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue