void append_card_to_object(JsonNode *obj, char *user, char *device) { char path[BUFSIZ], path1[BUFSIZ], *cardfile = NULL; JsonNode *card; if (!user || !*user) return; snprintf(path, BUFSIZ, "%s/cards/%s/%s/%s-%s.json", STORAGEDIR, user, device, user, device); if (access(path, R_OK) == 0) { cardfile = path; } else { snprintf(path1, BUFSIZ, "%s/cards/%s/%s.json", STORAGEDIR, user, user); if (access(path1, R_OK) == 0) { cardfile = path1; } } card = json_mkobject(); if (cardfile && json_copy_from_file(card, cardfile) == TRUE) { json_copy_to_object(obj, card, FALSE); } json_delete(card); }
static JsonNode *line_to_location(char *line) { JsonNode *json, *o, *j; char *ghash; char tstamp[64], *bp; double lat, lon; long tst; snprintf(tstamp, 21, "%s", line); if ((bp = strchr(line, '{')) == NULL) return (NULL); if ((json = json_decode(bp)) == NULL) { return (NULL); } if ((j = json_find_member(json, "_type")) == NULL) { json_delete(json); return (NULL); } if (j->tag != JSON_STRING || strcmp(j->string_, "location") != 0) { json_delete(json); return (NULL); } o = json_mkobject(); if (json_copy_to_object(o, json, FALSE) == FALSE) { json_delete(o); json_delete(json); return (NULL); } json_delete(json); /* Done with this -- we've copied it. */ lat = lon = 0.0; if ((j = json_find_member(o, "lat")) != NULL) { lat = j->number_; } if ((j = json_find_member(o, "lon")) != NULL) { lon = j->number_; } if ((ghash = geohash_encode(lat, lon, geohash_prec())) != NULL) { json_append_member(o, "ghash", json_mkstring(ghash)); get_geo(o, ghash); free(ghash); } json_append_member(o, "isorcv", json_mkstring(tstamp)); tst = 0L; if ((j = json_find_member(o, "tst")) != NULL) { tst = j->number_; } json_append_member(o, "isotst", json_mkstring(isotime(tst))); return (o); }
void get_geo(JsonNode *o, char *ghash) { JsonNode *geo; if ((geo = gcache_json_get(gc, ghash)) != NULL) { json_copy_to_object(o, geo, FALSE); json_delete(geo); } }
int json_copy_to_object(JsonNode * obj, JsonNode * object_or_array, int clobber) { JsonNode *node; if (obj->tag != JSON_OBJECT && obj->tag != JSON_ARRAY) return (FALSE); json_foreach(node, object_or_array) { if (!clobber & (json_find_member(obj, node->key) != NULL)) continue; /* Don't clobber existing keys */ if (obj->tag == JSON_OBJECT) { if (node->tag == JSON_STRING) json_append_member(obj, node->key, json_mkstring(node->string_)); else if (node->tag == JSON_NUMBER) json_append_member(obj, node->key, json_mknumber(node->number_)); else if (node->tag == JSON_BOOL) json_append_member(obj, node->key, json_mkbool(node->bool_)); else if (node->tag == JSON_NULL) json_append_member(obj, node->key, json_mknull()); else if (node->tag == JSON_ARRAY) { JsonNode *array = json_mkarray(); json_copy_to_object(array, node, clobber); json_append_member(obj, node->key, array); } else if (node->tag == JSON_OBJECT) { JsonNode *newobj = json_mkobject(); json_copy_to_object(newobj, node, clobber); json_append_member(obj, node->key, newobj); } else printf("PANIC: unhandled JSON type %d\n", node->tag); } else if (obj->tag == JSON_ARRAY) { if (node->tag == JSON_STRING) json_append_element(obj, json_mkstring(node->string_)); if (node->tag == JSON_NUMBER) json_append_element(obj, json_mknumber(node->number_)); if (node->tag == JSON_BOOL) json_append_element(obj, json_mkbool(node->bool_)); if (node->tag == JSON_NULL) json_append_element(obj, json_mknull()); } } return (TRUE); }
int json_copy_from_file(JsonNode *obj, char *filename) { char *js_string; JsonNode *node; if ((js_string = slurp_file(filename, TRUE)) == NULL) { return (FALSE); } if ((node = json_decode(js_string)) == NULL) { fprintf(stderr, "json_copy_from_file can't decode JSON from %s\n", filename); free(js_string); return (FALSE); } json_copy_to_object(obj, node, FALSE); json_delete(node); free(js_string); return (TRUE); }
void append_device_details(JsonNode *userlist, char *user, char *device) { char path[BUFSIZ]; JsonNode *node, *last, *card; snprintf(path, BUFSIZ, "%s/last/%s/%s/%s-%s.json", STORAGEDIR, user, device, user, device); last = json_mkobject(); if (json_copy_from_file(last, path) == TRUE) { JsonNode *tst; if ((tst = json_find_member(last, "tst")) != NULL) { json_append_member(last, "isotst", json_mkstring(isotime(tst->number_))); } json_append_element(userlist, last); } else { json_delete(last); } snprintf(path, BUFSIZ, "%s/cards/%s/%s.json", STORAGEDIR, user, user); card = json_mkobject(); if (json_copy_from_file(card, path) == TRUE) { json_copy_to_object(last, card, FALSE); } json_delete(card); if ((node = json_find_member(last, "ghash")) != NULL) { if (node->tag == JSON_STRING) { get_geo(last, node->string_); } } }
int main(int argc, char **argv) { int c, showversion = FALSE; char *kv, *js_string, *progname, buf[BUFSIZ], *p; int ttyin = isatty(fileno(stdin)), ttyout = isatty(fileno(stdout)); int flags = 0; JsonNode *json, *op; #if HAVE_PLEDGE if (pledge("stdio", NULL) == -1) { err(1, "pledge"); } #endif progname = (progname = strrchr(*argv, '/')) ? progname + 1 : *argv; while ((c = getopt(argc, argv, "aBpvV")) != EOF) { switch (c) { case 'a': flags |= FLAG_ARRAY; break; case 'B': flags |= FLAG_NOBOOL; break; case 'p': flags |= FLAG_PRETTY; break; case 'v': printf("jo %s\n", PACKAGE_VERSION); exit(0); case 'V': showversion = TRUE; break; default: exit(usage(progname)); } } if (showversion) { return(version(flags)); } argc -= optind; argv += optind; pile = json_mkobject(); json = (flags & FLAG_ARRAY) ? json_mkarray() : json_mkobject(); if (argc == 0) { while (fgets(buf, sizeof(buf), stdin) != NULL) { if (buf[strlen(buf) - 1] == '\n') buf[strlen(buf) - 1] = 0; p = ttyin ? utf8_from_locale(buf, -1) : buf; append_kv(json, flags, p); if (ttyin) utf8_free(p); } } else { while ((kv = *argv++)) { p = utf8_from_locale(kv, -1); append_kv(json, flags, p); utf8_free(p); } } /* * See if we have any nested objects or arrays in the pile, * and copy these into our main object if so. */ json_foreach(op, pile) { JsonNode *o; if (op->tag == JSON_ARRAY) { o = json_mkarray(); } else if (op->tag == JSON_OBJECT) { o = json_mkobject(); } else { continue; } json_copy_to_object(o, op, 0); json_append_member(json, op->key, o); }