|
| 1 | +Pull request by cloudwu: |
| 2 | +Support lua 5.3 integer representation |
| 3 | +https://github.com/mpx/lua-cjson/pull/59 |
| 4 | + |
| 5 | +From f13d18e4dd0ff3ec79c712452a57730fc1bbacc8 Mon Sep 17 00:00:00 2001 |
| 6 | +From: Cloud Wu < [email protected]> |
| 7 | +Date: Thu, 5 Mar 2015 17:42:42 +0800 |
| 8 | +Subject: [PATCH 1/3] Support lua 5.3 integer representation |
| 9 | + |
| 10 | +--- |
| 11 | + lua_cjson.c | 23 +++++++++++++++++------ |
| 12 | + 1 file changed, 17 insertions(+), 6 deletions(-) |
| 13 | + |
| 14 | +diff --git a/lua_cjson.c b/lua_cjson.c |
| 15 | +index 22f33f1..a601f02 100644 |
| 16 | +--- a/lua_cjson.c |
| 17 | ++++ b/lua_cjson.c |
| 18 | +@@ -87,6 +87,7 @@ typedef enum { |
| 19 | + T_ARR_END, |
| 20 | + T_STRING, |
| 21 | + T_NUMBER, |
| 22 | ++ T_INTEGER, |
| 23 | + T_BOOLEAN, |
| 24 | + T_NULL, |
| 25 | + T_COLON, |
| 26 | +@@ -104,6 +105,7 @@ static const char *json_token_type_name[] = { |
| 27 | + "T_ARR_END", |
| 28 | + "T_STRING", |
| 29 | + "T_NUMBER", |
| 30 | ++ "T_INTEGER", |
| 31 | + "T_BOOLEAN", |
| 32 | + "T_NULL", |
| 33 | + "T_COLON", |
| 34 | +@@ -149,6 +151,7 @@ typedef struct { |
| 35 | + union { |
| 36 | + const char *string; |
| 37 | + double number; |
| 38 | ++ lua_Integer integer; |
| 39 | + int boolean; |
| 40 | + } value; |
| 41 | + int string_len; |
| 42 | +@@ -1008,13 +1011,18 @@ static int json_is_invalid_number(json_parse_t *json) |
| 43 | + static void json_next_number_token(json_parse_t *json, json_token_t *token) |
| 44 | + { |
| 45 | + char *endptr; |
| 46 | +- |
| 47 | +- token->type = T_NUMBER; |
| 48 | +- token->value.number = fpconv_strtod(json->ptr, &endptr); |
| 49 | +- if (json->ptr == endptr) |
| 50 | ++ token->value.integer = strtoll(json->ptr, &endptr, 0); |
| 51 | ++ if (json->ptr == endptr) { |
| 52 | + json_set_token_error(token, json, "invalid number"); |
| 53 | +- else |
| 54 | +- json->ptr = endptr; /* Skip the processed number */ |
| 55 | ++ return; |
| 56 | ++ } |
| 57 | ++ if (*endptr == '.' || *endptr == 'e' || *endptr == 'E') { |
| 58 | ++ token->type = T_NUMBER; |
| 59 | ++ token->value.number = fpconv_strtod(json->ptr, &endptr); |
| 60 | ++ } else { |
| 61 | ++ token->type = T_INTEGER; |
| 62 | ++ } |
| 63 | ++ json->ptr = endptr; /* Skip the processed number */ |
| 64 | + |
| 65 | + return; |
| 66 | + } |
| 67 | +@@ -1243,6 +1251,9 @@ static void json_process_value(lua_State *l, json_parse_t *json, |
| 68 | + case T_NUMBER: |
| 69 | + lua_pushnumber(l, token->value.number); |
| 70 | + break;; |
| 71 | ++ case T_INTEGER: |
| 72 | ++ lua_pushinteger(l, token->value.integer); |
| 73 | ++ break;; |
| 74 | + case T_BOOLEAN: |
| 75 | + lua_pushboolean(l, token->value.boolean); |
| 76 | + break;; |
| 77 | + |
| 78 | +From a82560481c51b2719aa41ee541023f45d098042e Mon Sep 17 00:00:00 2001 |
| 79 | +From: Cloud Wu < [email protected]> |
| 80 | +Date: Tue, 1 Sep 2015 23:17:21 +0800 |
| 81 | +Subject: [PATCH 2/3] encode integer for lua 5.3 |
| 82 | + |
| 83 | +--- |
| 84 | + lua_cjson.c | 11 ++++++++++- |
| 85 | + 1 file changed, 10 insertions(+), 1 deletion(-) |
| 86 | + |
| 87 | +diff --git a/lua_cjson.c b/lua_cjson.c |
| 88 | +index a601f02..d8ed0e9 100644 |
| 89 | +--- a/lua_cjson.c |
| 90 | ++++ b/lua_cjson.c |
| 91 | +@@ -595,8 +595,17 @@ static void json_append_array(lua_State *l, json_config_t *cfg, int current_dept |
| 92 | + static void json_append_number(lua_State *l, json_config_t *cfg, |
| 93 | + strbuf_t *json, int lindex) |
| 94 | + { |
| 95 | +- double num = lua_tonumber(l, lindex); |
| 96 | + int len; |
| 97 | ++#if LUA_VERSION_NUM >= 503 |
| 98 | ++ if (lua_isinteger(l, lindex)) { |
| 99 | ++ lua_Integer num = lua_tointeger(l, lindex); |
| 100 | ++ strbuf_ensure_empty_length(json, FPCONV_G_FMT_BUFSIZE); /* max length of int64 is 19 */ |
| 101 | ++ len = lua_integer2str(strbuf_empty_ptr(json), num); |
| 102 | ++ strbuf_extend_length(json, len); |
| 103 | ++ return; |
| 104 | ++ } |
| 105 | ++#endif |
| 106 | ++ double num = lua_tonumber(l, lindex); |
| 107 | + |
| 108 | + if (cfg->encode_invalid_numbers == 0) { |
| 109 | + /* Prevent encoding invalid numbers */ |
| 110 | + |
| 111 | +From c3f001fc5b3f629b4b9872b50ed672e310c264ee Mon Sep 17 00:00:00 2001 |
| 112 | +From: Cloud Wu < [email protected]> |
| 113 | +Date: Tue, 1 Dec 2015 22:21:19 +0800 |
| 114 | +Subject: [PATCH 3/3] Compatible with lua 5.3.2 |
| 115 | + |
| 116 | +--- |
| 117 | + lua_cjson.c | 2 +- |
| 118 | + 1 file changed, 1 insertion(+), 1 deletion(-) |
| 119 | + |
| 120 | +diff --git a/lua_cjson.c b/lua_cjson.c |
| 121 | +index d8ed0e9..2cfaac3 100644 |
| 122 | +--- a/lua_cjson.c |
| 123 | ++++ b/lua_cjson.c |
| 124 | +@@ -600,7 +600,7 @@ static void json_append_number(lua_State *l, json_config_t *cfg, |
| 125 | + if (lua_isinteger(l, lindex)) { |
| 126 | + lua_Integer num = lua_tointeger(l, lindex); |
| 127 | + strbuf_ensure_empty_length(json, FPCONV_G_FMT_BUFSIZE); /* max length of int64 is 19 */ |
| 128 | +- len = lua_integer2str(strbuf_empty_ptr(json), num); |
| 129 | ++ len = sprintf(strbuf_empty_ptr(json), LUA_INTEGER_FMT, num); |
| 130 | + strbuf_extend_length(json, len); |
| 131 | + return; |
| 132 | + } |
0 commit comments