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 *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; }
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; }
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 *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 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; }
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; }
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); }
json_t *new_entry(char *name, char *phone) { json_t *entry, *label, *value; // 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); return label; }
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; }
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; }
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; }
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; }
/* 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; }
static void AddKeyConfigNode(KeyConfig *config, json_t *parent) { json_t *subConfig = json_new_object(); AddKeysNode(&config->Keys, subConfig); json_insert_child(parent, subConfig); }