void ConfigSaveJSON(const Config *config, const char *filename) { FILE *f = fopen(filename, "w"); char *text = NULL; json_t *root; if (f == NULL) { printf("Error saving config '%s'\n", filename); return; } setlocale(LC_ALL, ""); root = json_new_object(); json_insert_pair_into_object(root, "Version", json_new_number(VERSION)); ConfigSaveVisit(config, root); json_tree_to_string(root, &text); char *formatText = json_format_string(text); fputs(formatText, f); // clean up CFREE(formatText); CFREE(text); json_free_value(&root); fclose(f); }
int entryTest(void) { printf("%s", "====== Entry Test Starts =======\n"); setlocale (LC_ALL, ""); json_t *root, *subtree; // creates the root node root = json_new_object(); // creates the desired MJSON document subtree subtree = new_entry("Andrew", "555 123 456"); // inserts the subtree into the root object json_insert_child(root, subtree); // print the result char *text; json_tree_to_string(root, &text); printf("%s\n", text); // clean up free(text); json_free_value(&root); printf("%s", "====== Entry Test Ends =======\n"); return EXIT_SUCCESS; }
bool TrySaveJSONFile(json_t *node, const char *filename) { bool res = true; char *text; json_tree_to_string(node, &text); char *ftext = json_format_string(text); FILE *f = fopen(filename, "w"); if (f == NULL) { printf("failed to open. Reason: [%s].\n", strerror(errno)); res = false; goto bail; } size_t writeLen = strlen(ftext); const size_t rc = fwrite(ftext, 1, writeLen, f); if (rc != writeLen) { printf("Wrote (%d) of (%d) bytes. Reason: [%s].\n", (int)rc, (int)writeLen, strerror(errno)); res = false; goto bail; } bail: CFREE(text); CFREE(ftext); if (f != NULL) fclose(f); return res; }
bool TrySaveJSONFile(json_t *node, const char *filename) { bool res = true; char *text; json_tree_to_string(node, &text); char *ftext = json_format_string(text); FILE *f = fopen(filename, "w"); if (f == NULL) { LOG(LM_MAIN, LL_ERROR, "failed to open file(%s) for saving: %s", filename, strerror(errno)); res = false; goto bail; } size_t writeLen = strlen(ftext); const size_t rc = fwrite(ftext, 1, writeLen, f); if (rc != writeLen) { LOG(LM_MAIN, LL_ERROR, "Wrote (%d) of (%d) bytes: %s", (int)rc, (int)writeLen, strerror(errno)); res = false; goto bail; } bail: CFREE(text); CFREE(ftext); if (f != NULL) fclose(f); return res; }
void ConfigSaveJSON(Config *config, const char *filename) { FILE *f = fopen(filename, "w"); char *text = NULL; json_t *root; if (f == NULL) { printf("Error saving config '%s'\n", filename); return; } setlocale(LC_ALL, ""); root = json_new_object(); json_insert_pair_into_object(root, "Version", json_new_number(VERSION)); AddGameConfigNode(&config->Game, root); AddGraphicsConfigNode(&config->Graphics, root); AddInputConfigNode(&config->Input, root); AddInterfaceConfigNode(&config->Interface, root); AddSoundConfigNode(&config->Sound, root); AddQuickPlayConfigNode(&config->QuickPlay, root); json_tree_to_string(root, &text); char *formatText = json_format_string(text); fputs(formatText, f); // clean up CFREE(formatText); CFREE(text); json_free_value(&root); fclose(f); }
void AutosaveSave(Autosave *autosave, const char *filename) { FILE *f = fopen(filename, "w"); char *text = NULL; json_t *root; if (f == NULL) { printf("Error saving autosave '%s'\n", filename); return; } setlocale(LC_ALL, ""); root = json_new_object(); json_insert_pair_into_object(root, "Version", json_new_number("2")); json_insert_pair_into_object( root, "LastMission", CreateMissionNode(&autosave->LastMission)); AddMissionNodes(autosave, root, "Missions"); json_tree_to_string(root, &text); char *formatText = json_format_string(text); fputs(formatText, f); // clean up free(formatText); free(text); json_free_value(&root); fclose(f); }
static void mjson_bench_tostr(void *context, json_object_t root) { json_t *json = (json_t *) root; char *str; json_tree_to_string(json, &str); free(str); }
int treeTest1(void) { int err; printf("%s", "====== Tree Test 1 Starts =======\n"); setlocale (LC_ALL, ""); //char *document = "{\"entry\":{\"name\":\"Andew\",\"phone\":\"555 123 456\"}}"; int fd = open("./param_list.json", O_RDONLY); if (-1 == fd) { printf("open file failed\n"); return 1; } struct stat fs; if (fstat(fd, &fs)) { printf("failed to get file stat\n"); close(fd); return 1; } size_t fileLen = (size_t)fs.st_size; char *document = malloc(fileLen); bzero(document, fileLen); size_t count = read(fd, document, fileLen); if (count != fileLen) { printf("failed to read file\n"); free(document); close(fd); return 1; } json_t *root = NULL; //json_t *root = json_new_object(); if (NULL == root) { printf("%s\n", "new json object failed"); } printf("Parsing the document...\n"); err = json_parse_document(&root, document); if (err != JSON_OK) { printf("%s, err=%d\n", "parse error", err); goto cleanup; } printf("Printing the document tree...\n"); json_tree_to_string(root, &document); printf("%s\n", document); // clean up cleanup: json_free_value(&root); free(document); close(fd); printf("%s", "====== Tree Test 1 Ends =======\n"); return EXIT_SUCCESS; }
bool jps_method(LSHandle* lshandle, LSMessage *message, void *ctx) { bool returnVal = true; char line[MAXLINELEN]; // %%% MAGIC NUMBERS ALERT %%% char name[128]; LSError lserror; LSErrorInit(&lserror); char *jsonResponse = 0; int len = 0; json_t *response = json_new_object(); FILE *fp = popen("/usr/bin/jps", "r"); if (fp) { json_t *array = json_new_array(); // Skip the first line (void)fgets( line, sizeof line, fp); while ( fgets( line, sizeof line, fp)) { if (sscanf(line, "%*d %*d %*d %*d %*d %*d %*d %127c", (char*)&name) == 1) { // %%% HACK ALERT %%% *strchr(name,'\n') = 0; json_t *object = json_new_object(); // %%% IGNORING RETURN ALERT %%% json_insert_pair_into_object(object, "name", json_new_string(name)); json_insert_child(array, object); } } if (!pclose(fp)) { // %%% IGNORING RETURN ALERT %%% json_insert_pair_into_object(response, "returnValue", json_new_true()); json_insert_pair_into_object(response, "threads", array); json_tree_to_string(response, &jsonResponse); } } if (jsonResponse) { LSMessageReply(lshandle, message, jsonResponse, &lserror); free(jsonResponse); } else LSMessageReply(lshandle, message, "{\"returnValue\":false,\"errorCode\":-1,\"errorText\":\"Generic error\"}", &lserror); json_free_value(&response); LSErrorFree(&lserror); return returnVal; }
int basicTest() { printf("%s", "====== basic JSon Test Starts =======\n"); char *text; json_t *root, *entry, *label, *value; setlocale (LC_ALL, ""); // creates the root node root = json_new_object(); // create an entry node entry = json_new_object(); // insert the first label-value pair label = json_new_string("name"); value = json_new_string("Andew"); json_insert_child(label, value); json_insert_child(entry, label); // insert the second label-value pair label = json_new_string("phone"); value = json_new_string("555 123 456"); json_insert_child(label, value); json_insert_child(entry, label); // inserts that object as a value in a label-value pair label = json_new_string("entry"); json_insert_child(label, entry); // inserts that label-value pair into the root object json_insert_child(root, label); // print the result json_tree_to_string(root, &text); printf("%s\n",text); // clean up free(text); json_free_value(&root); printf("%s", "====== basic JSon Test Ends =======\n"); return EXIT_SUCCESS; }
int streamParse(void) { char buffer[BUFFER_SIZE]; char *temp = NULL; unsigned int error = JSON_INCOMPLETE_DOCUMENT; struct json_parsing_info state; json_jpi_init(&state); while ((error == JSON_WAITING_FOR_EOF) || (error == JSON_INCOMPLETE_DOCUMENT)) { if(fgets (buffer, BUFFER_SIZE, stdin) != NULL) { switch(error = json_parse_fragment( &state, buffer)) { case JSON_OK: printf("complete\n"); json_tree_to_string(state.cursor, &temp); printf("%s\n",temp); break; case JSON_WAITING_FOR_EOF: case JSON_INCOMPLETE_DOCUMENT: break; default: printf("Some error occurred: %d\n", error); } } else { if(error == JSON_WAITING_FOR_EOF) error = JSON_OK; else error = JSON_UNKNOWN_PROBLEM; } } if(error == JSON_OK) { json_render_tree(state.cursor); } else { printf("Document wasn't valid.\n"); } /* perform cleanup */ json_free_value(&state.cursor); return 0; }
std::string YtkUtils::jsonToString(json_t* root, bool format) { std::string ret; char *text = 0; if( JSON_OK != json_tree_to_string (root, &text) ) return ret; if(format) { char *o = json_format_string(text); ret.assign(o); delete[] o; } else { ret.assign(text); } delete[] text; return ret; }
// // Encrypt text // bool encrypt_method(LSHandle* lshandle, LSMessage *message, void *ctx) { LSError lserror; LSErrorInit(&lserror); // Local buffer to store the update command char command[MAXLINLEN]; char *document; // Extract the userdata argument from the message json_t *object = json_parse_document(LSMessageGetPayload(message)); json_t *userdata = json_find_first_label(object, "userdata"); if (!userdata || (userdata->child->type != JSON_OBJECT)) { if (!LSMessageRespond(message, "{\"returnValue\": false, \"errorCode\": -1, \"errorText\": \"Invalid or missing userdata\"}", &lserror)) goto error; return true; } if (json_tree_to_string(userdata->child, &document) != JSON_OK) { if (!LSMessageRespond(message, "{\"errorText\": \"Unable to parse userdata\", \"returnValue\": false, \"errorCode\": -1 }", &lserror)) goto error; return true; } // Store the command, so it can be used in the error report if necessary sprintf(command, "echo '%s' | openssl rsautl -encrypt -certin -inkey /media/cryptofs/apps/usr/palm/applications/com.apptuckerbox.register/certs/com.apptuckerbox.crt | openssl enc -e -a 2>&1", document); return simple_command(message, command); error: LSErrorPrint(&lserror, stderr); LSErrorFree(&lserror); end: return false; }
bool stop_method(LSHandle* lshandle, LSMessage *message, void *ctx) { bool returnVal = true; char line[MAXLINELEN]; // %%% MAGIC NUMBERS ALERT %%% char name[128]; char status[128]; LSError lserror; LSErrorInit(&lserror); char *jsonResponse = 0; int len = 0; json_t *object = json_parse_document(LSMessageGetPayload(message)); json_t *id = json_find_first_label(object, "id"); if (strspn(id->child->text, ALLOWED_CHARS) != strlen(id->child->text)) { LSMessageReply(lshandle, message, "{\"returnValue\":false,\"errorCode\":-1,\"errorText\":\"Invalid id\"}", &lserror); LSErrorFree(&lserror); return true; } // %%% MAGIC NUMBERS ALERT %%% char command[128]; char format[128]; // %%% IGNORING RETURN ALERT %%% sprintf((char *)&command, "/sbin/initctl stop %s 2>&1", id->child->text); json_t *response = json_new_object(); FILE *fp = popen(command, "r"); if (fp) { while ( fgets( line, sizeof line, fp)) { if (sscanf(line, "(%*d/%*d) Job not changed: %127s\n", (char *)&name) == 1) { // %%% IGNORING RETURN ALERT %%% json_insert_pair_into_object(response, "status", json_new_string("Job not changed")); } else if (sscanf(line, "(%*d/%*d) %s %127c\n", (char *)&name, (char *)&status) == 2) { // %%% HACK ALERT %%% *strchr(status,'\n') = 0; // %%% IGNORING RETURN ALERT %%% json_insert_pair_into_object(response, "status", json_new_string(status)); } } if (!pclose(fp)) { // %%% IGNORING RETURN ALERT %%% json_insert_pair_into_object(response, "returnValue", json_new_true()); } else { // %%% IGNORING RETURN ALERT %%% json_insert_pair_into_object(response, "returnValue", json_new_false()); } json_tree_to_string(response, &jsonResponse); } if (jsonResponse) { LSMessageReply(lshandle, message, jsonResponse, &lserror); free(jsonResponse); } else LSMessageReply(lshandle, message, "{\"returnValue\":false,\"errorCode\":-1,\"errorText\":\"Generic error\"}", &lserror); json_free_value(&response); LSErrorFree(&lserror); return returnVal; }
bool list_method(LSHandle* lshandle, LSMessage *message, void *ctx) { bool returnVal = true; char line[MAXLINELEN]; // %%% MAGIC NUMBERS ALERT %%% char name[128]; char state[16]; char status[128]; LSError lserror; LSErrorInit(&lserror); char *jsonResponse = 0; int len = 0; json_t *response = json_new_object(); FILE *fp = popen("/sbin/initctl list", "r"); if (fp) { json_t *array = json_new_array(); while ( fgets( line, sizeof line, fp)) { // %%% MAGIC NUMBERS ALERT %%% if (sscanf(line, "%127s (start) %127c", (char*)&name, (char *)&status) == 2) { // %%% HACK ALERT %%% *strchr(status,'\n') = 0; json_t *object = json_new_object(); // %%% IGNORING RETURN ALERT %%% json_insert_pair_into_object(object, "name", json_new_string(name)); json_insert_pair_into_object(object, "state", json_new_string("start")); json_insert_pair_into_object(object, "status", json_new_string(status)); json_insert_child(array, object); } // %%% MAGIC NUMBERS ALERT %%% else if (sscanf(line, "%127s (stop) %127c", (char*)&name, (char *)&status) == 2) { // %%% HACK ALERT %%% *strchr(status,'\n') = 0; json_t *object = json_new_object(); // %%% IGNORING RETURN ALERT %%% json_insert_pair_into_object(object, "name", json_new_string(name)); json_insert_pair_into_object(object, "state", json_new_string("stop")); json_insert_pair_into_object(object, "status", json_new_string(status)); json_insert_child(array, object); } } if (!pclose(fp)) { // %%% IGNORING RETURN ALERT %%% json_insert_pair_into_object(response, "returnValue", json_new_true()); json_insert_pair_into_object(response, "jobs", array); json_tree_to_string(response, &jsonResponse); } } if (jsonResponse) { LSMessageReply(lshandle, message, jsonResponse, &lserror); free(jsonResponse); } else LSMessageReply(lshandle, message, "{\"returnValue\":false,\"errorCode\":-1,\"errorText\":\"Generic error\"}", &lserror); json_free_value(&response); LSErrorFree(&lserror); return returnVal; }
bool put_file_method(LSHandle* lshandle, LSMessage *message, void *ctx) { LSError lserror; LSErrorInit(&lserror); char buffer[MAXBUFLEN]; json_t *object = json_parse_document(LSMessageGetPayload(message)); json_t *id; // Extract the url argument from the message id = json_find_first_label(object, "filename"); if (!id || (id->child->type != JSON_STRING) || (strlen(id->child->text) >= MAXLINLEN) || (strlen(id->child->text) < 8) || strncmp(id->child->text, "file://", 7)) { if (!LSMessageRespond(message, "{\"returnValue\": false, \"errorCode\": -1, " "\"errorText\": \"Invalid or missing filename parameter\"}", &lserror)) goto error; return true; } char filename[MAXLINLEN]; strcpy(filename, id->child->text+7); // Extract the object argument from the message id = json_find_first_label(object, "object"); if (!id || (id->child->type != JSON_OBJECT)) { if (!LSMessageRespond(message, "{\"returnValue\": false, \"errorCode\": -1, " "\"errorText\": \"Invalid or missing object parameter\"}", &lserror)) goto error; return true; } char *contents = NULL; if (json_tree_to_string(id->child, &contents) != JSON_OK) { sprintf(buffer, "{\"errorText\": \"Unable to parse object\", \"returnValue\": false, \"errorCode\": -1 }"); if (!LSMessageRespond(message, buffer, &lserror)) goto error; return true; } FILE *fp = fopen(filename, "w"); if (!fp) { sprintf(buffer, "{\"errorText\": \"Unable to open %s\", \"returnValue\": false, \"errorCode\": -1 }", filename); if (!LSMessageRespond(message, buffer, &lserror)) goto error; return true; } if (fputs(contents, fp) == EOF) { (void)fclose(fp); (void)unlink(filename); sprintf(buffer, "{\"errorText\": \"Unable to write to %s\", \"returnValue\": false, \"errorCode\": -1 }", filename); if (!LSMessageRespond(message, buffer, &lserror)) goto error; return true; } if (fclose(fp)) { sprintf(buffer, "{\"errorText\": \"Unable to close %s\", \"returnValue\": false, \"errorCode\": -1 }", filename); if (!LSMessageRespond(message, buffer, &lserror)) goto error; return true; } if (!LSMessageRespond(message, "{\"returnValue\": true}", &lserror)) goto error; return true; error: LSErrorPrint(&lserror, stderr); LSErrorFree(&lserror); end: return false; }
// // Impersonate a call to the requested service and return the output to webOS. // bool impersonate_method(LSHandle* lshandle, LSMessage *message, void *ctx) { bool retVal; LSError lserror; LSErrorInit(&lserror); LSMessageRef(message); if (access_denied(message)) return true; // Extract the method argument from the message json_t *object = json_parse_document(LSMessageGetPayload(message)); json_t *id = json_find_first_label(object, "id"); if (!id || (id->child->type != JSON_STRING)) { if (!LSMessageRespond(message, "{\"returnValue\": false, \"errorCode\": -1, \"errorText\": \"Invalid or missing id\"}", &lserror)) goto error; return true; } // Extract the service argument from the message object = json_parse_document(LSMessageGetPayload(message)); json_t *service = json_find_first_label(object, "service"); if (!service || (service->child->type != JSON_STRING)) { if (!LSMessageRespond(message, "{\"returnValue\": false, \"errorCode\": -1, \"errorText\": \"Invalid or missing service\"}", &lserror)) goto error; return true; } // Extract the method argument from the message object = json_parse_document(LSMessageGetPayload(message)); json_t *method = json_find_first_label(object, "method"); if (!method || (method->child->type != JSON_STRING)) { if (!LSMessageRespond(message, "{\"returnValue\": false, \"errorCode\": -1, \"errorText\": \"Invalid or missing method\"}", &lserror)) goto error; return true; } // Extract the params argument from the message object = json_parse_document(LSMessageGetPayload(message)); json_t *params = json_find_first_label(object, "params"); if (!params || (params->child->type != JSON_OBJECT)) { if (!LSMessageRespond(message, "{\"returnValue\": false, \"errorCode\": -1, \"errorText\": \"Invalid or missing params\"}", &lserror)) goto error; return true; } char uri[MAXLINLEN]; sprintf(uri, "palm://%s/%s", service->child->text, method->child->text); char *paramstring = NULL; json_tree_to_string (params->child, ¶mstring); if (!LSCallFromApplication(priv_serviceHandle, uri, paramstring, id->child->text, impersonate_handler, message, NULL, &lserror)) goto error; return true; error: LSErrorPrint(&lserror, stderr); LSErrorFree(&lserror); end: return false; }