| 1133 | } |
| 1134 | |
| 1135 | static void json_parse_object_context(lua_State *l, json_parse_t *json) |
| 1136 | { |
| 1137 | json_token_t token; |
| 1138 | |
| 1139 | /* 3 slots required: |
| 1140 | * .., table, key, value */ |
| 1141 | json_decode_descend(l, json, 3); |
| 1142 | |
| 1143 | lua_newtable(l); |
| 1144 | |
| 1145 | json_next_token(json, &token); |
| 1146 | |
| 1147 | /* Handle empty objects */ |
| 1148 | if (token.type == T_OBJ_END) { |
| 1149 | json_decode_ascend(json); |
| 1150 | return; |
| 1151 | } |
| 1152 | |
| 1153 | while (1) { |
| 1154 | if (token.type != T_STRING) |
| 1155 | json_throw_parse_error(l, json, "object key string", &token); |
| 1156 | |
| 1157 | /* Push key */ |
| 1158 | lua_pushlstring(l, token.value.string, token.string_len); |
| 1159 | |
| 1160 | json_next_token(json, &token); |
| 1161 | if (token.type != T_COLON) |
| 1162 | json_throw_parse_error(l, json, "colon", &token); |
| 1163 | |
| 1164 | /* Fetch value */ |
| 1165 | json_next_token(json, &token); |
| 1166 | json_process_value(l, json, &token); |
| 1167 | |
| 1168 | /* Set key = value */ |
| 1169 | lua_rawset(l, -3); |
| 1170 | |
| 1171 | json_next_token(json, &token); |
| 1172 | |
| 1173 | if (token.type == T_OBJ_END) { |
| 1174 | json_decode_ascend(json); |
| 1175 | return; |
| 1176 | } |
| 1177 | |
| 1178 | if (token.type != T_COMMA) |
| 1179 | json_throw_parse_error(l, json, "comma or object end", &token); |
| 1180 | |
| 1181 | json_next_token(json, &token); |
| 1182 | } |
| 1183 | } |
| 1184 | |
| 1185 | /* Handle the array context */ |
| 1186 | static void json_parse_array_context(lua_State *l, json_parse_t *json) |
no test coverage detected