2014-09-17 22:56:34 +01:00
|
|
|
/*
|
2016-10-13 01:43:28 +01:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-09-17 22:56:34 +01:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2019-08-22 06:22:42 +01:00
|
|
|
* Copyright (c) 2014-2019 Damien P. George
|
2014-09-17 22:56:34 +01:00
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
|
2015-01-01 20:35:21 +00:00
|
|
|
#include "py/objlist.h"
|
2016-10-13 01:43:28 +01:00
|
|
|
#include "py/objstringio.h"
|
2015-01-01 20:35:21 +00:00
|
|
|
#include "py/parsenum.h"
|
|
|
|
#include "py/runtime.h"
|
2016-10-13 01:43:28 +01:00
|
|
|
#include "py/stream.h"
|
2014-09-17 22:56:34 +01:00
|
|
|
|
|
|
|
#if MICROPY_PY_UJSON
|
|
|
|
|
2018-02-06 04:35:52 +00:00
|
|
|
STATIC mp_obj_t mod_ujson_dump(mp_obj_t obj, mp_obj_t stream) {
|
2018-06-13 03:37:49 +01:00
|
|
|
mp_get_stream_raise(stream, MP_STREAM_OP_WRITE);
|
2018-02-06 04:35:52 +00:00
|
|
|
mp_print_t print = {MP_OBJ_TO_PTR(stream), mp_stream_write_adaptor};
|
|
|
|
mp_obj_print_helper(&print, obj, PRINT_JSON);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_ujson_dump_obj, mod_ujson_dump);
|
|
|
|
|
2014-09-17 22:56:34 +01:00
|
|
|
STATIC mp_obj_t mod_ujson_dumps(mp_obj_t obj) {
|
|
|
|
vstr_t vstr;
|
2015-04-09 23:56:15 +01:00
|
|
|
mp_print_t print;
|
|
|
|
vstr_init_print(&vstr, 8, &print);
|
|
|
|
mp_obj_print_helper(&print, obj, PRINT_JSON);
|
2015-01-28 23:43:01 +00:00
|
|
|
return mp_obj_new_str_from_vstr(&mp_type_str, &vstr);
|
2014-09-17 22:56:34 +01:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_ujson_dumps_obj, mod_ujson_dumps);
|
|
|
|
|
2016-10-13 01:43:28 +01:00
|
|
|
// The function below implements a simple non-recursive JSON parser.
|
2014-09-21 23:42:33 +01:00
|
|
|
//
|
|
|
|
// The JSON specification is at http://www.ietf.org/rfc/rfc4627.txt
|
|
|
|
// The parser here will parse any valid JSON and return the correct
|
|
|
|
// corresponding Python object. It allows through a superset of JSON, since
|
|
|
|
// it treats commas and colons as "whitespace", and doesn't care if
|
|
|
|
// brackets/braces are correctly paired. It will raise a ValueError if the
|
|
|
|
// input is outside it's specs.
|
|
|
|
//
|
|
|
|
// Most of the work is parsing the primitives (null, false, true, numbers,
|
2016-10-13 01:43:28 +01:00
|
|
|
// strings). It does 1 pass over the input stream. It tries to be fast and
|
2014-09-21 23:42:33 +01:00
|
|
|
// small in code size, while not using more RAM than necessary.
|
2016-10-13 01:43:28 +01:00
|
|
|
|
|
|
|
typedef struct _ujson_stream_t {
|
|
|
|
mp_obj_t stream_obj;
|
|
|
|
mp_uint_t (*read)(mp_obj_t obj, void *buf, mp_uint_t size, int *errcode);
|
|
|
|
int errcode;
|
|
|
|
byte cur;
|
|
|
|
} ujson_stream_t;
|
|
|
|
|
|
|
|
#define S_EOF (0) // null is not allowed in json stream so is ok as EOF marker
|
|
|
|
#define S_END(s) ((s).cur == S_EOF)
|
|
|
|
#define S_CUR(s) ((s).cur)
|
|
|
|
#define S_NEXT(s) (ujson_stream_next(&(s)))
|
|
|
|
|
|
|
|
STATIC byte ujson_stream_next(ujson_stream_t *s) {
|
|
|
|
mp_uint_t ret = s->read(s->stream_obj, &s->cur, 1, &s->errcode);
|
|
|
|
if (s->errcode != 0) {
|
|
|
|
mp_raise_OSError(s->errcode);
|
|
|
|
}
|
|
|
|
if (ret == 0) {
|
|
|
|
s->cur = S_EOF;
|
|
|
|
}
|
|
|
|
return s->cur;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_obj_t mod_ujson_load(mp_obj_t stream_obj) {
|
|
|
|
const mp_stream_p_t *stream_p = mp_get_stream_raise(stream_obj, MP_STREAM_OP_READ);
|
|
|
|
ujson_stream_t s = {stream_obj, stream_p->read, 0, 0};
|
2014-09-21 11:40:01 +01:00
|
|
|
vstr_t vstr;
|
|
|
|
vstr_init(&vstr, 8);
|
2014-09-21 23:42:33 +01:00
|
|
|
mp_obj_list_t stack; // we use a list as a simple stack for nested JSON
|
2014-09-21 20:38:02 +01:00
|
|
|
stack.len = 0;
|
|
|
|
stack.items = NULL;
|
2014-09-21 11:40:01 +01:00
|
|
|
mp_obj_t stack_top = MP_OBJ_NULL;
|
2020-01-09 00:01:14 +00:00
|
|
|
const mp_obj_type_t *stack_top_type = NULL;
|
2014-09-21 11:40:01 +01:00
|
|
|
mp_obj_t stack_key = MP_OBJ_NULL;
|
2016-10-13 01:43:28 +01:00
|
|
|
S_NEXT(s);
|
2014-09-21 11:40:01 +01:00
|
|
|
for (;;) {
|
2020-02-27 04:36:53 +00:00
|
|
|
cont:
|
2016-10-13 01:43:28 +01:00
|
|
|
if (S_END(s)) {
|
2014-09-21 11:40:01 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
mp_obj_t next = MP_OBJ_NULL;
|
|
|
|
bool enter = false;
|
2016-10-13 01:43:28 +01:00
|
|
|
byte cur = S_CUR(s);
|
|
|
|
S_NEXT(s);
|
|
|
|
switch (cur) {
|
2014-09-21 11:40:01 +01:00
|
|
|
case ',':
|
|
|
|
case ':':
|
|
|
|
case ' ':
|
2015-01-23 16:36:28 +00:00
|
|
|
case '\t':
|
|
|
|
case '\n':
|
|
|
|
case '\r':
|
2014-09-21 11:40:01 +01:00
|
|
|
goto cont;
|
|
|
|
case 'n':
|
2016-10-13 01:43:28 +01:00
|
|
|
if (S_CUR(s) == 'u' && S_NEXT(s) == 'l' && S_NEXT(s) == 'l') {
|
|
|
|
S_NEXT(s);
|
2014-09-21 11:40:01 +01:00
|
|
|
next = mp_const_none;
|
|
|
|
} else {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'f':
|
2016-10-13 01:43:28 +01:00
|
|
|
if (S_CUR(s) == 'a' && S_NEXT(s) == 'l' && S_NEXT(s) == 's' && S_NEXT(s) == 'e') {
|
|
|
|
S_NEXT(s);
|
2014-09-21 11:40:01 +01:00
|
|
|
next = mp_const_false;
|
|
|
|
} else {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 't':
|
2016-10-13 01:43:28 +01:00
|
|
|
if (S_CUR(s) == 'r' && S_NEXT(s) == 'u' && S_NEXT(s) == 'e') {
|
|
|
|
S_NEXT(s);
|
2014-09-21 11:40:01 +01:00
|
|
|
next = mp_const_true;
|
|
|
|
} else {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '"':
|
|
|
|
vstr_reset(&vstr);
|
2016-10-13 01:43:28 +01:00
|
|
|
for (; !S_END(s) && S_CUR(s) != '"';) {
|
|
|
|
byte c = S_CUR(s);
|
2014-09-21 11:40:01 +01:00
|
|
|
if (c == '\\') {
|
2016-10-13 01:43:28 +01:00
|
|
|
c = S_NEXT(s);
|
2014-09-21 11:40:01 +01:00
|
|
|
switch (c) {
|
2020-02-27 04:36:53 +00: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 23:28:40 +01:00
|
|
|
case 'u': {
|
|
|
|
mp_uint_t num = 0;
|
|
|
|
for (int i = 0; i < 4; i++) {
|
2016-10-13 01:43:28 +01:00
|
|
|
c = (S_NEXT(s) | 0x20) - '0';
|
2014-09-21 23:28:40 +01:00
|
|
|
if (c > 9) {
|
|
|
|
c -= ('a' - ('9' + 1));
|
|
|
|
}
|
|
|
|
num = (num << 4) | c;
|
|
|
|
}
|
|
|
|
vstr_add_char(&vstr, num);
|
|
|
|
goto str_cont;
|
|
|
|
}
|
2014-09-21 11:40:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
vstr_add_byte(&vstr, c);
|
2014-09-21 23:28:40 +01:00
|
|
|
str_cont:
|
2016-10-13 01:43:28 +01:00
|
|
|
S_NEXT(s);
|
2014-09-21 11:40:01 +01:00
|
|
|
}
|
2016-10-13 01:43:28 +01:00
|
|
|
if (S_END(s)) {
|
2014-09-21 11:40:01 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
2016-10-13 01:43:28 +01:00
|
|
|
S_NEXT(s);
|
2017-11-16 02:17:51 +00:00
|
|
|
next = mp_obj_new_str(vstr.buf, vstr.len);
|
2014-09-21 11:40:01 +01:00
|
|
|
break;
|
|
|
|
case '-':
|
2020-02-27 04:36:53 +00:00
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
case '8':
|
|
|
|
case '9': {
|
2014-09-21 11:40:01 +01:00
|
|
|
bool flt = false;
|
|
|
|
vstr_reset(&vstr);
|
2016-10-13 01:43:28 +01:00
|
|
|
for (;;) {
|
|
|
|
vstr_add_byte(&vstr, cur);
|
|
|
|
cur = S_CUR(s);
|
|
|
|
if (cur == '.' || cur == 'E' || cur == 'e') {
|
2014-09-21 11:40:01 +01:00
|
|
|
flt = true;
|
2019-05-14 05:45:54 +01:00
|
|
|
} else if (cur == '+' || cur == '-' || unichar_isdigit(cur)) {
|
2014-09-21 11:40:01 +01:00
|
|
|
// pass
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2016-10-13 01:43:28 +01:00
|
|
|
S_NEXT(s);
|
2014-09-21 11:40:01 +01:00
|
|
|
}
|
|
|
|
if (flt) {
|
2015-02-08 01:57:40 +00:00
|
|
|
next = mp_parse_num_decimal(vstr.buf, vstr.len, false, false, NULL);
|
2014-09-21 11:40:01 +01:00
|
|
|
} else {
|
2015-02-08 01:57:40 +00:00
|
|
|
next = mp_parse_num_integer(vstr.buf, vstr.len, 10, NULL);
|
2014-09-21 11:40:01 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case '[':
|
|
|
|
next = mp_obj_new_list(0, NULL);
|
|
|
|
enter = true;
|
|
|
|
break;
|
|
|
|
case '{':
|
|
|
|
next = mp_obj_new_dict(0);
|
|
|
|
enter = true;
|
|
|
|
break;
|
|
|
|
case '}':
|
|
|
|
case ']': {
|
|
|
|
if (stack_top == MP_OBJ_NULL) {
|
|
|
|
// no object at all
|
|
|
|
goto fail;
|
|
|
|
}
|
2014-09-21 20:38:02 +01:00
|
|
|
if (stack.len == 0) {
|
2014-09-21 11:40:01 +01:00
|
|
|
// finished; compound object
|
|
|
|
goto success;
|
|
|
|
}
|
2014-09-21 20:38:02 +01:00
|
|
|
stack.len -= 1;
|
|
|
|
stack_top = stack.items[stack.len];
|
2014-09-21 11:40:01 +01: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 20:38:02 +01:00
|
|
|
if (stack.items == NULL) {
|
|
|
|
mp_obj_list_init(&stack, 1);
|
|
|
|
stack.items[0] = stack_top;
|
2014-09-21 11:40:01 +01:00
|
|
|
} else {
|
2015-11-27 17:01:44 +00:00
|
|
|
mp_obj_list_append(MP_OBJ_FROM_PTR(&stack), stack_top);
|
2014-09-21 11:40:01 +01:00
|
|
|
}
|
|
|
|
stack_top = next;
|
|
|
|
stack_top_type = mp_obj_get_type(stack_top);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-27 04:36:53 +00:00
|
|
|
success:
|
2014-09-21 11:40:01 +01:00
|
|
|
// eat trailing whitespace
|
2016-10-13 01:43:28 +01:00
|
|
|
while (unichar_isspace(S_CUR(s))) {
|
|
|
|
S_NEXT(s);
|
2014-09-21 11:40:01 +01:00
|
|
|
}
|
2016-10-13 01:43:28 +01:00
|
|
|
if (!S_END(s)) {
|
2014-09-21 11:40:01 +01:00
|
|
|
// unexpected chars
|
|
|
|
goto fail;
|
|
|
|
}
|
2015-02-02 21:52:19 +00:00
|
|
|
if (stack_top == MP_OBJ_NULL || stack.len != 0) {
|
|
|
|
// not exactly 1 object
|
2014-09-21 11:40:01 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
vstr_clear(&vstr);
|
|
|
|
return stack_top;
|
|
|
|
|
2020-02-27 04:36:53 +00:00
|
|
|
fail:
|
2020-03-02 11:35:22 +00:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("syntax error in JSON"));
|
2014-09-21 11:40:01 +01:00
|
|
|
}
|
2016-10-13 01:43:28 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_ujson_load_obj, mod_ujson_load);
|
|
|
|
|
|
|
|
STATIC mp_obj_t mod_ujson_loads(mp_obj_t obj) {
|
2019-08-22 06:22:42 +01:00
|
|
|
mp_buffer_info_t bufinfo;
|
|
|
|
mp_get_buffer_raise(obj, &bufinfo, MP_BUFFER_READ);
|
2020-02-27 04:36:53 +00:00
|
|
|
vstr_t vstr = {bufinfo.len, bufinfo.len, (char *)bufinfo.buf, true};
|
2017-07-05 01:38:20 +01:00
|
|
|
mp_obj_stringio_t sio = {{&mp_type_stringio}, &vstr, 0, MP_OBJ_NULL};
|
2016-10-13 02:09:18 +01:00
|
|
|
return mod_ujson_load(MP_OBJ_FROM_PTR(&sio));
|
2016-10-13 01:43:28 +01:00
|
|
|
}
|
2014-09-21 11:40:01 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_ujson_loads_obj, mod_ujson_loads);
|
|
|
|
|
2015-11-27 13:38:15 +00:00
|
|
|
STATIC const mp_rom_map_elem_t mp_module_ujson_globals_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ujson) },
|
2018-02-06 04:35:52 +00:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_dump), MP_ROM_PTR(&mod_ujson_dump_obj) },
|
2015-11-27 13:38:15 +00:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_dumps), MP_ROM_PTR(&mod_ujson_dumps_obj) },
|
2016-10-13 01:43:28 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_load), MP_ROM_PTR(&mod_ujson_load_obj) },
|
2015-11-27 13:38:15 +00:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_loads), MP_ROM_PTR(&mod_ujson_loads_obj) },
|
2014-09-17 22:56:34 +01:00
|
|
|
};
|
|
|
|
|
2014-11-29 14:39:27 +00:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(mp_module_ujson_globals, mp_module_ujson_globals_table);
|
2014-09-17 22:56:34 +01:00
|
|
|
|
|
|
|
const mp_obj_module_t mp_module_ujson = {
|
|
|
|
.base = { &mp_type_module },
|
2020-02-27 04:36:53 +00:00
|
|
|
.globals = (mp_obj_dict_t *)&mp_module_ujson_globals,
|
2014-09-17 22:56:34 +01:00
|
|
|
};
|
|
|
|
|
2020-04-16 08:13:57 +01:00
|
|
|
#endif // MICROPY_PY_UJSON
|