summaryrefslogtreecommitdiff
path: root/core/json.c
diff options
context:
space:
mode:
authorUnbit <info@unbit.it>2013-12-19 12:30:26 +0100
committerUnbit <info@unbit.it>2013-12-19 12:30:26 +0100
commit39fbea6d4851a6449de9fff2dbc6a68a1e4f3c57 (patch)
tree47c6007d766a56f01f73fc994948b681935a58df /core/json.c
parent4796f852776bf6ee3dc80e4df5b842e3279bb2bd (diff)
downloaduwsgi-39fbea6d4851a6449de9fff2dbc6a68a1e4f3c57.tar.gz
added support for yajl library
Diffstat (limited to 'core/json.c')
-rw-r--r--core/json.c86
1 files changed, 82 insertions, 4 deletions
diff --git a/core/json.c b/core/json.c
index a30e18f6..49d313e0 100644
--- a/core/json.c
+++ b/core/json.c
@@ -4,12 +4,89 @@
extern struct uwsgi_server uwsgi;
-#include <jansson.h>
+#ifdef UWSGI_JSON_YAJL
+#include <yajl/yajl_tree.h>
+
+void uwsgi_json_config(char *file, char *magic_table[]) {
+ size_t len = 0;
+
+ char *object_asked = "uwsgi";
+ char *colon;
+
+ if (uwsgi_check_scheme(file)) {
+ colon = uwsgi_get_last_char(file, '/');
+ colon = uwsgi_get_last_char(colon, ':');
+ }
+ else {
+ colon = uwsgi_get_last_char(file, ':');
+ }
+
+ if (colon) {
+ colon[0] = 0;
+ if (colon[1] != 0) {
+ object_asked = colon + 1;
+ }
+ }
+
+ uwsgi_log_initial("[uWSGI] getting JSON configuration from %s\n", file);
+
+ char *json_data = uwsgi_open_and_read(file, &len, 1, magic_table);
+
+ char errbuf[1024];
+ yajl_val node = yajl_tree_parse((const char *)json_data, errbuf, sizeof(errbuf));
+
+ if (!node) {
+ uwsgi_log("error parsing JSON data: %s\n", errbuf);
+ exit(1);
+ }
+ const char * path[] = { object_asked, NULL };
+ yajl_val v = yajl_tree_get(node, path, yajl_t_any);
+ if (!YAJL_IS_OBJECT(v)) {
+ uwsgi_log("you must define a object named %s in your JSON data\n", object_asked);
+ exit(1);
+ }
+
+ size_t i;
+ for(i=0;i<v->u.object.len;i++) {
+ char *key = (char *) v->u.object.keys[i];
+ yajl_val o = v->u.object.values[i];
+ if (YAJL_IS_STRING(o)) {
+ add_exported_option(key, YAJL_GET_STRING(o) , 0);
+ }
+ else if (YAJL_IS_TRUE(o)) {
+ add_exported_option(key, strdup("1"), 0);
+ }
+ else if (YAJL_IS_FALSE(o) || YAJL_IS_NULL(o)) {
+ add_exported_option(key, strdup("0"), 0);
+ }
+ else if (YAJL_IS_NUMBER(o) || YAJL_IS_INTEGER(o)) {
+ add_exported_option(key, YAJL_GET_NUMBER(o), 0);
+ }
+ else if (YAJL_IS_ARRAY(o)) {
+ size_t j;
+ for(j=0;j<o->u.array.len;j++) {
+ yajl_val a_o = o->u.array.values[j];
+ if (YAJL_IS_STRING(a_o)) {
+ add_exported_option(key, YAJL_GET_STRING(a_o) , 0);
+ }
+ else if (YAJL_IS_TRUE(a_o)) {
+ add_exported_option(key, strdup("1"), 0);
+ }
+ else if (YAJL_IS_FALSE(a_o) || YAJL_IS_NULL(a_o)) {
+ add_exported_option(key, strdup("0"), 0);
+ }
+ else if (YAJL_IS_NUMBER(a_o) || YAJL_IS_INTEGER(a_o)) {
+ add_exported_option(key, YAJL_GET_NUMBER(a_o), 0);
+ }
+ }
+ }
+ }
+}
+#else
+
+#include <jansson.h>
-/*
- I really love the jansson library...
- */
void uwsgi_json_config(char *file, char *magic_table[]) {
@@ -111,3 +188,4 @@ void uwsgi_json_config(char *file, char *magic_table[]) {
}
#endif
+#endif