2014-09-17 17:56:34 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2014 Damien P. George
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "mpconfig.h"
|
|
|
|
#include "misc.h"
|
2014-09-21 06:40:01 -04:00
|
|
|
#include "nlr.h"
|
2014-09-17 17:56:34 -04:00
|
|
|
#include "qstr.h"
|
|
|
|
#include "obj.h"
|
2014-09-21 06:40:01 -04:00
|
|
|
#include "objlist.h"
|
|
|
|
#include "parsenum.h"
|
2014-09-17 17:56:34 -04:00
|
|
|
#include "runtime.h"
|
|
|
|
|
|
|
|
#if MICROPY_PY_UJSON
|
|
|
|
|
|
|
|
STATIC mp_obj_t mod_ujson_dumps(mp_obj_t obj) {
|
|
|
|
vstr_t vstr;
|
|
|
|
vstr_init(&vstr, 8);
|
|
|
|
mp_obj_print_helper((void (*)(void *env, const char *fmt, ...))vstr_printf, &vstr, obj, PRINT_JSON);
|
|
|
|
mp_obj_t ret = mp_obj_new_str(vstr.buf, vstr.len, false);
|
|
|
|
vstr_clear(&vstr);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_ujson_dumps_obj, mod_ujson_dumps);
|
|
|
|
|
2014-09-21 06:40:01 -04:00
|
|
|
STATIC mp_obj_t mod_ujson_loads(mp_obj_t obj) {
|
|
|
|
mp_uint_t len;
|
|
|
|
const char *s = mp_obj_str_get_data(obj, &len);
|
|
|
|
const char *top = s + len;
|
|
|
|
vstr_t vstr;
|
|
|
|
vstr_init(&vstr, 8);
|
2014-09-21 15:38:02 -04:00
|
|
|
mp_obj_list_t stack;
|
|
|
|
stack.len = 0;
|
|
|
|
stack.items = NULL;
|
2014-09-21 06:40:01 -04:00
|
|
|
mp_obj_t stack_top = MP_OBJ_NULL;
|
2014-09-21 15:38:02 -04:00
|
|
|
mp_obj_type_t *stack_top_type = NULL;
|
2014-09-21 06:40:01 -04:00
|
|
|
mp_obj_t stack_key = MP_OBJ_NULL;
|
|
|
|
for (;;) {
|
|
|
|
cont:
|
|
|
|
if (s == top) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mp_obj_t next = MP_OBJ_NULL;
|
|
|
|
bool enter = false;
|
|
|
|
switch (*s) {
|
|
|
|
case ',':
|
|
|
|
case ':':
|
|
|
|
case ' ':
|
|
|
|
s += 1;
|
|
|
|
goto cont;
|
|
|
|
case 'n':
|
|
|
|
if (s + 3 < top && s[1] == 'u' && s[2] == 'l' && s[3] == 'l') {
|
|
|
|
s += 4;
|
|
|
|
next = mp_const_none;
|
|
|
|
} else {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
if (s + 4 < top && s[1] == 'a' && s[2] == 'l' && s[3] == 's' && s[4] == 'e') {
|
|
|
|
s += 5;
|
|
|
|
next = mp_const_false;
|
|
|
|
} else {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
if (s + 3 < top && s[1] == 'r' && s[2] == 'u' && s[3] == 'e') {
|
|
|
|
s += 4;
|
|
|
|
next = mp_const_true;
|
|
|
|
} else {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '"':
|
|
|
|
vstr_reset(&vstr);
|
|
|
|
for (s++; s < top && *s != '"'; s++) {
|
|
|
|
byte c = *s;
|
|
|
|
if (c == '\\') {
|
|
|
|
s++;
|
|
|
|
c = *s;
|
|
|
|
switch (c) {
|
2014-09-21 15:38:02 -04:00
|
|
|
case 'b': c = 0x08; break;
|
|
|
|
case 'f': c = 0x0c; break;
|
|
|
|
case 'n': c = 0x0a; break;
|
|
|
|
case 'r': c = 0x0d; break;
|
|
|
|
case 't': c = 0x09; break;
|
2014-09-21 06:40:01 -04:00
|
|
|
case 'u': if (s + 4 >= top) { goto fail; } else { assert(0); } //vstr_add_char(&vstr, s[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vstr_add_byte(&vstr, c);
|
|
|
|
}
|
|
|
|
if (s == top) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
next = mp_obj_new_str(vstr.buf, vstr.len, false);
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': {
|
|
|
|
bool flt = false;
|
|
|
|
vstr_reset(&vstr);
|
|
|
|
for (; s < top; s++) {
|
|
|
|
if (*s == '.' || *s == 'E' || *s == 'e') {
|
|
|
|
flt = true;
|
|
|
|
} else if (*s == '-' || unichar_isdigit(*s)) {
|
|
|
|
// pass
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
vstr_add_byte(&vstr, *s);
|
|
|
|
}
|
|
|
|
if (flt) {
|
|
|
|
next = mp_parse_num_decimal(vstr.buf, vstr.len, false, false);
|
|
|
|
} else {
|
|
|
|
next = mp_parse_num_integer(vstr.buf, vstr.len, 10);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case '[':
|
|
|
|
next = mp_obj_new_list(0, NULL);
|
|
|
|
enter = true;
|
|
|
|
s += 1;
|
|
|
|
break;
|
|
|
|
case '{':
|
|
|
|
next = mp_obj_new_dict(0);
|
|
|
|
enter = true;
|
|
|
|
s += 1;
|
|
|
|
break;
|
|
|
|
case '}':
|
|
|
|
case ']': {
|
|
|
|
s += 1;
|
|
|
|
if (stack_top == MP_OBJ_NULL) {
|
|
|
|
// no object at all
|
|
|
|
goto fail;
|
|
|
|
}
|
2014-09-21 15:38:02 -04:00
|
|
|
if (stack.len == 0) {
|
2014-09-21 06:40:01 -04:00
|
|
|
// finished; compound object
|
|
|
|
goto success;
|
|
|
|
}
|
2014-09-21 15:38:02 -04:00
|
|
|
stack.len -= 1;
|
|
|
|
stack_top = stack.items[stack.len];
|
2014-09-21 06:40:01 -04:00
|
|
|
stack_top_type = mp_obj_get_type(stack_top);
|
|
|
|
goto cont;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (stack_top == MP_OBJ_NULL) {
|
|
|
|
stack_top = next;
|
|
|
|
stack_top_type = mp_obj_get_type(stack_top);
|
|
|
|
if (!enter) {
|
|
|
|
// finished; single primitive only
|
|
|
|
goto success;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// append to list or dict
|
|
|
|
if (stack_top_type == &mp_type_list) {
|
|
|
|
mp_obj_list_append(stack_top, next);
|
|
|
|
} else {
|
|
|
|
if (stack_key == MP_OBJ_NULL) {
|
|
|
|
stack_key = next;
|
|
|
|
if (enter) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mp_obj_dict_store(stack_top, stack_key, next);
|
|
|
|
stack_key = MP_OBJ_NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (enter) {
|
2014-09-21 15:38:02 -04:00
|
|
|
if (stack.items == NULL) {
|
|
|
|
mp_obj_list_init(&stack, 1);
|
|
|
|
stack.items[0] = stack_top;
|
2014-09-21 06:40:01 -04:00
|
|
|
} else {
|
2014-09-21 15:38:02 -04:00
|
|
|
mp_obj_list_append(&stack, stack_top);
|
2014-09-21 06:40:01 -04:00
|
|
|
}
|
|
|
|
stack_top = next;
|
|
|
|
stack_top_type = mp_obj_get_type(stack_top);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
success:
|
|
|
|
// eat trailing whitespace
|
|
|
|
while (s < top && unichar_isspace(*s)) {
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
if (s < top) {
|
|
|
|
// unexpected chars
|
|
|
|
goto fail;
|
|
|
|
}
|
2014-09-21 15:38:02 -04:00
|
|
|
if (stack.len != 0) {
|
2014-09-21 06:40:01 -04:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
vstr_clear(&vstr);
|
|
|
|
return stack_top;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "syntax error in JSON"));
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_ujson_loads_obj, mod_ujson_loads);
|
|
|
|
|
2014-09-17 17:56:34 -04:00
|
|
|
STATIC const mp_map_elem_t mp_module_ujson_globals_table[] = {
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_ujson) },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_dumps), (mp_obj_t)&mod_ujson_dumps_obj },
|
2014-09-21 06:40:01 -04:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_loads), (mp_obj_t)&mod_ujson_loads_obj },
|
2014-09-17 17:56:34 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
STATIC const mp_obj_dict_t mp_module_ujson_globals = {
|
|
|
|
.base = {&mp_type_dict},
|
|
|
|
.map = {
|
|
|
|
.all_keys_are_qstrs = 1,
|
|
|
|
.table_is_fixed_array = 1,
|
|
|
|
.used = MP_ARRAY_SIZE(mp_module_ujson_globals_table),
|
|
|
|
.alloc = MP_ARRAY_SIZE(mp_module_ujson_globals_table),
|
|
|
|
.table = (mp_map_elem_t*)mp_module_ujson_globals_table,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const mp_obj_module_t mp_module_ujson = {
|
|
|
|
.base = { &mp_type_module },
|
|
|
|
.name = MP_QSTR_ujson,
|
|
|
|
.globals = (mp_obj_dict_t*)&mp_module_ujson_globals,
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif //MICROPY_PY_UJSON
|