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++; }
//## String JSON.getInt(String key); static KMETHOD kJSON_getInt(KonohaContext *kctx, KonohaStack *sfp) { JSON obj = ((kJSON *)sfp[0].asObject)->json; const char *key = S_text(sfp[1].asString); int32_t json = JSON_getInt(obj, key); KReturnUnboxValue(json); }
static int64_t GetJsonInt(KonohaContext *kctx, struct JsonBuf *jsonbuf, const char *key, size_t keylen_or_zero, int64_t defval) { if(key == NULL) { if(JSON_TYPE_CHECK(Int32, toJSON(jsonbuf->json_i))) { return JSONInt_get(toJSON(jsonbuf->json_i)); } } return JSON_getInt(toJSON(jsonbuf->json_i), key, KeyLen(key, keylen_or_zero)); }
Point * JSON_getPoints(json_t *root) { json_t *jPoints = json_object_get(root, "points"); Point * points = malloc(sizeof(Point)*json_array_size(jPoints)); double alphax = 0, alphay = 0; double borderx = 0.02*JSON_getInt(root, "width"); double bordery = 0.02*JSON_getInt(root, "height"); int i, width = JSON_getInt(root, "width"), height = JSON_getInt(root, "height"); for(i = 0; i < json_array_size(jPoints); ++i){ json_t * jPoint = json_array_get(jPoints, i); json_t *jX = json_object_get(jPoint, "x"); points[i].x = json_integer_value(jX); if (points[i].x > alphax) alphax = points[i].x; json_t *jY = json_object_get(jPoint, "y"); points[i].y = json_integer_value(jY); if (points[i].y > alphay) alphay = points[i].y; } for(i = 0; i< json_array_size(jPoints); ++i){ points[i].x = points[i].x * (width/alphax); points[i].y = 1.1*(height) - (points[i].y * (height/alphay)); } return points; }
static int64_t GetJsonInt(KonohaContext *kctx, struct JsonBuf *jsonbuf, const char *key, size_t keylen_or_zero, int64_t defval) { JSON json = AsJSON(jsonbuf); if(key == NULL) { if(JSON_TYPE_CHECK(Int32, json)) { return JSONInt_get(json); } else if(JSON_TYPE_CHECK(Int64, json)) { return JSONInt64_get(json); } else { return 0; } } return JSON_getInt(json, key, KeyLen(key, keylen_or_zero)); }
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; }