static void AddSoundConfigNode(SoundConfig *config, json_t *root) { json_t *subConfig = json_new_object(); AddIntPair(subConfig, "SoundVolume", config->SoundVolume); AddIntPair(subConfig, "MusicVolume", config->MusicVolume); json_insert_pair_into_object( subConfig, "Footsteps", json_new_bool(config->Footsteps)); json_insert_pair_into_object( subConfig, "Hits", json_new_bool(config->Hits)); json_insert_pair_into_object( subConfig, "Reloads", json_new_bool(config->Reloads)); json_insert_pair_into_object(root, "Sound", subConfig); }
static void AddInterfaceConfigNode(InterfaceConfig *config, json_t *root) { json_t *subConfig = json_new_object(); json_insert_pair_into_object( subConfig, "ShowFPS", json_new_bool(config->ShowFPS)); json_insert_pair_into_object( subConfig, "ShowTime", json_new_bool(config->ShowTime)); json_insert_pair_into_object( subConfig, "Splitscreen", json_new_string(SplitscreenStyleStr(config->Splitscreen))); json_insert_pair_into_object( subConfig, "ShowHUDMap", json_new_bool(config->ShowHUDMap)); json_insert_pair_into_object(root, "Interface", subConfig); }
static void AddQuickPlayConfigNode(QuickPlayConfig *config, json_t *root) { json_t *subConfig = json_new_object(); json_insert_pair_into_object( subConfig, "MapSize", json_new_string(QuickPlayQuantityStr(config->MapSize))); json_insert_pair_into_object( subConfig, "WallCount", json_new_string(QuickPlayQuantityStr(config->WallCount))); json_insert_pair_into_object( subConfig, "WallLength", json_new_string(QuickPlayQuantityStr(config->WallLength))); json_insert_pair_into_object( subConfig, "RoomCount", json_new_string(QuickPlayQuantityStr(config->RoomCount))); json_insert_pair_into_object( subConfig, "SquareCount", json_new_string(QuickPlayQuantityStr(config->SquareCount))); json_insert_pair_into_object( subConfig, "EnemyCount", json_new_string(QuickPlayQuantityStr(config->EnemyCount))); json_insert_pair_into_object( subConfig, "EnemySpeed", json_new_string(QuickPlayQuantityStr(config->EnemySpeed))); json_insert_pair_into_object( subConfig, "EnemyHealth", json_new_string(QuickPlayQuantityStr(config->EnemyHealth))); json_insert_pair_into_object( subConfig, "EnemiesWithExplosives", json_new_bool(config->EnemiesWithExplosives)); json_insert_pair_into_object( subConfig, "ItemCount", json_new_string(QuickPlayQuantityStr(config->ItemCount))); json_insert_pair_into_object(root, "QuickPlay", subConfig); }
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); }
static void test_json_render(void *p) { struct JsonContext *ctx; struct JsonValue *list, *dict; ctx = json_new_context(NULL, 128); tt_assert(ctx); list = json_new_list(ctx); tt_assert(json_list_append(list, json_new_null(ctx))); tt_assert(json_list_append(list, json_new_bool(ctx, 1))); tt_assert(json_list_append(list, json_new_bool(ctx, 0))); tt_assert(json_list_append(list, json_new_int(ctx, 600))); tt_assert(json_list_append(list, json_new_float(ctx, -0.5))); tt_assert(json_list_append(list, json_new_string(ctx, "q\\we"))); str_check(render(list), "[null,true,false,600,-0.5,\"q\\\\we\"]"); dict = json_new_dict(ctx); tt_assert(dict); str_check(render(dict), "{}"); tt_assert(json_dict_put(dict, "k", json_new_list(ctx))); str_check(render(dict), "{\"k\":[]}"); tt_assert(json_dict_put_null(dict, "n")); tt_assert(json_dict_put_bool(dict, "b", 1)); tt_assert(json_dict_put_int(dict, "i", 22)); tt_assert(json_dict_put_float(dict, "f", 1)); tt_assert(json_dict_put_string(dict, "s", "qwe")); str_check(render(dict), "{\"b\":true,\"f\":1.0,\"i\":22,\"k\":[],\"n\":null,\"s\":\"qwe\"}"); str_check(render(json_new_string(ctx, "\"\\ low:\a\b\f\n\r\t")), "\"\\\"\\\\ low:\\u0007\\b\\f\\n\\r\\t\""); list = json_new_list(ctx); tt_assert(json_list_append_null(list)); tt_assert(json_list_append_bool(list, false)); tt_assert(json_list_append_int(list, -1)); tt_assert(json_list_append_float(list, -2)); tt_assert(json_list_append_string(list, "qz\0foo")); str_check(render(list), "[null,false,-1,-2.0,\"qz\"]"); json_free_context(ctx); end:; }
static void AddGraphicsConfigNode(GraphicsConfig *config, json_t *root) { json_t *subConfig = json_new_object(); AddIntPair(subConfig, "Brightness", config->Brightness); AddIntPair(subConfig, "ResolutionWidth", config->Res.x); AddIntPair(subConfig, "ResolutionHeight", config->Res.y); json_insert_pair_into_object( subConfig, "Fullscreen", json_new_bool(config->Fullscreen)); AddIntPair(subConfig, "ScaleFactor", config->ScaleFactor); AddIntPair(subConfig, "ShakeMultiplier", config->ShakeMultiplier); JSON_UTILS_ADD_ENUM_PAIR(subConfig, "ScaleMode", config->ScaleMode, ScaleModeStr); json_insert_pair_into_object(root, "Graphics", subConfig); }
static void ConfigSaveVisit(const Config *c, json_t *node) { switch (c->Type) { case CONFIG_TYPE_STRING: CASSERT(false, "not implemented"); break; case CONFIG_TYPE_INT: AddIntPair(node, c->Name, c->u.Int.Value); break; case CONFIG_TYPE_FLOAT: CASSERT(false, "not implemented"); break; case CONFIG_TYPE_BOOL: json_insert_pair_into_object( node, c->Name, json_new_bool(c->u.Bool.Value)); break; case CONFIG_TYPE_ENUM: JSON_UTILS_ADD_ENUM_PAIR( node, c->Name, c->u.Enum.Value, c->u.Enum.EnumToStr); break; case CONFIG_TYPE_GROUP: { json_t *child = node; // If the config has no name, then it is the root element // Add children directly to the node // Otherwise, create a new child if (c->Name != NULL) { child = json_new_object(); } CA_FOREACH(Config, cg, c->u.Group) ConfigSaveVisit(cg, child); CA_FOREACH_END() if (c->Name != NULL) { json_insert_pair_into_object(node, c->Name, child); } } break; default: CASSERT(false, "Unknown config type"); break; } }
void AddBoolPair(json_t *parent, const char *name, int value) { json_insert_pair_into_object(parent, name, json_new_bool(value)); }