sniper00 / moon

A lightweight game server framework implemented with Actor Model
MIT License
721 stars 158 forks source link

Lua table json decode/encode support integer key #104

Closed sniper00 closed 3 years ago

sniper00 commented 4 years ago

Lua table经过json encode后,整型key会被编码成字符串,造成decode后只根据json数据类型无法还原整型key,现在想到一个方案: lua变量不能是 -,0-9 开头, json key decode时,先检测第一个字符是否是-,0-9 开头,如果是,就说明这是一个整型key。

这种方案有一些限制

  1. 不能用double类型做key,这里只处理integer类型。用浮点数做key本身就是错误的行为。
  2. 避免用 -,0-9 开头的非纯数字字符串做key

https://github.com/sniper00/moon/blob/9072cec2d6ff22cce942fc7b633e8e7238f94341/moon-src/luabind/lua_json.cpp#L267