aboutsummaryrefslogtreecommitdiff
path: root/extmod/modujson.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-09-17 22:56:34 +0100
committerDamien George <damien.p.george@gmail.com>2014-09-17 22:56:34 +0100
commit612045f53f7e5edf9faa359ee9ee52d490d58000 (patch)
treea021612e31ff6275246514405c4a8421c6eff5d7 /extmod/modujson.c
parent8a9b999f1c9a0edf84b2507940efb8deaaa380b8 (diff)
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.
Diffstat (limited to 'extmod/modujson.c')
-rw-r--r--extmod/modujson.c71
1 files changed, 71 insertions, 0 deletions
diff --git a/extmod/modujson.c b/extmod/modujson.c
new file mode 100644
index 000000000..2772f7d12
--- /dev/null
+++ b/extmod/modujson.c
@@ -0,0 +1,71 @@
+/*
+ * 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"
+#include "qstr.h"
+#include "obj.h"
+#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);
+
+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 },
+};
+
+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