lua cjson数字问题
在cjson的lua5.3版本中,对于数字 都是按照double来处理的 需要修改一下源代码 来正确处理 integer 还是double
添加类型 T_INT 表示 整数
static void json_next_number_token(json_parse_t *json, json_token_t *token) { char *endptr; // token->type = T_NUMBER; //token->value.number double value = fpconv_strtod(json->ptr, &endptr); if (json->ptr == endptr) { token->type = T_NUMBER; token->value.number = value; json_set_token_error(token, json, "invalid number"); } else { //scaning the str has dot-operation char *start = (char*)json->ptr; bool is_int = true; while (start != endptr) { if (*start == '.') { //this is int is_int = false; break; } ++start; } if (is_int) { token->type = T_INT; token->value.integer = (long long)value; } else { token->type = T_NUMBER; token->value.number = value; } json->ptr = endptr; /* Skip the processed number */ } return; }
修改一下 token 类型处理 (push到lua的数据类型)
/* Handle the "value" context */ static void json_process_value(lua_State *l, json_parse_t *json, json_token_t *token) { switch (token->type) { case T_STRING: lua_pushlstring(l, token->value.string, token->string_len); break;; case T_NUMBER: lua_pushnumber(l, token->value.number); break;; case T_INT: lua_pushinteger(l, token->value.integer); break;; case T_BOOLEAN: lua_pushboolean(l, token->value.boolean); break;; case T_OBJ_BEGIN: json_parse_object_context(l, json); break;; case T_ARR_BEGIN: json_parse_array_context(l, json); break;; case T_NULL: /* In Lua, setting "t[k] = nil" will delete k from the table. * Hence a NULL pointer lightuserdata object is used instead */ lua_pushlightuserdata(l, NULL); break;; default: json_throw_parse_error(l, json, "value", token); } }
重新编译 即可