void keys(json_t* json) // shoddy, prints directly { void* iter; const char** keys; size_t i, n; if (!json_is_object(json)) {json_err("has no keys", json); return;} if (!((keys = malloc(sizeof(char*) * json_object_size(json))))) {hard_err("internal error: out of memory");} iter = json_object_iter(json); n = 0; while (iter) { keys[n++] = json_object_iter_key(iter); iter = json_object_iter_next(json, iter); } if (dumps_flags & JSON_SORT_KEYS) {qsort(keys, n, sizeof(char*), compare_strcmp);} for (i = 0; i < n; ++i) {printf("%s\n", keys[i]);} free(keys); }
char* smart_dumps(json_t* json, int flags) // json_dumps is broken on simple types { char* temp; char* temp2; json_t* j2; int i; if (!flags) {flags = dumps_flags;} switch (json_typeof(json)) { case JSON_OBJECT: return json_dumps(json, flags); case JSON_ARRAY: return json_dumps(json, flags); case JSON_STRING: // hack to print escaped string j2 = json_array(); json_array_append(j2, json); temp = json_dumps(j2, JSON_ESCAPE_SLASH); i = asprintf(&temp2, "%.*s", (signed)strlen(temp)-2, &temp[1]); if (i == -1) {hard_err("internal error: out of memory");} return temp2; case JSON_INTEGER: i = asprintf(&temp, "%" JSON_INTEGER_FORMAT, json_integer_value(json)); if (i == -1) {hard_err("internal error: out of memory");} return temp; case JSON_REAL: i = asprintf(&temp, "%f", json_real_value(json)); if (i == -1) {hard_err("internal error: out of memory");} return temp; case JSON_TRUE: return "true"; case JSON_FALSE: return "false"; case JSON_NULL: return "null"; default: err("internal error: unknown type"); return "null"; } }
void json_err(char* message, json_t* json) { char* temp; int i; i = asprintf(&temp, "parse error: type '%s' %s (arg %i)", pretty_type(json), message, optind-1); if (i == -1) {hard_err("internal error: out of memory");} err(temp); }
void arg_err(char* message) { char* temp; int i; i = asprintf(&temp, message, optind-1, g_argv[optind-1]); if (i == -1) {hard_err("internal error: out of memory");} err(temp); }
void PUSH(json_t* json) { if (stackpointer >= &stack[STACKDEPTH]) {hard_err("internal error: stack overflow");} if (json == NULL) { arg_err("parse error: bad json on arg %i, \"%s\""); json = json_null(); } *stackpointer++ = json; }
json_t* smart_loads(char* j_string) // json_loads is broken on simple types { json_t* json; json_error_t error; char *temp; int i; i = asprintf(&temp, "[%s]", j_string); if (i == -1) {hard_err("internal error: out of memory");} json = compat_json_loads(temp, &error); if (!json) {return json_string(j_string);} return json_array_get(json, 0); }
void MAPPUSH() { if (mapstackpointer >= &mapstack[STACKDEPTH]) {hard_err("internal error: mapstack overflow");} mapstackpointer++; map_safe_peek()->stk = stack_safe_peek(); map_safe_peek()->opt = optind; map_safe_peek()->fin = 0; switch (json_typeof(PEEK)) { case JSON_OBJECT: map_safe_peek()->itr = json_object_iter(PEEK); break; case JSON_ARRAY: map_safe_peek()->lin = 0; break; default: err("parse error: type not mappable"); } }
mapping* map_safe_peek() { if (mapstackpointer < &mapstack[1]) {hard_err("internal error: mapstack underflow");} return mapstackpointer - 1; }