int dbrelay_db_fill_data(json_t *json, dbrelay_connection_t *conn) { int numcols, colnum; char tmp[256]; int maxcolname; json_add_key(json, "data"); json_new_array(json); while (api->has_results(conn->db)) { maxcolname = 0; json_new_object(json); json_add_key(json, "fields"); json_new_array(json); numcols = api->numcols(conn->db); for (colnum=1; colnum<=numcols; colnum++) { dbrelay_write_json_colinfo(json, conn->db, colnum, &maxcolname); } json_end_array(json); json_add_key(json, "rows"); if (json_get_mode(json)==DBRELAY_JSON_MODE_STD) json_new_array(json); else json_add_json(json, "\""); while (api->fetch_row(conn->db)) { maxcolname = 0; if (json_get_mode(json)==DBRELAY_JSON_MODE_STD) json_new_object(json); for (colnum=1; colnum<=numcols; colnum++) { dbrelay_write_json_column(json, conn->db, colnum, &maxcolname); if (json_get_mode(json)==DBRELAY_JSON_MODE_CSV && colnum!=numcols) json_add_json(json, ","); } if (json_get_mode(json)==DBRELAY_JSON_MODE_STD) json_end_object(json); else json_add_json(json, "\\n"); } if (json_get_mode(json)==DBRELAY_JSON_MODE_STD) json_end_array(json); else json_add_json(json, "\","); if (api->rowcount(conn->db)==-1) { json_add_null(json, "count"); } else { sprintf(tmp, "%d", api->rowcount(conn->db)); json_add_number(json, "count", tmp); } json_end_object(json); } /* sprintf(error_string, "rc = %d", rc); */ json_end_array(json); return 0; }
static json_t *SaveCharacters(CharacterStore *s) { json_t *charNode = json_new_array(); int i; for (i = 0; i < (int)s->OtherChars.size; i++) { json_t *node = json_new_object(); Character *c = CArrayGet(&s->OtherChars, i); AddIntPair(node, "face", c->looks.Face); AddIntPair(node, "skin", c->looks.Skin); AddIntPair(node, "arm", c->looks.Arm); AddIntPair(node, "body", c->looks.Body); AddIntPair(node, "leg", c->looks.Leg); AddIntPair(node, "hair", c->looks.Hair); AddIntPair(node, "speed", c->speed); json_insert_pair_into_object( node, "Gun", json_new_string(c->Gun->name)); AddIntPair(node, "maxHealth", c->maxHealth); AddIntPair(node, "flags", c->flags); AddIntPair(node, "probabilityToMove", c->bot->probabilityToMove); AddIntPair(node, "probabilityToTrack", c->bot->probabilityToTrack); AddIntPair(node, "probabilityToShoot", c->bot->probabilityToShoot); AddIntPair(node, "actionDelay", c->bot->actionDelay); json_insert_child(charNode, node); } return charNode; }
json_node_t* json_tsfile_get_array(tsfile_t* file, unsigned start, unsigned end) { json_node_t** nodes = NULL; json_node_t* node_array = NULL; unsigned long entry_size = file->header->schema.hdr.entry_size; void* entries = NULL; void* entry; int count = end - start; int ni; if(count <= 0) { tsfile_errno = TSFILE_INVAL_RANGE; return NULL; } entries = mp_malloc(entry_size * count); if(tsfile_get_entries(file, entries, start, end) != TSFILE_OK) { goto end; } nodes = tsfile_get_nodes(file, count); node_array = json_new_array(); for(ni = 0; ni < count; ++ni) { entry = ((char*) entries) + entry_size * ni; tsfile_fill_node(file, nodes[ni], entry); json_add_node(node_array, NULL, nodes[ni]); } end: if(nodes) mp_free(nodes); mp_free(entries); return node_array; }
void subuser_restor(subuser *sub, acetables *g_ape) { CHANLIST *chanl; CHANNEL *chan; json_item *jlist; RAW *newraw; USERS *user = sub->user; userslist *ulist; chanl = user->chan_foot; while (chanl != NULL) { chan = chanl->chaninfo; /* * quiet channel won't be posted on subuser_restor */ if (!(chan->flags & CHANNEL_QUIET)) { jlist = json_new_object(); if ( !(chan->flags & CHANNEL_NONINTERACTIVE) && chan->head != NULL ) { json_item *user_list = json_new_array(); ulist = chan->head; while (ulist != NULL) { json_item *juser = get_json_object_user(ulist->userinfo); if (ulist->userinfo != user) { //make_link(user, ulist->userinfo); } json_set_property_intN(juser, "level", 5, ulist->level); json_set_element_obj(user_list, juser); ulist = ulist->next; } json_set_property_objN(jlist, "users", 5, user_list); } json_set_property_objN(jlist, "pipe", 4, get_json_object_channel(chan)); newraw = forge_raw(RAW_CHANNEL, jlist); newraw->priority = RAW_PRI_HI; post_raw_sub(newraw, sub, g_ape); POSTRAW_DONE(newraw); } chanl = chanl->next; } jlist = json_new_object(); json_set_property_objN(jlist, "user", 4, get_json_object_user(user)); newraw = forge_raw("IDENT", jlist); newraw->priority = RAW_PRI_HI; post_raw_sub(newraw, sub, g_ape); POSTRAW_DONE(newraw); }
static void AddMissionNodes(Autosave *a, json_t *root, const char *nodeName) { json_t *missions = json_new_array(); CA_FOREACH(MissionSave, m, a->Missions) json_insert_child(missions, CreateMissionNode(m)); CA_FOREACH_END() json_insert_pair_into_object(root, nodeName, missions); }
static json_t *SaveStaticCharacters(Mission *m) { json_t *chars = json_new_array(); CA_FOREACH(CharacterPositions, cp, m->u.Static.Characters) json_t *charNode = json_new_object(); AddIntPair(charNode, "Index", cp->Index); json_t *positions = json_new_array(); for (int j = 0; j < (int)cp->Positions.size; j++) { Vec2i *pos = CArrayGet(&cp->Positions, j); json_insert_child(positions, SaveVec2i(*pos)); } json_insert_pair_into_object( charNode, "Positions", positions); json_insert_child(chars, charNode); CA_FOREACH_END() return chars; }
static json_t *SaveStaticKeys(Mission *m) { json_t *keys = json_new_array(); CA_FOREACH(KeyPositions, kp, m->u.Static.Keys) json_t *keyNode = json_new_object(); AddIntPair(keyNode, "Index", kp->Index); json_t *positions = json_new_array(); for (int j = 0; j < (int)kp->Positions.size; j++) { Vec2i *pos = CArrayGet(&kp->Positions, j); json_insert_child(positions, SaveVec2i(*pos)); } json_insert_pair_into_object( keyNode, "Positions", positions); json_insert_child(keys, keyNode); CA_FOREACH_END() return keys; }
static json_t *SaveStaticWrecks(Mission *m) { json_t *wrecks = json_new_array(); CA_FOREACH(MapObjectPositions, mop, m->u.Static.Wrecks) json_t *wreckNode = json_new_object(); AddStringPair(wreckNode, "MapObject", mop->M->Name); json_t *positions = json_new_array(); for (int j = 0; j < (int)mop->Positions.size; j++) { Vec2i *pos = CArrayGet(&mop->Positions, j); json_insert_child(positions, SaveVec2i(*pos)); } json_insert_pair_into_object( wreckNode, "Positions", positions); json_insert_child(wrecks, wreckNode); CA_FOREACH_END() return wrecks; }
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; }
static json_t *SaveWeapons(const CArray *weapons) { json_t *node = json_new_array(); for (int i = 0; i < (int)weapons->size; i++) { const GunDescription **g = CArrayGet(weapons, i); json_insert_child(node, json_new_string((*g)->name)); } return node; }
static json_t *SaveStaticObjectives(Mission *m) { json_t *objs = json_new_array(); CA_FOREACH(ObjectivePositions, op, m->u.Static.Objectives) json_t *objNode = json_new_object(); AddIntPair(objNode, "Index", op->Index); json_t *positions = json_new_array(); for (int j = 0; j < (int)op->Positions.size; j++) { Vec2i *pos = CArrayGet(&op->Positions, j); json_insert_child(positions, SaveVec2i(*pos)); } json_insert_pair_into_object( objNode, "Positions", positions); json_insert_pair_into_object( objNode, "Indices", SaveIntArray(&op->Indices)); json_insert_child(objs, objNode); CA_FOREACH_END() return objs; }
static void AddInputConfigNode(InputConfig *config, json_t *root) { int i; json_t *subConfig = json_new_object(); json_t *keyConfigs = json_new_array(); for (i = 0; i < 2; i++) { AddKeyConfigNode(&config->PlayerKeys[i], keyConfigs); } json_insert_pair_into_object(subConfig, "Keys", keyConfigs); json_insert_pair_into_object(root, "Input", subConfig); }
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; }
static json_t *SaveStaticItems(Mission *m) { json_t *items = json_new_array(); for (int i = 0; i < (int)m->u.Static.Items.size; i++) { MapObjectPositions *mop = CArrayGet(&m->u.Static.Items, i); json_t *itemNode = json_new_object(); AddStringPair(itemNode, "MapObject", mop->M->Name); json_t *positions = json_new_array(); for (int j = 0; j < (int)mop->Positions.size; j++) { Vec2i *pos = CArrayGet(&mop->Positions, j); json_insert_child(positions, SaveVec2i(*pos)); } json_insert_pair_into_object( itemNode, "Positions", positions); json_insert_child(items, itemNode); } return items; }
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; }
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); }
int main() { int i = 0; int rc; while (i++ < 1) { json_t *jso = json_new(); json_add_string(jso, "string1", "string1_val"); json_add_string(jso, "string2", "string2_val"); json_add_int(jso, "integer1", 12345); printf("%s\n", json_get_string(jso, "string1")); printf("%s\n%d\n", json_get_string(jso, "string2"), json_get_int(jso, "integer1")); json_padd_string(jso, "sub-object/sub-string", "sub-string-val"); json_padd_string(jso, "object1/sub-object/sub-string", "sub-string-val"); char *path = "object1/sub-object1/sub-sub-object1//sub-sub-string1"; json_padd_string(jso, path, "sub-sub-string1-val "); printf("sub-sub-string1=[%s]\n", json_pget_string(jso, path)); json_add_string(jso, "string3", "string3_val"); json_padd_string(jso, "string4", "string4_val"); // LOG("\n%s", json_to_string(jso)); array_t *arr = json_new_array( jso, "array0" ); rc = json_array_add_string( arr, 0, "array-item", "array-item0-val" ); rc = json_array_add_int( arr, 0, "array-item-int", 1234 ); rc = json_array_add_string( arr, 1, "array-item", "array-item1-val" ); rc = json_array_add_int( arr, 1, "array-item-int", 2234 ); rc = json_array_add_string( arr, 2, "array-item", "array-item2-val" ); rc = json_array_add_int( arr, 2, "array-item-int", 3234 ); /* LOG( "rc = [%d][%s][%s][%s][%d]", rc, json_array_get_string( arr, 0, "array-item"), json_array_get_string( arr, 1, "array-item" ), json_array_get_string( arr, 2, "array-item" ), json_array_get_int( arr, 2, "array-item-int") ); */ //printf( "====[%d]===[%s]-=-----\n", json_array_getlen(arr),json_array_get_string( arr, 0, "array-item" ) ); json_t *tmp1 = json_array_get_item( arr, 1 ); //printf( "-----------\n%s\n--------\n", json_to_string(tmp1) ); //printf("\n%s\n", json_to_string(jso)); LOG( "\n%s\n", json_to_string(jso) ); json_free(&jso); } //system("pause"); }
static json_t *SaveObjectives(CArray *a) { json_t *objectivesNode = json_new_array(); int i; for (i = 0; i < (int)a->size; i++) { json_t *objNode = json_new_object(); MissionObjective *mo = CArrayGet(a, i); AddStringPair(objNode, "Description", mo->Description); AddStringPair(objNode, "Type", ObjectiveTypeStr(mo->Type)); AddIntPair(objNode, "Index", mo->Index); AddIntPair(objNode, "Count", mo->Count); AddIntPair(objNode, "Required", mo->Required); AddIntPair(objNode, "Flags", mo->Flags); json_insert_child(objectivesNode, objNode); } return objectivesNode; }
static json_t *SaveCharacters(CharacterStore *s) { json_t *charNode = json_new_array(); CA_FOREACH(Character, c, s->OtherChars) json_t *node = json_new_object(); AddStringPair(node, "Class", c->Class->Name); AddColorPair(node, "Skin", c->Colors.Skin); AddColorPair(node, "Arms", c->Colors.Arms); AddColorPair(node, "Body", c->Colors.Body); AddColorPair(node, "Legs", c->Colors.Legs); AddColorPair(node, "Hair", c->Colors.Hair); AddIntPair(node, "speed", c->speed); json_insert_pair_into_object( node, "Gun", json_new_string(c->Gun->name)); AddIntPair(node, "maxHealth", c->maxHealth); AddIntPair(node, "flags", c->flags); AddIntPair(node, "probabilityToMove", c->bot->probabilityToMove); AddIntPair(node, "probabilityToTrack", c->bot->probabilityToTrack); AddIntPair(node, "probabilityToShoot", c->bot->probabilityToShoot); AddIntPair(node, "actionDelay", c->bot->actionDelay); json_insert_child(charNode, node); CA_FOREACH_END() return charNode; }
/* 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; } } }
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; }
void join(USERS *user, CHANNEL *chan, acetables *g_ape) { userslist *list, *ulist; CHANLIST *chanl; FIRE_EVENT_NULL(join, user, chan, g_ape); RAW *newraw; json_item *jlist = json_new_object(); if (isonchannel(user, chan)) { return; } list = xmalloc(sizeof(*list)); // TODO is it free ? list->userinfo = user; list->level = 1; list->next = chan->head; chan->head = list; chanl = xmalloc(sizeof(*chanl)); // TODO is it free ? chanl->chaninfo = chan; chanl->next = user->chan_foot; user->chan_foot = chanl; if (chan->interactive) { json_item *user_list = json_new_array(); json_item *uinfo = json_new_object(); json_set_property_objN(uinfo, "user", 4, get_json_object_user(user)); json_set_property_objN(uinfo, "pipe", 4, get_json_object_channel(chan)); newraw = forge_raw(RAW_JOIN, uinfo); post_raw_channel_restricted(newraw, chan, user, g_ape); ulist = chan->head; while (ulist != NULL) { json_item *juser = get_json_object_user(ulist->userinfo); if (ulist->userinfo != user) { //make_link(user, ulist->userinfo); } json_set_property_intN(juser, "level", 5, ulist->level); json_set_element_obj(user_list, juser); ulist = ulist->next; } json_set_property_objN(jlist, "users", 5, user_list); } json_set_property_objN(jlist, "pipe", 4, get_json_object_channel(chan)); newraw = forge_raw(RAW_CHANNEL, jlist); post_raw(newraw, user, g_ape); #if 0 if (user->flags & FLG_AUTOOP) { setlevel(NULL, user, chan, 3); } #endif }
u_char *dbrelay_db_status(dbrelay_request_t *request) { dbrelay_connection_t *connections; dbrelay_connection_t *conn; json_t *json = json_new(); int i; char tmpstr[100]; u_char *json_output; struct tm *ts; json_new_object(json); json_add_key(json, "status"); json_new_object(json); json_add_key(json, "info"); json_new_object(json); json_add_string(json, "build", DBRELAY_BUILD); sprintf(tmpstr, "0x%08x", dbrelay_get_ipc_key()); json_add_string(json, "ipckey", tmpstr); json_end_object(json); json_add_key(json, "connections"); json_new_array(json); connections = dbrelay_time_get_shmem(request); for (i=0; i<DBRELAY_MAX_CONN; i++) { conn = &connections[i]; if (connections[i].pid!=0) { json_new_object(json); sprintf(tmpstr, "%u", conn->slot); json_add_number(json, "slot", tmpstr); sprintf(tmpstr, "%u", conn->pid); json_add_number(json, "pid", tmpstr); json_add_string(json, "name", conn->connection_name ? conn->connection_name : ""); ts = localtime(&conn->tm_create); strftime(tmpstr, sizeof(tmpstr), "%Y-%m-%d %H:%M:%S", ts); json_add_string(json, "tm_created", tmpstr); ts = localtime(&conn->tm_accessed); strftime(tmpstr, sizeof(tmpstr), "%Y-%m-%d %H:%M:%S", ts); json_add_string(json, "tm_accessed", tmpstr); json_add_string(json, "sql_server", conn->sql_server ? conn->sql_server : ""); json_add_string(json, "sql_port", conn->sql_port ? conn->sql_port : ""); json_add_string(json, "sql_database", conn->sql_database ? conn->sql_database : ""); json_add_string(json, "sql_user", conn->sql_user ? conn->sql_user : ""); sprintf(tmpstr, "%ld", conn->connection_timeout); json_add_number(json, "connection_timeout", tmpstr); sprintf(tmpstr, "%u", conn->in_use); json_add_number(json, "in_use", tmpstr); json_add_string(json, "sock_path", conn->sock_path); sprintf(tmpstr, "%u", conn->helper_pid); json_add_number(json, "helper_pid", tmpstr); json_end_object(json); } } dbrelay_time_release_shmem(request, connections); json_end_array(json); json_end_object(json); json_end_object(json); json_output = (u_char *) json_to_string(json); json_free(json); return json_output; }
static json_t *SaveMissions(CArray *a) { json_t *missionsNode = json_new_array(); for (int i = 0; i < (int)a->size; i++) { json_t *node = json_new_object(); Mission *mission = CArrayGet(a, i); AddStringPair(node, "Title", mission->Title); AddStringPair(node, "Description", mission->Description); AddStringPair(node, "Type", MapTypeStr(mission->Type)); AddIntPair(node, "Width", mission->Size.x); AddIntPair(node, "Height", mission->Size.y); AddIntPair(node, "WallStyle", mission->WallStyle); AddIntPair(node, "FloorStyle", mission->FloorStyle); AddIntPair(node, "RoomStyle", mission->RoomStyle); AddIntPair(node, "ExitStyle", mission->ExitStyle); AddIntPair(node, "KeyStyle", mission->KeyStyle); AddStringPair(node, "DoorStyle", mission->DoorStyle); json_insert_pair_into_object( node, "Objectives", SaveObjectives(&mission->Objectives)); json_insert_pair_into_object( node, "Enemies", SaveIntArray(&mission->Enemies)); json_insert_pair_into_object( node, "SpecialChars", SaveIntArray(&mission->SpecialChars)); json_t *modsNode = json_new_array(); for (int j = 0; j < (int)mission->MapObjectDensities.size; j++) { const MapObjectDensity *mod = CArrayGet(&mission->MapObjectDensities, j); json_t *modNode = json_new_object(); AddStringPair(modNode, "MapObject", mod->M->Name); AddIntPair(modNode, "Density", mod->Density); json_insert_child(modsNode, modNode); } json_insert_pair_into_object(node, "MapObjectDensities", modsNode); AddIntPair(node, "EnemyDensity", mission->EnemyDensity); json_insert_pair_into_object( node, "Weapons", SaveWeapons(&mission->Weapons)); json_insert_pair_into_object( node, "Song", json_new_string(mission->Song)); AddColorPair(node, "WallMask", mission->WallMask); AddColorPair(node, "FloorMask", mission->FloorMask); AddColorPair(node, "RoomMask", mission->RoomMask); AddColorPair(node, "AltMask", mission->AltMask); switch (mission->Type) { case MAPTYPE_CLASSIC: AddIntPair(node, "Walls", mission->u.Classic.Walls); AddIntPair(node, "WallLength", mission->u.Classic.WallLength); AddIntPair( node, "CorridorWidth", mission->u.Classic.CorridorWidth); json_insert_pair_into_object( node, "Rooms", SaveClassicRooms(mission)); AddIntPair(node, "Squares", mission->u.Classic.Squares); json_insert_pair_into_object( node, "Doors", SaveClassicDoors(mission)); json_insert_pair_into_object( node, "Pillars", SaveClassicPillars(mission)); break; case MAPTYPE_STATIC: { json_insert_pair_into_object( node, "Tiles", SaveStaticTiles(mission)); json_insert_pair_into_object( node, "StaticItems", SaveStaticItems(mission)); json_insert_pair_into_object( node, "StaticWrecks", SaveStaticWrecks(mission)); json_insert_pair_into_object( node, "StaticCharacters", SaveStaticCharacters(mission)); json_insert_pair_into_object( node, "StaticObjectives", SaveStaticObjectives(mission)); json_insert_pair_into_object( node, "StaticKeys", SaveStaticKeys(mission)); json_insert_pair_into_object( node, "Start", SaveVec2i(mission->u.Static.Start)); json_t *exitNode = json_new_object(); json_insert_pair_into_object( exitNode, "Start", SaveVec2i(mission->u.Static.Exit.Start)); json_insert_pair_into_object( exitNode, "End", SaveVec2i(mission->u.Static.Exit.End)); json_insert_pair_into_object(node, "Exit", exitNode); } break; default: assert(0 && "unknown map type"); break; } json_insert_child(missionsNode, node); } return missionsNode; }