Skip to content

Commit b29ac40

Browse files
committed
Keep decimal point to indicate the number is floating point.
1 parent ab3be8e commit b29ac40

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/lualib-src/lua_json.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
#include "common/hash.hpp"
33
#include "common/string.hpp"
44
#include "lua.hpp"
5-
#include "yyjson/yyjson.h"
65
#include <charconv>
76
#include <codecvt>
87
#include <cstdlib>
98
#include <string_view>
9+
#include "yyjson/yyjson.c"
1010

1111
using namespace moon;
1212

@@ -167,6 +167,13 @@ static void format_space(buffer* writer, int n) {
167167
}
168168
}
169169

170+
inline void write_number(buffer* writer, lua_Number num) {
171+
auto[buf, n] = writer->prepare(32);
172+
uint64_t raw = f64_to_raw(num);
173+
auto e = write_f64_raw((uint8_t*)buf, raw, YYJSON_WRITE_ALLOW_INF_AND_NAN);
174+
writer->commit(e - (uint8_t*)buf);
175+
}
176+
170177
template<bool format>
171178
static void encode_table(lua_State* L, buffer* writer, int idx, int depth);
172179

@@ -185,7 +192,7 @@ static void encode_one(lua_State* L, buffer* writer, int idx, int depth, json_co
185192
if (lua_isinteger(L, idx))
186193
writer->write_chars(lua_tointeger(L, idx));
187194
else
188-
writer->write_chars(lua_tonumber(L, idx));
195+
write_number(writer, lua_tonumber(L, idx));
189196
return;
190197
}
191198
case LUA_TSTRING: {
@@ -560,7 +567,7 @@ static int concat(lua_State* L) {
560567
if (lua_isinteger(L, -1))
561568
buf->write_chars(lua_tointeger(L, -1));
562569
else
563-
buf->write_chars(lua_tonumber(L, -1));
570+
write_number(buf, lua_tonumber(L, -1));
564571
break;
565572
}
566573
case LUA_TBOOLEAN: {

0 commit comments

Comments
 (0)