2014-09-17 17:56:34 -04:00
|
|
|
/*
|
2016-10-12 20:43:28 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-09-17 17:56:34 -04:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2019-08-22 01:22:42 -04:00
|
|
|
* Copyright (c) 2014-2019 Damien P. George
|
2014-09-17 17:56:34 -04: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>
|
|
|
|
|
2023-10-19 21:29:57 -04:00
|
|
|
#include "py/binary.h"
|
|
|
|
#include "py/objarray.h"
|
2015-01-01 15:35:21 -05:00
|
|
|
#include "py/objlist.h"
|
2016-10-12 20:43:28 -04:00
|
|
|
#include "py/objstringio.h"
|
2015-01-01 15:35:21 -05:00
|
|
|
#include "py/parsenum.h"
|
|
|
|
#include "py/runtime.h"
|
2016-10-12 20:43:28 -04:00
|
|
|
#include "py/stream.h"
|
2014-09-17 17:56:34 -04:00
|
|
|
|
2022-08-18 01:01:26 -04:00
|
|
|
#if MICROPY_PY_JSON
|
2014-09-17 17:56:34 -04:00
|
|
|
|
2022-08-18 01:01:26 -04:00
|
|
|
#if MICROPY_PY_JSON_SEPARATORS
|
2021-02-03 03:24:25 -05:00
|
|
|
|
|
|
|
enum {
|
|
|
|
DUMP_MODE_TO_STRING = 1,
|
|
|
|
DUMP_MODE_TO_STREAM = 2,
|
|
|
|
};
|
|
|
|
|
2022-08-18 00:49:57 -04:00
|
|
|
STATIC mp_obj_t mod_json_dump_helper(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args, unsigned int mode) {
|
2021-02-03 03:24:25 -05:00
|
|
|
enum { ARG_separators };
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_separators, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
|
|
|
|
};
|
|
|
|
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
mp_arg_parse_all(n_args - mode, pos_args + mode, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
|
|
|
|
|
|
|
mp_print_ext_t print_ext;
|
|
|
|
|
|
|
|
if (args[ARG_separators].u_obj == mp_const_none) {
|
|
|
|
print_ext.item_separator = ", ";
|
|
|
|
print_ext.key_separator = ": ";
|
|
|
|
} else {
|
|
|
|
mp_obj_t *items;
|
|
|
|
mp_obj_get_array_fixed_n(args[ARG_separators].u_obj, 2, &items);
|
|
|
|
print_ext.item_separator = mp_obj_str_get_str(items[0]);
|
|
|
|
print_ext.key_separator = mp_obj_str_get_str(items[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode == DUMP_MODE_TO_STRING) {
|
|
|
|
// dumps(obj)
|
|
|
|
vstr_t vstr;
|
|
|
|
vstr_init_print(&vstr, 8, &print_ext.base);
|
|
|
|
mp_obj_print_helper(&print_ext.base, pos_args[0], PRINT_JSON);
|
2022-08-23 04:35:37 -04:00
|
|
|
return mp_obj_new_str_from_utf8_vstr(&vstr);
|
2021-02-03 03:24:25 -05:00
|
|
|
} else {
|
|
|
|
// dump(obj, stream)
|
|
|
|
print_ext.base.data = MP_OBJ_TO_PTR(pos_args[1]);
|
|
|
|
print_ext.base.print_strn = mp_stream_write_adaptor;
|
|
|
|
mp_get_stream_raise(pos_args[1], MP_STREAM_OP_WRITE);
|
|
|
|
mp_obj_print_helper(&print_ext.base, pos_args[0], PRINT_JSON);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-18 00:49:57 -04:00
|
|
|
STATIC mp_obj_t mod_json_dump(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
return mod_json_dump_helper(n_args, pos_args, kw_args, DUMP_MODE_TO_STREAM);
|
2021-02-03 03:24:25 -05:00
|
|
|
}
|
2022-08-18 00:49:57 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(mod_json_dump_obj, 2, mod_json_dump);
|
2021-02-03 03:24:25 -05:00
|
|
|
|
2022-08-18 00:49:57 -04:00
|
|
|
STATIC mp_obj_t mod_json_dumps(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
return mod_json_dump_helper(n_args, pos_args, kw_args, DUMP_MODE_TO_STRING);
|
2021-02-03 03:24:25 -05:00
|
|
|
}
|
2022-08-18 00:49:57 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(mod_json_dumps_obj, 1, mod_json_dumps);
|
2021-02-03 03:24:25 -05:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2022-08-18 00:49:57 -04:00
|
|
|
STATIC mp_obj_t mod_json_dump(mp_obj_t obj, mp_obj_t stream) {
|
2018-06-12 22:37:49 -04:00
|
|
|
mp_get_stream_raise(stream, MP_STREAM_OP_WRITE);
|
2018-02-05 23:35:52 -05: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;
|
|
|
|
}
|
2022-08-18 00:49:57 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_json_dump_obj, mod_json_dump);
|
2018-02-05 23:35:52 -05:00
|
|
|
|
2022-08-18 00:49:57 -04:00
|
|
|
STATIC mp_obj_t mod_json_dumps(mp_obj_t obj) {
|
2014-09-17 17:56:34 -04:00
|
|
|
vstr_t vstr;
|
2015-04-09 18:56:15 -04:00
|
|
|
mp_print_t print;
|
|
|
|
vstr_init_print(&vstr, 8, &print);
|
|
|
|
mp_obj_print_helper(&print, obj, PRINT_JSON);
|
2022-08-23 04:35:37 -04:00
|
|
|
return mp_obj_new_str_from_utf8_vstr(&vstr);
|
2014-09-17 17:56:34 -04:00
|
|
|
}
|
2022-08-18 00:49:57 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_json_dumps_obj, mod_json_dumps);
|
2014-09-17 17:56:34 -04:00
|
|
|
|
2021-02-03 03:24:25 -05:00
|
|
|
#endif
|
2014-09-17 17:56:34 -04:00
|
|
|
|
2020-05-10 23:45:42 -04:00
|
|
|
#define JSON_DEBUG(...) (void)0
|
|
|
|
// #define JSON_DEBUG(...) mp_printf(&mp_plat_print __VA_OPT__(,) __VA_ARGS__)
|
|
|
|
|
|
|
|
|
2016-10-12 20:43:28 -04:00
|
|
|
// The function below implements a simple non-recursive JSON parser.
|
2014-09-21 18:42:33 -04: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-12 20:43:28 -04:00
|
|
|
// strings). It does 1 pass over the input stream. It tries to be fast and
|
2014-09-21 18:42:33 -04:00
|
|
|
// small in code size, while not using more RAM than necessary.
|
2016-10-12 20:43:28 -04:00
|
|
|
|
2022-08-18 00:49:57 -04:00
|
|
|
typedef struct _json_stream_t {
|
2016-10-12 20:43:28 -04:00
|
|
|
mp_obj_t stream_obj;
|
|
|
|
mp_uint_t (*read)(mp_obj_t obj, void *buf, mp_uint_t size, int *errcode);
|
|
|
|
int errcode;
|
2023-10-19 16:42:36 -04:00
|
|
|
// CIRCUITPY-CHANGE
|
2020-07-10 19:42:32 -04:00
|
|
|
mp_obj_t python_readinto[2 + 1];
|
|
|
|
mp_obj_array_t bytearray_obj;
|
2020-11-19 20:13:24 -05:00
|
|
|
size_t start;
|
|
|
|
size_t end;
|
2016-10-12 20:43:28 -04:00
|
|
|
byte cur;
|
2022-08-18 00:49:57 -04:00
|
|
|
} json_stream_t;
|
2016-10-12 20:43:28 -04:00
|
|
|
|
|
|
|
#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)
|
2022-08-18 00:49:57 -04:00
|
|
|
#define S_NEXT(s) (json_stream_next(&(s)))
|
2016-10-12 20:43:28 -04:00
|
|
|
|
2022-08-18 00:49:57 -04:00
|
|
|
STATIC byte json_stream_next(json_stream_t *s) {
|
2016-10-12 20:43:28 -04:00
|
|
|
mp_uint_t ret = s->read(s->stream_obj, &s->cur, 1, &s->errcode);
|
2023-10-19 21:29:57 -04:00
|
|
|
JSON_DEBUG(" usjon_stream_next err:%2d cur: %c \n", s->errcode, s->cur);
|
2016-10-12 20:43:28 -04:00
|
|
|
if (s->errcode != 0) {
|
|
|
|
mp_raise_OSError(s->errcode);
|
|
|
|
}
|
|
|
|
if (ret == 0) {
|
|
|
|
s->cur = S_EOF;
|
|
|
|
}
|
|
|
|
return s->cur;
|
|
|
|
}
|
|
|
|
|
2023-10-19 16:42:36 -04:00
|
|
|
// CIRCUITPY-CHANGE
|
2023-08-01 13:50:05 -04:00
|
|
|
|
2020-11-19 20:13:24 -05:00
|
|
|
// We read from an object's `readinto` method in chunks larger than the json
|
|
|
|
// parser needs to reduce the number of function calls done.
|
|
|
|
|
|
|
|
#define CIRCUITPY_JSON_READ_CHUNK_SIZE 64
|
|
|
|
|
2023-10-18 17:49:14 -04:00
|
|
|
STATIC mp_uint_t json_python_readinto(mp_obj_t obj, void *buf, mp_uint_t size, int *errcode) {
|
2021-03-15 09:57:36 -04:00
|
|
|
(void)size; // Ignore size because we know it's always 1.
|
2023-10-19 21:29:57 -04:00
|
|
|
json_stream_t *s = obj;
|
2020-11-19 20:13:24 -05:00
|
|
|
|
|
|
|
if (s->start == s->end) {
|
|
|
|
*errcode = 0;
|
|
|
|
mp_obj_t ret = mp_call_method_n_kw(1, 0, s->python_readinto);
|
|
|
|
if (ret == mp_const_none) {
|
|
|
|
*errcode = MP_EAGAIN;
|
|
|
|
return MP_STREAM_ERROR;
|
|
|
|
}
|
|
|
|
s->start = 0;
|
|
|
|
s->end = mp_obj_get_int(ret);
|
2023-10-05 14:33:06 -04:00
|
|
|
if (s->end == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2020-07-10 19:42:32 -04:00
|
|
|
}
|
2020-11-19 20:13:24 -05:00
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
*((uint8_t *)buf) = ((uint8_t *)s->bytearray_obj.items)[s->start];
|
2020-11-19 20:13:24 -05:00
|
|
|
s->start++;
|
|
|
|
return 1;
|
2020-07-10 19:42:32 -04:00
|
|
|
}
|
|
|
|
|
2023-10-18 17:49:14 -04:00
|
|
|
STATIC mp_obj_t _mod_json_load(mp_obj_t stream_obj, bool return_first_json) {
|
2020-07-10 19:42:32 -04:00
|
|
|
const mp_stream_p_t *stream_p = mp_proto_get(MP_QSTR_protocol_stream, stream_obj);
|
2023-10-19 21:29:57 -04:00
|
|
|
json_stream_t s;
|
2020-11-19 20:13:24 -05:00
|
|
|
uint8_t character_buffer[CIRCUITPY_JSON_READ_CHUNK_SIZE];
|
2020-07-10 19:42:32 -04:00
|
|
|
if (stream_p == NULL) {
|
2020-11-19 20:13:24 -05:00
|
|
|
s.start = 0;
|
|
|
|
s.end = 0;
|
2020-07-10 19:42:32 -04:00
|
|
|
mp_load_method(stream_obj, MP_QSTR_readinto, s.python_readinto);
|
|
|
|
s.bytearray_obj.base.type = &mp_type_bytearray;
|
|
|
|
s.bytearray_obj.typecode = BYTEARRAY_TYPECODE;
|
2020-11-19 20:13:24 -05:00
|
|
|
s.bytearray_obj.len = CIRCUITPY_JSON_READ_CHUNK_SIZE;
|
2020-07-10 19:42:32 -04:00
|
|
|
s.bytearray_obj.free = 0;
|
2020-11-19 20:13:24 -05:00
|
|
|
s.bytearray_obj.items = character_buffer;
|
2020-07-10 19:42:32 -04:00
|
|
|
s.python_readinto[2] = MP_OBJ_FROM_PTR(&s.bytearray_obj);
|
|
|
|
s.stream_obj = &s;
|
2023-10-19 21:29:57 -04:00
|
|
|
s.read = json_python_readinto;
|
2020-07-10 19:42:32 -04:00
|
|
|
} else {
|
|
|
|
stream_p = mp_get_stream_raise(stream_obj, MP_STREAM_OP_READ);
|
|
|
|
s.stream_obj = stream_obj;
|
|
|
|
s.read = stream_p->read;
|
2020-07-10 20:33:17 -04:00
|
|
|
s.errcode = 0;
|
|
|
|
s.cur = 0;
|
2020-07-10 19:42:32 -04:00
|
|
|
}
|
|
|
|
|
2020-05-10 23:45:42 -04:00
|
|
|
JSON_DEBUG("got JSON stream\n");
|
2014-09-21 06:40:01 -04:00
|
|
|
vstr_t vstr;
|
|
|
|
vstr_init(&vstr, 8);
|
2014-09-21 18:42:33 -04:00
|
|
|
mp_obj_list_t stack; // we use a list as a simple stack for nested JSON
|
2014-09-21 15:38:02 -04:00
|
|
|
stack.len = 0;
|
|
|
|
stack.items = NULL;
|
2014-09-21 06:40:01 -04:00
|
|
|
mp_obj_t stack_top = MP_OBJ_NULL;
|
2020-01-08 19:01:14 -05:00
|
|
|
const mp_obj_type_t *stack_top_type = NULL;
|
2014-09-21 06:40:01 -04:00
|
|
|
mp_obj_t stack_key = MP_OBJ_NULL;
|
2016-10-12 20:43:28 -04:00
|
|
|
S_NEXT(s);
|
2014-09-21 06:40:01 -04:00
|
|
|
for (;;) {
|
2021-03-15 09:57:36 -04:00
|
|
|
cont:
|
2016-10-12 20:43:28 -04:00
|
|
|
if (S_END(s)) {
|
2014-09-21 06:40:01 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
mp_obj_t next = MP_OBJ_NULL;
|
|
|
|
bool enter = false;
|
2016-10-12 20:43:28 -04:00
|
|
|
byte cur = S_CUR(s);
|
|
|
|
S_NEXT(s);
|
|
|
|
switch (cur) {
|
2014-09-21 06:40:01 -04:00
|
|
|
case ',':
|
|
|
|
case ':':
|
|
|
|
case ' ':
|
2015-01-23 11:36:28 -05:00
|
|
|
case '\t':
|
|
|
|
case '\n':
|
|
|
|
case '\r':
|
2014-09-21 06:40:01 -04:00
|
|
|
goto cont;
|
|
|
|
case 'n':
|
2016-10-12 20:43:28 -04:00
|
|
|
if (S_CUR(s) == 'u' && S_NEXT(s) == 'l' && S_NEXT(s) == 'l') {
|
|
|
|
S_NEXT(s);
|
2014-09-21 06:40:01 -04:00
|
|
|
next = mp_const_none;
|
|
|
|
} else {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'f':
|
2016-10-12 20:43:28 -04:00
|
|
|
if (S_CUR(s) == 'a' && S_NEXT(s) == 'l' && S_NEXT(s) == 's' && S_NEXT(s) == 'e') {
|
|
|
|
S_NEXT(s);
|
2014-09-21 06:40:01 -04:00
|
|
|
next = mp_const_false;
|
|
|
|
} else {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 't':
|
2016-10-12 20:43:28 -04:00
|
|
|
if (S_CUR(s) == 'r' && S_NEXT(s) == 'u' && S_NEXT(s) == 'e') {
|
|
|
|
S_NEXT(s);
|
2014-09-21 06:40:01 -04:00
|
|
|
next = mp_const_true;
|
|
|
|
} else {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '"':
|
|
|
|
vstr_reset(&vstr);
|
2016-10-12 20:43:28 -04:00
|
|
|
for (; !S_END(s) && S_CUR(s) != '"';) {
|
|
|
|
byte c = S_CUR(s);
|
2014-09-21 06:40:01 -04:00
|
|
|
if (c == '\\') {
|
2016-10-12 20:43:28 -04:00
|
|
|
c = S_NEXT(s);
|
2014-09-21 06:40:01 -04:00
|
|
|
switch (c) {
|
2021-03-15 09:57:36 -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 18:28:40 -04:00
|
|
|
case 'u': {
|
|
|
|
mp_uint_t num = 0;
|
|
|
|
for (int i = 0; i < 4; i++) {
|
2016-10-12 20:43:28 -04:00
|
|
|
c = (S_NEXT(s) | 0x20) - '0';
|
2014-09-21 18:28:40 -04:00
|
|
|
if (c > 9) {
|
|
|
|
c -= ('a' - ('9' + 1));
|
|
|
|
}
|
|
|
|
num = (num << 4) | c;
|
|
|
|
}
|
|
|
|
vstr_add_char(&vstr, num);
|
|
|
|
goto str_cont;
|
|
|
|
}
|
2014-09-21 06:40:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
vstr_add_byte(&vstr, c);
|
2014-09-21 18:28:40 -04:00
|
|
|
str_cont:
|
2016-10-12 20:43:28 -04:00
|
|
|
S_NEXT(s);
|
2014-09-21 06:40:01 -04:00
|
|
|
}
|
2016-10-12 20:43:28 -04:00
|
|
|
if (S_END(s)) {
|
2014-09-21 06:40:01 -04:00
|
|
|
goto fail;
|
|
|
|
}
|
2016-10-12 20:43:28 -04:00
|
|
|
S_NEXT(s);
|
2017-11-15 21:17:51 -05:00
|
|
|
next = mp_obj_new_str(vstr.buf, vstr.len);
|
2014-09-21 06:40:01 -04:00
|
|
|
break;
|
|
|
|
case '-':
|
2021-03-15 09:57:36 -04:00
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
case '8':
|
|
|
|
case '9': {
|
2014-09-21 06:40:01 -04:00
|
|
|
bool flt = false;
|
|
|
|
vstr_reset(&vstr);
|
2016-10-12 20:43:28 -04:00
|
|
|
for (;;) {
|
|
|
|
vstr_add_byte(&vstr, cur);
|
|
|
|
cur = S_CUR(s);
|
|
|
|
if (cur == '.' || cur == 'E' || cur == 'e') {
|
2014-09-21 06:40:01 -04:00
|
|
|
flt = true;
|
2021-04-22 20:55:39 -04:00
|
|
|
} else if (cur == '+' || cur == '-' || unichar_isdigit(cur)) {
|
2014-09-21 06:40:01 -04:00
|
|
|
// pass
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2016-10-12 20:43:28 -04:00
|
|
|
S_NEXT(s);
|
2014-09-21 06:40:01 -04:00
|
|
|
}
|
|
|
|
if (flt) {
|
2022-06-21 20:37:48 -04:00
|
|
|
next = mp_parse_num_float(vstr.buf, vstr.len, false, NULL);
|
2014-09-21 06:40:01 -04:00
|
|
|
} else {
|
2015-02-07 20:57:40 -05:00
|
|
|
next = mp_parse_num_integer(vstr.buf, vstr.len, 10, NULL);
|
2014-09-21 06:40:01 -04: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 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 {
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_list_append(MP_OBJ_FROM_PTR(&stack), stack_top);
|
2014-09-21 06:40:01 -04:00
|
|
|
}
|
|
|
|
stack_top = next;
|
|
|
|
stack_top_type = mp_obj_get_type(stack_top);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-15 09:57:36 -04:00
|
|
|
success:
|
2023-10-19 16:42:36 -04:00
|
|
|
// CIRCUITPY-CHANGE
|
2023-08-01 13:50:05 -04:00
|
|
|
|
2020-05-11 00:56:01 -04:00
|
|
|
// It is legal for a stream to have contents after JSON.
|
|
|
|
// E.g., A UART is not closed after receiving an object; in load() we will
|
|
|
|
// return the first complete JSON object, while in loads() we will retain
|
|
|
|
// strict adherence to the buffer's complete semantic.
|
|
|
|
if (!return_first_json) {
|
|
|
|
while (unichar_isspace(S_CUR(s))) {
|
|
|
|
S_NEXT(s);
|
|
|
|
}
|
|
|
|
if (!S_END(s)) {
|
|
|
|
// unexpected chars
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
2015-02-02 16:52:19 -05:00
|
|
|
if (stack_top == MP_OBJ_NULL || stack.len != 0) {
|
|
|
|
// not exactly 1 object
|
2014-09-21 06:40:01 -04:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
vstr_clear(&vstr);
|
|
|
|
return stack_top;
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
fail:
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("syntax error in JSON"));
|
2014-09-21 06:40:01 -04:00
|
|
|
}
|
2020-05-11 00:56:01 -04:00
|
|
|
|
2023-10-18 17:49:14 -04:00
|
|
|
STATIC mp_obj_t mod_json_load(mp_obj_t stream_obj) {
|
|
|
|
return _mod_json_load(stream_obj, true);
|
2020-05-11 00:56:01 -04:00
|
|
|
}
|
2022-08-18 00:49:57 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_json_load_obj, mod_json_load);
|
2016-10-12 20:43:28 -04:00
|
|
|
|
2022-08-18 00:49:57 -04:00
|
|
|
STATIC mp_obj_t mod_json_loads(mp_obj_t obj) {
|
2021-04-23 15:26:42 -04:00
|
|
|
mp_buffer_info_t bufinfo;
|
|
|
|
mp_get_buffer_raise(obj, &bufinfo, MP_BUFFER_READ);
|
|
|
|
vstr_t vstr = {bufinfo.len, bufinfo.len, (char *)bufinfo.buf, true};
|
2017-07-04 20:38:20 -04:00
|
|
|
mp_obj_stringio_t sio = {{&mp_type_stringio}, &vstr, 0, MP_OBJ_NULL};
|
2023-10-20 19:56:30 -04:00
|
|
|
return _mod_json_load(MP_OBJ_FROM_PTR(&sio), false);
|
2016-10-12 20:43:28 -04:00
|
|
|
}
|
2022-08-18 00:49:57 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_json_loads_obj, mod_json_loads);
|
2014-09-21 06:40:01 -04:00
|
|
|
|
2022-08-18 00:47:56 -04:00
|
|
|
STATIC const mp_rom_map_elem_t mp_module_json_globals_table[] = {
|
2019-02-21 00:33:36 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_json) },
|
2022-08-18 00:49:57 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_dump), MP_ROM_PTR(&mod_json_dump_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_dumps), MP_ROM_PTR(&mod_json_dumps_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_load), MP_ROM_PTR(&mod_json_load_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_loads), MP_ROM_PTR(&mod_json_loads_obj) },
|
2014-09-17 17:56:34 -04:00
|
|
|
};
|
|
|
|
|
2022-08-18 00:47:56 -04:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(mp_module_json_globals, mp_module_json_globals_table);
|
2014-09-17 17:56:34 -04:00
|
|
|
|
2022-08-18 00:47:56 -04:00
|
|
|
const mp_obj_module_t mp_module_json = {
|
2014-09-17 17:56:34 -04:00
|
|
|
.base = { &mp_type_module },
|
2022-08-18 00:47:56 -04:00
|
|
|
.globals = (mp_obj_dict_t *)&mp_module_json_globals,
|
2014-09-17 17:56:34 -04:00
|
|
|
};
|
|
|
|
|
2023-06-01 22:33:25 -04:00
|
|
|
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_json, mp_module_json);
|
2021-10-05 09:15:44 -04:00
|
|
|
|
2022-08-18 01:01:26 -04:00
|
|
|
#endif // MICROPY_PY_JSON
|