py: Add native json printing using existing print framework.
Also add start of ujson module with dumps implemented. Enabled in unix and stmhal ports. Test passes on both.
This commit is contained in:
@@ -91,8 +91,44 @@ STATIC void uni_print_quoted(void (*print)(void *env, const char *fmt, ...), voi
|
||||
print(env, "%c", quote_char);
|
||||
}
|
||||
|
||||
#if MICROPY_PY_UJSON
|
||||
STATIC void uni_print_json(void (*print)(void *env, const char *fmt, ...), void *env, const byte *str_data, uint str_len) {
|
||||
print(env, "\"");
|
||||
const byte *s = str_data, *top = str_data + str_len;
|
||||
while (s < top) {
|
||||
unichar ch;
|
||||
ch = utf8_get_char(s);
|
||||
s = utf8_next_char(s);
|
||||
if (ch == '"' || ch == '\\' || ch == '/') {
|
||||
print(env, "\\%c", ch);
|
||||
} else if (32 <= ch && ch <= 126) {
|
||||
print(env, "%c", ch);
|
||||
} else if (*s == '\b') {
|
||||
print(env, "\\b");
|
||||
} else if (*s == '\f') {
|
||||
print(env, "\\f");
|
||||
} else if (*s == '\n') {
|
||||
print(env, "\\n");
|
||||
} else if (*s == '\r') {
|
||||
print(env, "\\r");
|
||||
} else if (*s == '\t') {
|
||||
print(env, "\\t");
|
||||
} else {
|
||||
print(env, "\\u%04x", ch);
|
||||
}
|
||||
}
|
||||
print(env, "\"");
|
||||
}
|
||||
#endif
|
||||
|
||||
STATIC void uni_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
|
||||
GET_STR_DATA_LEN(self_in, str_data, str_len);
|
||||
#if MICROPY_PY_UJSON
|
||||
if (kind == PRINT_JSON) {
|
||||
uni_print_json(print, env, str_data, str_len);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
if (kind == PRINT_STR) {
|
||||
print(env, "%.*s", str_len, str_data);
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user