static void test_string(void) { JSON json = parseJSON(data, data+sizeof(data)); JSON child = JSON_get(json, "app"); assert(JSON_isValid(child)); assert(JSON_type(child) == JSON_Array); assert(JSON_length(child) == 3); JSONArray *a; JSON *I, *E; int i = 0; JSON_ARRAY_EACH(child, a, I, E) { assert(JSON_isValid(JSON_get(*I, "name"))); assert(JSON_isValid(JSON_get(*I, "line"))); assert(JSON_isValid(JSON_get(*I, "version"))); assert(JSON_isValid(JSON_get(*I, "flag"))); size_t len; const char *name = JSON_getString(*I, "name", &len); int line = JSON_getInt(*I, "line"); double ver = JSON_getDouble(*I, "version"); assert(len == strlen(names[i]) && strncmp(name, names[i], len) == 0); assert(line == lines[i]); assert(ver == versions[i]); fprintf(stderr, "%s %d %f\n", name, line, ver); i++; }
static const char *GetJsonText(KonohaContext *kctx, struct JsonBuf *jsonbuf, const char *key, size_t keylen_or_zero, const char *defval) { if(key == NULL) JSONString_get(toJSON(jsonbuf->json_i)); size_t length = KeyLen(key, keylen_or_zero); return JSON_getString(toJSON(jsonbuf->json_i), key, &length); }
//## String JSON.getString(String key); static KMETHOD kJSON_getString(KonohaContext *kctx, KonohaStack *sfp) { JSON obj = ((kJSON *)sfp[0].asObject)->json; const char *key = S_text(sfp[1].asString); size_t len; const char *text = JSON_getString(obj, key, &len); KReturn(KLIB new_kString(kctx, text, len, 0)); }
static const char *GetJsonText(KonohaContext *kctx, struct JsonBuf *jsonbuf, const char *key, size_t keylen_or_zero, const char *defval) { size_t length; JSON json = AsJSON(jsonbuf); if(key == NULL) return JSONString_get(json); length = KeyLen(key, keylen_or_zero); return JSON_getString(json, key, &length); }
int main() { json_t *root; json_error_t error; root = json_load_file("../test.json", 0, &error); if(root == 0){ printf("ERRO AO LER ARQUIVO, linha %d!\n", error.line); return error.line; } const char * fileName = JSON_getString(root, "fileName"); printf("O nome do arquivo é: %s\n", fileName); const char * format = JSON_getString(root, "format"); if(strcmp(format, "png") != 0 && strcmp(format, "pdf") != 0 ){ printf("Formato: o formato %s não é suportado!\n", format); return 0; } printf("O formato é: %s\n", JSON_getString(root, "format")); const char * name = JSON_getString(root, "name"); printf("O titulo do grafico é: %s\n", name); int width = JSON_getInt(root, "width"); if(width < 200 || width > 1000){ printf("Width: medida não suportada!\n"); return 0; } printf("Width: %d\n", width); int height = JSON_getInt(root, "height"); if(height < 200 || height > 1000){ printf("Height: medida não suportada!\n"); return 0; } printf("Height: %d\n", height); int type = JSON_getInt(root, "type"); printf("Type: %d\n", type); double aux; //aux = height/width; if(height>width) aux = (width/200); else aux = (height/200); cairo_surface_t *surface; if(strcmp(format, "png") == 0){ surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height); } else{ surface = cairo_pdf_surface_create(fileName, width, height); } cairo_t *context = cairo_create(surface); cairo_set_source_rgba(context, 1, 1, 1, 1); cairo_rectangle(context, 0, 0, width, height); cairo_fill(context); Color pointColor = JSON_getColor(root); Point *points = JSON_getPoints(root); if(type == 0 || type == 1) create_point(context, points, width, height, type, aux, pointColor); if (type==2) { draw_area(context, points, width, height, aux, pointColor); create_point(context, points, width, height, type, aux, pointColor); } draw_line(context, points, width, height, type, aux, pointColor); write_text(context, name, width, height, aux, pointColor); if(strcmp(JSON_getString(root, "format"), "png") == 0) cairo_surface_write_to_png(surface, fileName); else cairo_show_page(context); cairo_destroy(context); cairo_surface_destroy(surface); return 0; }