@Override public void accept(JsonObject json, RequestMetaData req, ResponseMetaData res) { JsonObject data = json.getJsonObject("api_data"); if (data != null) { this.apiBasic(data.getJsonObject("api_basic")); this.apiSlotItem(data.getJsonArray("api_slot_item")); } }
/** * Create a new Twist based on the given JSON object. Any missing values will be set to their * defaults. * * @param jsonObject The JSON object to parse. * @return A Twist message based on the given JSON object. */ public static Twist fromJsonObject(JsonObject jsonObject) { // check the fields Vector3 linear = jsonObject.containsKey(Twist.FIELD_LINEAR) ? Vector3.fromJsonObject(jsonObject.getJsonObject(Twist.FIELD_LINEAR)) : new Vector3(); Vector3 angular = jsonObject.containsKey(Twist.FIELD_ANGULAR) ? Vector3.fromJsonObject(jsonObject.getJsonObject(Twist.FIELD_ANGULAR)) : new Vector3(); return new Twist(linear, angular); }
private void readResultJson(JsonObject object) { if (object.get("api_quest_name") != null) { this.questName = object.getString("api_quest_name"); } else { // 演習の場合はない this.questName = null; } this.rank = ResultRank.fromRank(object.getString("api_win_rank")); // 完全勝利Sは分からないので戦闘結果を見る Phase lastPhase = this.getLastPhase(); if ((lastPhase != null) && (lastPhase.getEstimatedRank() == ResultRank.PERFECT)) { this.rank = ResultRank.PERFECT; } this.enemyName = object.getJsonObject("api_enemy_info").getString("api_deck_name"); this.dropShip = object.containsKey("api_get_ship"); this.dropItem = object.containsKey("api_get_useitem"); if (this.dropShip || this.dropItem) { if (this.dropShip) { JsonObject getShip = object.getJsonObject("api_get_ship"); this.dropShipId = getShip.getInt("api_ship_id"); this.dropType = getShip.getString("api_ship_type"); this.dropName = getShip.getString("api_ship_name"); } else { String name = UseItem.get(object.getJsonObject("api_get_useitem").getInt("api_useitem_id")); this.dropType = "アイテム"; this.dropName = StringUtils.defaultString(name); } } else { this.dropType = ""; this.dropName = ""; } this.mvp = object.getInt("api_mvp"); if (JsonUtils.hasKey(object, "api_mvp_combined")) { this.mvpCombined = object.getInt("api_mvp_combined"); } this.hqLv = object.getInt("api_member_lv"); if (JsonUtils.hasKey(object, "api_escape")) { JsonObject jsonEscape = object.getJsonObject("api_escape"); this.escapeInfo = new int[] { jsonEscape.getJsonArray("api_escape_idx").getInt(0) - 1, jsonEscape.getJsonArray("api_tow_idx").getInt(0) - 1 }; } if (JsonUtils.hasKey(object, "api_lost_flag")) { this.lostflag = new boolean[6]; JsonArray jsonLostflag = object.getJsonArray("api_lost_flag"); for (int i = 1; i < jsonLostflag.size(); i++) { this.lostflag[i - 1] = (jsonLostflag.getInt(i) != 0); } } }
public static void main(String[] args) throws Exception { if (args.length < 1) { System.out.println("USAGE : java -jar GoEuroTest.jar <CITY_NAME>"); System.exit(1); } String urlStr = "http://api.goeuro.com/api/v2/position/suggest/en/"; urlStr = urlStr + URLEncoder.encode(args[0], "UTF-8"); URL url = new URL(urlStr); String lines = ""; String fileHeader = "_id,name,type,latitude,longitude\r\n"; // out csv file header lines += fileHeader; try { InputStream inputStream = url.openStream(); JsonReader jsonReader = Json.createReader(inputStream); JsonArray results = jsonReader.readArray(); if (results.isEmpty()) { System.out.println("INFO: City '" + args[0] + "' returned 0 results."); return; } for (JsonObject result : results.getValuesAs(JsonObject.class)) { lines += result.getJsonNumber("_id") + ","; lines += result.getString("name") + ","; lines += result.getString("type") + ","; lines += result.getJsonObject("geo_position").getJsonNumber("latitude") + ","; lines += result.getJsonObject("geo_position").getJsonNumber("longitude") + "\r\n"; } Files.write(Paths.get("./out.csv"), lines.getBytes()); System.out.println("INFO: Wrote results to 'out.csv'."); } catch (Throwable e) { System.out.println("ERROR: Could not write CSV File 'out.csv'. Error: " + e); } }
@Test public void testEjbSessionBeans() throws IOException { JsonObject allBeans = getPageAsJSONObject(BEANS_PATH_ALL, url); JsonArray beansData = allBeans.getJsonArray(DATA); List<JsonObject> statefulEjbSessionList = getAllJsonObjectsByClass(StatefulEjbSession.class, beansData); assertEquals(statefulEjbSessionList.size(), 1); JsonObject statefulEjbJson = statefulEjbSessionList.get(0); String statefulEjbSessionId = statefulEjbJson.getString(ID); JsonObject sessionBeansDetail = getPageAsJSONObject(BEANS_PATH + "/" + statefulEjbSessionId, url); assertEquals(BeanType.SESSION.name(), sessionBeansDetail.getString(KIND)); assertTrue( checkStringInArrayRecursively( DecoratedInterface.class.getName(), TYPES, sessionBeansDetail.getJsonArray(TYPES), false)); assertEquals(Boolean.TRUE.booleanValue(), sessionBeansDetail.getBoolean(IS_ALTERNATIVE)); assertEquals(Boolean.TRUE.booleanValue(), sessionBeansDetail.getBoolean(EJB_NAME)); assertEquals(SessionBeanType.STATEFUL.name(), sessionBeansDetail.getString(SESSION_BEAN_TYPE)); JsonObject sessionBeanEnablement = sessionBeansDetail.getJsonObject(ENABLEMENT); // TODO introduce enum with priority ranges assertEquals("APPLICATION", sessionBeanEnablement.getString(PRIORITY_RANGE)); assertEquals(2500, sessionBeanEnablement.getInt(PRIORITY)); }
@Test public void shouldSaveCommands() { // Given String msg = "{ \n" + " \"messageBroadcast\":{ \n" + " \"cameraInfos\":{ \n" + " \"target\":{ \n" + " \"x\":0,\n" + " \"y\":0,\n" + " \"z\":0\n" + " },\n" + " \"camPos\":{ \n" + " \"x\":2283.8555345202267,\n" + " \"y\":1742.2368392950543,\n" + " \"z\":306.5925754554133\n" + " },\n" + " \"camOrientation\":{ \n" + " \"x\":-0.16153026619659236,\n" + " \"y\":0.9837903505522302,\n" + " \"z\":0.07787502335635015\n" + " },\n" + " \"layers\":\"create layer\",\n" + " \"colourEditedObjects\":true,\n" + " \"clipping\":\"1\",\n" + " \"explode\":\"3\",\n" + " \"smartPath\":[ \n" + " \"9447-9445-9441\",\n" + " \"9447-9445-9443\",\n" + " \"9447-9445-9444\",\n" + " \"9446\"\n" + " ]\n" + " }\n" + " }\n" + "}"; JsonObject jsObj = Json.createReader(new StringReader(msg)).readObject(); JsonObject messageBroadcast = jsObj.containsKey("messageBroadcast") ? jsObj.getJsonObject("messageBroadcast") : null; CollaborativeMessage collaborativeMessage = Mockito.spy( new CollaborativeMessage( ChannelMessagesType.COLLABORATIVE_COMMANDS, "key-12545695-7859-458", messageBroadcast, "slave1")); CollaborativeRoom room = Mockito.spy(new CollaborativeRoom(master)); // When room.addSlave(slave1); room.addSlave(slave2); JsonObject commands = collaborativeMessage.getMessageBroadcast(); room.saveCommand(commands); Assert.assertTrue(room.getCommands().entrySet().size() == 1); }
@Test public void should_create_attempt() { final String uri = String.format(URI, "rec", "123"); Response response = invoke(uri).post(Entity.entity(new Form(), "application/vnd.resource+json; charset=utf-8")); assertEquals(Status.CREATED.getStatusCode(), response.getStatus()); JsonObject token1 = response.readEntity(JsonObject.class); String type = token1.getJsonObject("item").getString("type"); String value = token1.getJsonObject("item").getString("key"); Integer attempts = token1.getJsonObject("item").getInt("attempts"); assertEquals("rec", type); assertEquals("123", value); assertEquals(1, attempts.intValue()); }
@Test public void shouldReturnValidMetadataJson() throws JMSException { when(message.getText()).thenReturn(envelopeString()); JsonObject result = toJsonObject(message); assertThat(result.getString("id"), is(A_MESSAGE_ID)); assertThat(result.getString("name"), is(A_NAME)); assertThat(result.getJsonObject("context").getString("user"), is(A_USER_ID)); assertThat(result.getJsonObject("context").getString("session"), is(A_SESSION_ID)); }
@Override public JsonObject changeFilterState(JsonObject newFilterState) { if (newFilterState != null) { // new value of amount JsonObject amountJson = newFilterState.getJsonObject("amount"); setAmount((float) amountJson.getJsonNumber("value").doubleValue()); JsonObject sizeJson = newFilterState.getJsonObject("radius"); setRadius(sizeJson.getJsonNumber("value").intValue()); JsonObject thresholdJson = newFilterState.getJsonObject("threshold"); setThreshold(thresholdJson.getJsonNumber("value").intValue()); Main.debug(id.toString() + " \n" + toString()); return newFilterState; } return null; }
@Test public void should_return_notFound_if_attempt_doesnt_exist() { final String uri = String.format(URI, "type", "key"); Response response = invoke(uri).get(); assertEquals(Status.NOT_FOUND.getStatusCode(), response.getStatus()); JsonObject token1 = response.readEntity(JsonObject.class); JsonArray jsonArray = token1.getJsonObject("errors").getJsonArray("messages"); for (int i = 0; jsonArray.size() > i; i++) { JsonObject item = jsonArray.getJsonObject(i); assertEquals( NotFoundEnum.ATTEMPT.getCode().longValue(), item.getJsonNumber("code").longValue()); assertNull(item.getJsonString("field")); assertEquals("attempt_not_found", item.getJsonString("message").getString()); } }
private synchronized void newPacketReceived() { byte[] bytes = new byte[1024]; bytes = Arrays.copyOfRange(bytes, 0, packet.getLength()); PieLogger.info(this.getClass(), String.format("Input Message: %s", new String(bytes))); JsonObject input = processInput(bytes); if (input.getString("type").equals("connection")) { JsonObject newClient = input.getJsonObject("client"); callback.Handle(newClient); } if (input.getString("type").equals("msg")) { System.out.println("Message Arrived: " + input.getString("msg")); } if (input.getString("type").equals("punch")) { sendTask.send(ackMsg.getBytes(), packet.getAddress().getHostAddress(), packet.getPort()); } if (input.getString("type").equals("ACK")) { sendTask.setACK(true); } }
public static <T extends Factory> T objectFromJson(JsonObject json, T prototype) throws ParseException { T object = (T) prototype.create(); Method[] methods = object.getClass().getMethods(); for (final Method method : methods) { if (method.getName().startsWith("set") && method.getParameterTypes().length == 1) { final String name = Introspector.decapitalize(method.getName().substring(3)); Class<?> parameterType = method.getParameterTypes()[0]; if (json.containsKey(name)) try { if (parameterType.equals(boolean.class)) { method.invoke(object, json.getBoolean(name)); } else if (parameterType.equals(int.class)) { method.invoke(object, json.getJsonNumber(name).intValue()); } else if (parameterType.equals(long.class)) { if (json.get(name).getValueType() == JsonValue.ValueType.NUMBER) { method.invoke(object, json.getJsonNumber(name).longValue()); } } else if (parameterType.equals(double.class)) { method.invoke(object, json.getJsonNumber(name).doubleValue()); } else if (parameterType.equals(String.class)) { method.invoke(object, json.getString(name)); } else if (parameterType.equals(Date.class)) { method.invoke(object, dateFormat.parse(json.getString(name))); } else if (parameterType.equals(Map.class)) { method.invoke(object, MiscFormatter.fromJson(json.getJsonObject(name))); } } catch (IllegalAccessException | InvocationTargetException error) { } } } return object; }
ObservableList<Question> getObservableList() throws IOException { String url = "http://api.stackexchange.com/2.2/search?tagged=javafx&site=stackoverflow"; URL host = new URL(url); JsonReader jr = Json.createReader(new GZIPInputStream(host.openConnection().getInputStream())); JsonObject jsonObject = jr.readObject(); JsonArray jsonArray = jsonObject.getJsonArray("items"); ObservableList<Question> answer = FXCollections.observableArrayList(); jsonArray .iterator() .forEachRemaining( (JsonValue e) -> { JsonObject obj = (JsonObject) e; JsonString name = obj.getJsonObject("owner").getJsonString("display_name"); JsonString quest = obj.getJsonString("title"); JsonNumber jsonNumber = obj.getJsonNumber("creation_date"); Question q = new Question(name.getString(), quest.getString(), jsonNumber.longValue() * 1000); answer.add(q); }); return answer; }
@Override public Content create(final JsonObject json) throws IOException { this.storage.lock(); // @checkstyle MultipleStringLiterals (40 lines) final String path = json.getString("path"); try { this.storage.apply( new Directives() .xpath(this.xpath()) .add("content") .add("name") .set(path) .up() .add("path") .set(path) .up() .add("content") .set(json.getString("content")) .up() .add("type") .set("file") .up() .add("encoding") .set("base64") .up() .add("sha") .set(fakeSha()) .up() .add("url") .set("http://localhost/1") .up() .add("git_url") .set("http://localhost/2") .up() .add("html_url") .set("http://localhost/3") .up()); final Directives commit = new Directives() .xpath(this.commitXpath()) .add("commit") .add("sha") .set(fakeSha()) .up() .add("url") .set("http://localhost/4") .up() .add("html_url") .set("http://localhost/5") .up() .add("message") .set(json.getString("message")) .up(); if (json.containsKey("committer")) { final JsonObject committer = json.getJsonObject("committer"); commit .add("committer") .add("email") .set(committer.getString("email")) .up() .add("name") .set(committer.getString("name")) .up(); } if (json.containsKey("author")) { final JsonObject author = json.getJsonObject("author"); commit .add("author") .add("email") .set(author.getString("email")) .up() .add("name") .set(author.getString("name")) .up(); } this.storage.apply(commit); } finally { this.storage.unlock(); } return new MkContent(this.storage, this.self, this.coords, path); }
public Phase( BattleExDto battle, JsonObject object, BattlePhaseKind kind, int[] beforeFriendHp, int[] beforeFriendHpCombined, int[] beforeEnemyHp) { boolean isCombined = (beforeFriendHpCombined != null); this.kind = kind; this.isNight = kind.isNight(); this.nowFriendHp = beforeFriendHp.clone(); this.nowEnemyHp = beforeEnemyHp.clone(); if (isCombined) { this.nowFriendHpCombined = beforeFriendHpCombined.clone(); } else { this.nowFriendHpCombined = null; } // 夜間触接 JsonValue jsonTouchPlane = object.get("api_touch_plane"); if ((jsonTouchPlane != null) && (jsonTouchPlane != JsonValue.NULL)) { this.touchPlane = JsonUtils.getIntArray(object, "api_touch_plane"); } // 照明弾発射艦 JsonValue jsonFlarePos = object.get("api_flare_pos"); if ((jsonFlarePos != null) && (jsonFlarePos != JsonValue.NULL)) { this.flarePos = JsonUtils.getIntArray(object, "api_flare_pos"); } // 攻撃シーケンスを読み取る // // 航空戦(通常) JsonObject kouku = object.getJsonObject("api_kouku"); if (kouku != null) { this.air = new AirBattleDto(kouku, isCombined); // 昼戦の触接はここ this.touchPlane = this.air.touchPlane; // 制空はここから取る this.seiku = this.air.seiku; } // 支援艦隊 JsonNumber support_flag = object.getJsonNumber("api_support_flag"); if ((support_flag != null) && (support_flag.intValue() != 0)) { JsonObject support = object.getJsonObject("api_support_info"); JsonValue support_hourai = support.get("api_support_hourai"); JsonValue support_air = support.get("api_support_airatack"); if ((support_hourai != null) && (support_hourai != JsonValue.NULL)) { JsonArray edam = ((JsonObject) support_hourai).getJsonArray("api_damage"); if (edam != null) { this.support = BattleAtackDto.makeSupport(edam); } } else if ((support_air != null) && (support_air != JsonValue.NULL)) { JsonValue stage3 = ((JsonObject) support_air).get("api_stage3"); if ((stage3 != null) && (stage3 != JsonValue.NULL)) { this.support = BattleAtackDto.makeSupport(((JsonObject) stage3).getJsonArray("api_edam")); } } this.supportType = toSupport(support_flag.intValue()); } else { this.supportType = ""; } // 航空戦(連合艦隊のみ?) JsonObject kouku2 = object.getJsonObject("api_kouku2"); if (kouku2 != null) this.air2 = new AirBattleDto(kouku2, isCombined); // 開幕 this.opening = BattleAtackDto.makeRaigeki(object.get("api_opening_atack"), kind.isOpeningSecond()); // 砲撃 this.hougeki = BattleAtackDto.makeHougeki(object.get("api_hougeki"), kind.isHougekiSecond()); // 夜戦 this.hougeki1 = BattleAtackDto.makeHougeki(object.get("api_hougeki1"), kind.isHougeki1Second()); this.hougeki2 = BattleAtackDto.makeHougeki(object.get("api_hougeki2"), kind.isHougeki2Second()); this.hougeki3 = BattleAtackDto.makeHougeki(object.get("api_hougeki3"), kind.isHougeki3Second()); // 雷撃 this.raigeki = BattleAtackDto.makeRaigeki(object.get("api_raigeki"), kind.isRaigekiSecond()); // ダメージを反映 // if (this.air != null) this.doAtack(this.air.atacks); this.doAtack(this.support); if (this.air2 != null) this.doAtack(this.air2.atacks); this.doAtack(this.opening); this.doAtack(this.hougeki); this.doAtack(this.hougeki1); this.doAtack(this.raigeki); this.doAtack(this.hougeki2); this.doAtack(this.hougeki3); this.json = object.toString(); }