static json_t *SaveVec2i(Vec2i v) { json_t *node = json_new_array(); char buf[32]; sprintf(buf, "%d", v.x); json_insert_child(node, json_new_number(buf)); sprintf(buf, "%d", v.y); json_insert_child(node, json_new_number(buf)); return node; }
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); }
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 AddGameConfigNode(GameConfig *config, json_t *root) { char buf[32]; json_t *subConfig = json_new_object(); json_insert_pair_into_object( subConfig, "FriendlyFire", json_new_bool(config->FriendlyFire)); sprintf(buf, "%u", config->RandomSeed); json_insert_pair_into_object( subConfig, "RandomSeed", json_new_number(buf)); JSON_UTILS_ADD_ENUM_PAIR(subConfig, "Difficulty", config->Difficulty, DifficultyStr); json_insert_pair_into_object( subConfig, "SlowMotion", json_new_bool(config->SlowMotion)); AddIntPair(subConfig, "EnemyDensity", config->EnemyDensity); AddIntPair(subConfig, "NonPlayerHP", config->NonPlayerHP); AddIntPair(subConfig, "PlayerHP", config->PlayerHP); json_insert_pair_into_object( subConfig, "Fog", json_new_bool(config->Fog)); AddIntPair(subConfig, "SightRange", config->SightRange); json_insert_pair_into_object( subConfig, "Shadows", json_new_bool(config->Shadows)); json_insert_pair_into_object( subConfig, "MoveWhenShooting", json_new_bool(config->MoveWhenShooting)); JSON_UTILS_ADD_ENUM_PAIR( subConfig, "SwitchMoveStyle", config->SwitchMoveStyle, SwitchMoveStyleStr); json_insert_pair_into_object( subConfig, "ShotsPushback", json_new_bool(config->ShotsPushback)); JSON_UTILS_ADD_ENUM_PAIR( subConfig, "AllyCollision", config->AllyCollision, AllyCollisionStr); json_insert_pair_into_object( subConfig, "HealthPickups", json_new_bool(config->HealthPickups)); json_insert_pair_into_object(root, "Game", subConfig); }
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); }
static json_t *SaveIntArray(CArray *a) { json_t *node = json_new_array(); int i; for (i = 0; i < (int)a->size; i++) { char buf[32]; sprintf(buf, "%d", *(int *)CArrayGet(a, i)); json_insert_child(node, json_new_number(buf)); } return node; }
void ScriptDialog::saveJson() { json_t *root = json_new_object(); json_t *t; t = json_new_number((const char*)m_id.toLatin1()); json_insert_pair_into_object(root, "id", t); t = json_new_string((const char*)m_title.toLocal8Bit()); json_insert_pair_into_object(root, "title", t); t = json_new_string((const char*)m_url.toLocal8Bit()); json_insert_pair_into_object(root, "url", t); json_t *seqNode = json_new_array(); for(int i=0, len=m_stepSeq.size(); i<len; ++i){ QString s = m_stepSeq.at(i); json_t *v = json_new_string((const char*)s.toLocal8Bit()); json_insert_child(seqNode, v); } json_t *stepsNode = json_new_object(); json_insert_pair_into_object(stepsNode, "seq", seqNode); QMapIterator<QString, QString> iter(m_steps); while(iter.hasNext()){ iter.next(); if(iter.value().isEmpty())continue; QByteArray ba_k = iter.key().toLocal8Bit(); const char *k = (const char*)ba_k; QByteArray ba_v = iter.value().toLocal8Bit(); char *v = ba_v.data(); QByteArray ba_t = m_stepTests.value(iter.key()).toLocal8Bit(); char *t = ba_t.data(); v = json_escape(v); t = json_escape(t); json_t *vn = json_new_string( v ); json_t *tn = json_new_string( t ); json_t *step = json_new_object(); json_insert_pair_into_object(step, "test", tn); json_insert_pair_into_object(step, "script", vn); json_insert_pair_into_object(stepsNode, k, step); } json_insert_pair_into_object(root, "steps", stepsNode); BatDownUtils::writeJsonToFile(root, m_fn); }
void AddIntPair(json_t *parent, const char *name, int number) { char buf[32]; sprintf(buf, "%d", number); json_insert_pair_into_object(parent, name, json_new_number(buf)); }
/* JSON metadata dumping */ static void amf_to_json(const amf_data * data, json_t ** object) { if (data != NULL) { json_t * value; amf_node * node; time_t time; struct tm * t; char str[128]; char * escaped_str; switch (data->type) { case AMF_TYPE_NUMBER: sprintf(str, "%.12g", data->number_data); *object = json_new_number(str); break; case AMF_TYPE_BOOLEAN: *object = (data->boolean_data) ? json_new_true() : json_new_false(); break; case AMF_TYPE_STRING: escaped_str = json_escape((char *)amf_string_get_bytes(data)); *object = json_new_string(escaped_str); free(escaped_str); break; case AMF_TYPE_OBJECT: *object = json_new_object(); node = amf_object_first(data); while (node != NULL) { amf_to_json(amf_object_get_data(node), &value); escaped_str = json_escape((char *)amf_string_get_bytes(amf_object_get_name(node))); json_insert_pair_into_object(*object, escaped_str, value); free(escaped_str); node = amf_object_next(node); } break; case AMF_TYPE_NULL: case AMF_TYPE_UNDEFINED: *object = json_new_null(); break; case AMF_TYPE_ASSOCIATIVE_ARRAY: *object = json_new_object(); node = amf_associative_array_first(data); while (node != NULL) { amf_to_json(amf_associative_array_get_data(node), &value); json_insert_pair_into_object(*object, (const char *)amf_string_get_bytes(amf_associative_array_get_name(node)), value); node = amf_associative_array_next(node); } break; case AMF_TYPE_ARRAY: *object = json_new_array(); node = amf_array_first(data); while (node != NULL) { amf_to_json(amf_array_get(node), &value); json_insert_child(*object, value); node = amf_array_next(node); } break; case AMF_TYPE_DATE: time = amf_date_to_time_t(data); tzset(); t = localtime(&time); strftime(str, sizeof(str), "%Y-%m-%dT%H:%M:%S", t); *object = json_new_string(str); break; case AMF_TYPE_XML: break; case AMF_TYPE_CLASS: break; default: break; } } }