// ----------------------------------------------- // load JSON // ----------------------------------------------- bool DataFile::load() { bool BINARY = false; bool ret = false; char buffer[64]; // check length StopWatch s; s.start(); sprintf_s(buffer, 64, "content\\%s", getFileName()); // FIXME: assert that file name contains . LOG << "Reading simplified json file: " << buffer; JSONReader reader; if (BINARY) { sprintf_s(buffer, 64, "assets\\%u", string::murmur_hash(getFileName())); if (reader.load_binary(buffer)) { ret = loadData(reader); } } else { if (reader.parse(buffer)) { ret = loadData(reader); } if (ret) { sprintf_s(buffer, 64, "assets\\%u", string::murmur_hash(getFileName())); LOG << "saving binary file: " << buffer; reader.save_binary(buffer); } } if (!ret) { LOG << "Error: Cannot parse file: " << buffer; ret = false; } s.end(); LOG << "----> elapsed: " << s.elapsed(); return ret; }
TEST(JSON, read_err) { std::stringstream ss; JSONReader reader; ASSERT_ANY_THROW(reader.parse("[,]")); ASSERT_ANY_THROW(reader.parse("[],")); ASSERT_ANY_THROW(reader.parse("{a:1}")); }
bool SpriteSheet::loadData(const JSONReader& loader) { int categories[256]; int num = loader.get_categories(categories, 256); for (int i = 0; i < num; ++i) { Rect r; loader.get(categories[i], "rect", &r); const char* sn = loader.get_string(categories[i], "name"); add(sn, r); } return true; }
void ChromiumProfileImporter::ReadProfiles(vector<ChromeProfileInfo> *cp, FilePath profileDirectory) { FilePath profileFileName = profileDirectory.AppendASCII("Local State"); if (!PathExists(profileFileName)) return; string input; ReadFileToString(profileFileName, &input); JSONReader reader; scoped_ptr<Value> root(reader.ReadToValue(input)); DictionaryValue* dict = NULL; if (!root->GetAsDictionary(&dict)) { return; } const Value* roots; if (!dict->Get("profile", &roots)) { return; } const DictionaryValue* roots_d_value = static_cast<const DictionaryValue*>(roots); const Value* vInfoCache; const DictionaryValue* vDictValue; if (roots_d_value->Get("info_cache", &vInfoCache)) { vInfoCache->GetAsDictionary(&vDictValue); for (DictionaryValue::Iterator it(*vDictValue); !it.IsAtEnd(); it.Advance()){ const Value* child_copy = &it.value(); string profileName = it.key(); const DictionaryValue* roots_sd_value = static_cast<const DictionaryValue*>(&it.value()); ChromeProfileInfo prof; const Value* namVal; if (roots_sd_value->Get("name", &namVal)) { string16 displayName; namVal->GetAsString(&displayName); prof.profileDisplayName = displayName; } else { prof.profileDisplayName = base::UTF8ToUTF16(profileName); } prof.profileName = profileName; cp->push_back(prof); } } }
TEST(JSON, read) { JSONReader reader; JSONValue val = reader.parse("{\"a\": 1337, \"b\": false, \"c\":null, \"d\":[1,2,3], \"e\":{\"abc\":\"xyz\"} }"); JSONObject* obj = val.asObject(); ASSERT_EQ(1337, obj->getInt("a")); ASSERT_EQ(false, obj->getBoolean("b")); ASSERT_TRUE(obj->containsValue("c")); ASSERT_EQ(1, obj->getArray("d")->get(0).asInt()); ASSERT_EQ(2, obj->getArray("d")->get(1).asInt()); ASSERT_EQ(3, obj->getArray("d")->get(2).asInt()); ASSERT_EQ("xyz", obj->getObject("e")->getString("abc")); }
bool SpriteSheet::reloadData(const JSONReader& loader) { int categories[256]; int num = loader.get_categories(categories, 256); for (int i = 0; i < num; ++i) { Rect r; loader.get(categories[i], "rect", &r); const char* sn = loader.get_string(categories[i], "name"); int idx = findIndex(sn); if (idx == -1) { add(sn, r); } else { SheetEntry& se = _entries[idx]; se.texture = Texture(r); } } return true; }
// static Value* JSONReader::ReadAndReturnError(const std::string& json, bool allow_trailing_comma, int* error_code_out, std::string* error_msg_out) { JSONReader reader = JSONReader(); Value* root = reader.JsonToValue(json, true, allow_trailing_comma); if(root) { return root; } if(error_code_out) { *error_code_out = reader.error_code(); } if(error_msg_out) { *error_msg_out = reader.GetErrorMessage(); } return NULL; }
RID MeshBufferParser::parse(JSONReader& reader, int childIndex) { MeshBufferDescriptor descriptor; reader.get(childIndex, "size", &descriptor.size); const char* indexBufferName = reader.get_string(childIndex, "index_buffer"); descriptor.indexBuffer = find(indexBufferName, ResourceType::INDEXBUFFER); const char* constantBufferName = reader.get_string(childIndex, "constant_buffer"); descriptor.constantBuffer = find(constantBufferName, ResourceType::CONSTANTBUFFER); const char* vertexBufferName = reader.get_string(childIndex, "vertex_buffer"); descriptor.vertexBuffer = find(vertexBufferName, ResourceType::VERTEXBUFFER); const char* materialName = reader.get_string(childIndex, "material"); descriptor.material = find(materialName, ResourceType::MATERIAL); const char* name = reader.get_string(childIndex, "name"); return createMeshBuffer(name, descriptor); }
RID BlendStateParser::parse(JSONReader& reader, int childIndex) { BlendStateDescriptor descriptor; const char* entry = reader.get_string(childIndex, "src_blend"); descriptor.srcBlend = findBlendStateMapping(entry); entry = reader.get_string(childIndex, "dest_blend"); descriptor.destBlend = findBlendStateMapping(entry); entry = reader.get_string(childIndex, "src_blend_alpha"); descriptor.srcAlphaBlend = findBlendStateMapping(entry); entry = reader.get_string(childIndex, "dest_blend_alpha"); descriptor.destAlphaBlend = findBlendStateMapping(entry); reader.get(childIndex, "alpha_enabled", &descriptor.alphaEnabled); const char* name = reader.get_string(childIndex, "name"); return createBlendState(name, descriptor); }
bool SerializerJSON::fromString(Variant & obj, const std::string & string) { JSONReader reader; return reader.parse(string, obj); }
RID IndexBufferParser::parse(JSONReader& reader, int childIndex) { IndexBufferDescriptor descriptor; reader.get(childIndex, "size", &descriptor.size); const char* name = reader.get_string(childIndex, "name"); return createIndexBuffer(name, descriptor); }
TEST(JSON, read_ok) { std::stringstream ss; JSONReader reader; JSONWriter writer(ss, false); JSONValue e0 = reader.parse("[]"); ss.str(""); writer.write(e0); ASSERT_EQ("[]", ss.str()); JSONValue e1 = reader.parse("{}"); ss.str(""); writer.write(e1); ASSERT_EQ("{}", ss.str()); JSONValue e2 = reader.parse("[ {}, {}, {},{}]"); ss.str(""); writer.write(e2); ASSERT_EQ("[{},{},{},{}]", ss.str()); JSONValue e3 = reader.parse("[ { }, [ {},{},{} ] ]"); ss.str(""); writer.write(e3); ASSERT_EQ("[{},[{},{},{}]]", ss.str()); JSONValue e4 = reader.parse("{\"a\":{}}"); ss.str(""); writer.write(e4); ASSERT_EQ("{\"a\":{}}", ss.str()); JSONValue e5 = reader.parse("{ \"a\" : [] }"); ss.str(""); writer.write(e5); ASSERT_EQ("{\"a\":[]}", ss.str()); JSONValue e6 = reader.parse("{ \"a\" : null }"); ss.str(""); writer.write(e6); ASSERT_EQ("{\"a\":null}", ss.str()); JSONValue e7 = reader.parse(" { } "); ss.str(""); writer.write(e7); ASSERT_EQ("{}", ss.str()); JSONValue e8 = reader.parse("[ null , true, false ,1, 1.1] "); ss.str(""); writer.write(e8); ASSERT_EQ("[null,true,false,1,1.1]", ss.str()); JSONValue e10 = reader.parse("[ {\"xyz\":133.7, \"abc\":\"11\\\"1\"}, [ { }, [ {},{},{} ] ] , [{\"a\":true}], [{\"b\":true}, {\"b\":false}] ]"); ss.str(""); writer.write(e10); ASSERT_EQ("[{\"abc\":\"11\\\"1\",\"xyz\":133.7},[{},[{},{},{}]],[{\"a\":true}],[{\"b\":true},{\"b\":false}]]", ss.str()); }
RID ParticleManagerParser::parse(JSONReader& reader, int childIndex) { ParticleSystemsDescriptor descriptor; reader.get(childIndex, "sprite_buffer", &descriptor.spriteBuffer); const char* name = reader.get_string(childIndex, "name"); return createParticleManager(descriptor); }
void MainMenuController::preload() { // Tutorial button GameController* gc = GameController::create(TUTORIAL_KEY, TUTORIAL_FILE); gc->preload(); _tutButt = MainMenuButton::create(gc); _tutButt->index = -1; _tutButt->loadTextures("textures/Tutorial/tutorial.png", "textures/Tutorial/tutorial.png"); _tutButt->setTouchEnabled(true); //Add event listener _tutButt->addTouchEventListener([&](Ref* sender, ui::Widget::TouchEventType type) { switch (type) { case ui::Widget::TouchEventType::ENDED: _tutButt = ((MainMenuButton*)sender); _currController = _tutButt->index; _activeController = _tutButt->getController(); _rootnode->removeAllChildren(); _activeController->initialize(_rootnode); } }); _tutButt->retain(); loadGameController(LEVEL_ONE_KEY, LEVEL_ONE_FILE); loadGameController(LEVEL_TWO_KEY, LEVEL_TWO_FILE); loadGameController(LEVEL_THREE_KEY, LEVEL_THREE_FILE); loadGameController(LEVEL_FOUR_KEY, LEVEL_FOUR_FILE); loadGameController(LEVEL_FIVE_KEY, LEVEL_FIVE_FILE); loadGameController(LEVEL_SIX_KEY, LEVEL_SIX_FILE); loadGameController(LEVEL_SEVEN_KEY, LEVEL_SEVEN_FILE); loadGameController(LEVEL_EIGHT_KEY, LEVEL_EIGHT_FILE); loadGameController(LEVEL_NINE_KEY, LEVEL_NINE_FILE); // Load the textures (Autorelease objects) _assets = AssetManager::getInstance()->getCurrent(); TextureLoader* tloader = (TextureLoader*)_assets->access<Texture2D>(); tloader->loadAsync(EXPOSURE_BAR, "textures/exposure_bar.png"); tloader->loadAsync(EXPOSURE_FRAME, "textures/exposure_bar_frame.png"); tloader->loadAsync(DUDE_TEXTURE, "textures/player_animation.png"); tloader->loadAsync("dudepool", "textures/Level Pool/Shade_Swim_Animation.png"); tloader->loadAsync(PEDESTRIAN_TEXTURE, "textures/Pedestrian.png"); tloader->loadAsync(PEDESTRIAN_SHADOW_TEXTURE, "textures/Pedestrian_S.png"); tloader->loadAsync(PEDESTRIAN_POOL_TEXTURE, "textures/Level Pool/Level2_RotationP_Animation_New.png"); tloader->loadAsync(PEDESTRIAN_POOL_SHADOW_TEXTURE, "textures/Level2_RotationP_Animation_S.png"); tloader->loadAsync(INDICATOR, "textures/indicator.png"); tloader->loadAsync(CAR_TEXTURE, "textures/car_animation.png"); tloader->loadAsync(CAR_SHADOW_TEXTURE, "textures/Car1_S.png"); tloader->loadAsync(GOAL_TEXTURE, "textures/caster_animation.png"); //tloader->loadAsync("goalpool", "textures/caster_animation.png"); tloader->loadAsync(WIN_IMAGE, "textures/menu/Win Icon.png"); tloader->loadAsync(LOSE_IMAGE, "textures/menu/lose_icon.png"); tloader->loadAsync(WIN_TEXTURE, "textures/Shade_Win.png"); tloader->loadAsync(LOSE_TEXTURE, "textures/Shade_Sun.png"); _assets->loadAsync<Sound>(GAME_MUSIC, "sounds/DD_Main.mp3"); _assets->loadAsync<Sound>(WIN_MUSIC, "sounds/win.mp3"); _assets->loadAsync<Sound>(LOSE_MUSIC, "sounds/lose.mp3"); _assets->loadAsync<Sound>(LATCH_SOUND, "sounds/latch.mp3"); _assets->loadAsync<Sound>(SIGHTED_SOUND, "sounds/sighted.mp3"); _assets->loadAsync<Sound>(RUN_SOUND, "sounds/run.mp3"); JSONReader reader; reader.initWithFile(STATIC_OBJECTS); if (!reader.startJSON()) { CCASSERT(false, "Failed to load static objects"); return; } int count = reader.startArray("types"); for (int index = 0; index < count; index++) { reader.startObject(); string name = reader.getString("name"); string imageFormat = reader.getString("imageFormat"); string shadowImageFormat = reader.getString("shadowImageFormat"); if (shadowImageFormat == "") { shadowImageFormat = imageFormat; } tloader->loadAsync(name + OBJECT_TAG, "textures/static_objects/" + name + "." + imageFormat); tloader->loadAsync(name + SHADOW_TAG, "textures/static_objects/" + name + "_S." + shadowImageFormat); reader.endObject(); reader.advance(); } reader.endArray(); reader.endJSON(); // Background tloader->loadAsync(MENU_BACKGROUND_KEY, "textures/menu/Level Background-01.png"); }