summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: ba0be16)
raw | patch | inline | side by side (parent: ba0be16)
author | Jim Radford <radford@galvanix.com> | |
Sat, 10 Aug 2013 17:25:02 +0000 (10:25 -0700) | ||
committer | Jim Radford <radford@galvanix.com> | |
Sat, 10 Aug 2013 17:33:28 +0000 (10:33 -0700) |
Allow access to data in JSON arrays. For example
{ workers: [ { requests: 10 }, { requests: 10 } ] }
can all be accessed with
workers/*/requests
or just one by using the index.
workers/1/requests
{ workers: [ { requests: 10 }, { requests: 10 } ] }
can all be accessed with
workers/*/requests
or just one by using the index.
workers/1/requests
src/curl_json.c | patch | blob | history |
diff --git a/src/curl_json.c b/src/curl_json.c
index deee460bb3327e6291d6536190ac68e7a7d87cb0..5bc3e4199a39b9ea86243e642226de31acf41f65 100644 (file)
--- a/src/curl_json.c
+++ b/src/curl_json.c
c_avl_tree_t *tree;
cj_key_t *key;
};
+ _Bool in_array;
+ int index;
char name[DATA_MAX_NAME_LEN];
} state[YAJL_MAX_DEPTH];
};
return ds->ds[0].type;
}
+static int cj_cb_map_key (void *ctx, const unsigned char *val,
+ yajl_len_t len);
+
+static void cj_cb_inc_array_index (void * ctx, _Bool ignore)
+{
+ cj_t *db = (cj_t *)ctx;
+
+ if (db->state[db->depth].in_array) {
+ if (ignore)
+ db->state[db->depth].index++;
+ else {
+ char name[DATA_MAX_NAME_LEN];
+ cj_cb_map_key (ctx, (unsigned char *)name,
+ ssnprintf (name, sizeof (name),
+ "%d", db->state[db->depth].index++));
+ }
+ }
+}
+
/* yajl callbacks */
#define CJ_CB_ABORT 0
#define CJ_CB_CONTINUE 1
+static int cj_cb_boolean (void * ctx, int boolVal)
+{
+ cj_cb_inc_array_index (ctx, 1);
+ return (CJ_CB_CONTINUE);
+}
+
+static int cj_cb_null (void * ctx)
+{
+ cj_cb_inc_array_index (ctx, 1);
+ return (CJ_CB_CONTINUE);
+}
+
/* "number" may not be null terminated, so copy it into a buffer before
* parsing. */
static int cj_cb_number (void *ctx,
int type;
int status;
- if ((key == NULL) || !CJ_IS_KEY (key))
+ if ((key == NULL) || !CJ_IS_KEY (key)) {
+ cj_cb_inc_array_index (ctx, 1);
return (CJ_CB_CONTINUE);
+ } else
+ cj_cb_inc_array_index (ctx, 0);
memcpy (buffer, number, number_len);
buffer[sizeof (buffer) - 1] = 0;
cj_t *db = (cj_t *)ctx;
char str[len + 1];
+ cj_cb_inc_array_index (ctx, 1);
+ return (CJ_CB_CONTINUE);
+
/* Create a null-terminated version of the string. */
memcpy (str, val, len);
str[len] = 0;
static int cj_cb_start_map (void *ctx)
{
+ cj_cb_inc_array_index (ctx, 0);
return cj_cb_start (ctx);
}
static int cj_cb_start_array (void * ctx)
{
+ cj_t *db = (cj_t *)ctx;
+ cj_cb_inc_array_index (ctx, 0);
+ if (db->depth+1 < YAJL_MAX_DEPTH) {
+ db->state[db->depth+1].in_array = 1;
+ db->state[db->depth+1].index = 0;
+ }
return cj_cb_start (ctx);
}
static int cj_cb_end_array (void * ctx)
{
+ cj_t *db = (cj_t *)ctx;
+ db->state[db->depth].in_array = 0;
return cj_cb_end (ctx);
}
static yajl_callbacks ycallbacks = {
- NULL, /* null */
- NULL, /* boolean */
+ cj_cb_null, /* null */
+ cj_cb_boolean, /* boolean */
NULL, /* integer */
NULL, /* double */
cj_cb_number,