webmcp

annotate libraries/json/json.c @ 123:402fce94f98c

Added function json.isnull(...) to JSON library
author jbe
date Sat Jul 26 05:27:42 2014 +0200 (2014-07-26)
parents ff39d4a310b9
children ece4e6f683c9
rev   line source
jbe@121 1 #include <lua.h>
jbe@121 2 #include <lauxlib.h>
jbe@122 3 #include <stdlib.h>
jbe@121 4 #include <string.h>
jbe@121 5
jbe@123 6 #define JSON_UPVAL_ARYLEN lua_upvalueindex(1)
jbe@123 7 #define JSON_UPVAL_NULLS lua_upvalueindex(2)
jbe@123 8
jbe@121 9 #define JSON_VALUE 0
jbe@121 10 #define JSON_OBJECT_KEY 1
jbe@121 11 #define JSON_OBJECT_KEY_TERMINATOR 2
jbe@121 12 #define JSON_OBJECT_VALUE 3
jbe@121 13 #define JSON_OBJECT_SEPARATOR 4
jbe@121 14 #define JSON_ARRAY_VALUE 5
jbe@121 15 #define JSON_ARRAY_SEPARATOR 6
jbe@121 16 #define JSON_END 7
jbe@121 17
jbe@121 18 static int json_import(lua_State *L) {
jbe@121 19 const char *str;
jbe@121 20 size_t total;
jbe@121 21 size_t pos = 0;
jbe@121 22 size_t level = 0;
jbe@121 23 int mode = JSON_VALUE;
jbe@121 24 char c;
jbe@121 25 luaL_Buffer luabuf;
jbe@121 26 char *cbuf;
jbe@121 27 size_t writepos;
jbe@121 28 int aryidx;
jbe@121 29 lua_settop(L, 1);
jbe@121 30 str = lua_tostring(L, 1);
jbe@121 31 total = strlen(str);
jbe@121 32 json_import_loop:
jbe@121 33 while (c = str[pos], c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == '\f') pos++;
jbe@121 34 switch (c) {
jbe@121 35 case 0:
jbe@121 36 if (mode == JSON_END) return 1;
jbe@121 37 json_import_unexpected_eof:
jbe@121 38 lua_pushnil(L);
jbe@121 39 if (level == 0) lua_pushliteral(L, "Empty string");
jbe@121 40 else lua_pushliteral(L, "Unexpected end of JSON document");
jbe@121 41 return 2;
jbe@121 42 case '{':
jbe@121 43 if (mode != JSON_VALUE && mode != JSON_OBJECT_VALUE && mode != JSON_ARRAY_VALUE)
jbe@121 44 goto json_import_syntax_error;
jbe@121 45 pos++;
jbe@121 46 lua_newtable(L);
jbe@123 47 lua_newtable(L); // stores set of NULL values
jbe@123 48 lua_pushvalue(L, -2);
jbe@123 49 lua_pushvalue(L, -2);
jbe@123 50 lua_rawset(L, JSON_UPVAL_NULLS);
jbe@121 51 mode = JSON_OBJECT_KEY;
jbe@121 52 level++;
jbe@121 53 goto json_import_loop;
jbe@121 54 case '[':
jbe@121 55 if (mode != JSON_VALUE && mode != JSON_OBJECT_VALUE && mode != JSON_ARRAY_VALUE)
jbe@121 56 goto json_import_syntax_error;
jbe@121 57 pos++;
jbe@121 58 lua_newtable(L);
jbe@123 59 lua_newtable(L); // stores set of NULL values
jbe@123 60 lua_pushvalue(L, -2);
jbe@123 61 lua_pushvalue(L, -2);
jbe@123 62 lua_rawset(L, JSON_UPVAL_NULLS);
jbe@121 63 lua_pushinteger(L, 0); // length of array (since it may contain nil's)
jbe@121 64 mode = JSON_ARRAY_VALUE;
jbe@121 65 level++;
jbe@121 66 goto json_import_loop;
jbe@121 67 case '}':
jbe@121 68 if (mode != JSON_OBJECT_KEY && mode != JSON_OBJECT_SEPARATOR)
jbe@121 69 goto json_import_syntax_error;
jbe@121 70 goto json_import_close;
jbe@121 71 case ']':
jbe@121 72 if (mode != JSON_ARRAY_VALUE && mode != JSON_ARRAY_SEPARATOR)
jbe@121 73 goto json_import_syntax_error;
jbe@121 74 lua_pushvalue(L, -2); // use array table as key
jbe@121 75 lua_insert(L, -2); // use length of array as value
jbe@123 76 lua_rawset(L, JSON_UPVAL_ARYLEN); // store length in ephemeron table
jbe@121 77 // leaves array table on top of stack
jbe@121 78 json_import_close:
jbe@121 79 pos++;
jbe@123 80 lua_pop(L, 1); // pop table that stores set of NULL values
jbe@121 81 if (--level) {
jbe@121 82 if (lua_type(L, -2) == LUA_TNUMBER) {
jbe@121 83 mode = JSON_ARRAY_VALUE;
jbe@121 84 } else {
jbe@121 85 mode = JSON_OBJECT_VALUE;
jbe@121 86 }
jbe@121 87 goto json_import_process_value;
jbe@121 88 } else {
jbe@121 89 mode = JSON_END;
jbe@121 90 }
jbe@121 91 goto json_import_loop;
jbe@121 92 case ':':
jbe@121 93 if (mode != JSON_OBJECT_KEY_TERMINATOR)
jbe@121 94 goto json_import_syntax_error;
jbe@121 95 pos++;
jbe@121 96 mode = JSON_OBJECT_VALUE;
jbe@121 97 goto json_import_loop;
jbe@121 98 case ',':
jbe@121 99 if (mode == JSON_OBJECT_SEPARATOR) {
jbe@121 100 mode = JSON_OBJECT_KEY;
jbe@121 101 } else if (mode == JSON_ARRAY_SEPARATOR) {
jbe@121 102 mode = JSON_ARRAY_VALUE;
jbe@121 103 } else {
jbe@121 104 goto json_import_syntax_error;
jbe@121 105 }
jbe@121 106 pos++;
jbe@121 107 goto json_import_loop;
jbe@121 108 case '"':
jbe@121 109 cbuf = luaL_buffinitsize(L, &luabuf, total-pos);
jbe@121 110 writepos = 0;
jbe@121 111 pos++;
jbe@121 112 while ((c = str[pos++]) != '"') {
jbe@121 113 if (c == 0) {
jbe@121 114 goto json_import_unexpected_eof;
jbe@121 115 } else if (c < 32 || c == 127) {
jbe@121 116 lua_pushnil(L);
jbe@121 117 lua_pushliteral(L, "Unexpected control character in JSON string");
jbe@121 118 return 2;
jbe@121 119 } else if (c == '\\') {
jbe@121 120 c = str[pos++];
jbe@121 121 switch (c) {
jbe@121 122 case 0:
jbe@121 123 goto json_import_unexpected_eof;
jbe@121 124 case '"':
jbe@121 125 case '/':
jbe@121 126 case '\\':
jbe@121 127 cbuf[writepos++] = c;
jbe@121 128 break;
jbe@121 129 case 'b':
jbe@121 130 cbuf[writepos++] = '\b';
jbe@121 131 break;
jbe@121 132 case 'f':
jbe@121 133 cbuf[writepos++] = '\f';
jbe@121 134 break;
jbe@121 135 case 'n':
jbe@121 136 cbuf[writepos++] = '\n';
jbe@121 137 break;
jbe@121 138 case 'r':
jbe@121 139 cbuf[writepos++] = '\r';
jbe@121 140 break;
jbe@121 141 case 't':
jbe@121 142 cbuf[writepos++] = '\t';
jbe@121 143 break;
jbe@121 144 case 'u':
jbe@121 145 lua_pushnil(L);
jbe@121 146 lua_pushliteral(L, "JSON unicode escape sequences are not implemented yet"); // TODO
jbe@121 147 return 2;
jbe@121 148 default:
jbe@121 149 lua_pushnil(L);
jbe@121 150 lua_pushliteral(L, "Unexpected string escape sequence in JSON document");
jbe@121 151 return 2;
jbe@121 152 }
jbe@121 153 } else {
jbe@121 154 cbuf[writepos++] = c;
jbe@121 155 }
jbe@121 156 }
jbe@121 157 if (!c) goto json_import_unexpected_eof;
jbe@121 158 luaL_pushresultsize(&luabuf, writepos);
jbe@121 159 goto json_import_process_value;
jbe@121 160 }
jbe@122 161 if (c == '-' || (c >= '0' && c <= '9')) {
jbe@122 162 char *endptr;
jbe@122 163 double numval;
jbe@122 164 numval = strtod(str+pos, &endptr);
jbe@122 165 if (endptr == str+pos) goto json_import_syntax_error;
jbe@122 166 pos += endptr - (str+pos);
jbe@122 167 lua_pushnumber(L, numval);
jbe@122 168 } else if (!strncmp(str+pos, "true", 4)) {
jbe@121 169 lua_pushboolean(L, 1);
jbe@121 170 pos += 4;
jbe@121 171 } else if (!strncmp(str+pos, "false", 5)) {
jbe@121 172 lua_pushboolean(L, 0);
jbe@121 173 pos += 5;
jbe@121 174 } else if (!strncmp(str+pos, "null", 4)) {
jbe@121 175 lua_pushnil(L);
jbe@121 176 pos += 4;
jbe@121 177 } else {
jbe@121 178 goto json_import_syntax_error;
jbe@121 179 }
jbe@121 180 json_import_process_value:
jbe@121 181 switch (mode) {
jbe@121 182 case JSON_OBJECT_KEY:
jbe@121 183 if (lua_type(L, -1) != LUA_TSTRING) goto json_import_syntax_error;
jbe@121 184 mode = JSON_OBJECT_KEY_TERMINATOR;
jbe@121 185 goto json_import_loop;
jbe@121 186 case JSON_OBJECT_VALUE:
jbe@123 187 if (lua_isnil(L, -1)) {
jbe@123 188 lua_pushvalue(L, -2);
jbe@123 189 lua_pushboolean(L, 1);
jbe@123 190 lua_rawset(L, -5);
jbe@123 191 }
jbe@123 192 lua_rawset(L, -4);
jbe@121 193 mode = JSON_OBJECT_SEPARATOR;
jbe@121 194 goto json_import_loop;
jbe@121 195 case JSON_ARRAY_VALUE:
jbe@121 196 aryidx = lua_tointeger(L, -2) + 1;
jbe@123 197 if (lua_isnil(L, -1)) {
jbe@123 198 lua_pushinteger(L, aryidx);
jbe@123 199 lua_pushboolean(L, 1);
jbe@123 200 lua_rawset(L, -5);
jbe@123 201 }
jbe@123 202 lua_rawseti(L, -4, aryidx);
jbe@121 203 lua_pop(L, 1);
jbe@121 204 lua_pushinteger(L, aryidx);
jbe@121 205 mode = JSON_ARRAY_SEPARATOR;
jbe@121 206 goto json_import_loop;
jbe@121 207 case JSON_VALUE:
jbe@121 208 mode = JSON_END;
jbe@121 209 goto json_import_loop;
jbe@121 210 }
jbe@121 211 json_import_syntax_error:
jbe@121 212 lua_pushnil(L);
jbe@121 213 lua_pushliteral(L, "Syntax error in JSON document");
jbe@121 214 return 2;
jbe@121 215 }
jbe@121 216
jbe@121 217 static int json_arylen(lua_State *L) {
jbe@121 218 lua_settop(L, 1);
jbe@123 219 lua_rawget(L, JSON_UPVAL_ARYLEN);
jbe@123 220 return 1;
jbe@123 221 }
jbe@123 222
jbe@123 223 static int json_isnull(lua_State *L) {
jbe@123 224 lua_pushvalue(L, 1);
jbe@123 225 lua_rawget(L, JSON_UPVAL_NULLS);
jbe@123 226 if (lua_isnil(L, -1)) goto json_isnull_false;
jbe@123 227 lua_pushvalue(L, 2);
jbe@123 228 lua_rawget(L, -2);
jbe@123 229 if (!lua_isnil(L, -1)) return 1;
jbe@123 230 json_isnull_false:
jbe@123 231 lua_pushboolean(L, 0);
jbe@121 232 return 1;
jbe@121 233 }
jbe@121 234
jbe@121 235 static const struct luaL_Reg json_module_functions[] = {
jbe@121 236 {"import", json_import},
jbe@121 237 {"arylen", json_arylen},
jbe@123 238 {"isnull", json_isnull},
jbe@121 239 {NULL, NULL}
jbe@121 240 };
jbe@121 241
jbe@121 242 int luaopen_json(lua_State *L) {
jbe@121 243 lua_newtable(L); // library table
jbe@121 244 lua_newtable(L); // ephemeron table to store the length of arrays (that may contain nil's)
jbe@123 245 lua_newtable(L); // ephemeron table to store a set of keys associated with JSON-null values
jbe@121 246 lua_newtable(L); // meta table for ephemeron table
jbe@121 247 lua_pushliteral(L, "__mode");
jbe@121 248 lua_pushliteral(L, "k");
jbe@121 249 lua_rawset(L, -3);
jbe@123 250 lua_pushvalue(L, -1);
jbe@123 251 lua_setmetatable(L, -3);
jbe@121 252 lua_setmetatable(L, -2);
jbe@123 253 luaL_setfuncs(L, json_module_functions, 2);
jbe@121 254 return 1;
jbe@121 255 }

Impressum / About Us