int BaseGameObject::deserialize(json_t * root) { json_t *tmp_obj; if (!root) { fprintf(stderr, "%s/%s+%d: unable to allocate json object\n", __FILE__, __FUNCTION__, __LINE__); return (1); } id = (int) json_integer_value(json_object_get(root, "id")); fps = (float) json_real_value(json_object_get(root, "fps")); sorting_layer = (int) json_integer_value(json_object_get(root, "sorting_layer")); sorting_order = (int) json_integer_value(json_object_get(root, "sorting_order")); enabled = json_boolean_value(json_object_get(root, "enabled")); draw_sprite = json_boolean_value(json_object_get(root, "draw_sprite")); position.deserialize(json_object_get(root, "position")); size.deserialize(json_object_get(root, "size")); tmp_obj = json_object_get(root, "dst"); dst.x = (int) json_integer_value(json_object_get(tmp_obj, "x")); dst.y = (int) json_integer_value(json_object_get(tmp_obj, "y")); dst.w = (int) json_integer_value(json_object_get(tmp_obj, "w")); dst.h = (int) json_integer_value(json_object_get(tmp_obj, "h")); // read the sprite filenames tmp_obj = json_object_get(root, "sprites"); if (tmp_obj) { sprite_filenames.resize(json_array_size(tmp_obj)); for (size_t i = 0; i < json_array_size(tmp_obj); i++) { json_t *entry = json_array_get(tmp_obj, i); int index = (int) json_integer_value(json_object_get(entry, "index")); json_t *file_object = json_object_get(entry, "file"); const char *file = json_string_value(file_object); if (index < 0) { index = 0; } sprite_filenames[index] = (char*) calloc(json_string_length(file_object) + 1, sizeof(char)); strcpy_s(sprite_filenames[index], json_string_length(file_object) + 1, file); } } // read the children tmp_obj = json_object_get(root, "children"); if (tmp_obj) { children.resize(json_array_size(tmp_obj)); for (size_t i = 0; i < json_array_size(tmp_obj); i++) { children[i] = new BaseGameObject(); children[i]->deserialize(json_array_get(tmp_obj, i)); } } update_dst(); init(); return 0; }
ctr_object* ctr_json_create_object(json_t* root, ctr_object* gt) { switch(json_typeof(root)) { case JSON_OBJECT: { ctr_object* sub = ctr_internal_create_object(CTR_OBJECT_TYPE_OTOBJECT); ctr_set_link_all(sub, gt); // size_t size; const char *key; json_t *value; ctr_argument* argl = ctr_heap_allocate(sizeof(*argl)); argl->next = ctr_heap_allocate(sizeof(*argl)); // size = json_object_size(root); json_object_foreach(root, key, value) { char* k = (char*)key; ctr_object* ko = ctr_build_string_from_cstring(k); ctr_object* vo = ctr_json_create_object(value, gt); argl->object = vo; argl->next->object = ko; sub = ctr_map_put(sub, argl); } ctr_heap_free(argl->next); ctr_heap_free(argl); return sub; } case JSON_ARRAY: { ctr_object* arr = ctr_array_new(CtrStdArray, NULL); ctr_argument* arg = ctr_heap_allocate(sizeof(ctr_argument)); size_t i; size_t size = json_array_size(root); for (i = 0; i < size; i++) { arg->object = ctr_json_create_object(json_array_get(root, i), gt); ctr_array_push(arr, arg); } ctr_heap_free(arg); return arr; } case JSON_STRING: { ctr_object* str = ctr_build_string((char*)json_string_value(root), json_string_length(root)); return str; } case JSON_INTEGER: { return ctr_build_number_from_float(json_integer_value(root)); } case JSON_REAL: { return ctr_build_number_from_float(json_real_value(root)); } case JSON_FALSE: { return ctr_build_bool(0); } case JSON_TRUE: { return ctr_build_bool(1); } case JSON_NULL: { return ctr_build_nil(); } default: { CtrStdFlow = ctr_build_string_from_cstring("Unrecognized JSON type"); return CtrStdNil; } }
clevis_buf_t * clevis_buf_decode(const json_t *json) { clevis_buf_t *tmp = NULL; clevis_buf_t *out = NULL; BIO *mem = NULL; BIO *b64 = NULL; int r = 0; if (!json_is_string(json)) return NULL; tmp = clevis_buf_make(json_string_length(json), NULL); if (!tmp) return NULL; mem = BIO_new_mem_buf((void *) json_string_value(json), -1); if (!mem) goto error; b64 = BIO_new(BIO_f_base64()); if (!b64) goto error; BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); if (!BIO_push(b64, mem)) goto error; r = BIO_read(b64, tmp->buf, tmp->len); if ((r + 2) / 3 * 4 != (int) json_string_length(json)) goto error; out = clevis_buf_make(r, tmp->buf); error: clevis_buf_free(tmp); BIO_free(b64); BIO_free(mem); return out; }
map_table_t *json_to_map(bombyx_env_t *env, json_t *json) { size_t index; json_t *value; const char *key; map_table_t *map; // TODO: minimum map size (100?) if (json_is_object(json)) { map = map_table_create(json_object_size(json)); json_object_foreach(json, key, value) { var v = {0}; if (json_is_object(value)) { // process recursively v.type = VAR_MAP; v.data = json_to_map(env, value); v.data_size = sizeof(map_table_t); } else if (json_is_array(value)) { v.type = VAR_ARRAY; v.data = json_to_array(env, value); v.data_size = sizeof(array_t); } else if (json_is_string(value)) { v.type = VAR_STRING; v.data = strdup(json_string_value(value)); v.data_size = json_string_length(value) + 1; } else if (json_is_number(value)) { v.type = VAR_DOUBLE; v.data = challoc(env->pool_of_doubles); v.data_size = sizeof(double); *(double *)v.data = json_number_value(value); } map_add(env, map, (char *)key, v); } }
static void allow_nul() { const char *text = "\"nul byte \\u0000 in string\""; const char *expected = "nul byte \0 in string"; size_t len = 20; json_t *json; json = json_loads(text, JSON_ALLOW_NUL | JSON_DECODE_ANY, NULL); if(!json || !json_is_string(json)) fail("unable to decode embedded NUL byte"); if(json_string_length(json) != len) fail("decoder returned wrong string length"); if(memcmp(json_string_value(json), expected, len + 1)) fail("decoder returned wrong string content"); json_decref(json); }
int game_is_trial(void) { static int trial = -2; if(trial == -2) { json_t *build = json_object_get(runconfig_get(), "build"); const char *build_str = json_string_value(build); size_t build_len = json_string_length(build); if(!build_str || build_len < 2) { return trial = -1; } assert( (build_str[0] == 'v' && (build_str[1] == '0' || build_str[1] == '1')) || !"invalid build format?" ); trial = build_str[1] == '0'; } return trial; }
json_t* patch_bootstrap(const json_t *sel, json_t *repo_servers) { const char *main_fn = "patch.js"; char *patch_js_buffer; DWORD patch_js_size; json_t *patch_info = patch_build(sel); const json_t *patch_id = json_array_get(sel, 1); size_t patch_len = json_string_length(patch_id) + 1; size_t remote_patch_fn_len = patch_len + 1 + strlen(main_fn) + 1; VLA(char, remote_patch_fn, remote_patch_fn_len); sprintf(remote_patch_fn, "%s/%s", json_string_value(patch_id), main_fn); patch_js_buffer = (char*)ServerDownloadFile(repo_servers, remote_patch_fn, &patch_js_size, NULL); patch_file_store(patch_info, main_fn, patch_js_buffer, patch_js_size); // TODO: Nice, friendly error VLA_FREE(remote_patch_fn); SAFE_FREE(patch_js_buffer); return patch_info; }
static void GenStat(Stat* s, json_t* v) { switch (json_typeof(v)) { case JSON_OBJECT: { const char *key; json_t *value; json_object_foreach(v, key, value) { GenStat(s, value); s->stringLength += strlen(key); s->stringCount++; s->memberCount++; } s->objectCount++; } break; case JSON_ARRAY: { size_t arraySize = json_array_size(v); for (size_t i = 0; i < arraySize; i++) GenStat(s, json_array_get(v, i)); s->arrayCount++; s->elementCount += arraySize; } break; case JSON_STRING: s->stringCount++; s->stringLength += json_string_length(v); break; case JSON_REAL: case JSON_INTEGER: s->numberCount++; break; case JSON_TRUE: s->trueCount++; break; case JSON_FALSE: s->falseCount++; break; case JSON_NULL: s->nullCount++; break; }
static char * parse_json_get_object_string (const char *json, const char *key, int flags, const char *func, const char *cmd) { const char *str; char *ret; json_t *tree = NULL, *node; tree = parse_json (json, func); if (tree == NULL) return NULL; if (!json_is_object (tree)) goto bad_type; node = json_object_get (tree, key); if (node == NULL) goto bad_type; if ((flags & GET_STRING_NULL_TO_EMPTY) && json_is_null (node)) ret = strdup (""); else { str = json_string_value (node); if (str == NULL) goto bad_type; ret = strndup (str, json_string_length (node)); } if (ret == NULL) reply_with_perror ("strdup"); json_decref (tree); return ret; bad_type: reply_with_error ("output of '%s' was not a JSON object " "containing a key '%s' of type string", cmd, key); json_decref (tree); return NULL; }
char * guestfs_impl_disk_format (guestfs_h *g, const char *filename) { CLEANUP_JSON_T_DECREF json_t *tree = get_json_output (g, filename); json_t *node; if (tree == NULL) return NULL; if (!json_is_object (tree)) goto bad_type; node = json_object_get (tree, "format"); if (!json_is_string (node)) goto bad_type; return safe_strndup (g, json_string_value (node), json_string_length (node)); /* caller frees */ bad_type: error (g, _("qemu-img info: JSON output did not contain ‘format’ key")); return NULL; }
static void task_populate_titledb_thread(void* arg) { populate_titledb_data* data = (populate_titledb_data*) arg; Result res = 0; linked_list titles; linked_list_init(&titles); json_t* root = NULL; if(R_SUCCEEDED(res = http_download_json("https://api.titledb.com/v1/entry?nested=true" "&only=id&only=name&only=author&only=headline&only=category" "&only=cia.id&only=cia.mtime&only=cia.version&only=cia.size&only=cia.titleid" "&only=tdsx.id&only=tdsx.mtime&only=tdsx.version&only=tdsx.size&only=tdsx.smdh.id", &root, 1024 * 1024))) { if(json_is_array(root)) { for(u32 i = 0; i < json_array_size(root) && R_SUCCEEDED(res); i++) { svcWaitSynchronization(task_get_pause_event(), U64_MAX); if(task_is_quit_all() || svcWaitSynchronization(data->cancelEvent, 0) == 0) { break; } json_t* entry = json_array_get(root, i); if(json_is_object(entry)) { list_item* item = (list_item*) calloc(1, sizeof(list_item)); if(item != NULL) { titledb_info* titledbInfo = (titledb_info*) calloc(1, sizeof(titledb_info)); if(titledbInfo != NULL) { titledbInfo->id = (u32) json_object_get_integer(entry, "id", 0); string_copy(titledbInfo->category, json_object_get_string(entry, "category", "Unknown"), sizeof(titledbInfo->category)); string_copy(titledbInfo->meta.shortDescription, json_object_get_string(entry, "name", ""), sizeof(titledbInfo->meta.shortDescription)); string_copy(titledbInfo->meta.publisher, json_object_get_string(entry, "author", ""), sizeof(titledbInfo->meta.publisher)); json_t* headline = json_object_get(entry, "headline"); if(json_is_string(headline)) { const char* val = json_string_value(headline); if(json_string_length(headline) > sizeof(titledbInfo->headline) - 1) { snprintf(titledbInfo->headline, sizeof(titledbInfo->headline), "%.508s...", val); } else { string_copy(titledbInfo->headline, val, sizeof(titledbInfo->headline)); } } else { titledbInfo->headline[0] = '\0'; } json_t* cias = json_object_get(entry, "cia"); if(json_is_array(cias)) { for(u32 j = 0; j < json_array_size(cias); j++) { json_t* cia = json_array_get(cias, j); if(json_is_object(cia)) { const char* mtime = json_object_get_string(cia, "mtime", "Unknown"); if(!titledbInfo->cia.exists || task_populate_titledb_compare_dates(mtime, titledbInfo->cia.mtime, sizeof(titledbInfo->cia.mtime)) >= 0) { titledbInfo->cia.exists = true; titledbInfo->cia.id = (u32) json_object_get_integer(cia, "id", 0); string_copy(titledbInfo->cia.mtime, mtime, sizeof(titledbInfo->cia.mtime)); string_copy(titledbInfo->cia.version, json_object_get_string(cia, "version", "Unknown"), sizeof(titledbInfo->cia.version)); titledbInfo->cia.size = (u32) json_object_get_integer(cia, "size", 0); titledbInfo->cia.titleId = strtoull(json_object_get_string(cia, "titleid", "0"), NULL, 16); } } } } json_t* tdsxs = json_object_get(entry, "tdsx"); if(json_is_array(tdsxs)) { for(u32 j = 0; j < json_array_size(tdsxs); j++) { json_t* tdsx = json_array_get(tdsxs, j); if(json_is_object(tdsx)) { const char* mtime = json_object_get_string(tdsx, "mtime", "Unknown"); if(!titledbInfo->tdsx.exists || task_populate_titledb_compare_dates(mtime, titledbInfo->tdsx.mtime, sizeof(titledbInfo->tdsx.mtime)) >= 0) { titledbInfo->tdsx.exists = true; titledbInfo->tdsx.id = (u32) json_object_get_integer(tdsx, "id", 0); string_copy(titledbInfo->tdsx.mtime, mtime, sizeof(titledbInfo->tdsx.mtime)); string_copy(titledbInfo->tdsx.version, json_object_get_string(tdsx, "version", "Unknown"), sizeof(titledbInfo->tdsx.version)); titledbInfo->tdsx.size = (u32) json_object_get_integer(tdsx, "size", 0); json_t* smdh = json_object_get(tdsx, "smdh"); if(json_is_object(smdh)) { titledbInfo->tdsx.smdh.exists = true; titledbInfo->tdsx.smdh.id = (u32) json_object_get_integer(smdh, "id", 0); } } } } } char* latestTime = "Unknown"; if(titledbInfo->cia.exists && titledbInfo->tdsx.exists) { if(task_populate_titledb_compare_dates(titledbInfo->cia.mtime, titledbInfo->tdsx.mtime, sizeof(titledbInfo->cia.mtime)) >= 0) { latestTime = titledbInfo->cia.mtime; } else { latestTime = titledbInfo->tdsx.mtime; } } else if(titledbInfo->cia.exists) { latestTime = titledbInfo->cia.mtime; } else if(titledbInfo->tdsx.exists) { latestTime = titledbInfo->tdsx.mtime; } string_copy(titledbInfo->mtime, latestTime, sizeof(titledbInfo->mtime)); if((titledbInfo->cia.exists || titledbInfo->tdsx.exists) && (data->filter == NULL || data->filter(data->userData, titledbInfo))) { string_copy(item->name, titledbInfo->meta.shortDescription, LIST_ITEM_NAME_MAX); item->data = titledbInfo; task_populate_titledb_update_status(item); linked_list_add_sorted(&titles, item, data->userData, data->compare); } else { free(titledbInfo); free(item); } } else { free(item); res = R_APP_OUT_OF_MEMORY; } } else { res = R_APP_OUT_OF_MEMORY; } } } linked_list_iter iter; linked_list_iterate(&titles, &iter); while(linked_list_iter_has_next(&iter)) { list_item* item = linked_list_iter_next(&iter); if(R_SUCCEEDED(res)) { linked_list_add(data->items, item); } else { task_free_titledb(item); linked_list_iter_remove(&iter); } } } else { res = R_APP_BAD_DATA; } json_decref(root); } data->itemsListed = true; if(R_SUCCEEDED(res)) { linked_list_iter iter; linked_list_iterate(&titles, &iter); while(linked_list_iter_has_next(&iter)) { svcWaitSynchronization(task_get_pause_event(), U64_MAX); Handle events[2] = {data->resumeEvent, data->cancelEvent}; s32 index = 0; svcWaitSynchronizationN(&index, events, 2, false, U64_MAX); if(task_is_quit_all() || svcWaitSynchronization(data->cancelEvent, 0) == 0) { break; } list_item* item = (list_item*) linked_list_iter_next(&iter); titledb_info* titledbInfo = (titledb_info*) item->data; char url[128]; if(titledbInfo->cia.exists) { snprintf(url, sizeof(url), "https://3ds.titledb.com/v1/cia/%lu/icon_l.bin", titledbInfo->cia.id); } else if(titledbInfo->tdsx.exists && titledbInfo->tdsx.smdh.exists) { snprintf(url, sizeof(url), "https://3ds.titledb.com/v1/smdh/%lu/icon_l.bin", titledbInfo->tdsx.smdh.id); } else { continue; } u8 icon[0x1200]; u32 iconSize = 0; if(R_SUCCEEDED(http_download(url, &iconSize, &icon, sizeof(icon))) && iconSize == sizeof(icon)) { titledbInfo->meta.texture = screen_allocate_free_texture(); screen_load_texture_tiled(titledbInfo->meta.texture, icon, sizeof(icon), 48, 48, GPU_RGB565, false); } } } linked_list_destroy(&titles); svcCloseHandle(data->resumeEvent); svcCloseHandle(data->cancelEvent); data->result = res; data->finished = true; }
/* Call the simple functions not covered by other tests of the public API */ static void run_tests() { json_t *value; value = json_boolean(1); if(!json_is_true(value)) fail("json_boolean(1) failed"); json_decref(value); value = json_boolean(-123); if(!json_is_true(value)) fail("json_boolean(-123) failed"); json_decref(value); value = json_boolean(0); if(!json_is_false(value)) fail("json_boolean(0) failed"); json_decref(value); value = json_integer(1); if(json_typeof(value) != JSON_INTEGER) fail("json_typeof failed"); if(json_is_object(value)) fail("json_is_object failed"); if(json_is_array(value)) fail("json_is_array failed"); if(json_is_string(value)) fail("json_is_string failed"); if(!json_is_integer(value)) fail("json_is_integer failed"); if(json_is_real(value)) fail("json_is_real failed"); if(!json_is_number(value)) fail("json_is_number failed"); if(json_is_true(value)) fail("json_is_true failed"); if(json_is_false(value)) fail("json_is_false failed"); if(json_is_boolean(value)) fail("json_is_boolean failed"); if(json_is_null(value)) fail("json_is_null failed"); json_decref(value); value = json_string("foo"); if(!value) fail("json_string failed"); if(strcmp(json_string_value(value), "foo")) fail("invalid string value"); if (json_string_length(value) != 3) fail("invalid string length"); if(json_string_set(value, "barr")) fail("json_string_set failed"); if(strcmp(json_string_value(value), "barr")) fail("invalid string value"); if (json_string_length(value) != 4) fail("invalid string length"); if(json_string_setn(value, "hi\0ho", 5)) fail("json_string_set failed"); if(memcmp(json_string_value(value), "hi\0ho\0", 6)) fail("invalid string value"); if (json_string_length(value) != 5) fail("invalid string length"); json_decref(value); value = json_string(NULL); if(value) fail("json_string(NULL) failed"); /* invalid UTF-8 */ value = json_string("a\xefz"); if(value) fail("json_string(<invalid utf-8>) failed"); value = json_string_nocheck("foo"); if(!value) fail("json_string_nocheck failed"); if(strcmp(json_string_value(value), "foo")) fail("invalid string value"); if (json_string_length(value) != 3) fail("invalid string length"); if(json_string_set_nocheck(value, "barr")) fail("json_string_set_nocheck failed"); if(strcmp(json_string_value(value), "barr")) fail("invalid string value"); if (json_string_length(value) != 4) fail("invalid string length"); if(json_string_setn_nocheck(value, "hi\0ho", 5)) fail("json_string_set failed"); if(memcmp(json_string_value(value), "hi\0ho\0", 6)) fail("invalid string value"); if (json_string_length(value) != 5) fail("invalid string length"); json_decref(value); /* invalid UTF-8 */ value = json_string_nocheck("qu\xff"); if(!value) fail("json_string_nocheck failed"); if(strcmp(json_string_value(value), "qu\xff")) fail("invalid string value"); if (json_string_length(value) != 3) fail("invalid string length"); if(json_string_set_nocheck(value, "\xfd\xfe\xff")) fail("json_string_set_nocheck failed"); if(strcmp(json_string_value(value), "\xfd\xfe\xff")) fail("invalid string value"); if (json_string_length(value) != 3) fail("invalid string length"); json_decref(value); value = json_integer(123); if(!value) fail("json_integer failed"); if(json_integer_value(value) != 123) fail("invalid integer value"); if(json_number_value(value) != 123.0) fail("invalid number value"); if(json_integer_set(value, 321)) fail("json_integer_set failed"); if(json_integer_value(value) != 321) fail("invalid integer value"); if(json_number_value(value) != 321.0) fail("invalid number value"); json_decref(value); value = json_real(123.123); if(!value) fail("json_real failed"); if(json_real_value(value) != 123.123) fail("invalid integer value"); if(json_number_value(value) != 123.123) fail("invalid number value"); if(json_real_set(value, 321.321)) fail("json_real_set failed"); if(json_real_value(value) != 321.321) fail("invalid real value"); if(json_number_value(value) != 321.321) fail("invalid number value"); json_decref(value); value = json_true(); if(!value) fail("json_true failed"); json_decref(value); value = json_false(); if(!value) fail("json_false failed"); json_decref(value); value = json_null(); if(!value) fail("json_null failed"); json_decref(value); /* Test reference counting on singletons (true, false, null) */ value = json_true(); if(value->refcount != (size_t)-1) fail("refcounting true works incorrectly"); json_decref(value); if(value->refcount != (size_t)-1) fail("refcounting true works incorrectly"); json_incref(value); if(value->refcount != (size_t)-1) fail("refcounting true works incorrectly"); value = json_false(); if(value->refcount != (size_t)-1) fail("refcounting false works incorrectly"); json_decref(value); if(value->refcount != (size_t)-1) fail("refcounting false works incorrectly"); json_incref(value); if(value->refcount != (size_t)-1) fail("refcounting false works incorrectly"); value = json_null(); if(value->refcount != (size_t)-1) fail("refcounting null works incorrectly"); json_decref(value); if(value->refcount != (size_t)-1) fail("refcounting null works incorrectly"); json_incref(value); if(value->refcount != (size_t)-1) fail("refcounting null works incorrectly"); }
static int json_find(void *handle, uschar *filename, const uschar *keystring, int length, uschar **result, uschar **errmsg, uint *do_cache) { FILE * f = handle; json_t * j, * j0; json_error_t jerr; uschar * key; int sep = 0; length = length; /* Keep picky compilers happy */ do_cache = do_cache; /* Keep picky compilers happy */ rewind(f); if (!(j = json_loadf(f, 0, &jerr))) { *errmsg = string_sprintf("json error on open: %.*s\n", JSON_ERROR_TEXT_LENGTH, jerr.text); return FAIL; } j0 = j; for (int k = 1; (key = string_nextinlist(&keystring, &sep, NULL, 0)); k++) { BOOL numeric = TRUE; for (uschar * s = key; *s; s++) if (!isdigit(*s)) { numeric = FALSE; break; } if (!(j = numeric ? json_array_get(j, (size_t) strtoul(CS key, NULL, 10)) : json_object_get(j, CCS key) ) ) { DEBUG(D_lookup) debug_printf("%s, for key %d: '%s'\n", numeric ? US"bad index, or not json array" : US"no such key, or not json object", k, key); json_decref(j0); return FAIL; } } switch (json_typeof(j)) { case JSON_STRING: *result = string_copyn(CUS json_string_value(j), json_string_length(j)); break; case JSON_INTEGER: *result = string_sprintf("%" JSON_INTEGER_FORMAT, json_integer_value(j)); break; case JSON_REAL: *result = string_sprintf("%f", json_real_value(j)); break; case JSON_TRUE: *result = US"true"; break; case JSON_FALSE: *result = US"false"; break; case JSON_NULL: *result = NULL; break; default: *result = US json_dumps(j, 0); break; } json_decref(j0); return OK; }