void addSession(char* id_sesji, char* login, char* rola){ json_object *plik, *wpis, *sesja, *new_file_content; char* fileName = "../resources/Session.json"; //pobranie struktury z pliku Session.json plik = json_object_from_file(fileName); //stworzenie obieku JSONa wpis = json_object_new_object(); new_file_content = json_object_new_object(); //dodanie wpisow do obiektu addJsonObject(wpis, "id", id_sesji); addJsonObject(wpis, "login", login); addJsonObject(wpis, "rola", rola); //pobranie tablicy 'sesja' sesja = json_object_object_get(plik, "sesja"); //dodanie do tablicy wpisu z nowym id_sesji json_object_array_add(sesja, wpis); //zapisanie w obiekcie JSON nowej tresci pliku json_object_object_add(new_file_content, "sesja", sesja); //zrzucenie tresci do pliku razem z formatowaniem json_object_to_file_ext(fileName, new_file_content, JSON_C_TO_STRING_PRETTY); }
static void test_write_to_file() { json_object *jso; jso = json_tokener_parse("{" "\"foo\":1234," "\"foo1\":\"abcdefghijklmnopqrstuvwxyz\"," "\"foo2\":\"abcdefghijklmnopqrstuvwxyz\"," "\"foo3\":\"abcdefghijklmnopqrstuvwxyz\"," "\"foo4\":\"abcdefghijklmnopqrstuvwxyz\"," "\"foo5\":\"abcdefghijklmnopqrstuvwxyz\"," "\"foo6\":\"abcdefghijklmnopqrstuvwxyz\"," "\"foo7\":\"abcdefghijklmnopqrstuvwxyz\"," "\"foo8\":\"abcdefghijklmnopqrstuvwxyz\"," "\"foo9\":\"abcdefghijklmnopqrstuvwxyz\"" "}"); const char *outfile = "json.out"; int rv = json_object_to_file(outfile, jso); printf("%s: json_object_to_file(%s, jso)=%d\n", (rv == 0) ? "OK" : "FAIL", outfile, rv); if (rv == 0) stat_and_cat(outfile); printf("\n"); const char *outfile2 = "json2.out"; rv = json_object_to_file_ext(outfile2, jso, JSON_C_TO_STRING_PRETTY); printf("%s: json_object_to_file_ext(%s, jso, JSON_C_TO_STRING_PRETTY)=%d\n", (rv == 0) ? "OK" : "FAIL", outfile2, rv); if (rv == 0) stat_and_cat(outfile2); }
/** * Retrieve the authentication token and service catalog from a now-complete Keystone JSON response, * and store them in the Keystone context structure for later use. */ static enum keystone_error process_keystone_json(keystone_context_t *context, struct json_object *response) { struct json_object *access, *token, *id; if (context->pvt.debug) { json_object_to_file_ext("/dev/stderr", response, JSON_C_TO_STRING_PRETTY); } if (!json_object_is_type(response, json_type_object)) { context->keystone_error("response is not an object", KSERR_PARSE); return KSERR_PARSE; /* Not the expected JSON object */ } /* Everything is in an "access" sub-object */ if (!json_object_object_get_ex(response, "access", &access)) { context->keystone_error("response lacks 'access' key", KSERR_PARSE); return KSERR_PARSE; /* Lacking the expected key */ } if (!json_object_is_type(access, json_type_object)) { context->keystone_error("response.access is not an object", KSERR_PARSE); return KSERR_PARSE; /* Not the expected JSON object */ } /* Service catalog */ if (!json_object_object_get_ex(access, "serviceCatalog", &context->pvt.services)) { context->keystone_error("response.access lacks 'serviceCatalog' key", KSERR_PARSE); return KSERR_PARSE; } if (!json_object_is_type(context->pvt.services, json_type_array)) { context->keystone_error("response.access.serviceCatalog not an array", KSERR_PARSE); return KSERR_PARSE; } /* Authentication token */ if (!json_object_object_get_ex(access, "token", &token)) { context->keystone_error("reponse.access lacks 'token' key", KSERR_PARSE); return KSERR_PARSE; /* Lacking the expected key */ } if (!json_object_is_type(token, json_type_object)) { context->keystone_error("response.access.token is not an object", KSERR_PARSE); return KSERR_PARSE; /* Not the expected JSON object */ } if (!json_object_object_get_ex(token, "id", &id)) { context->keystone_error("response.access.token lacks 'id' key", KSERR_PARSE); return KSERR_PARSE; /* Lacking the expected key */ } if (!json_object_is_type(id, json_type_string)) { context->keystone_error("response.access.token.id is not a string", KSERR_PARSE); return KSERR_PARSE; /* Not the expected JSON string */ } context->pvt.auth_token = context->allocator( context->pvt.auth_token, json_object_get_string_len(id) + 1 /* '\0' */ ); if (NULL == context->pvt.auth_token) { return KSERR_PARSE; /* Allocation failed */ } strcpy(context->pvt.auth_token, json_object_get_string(id)); return KSERR_SUCCESS; }
DLL_PUBLIC int result(judgm_manage_info *info,line_result_data *res_data){ manage_result_info *res_info; json_object *jso_item; char tpath[PATH_MAX + 1]; res_info = (manage_result_info*)info->private_data; res_info->count++; if(res_data->status > res_info->result){ res_info->result = res_data->status; } res_info->totalscore += res_data->score; res_info->totalruntime += res_data->runtime; if(res_data->memory > res_info->maxmemory){ res_info->maxmemory = res_data->memory; } jso_item = json_object_new_object(); json_object_object_add(jso_item,"status",json_object_new_int(res_data->status)); json_object_object_add(jso_item,"score",json_object_new_double(res_data->score)); json_object_object_add(jso_item,"runtime",json_object_new_int64(res_data->runtime)); json_object_object_add(jso_item,"memory",json_object_new_int64(res_data->memory / 1024UL)); if(strlen(res_data->err_msg) > 0){ printf(" strlen %d\n",strlen(res_data->err_msg)); json_object_object_add(jso_item,"errmsg",json_object_new_string(res_data->err_msg)); } json_object_array_put_idx(res_info->jso_resarray,res_data->id - 1,jso_item); printf("jmod count %d %d\n",res_info->count,res_info->allcount); if(res_info->count == res_info->allcount){ snprintf(tpath,sizeof(tpath),"%s/result",info->res_path); json_object_to_file_ext(tpath,res_info->jso_res,JSON_C_TO_STRING_PLAIN); info->result = res_info->result; info->score = res_info->totalscore; info->runtime = res_info->totalruntime; info->memory = res_info->maxmemory; delete res_info; return 1; } return 0; }
int json_object_to_file(const char *filename, struct json *obj) { return json_object_to_file_ext(filename, obj, 0); }
int json_object_to_file(const char *filename, struct json_object *obj) { return json_object_to_file_ext(filename, obj, JSON_C_TO_STRING_PLAIN); }
int json_object_to_file(wchar_t *FileName, struct json_object *obj) { return json_object_to_file_ext(FileName, obj, JSON_C_TO_STRING_PRETTY); }