List of usage examples for org.json JSONObject getLong
public long getLong(String key) throws JSONException
From source file:com.daskiworks.ghwatch.backend.NotificationStreamParser.java
public static NotificationStream parseNotificationStream(JSONArray json) throws InvalidObjectException { NotificationStream ret = new NotificationStream(); try {//from w w w . j a v a 2 s . c o m for (int i = 0; i < json.length(); i++) { JSONObject notification = json.getJSONObject(i); JSONObject subject = notification.getJSONObject("subject"); JSONObject repository = notification.getJSONObject("repository"); String updatedAtStr = Utils.trimToNull(notification.getString("updated_at")); Date updatedAt = null; try { if (updatedAtStr != null) { if (updatedAtStr.endsWith("Z")) updatedAtStr = updatedAtStr.replace("Z", "GMT"); updatedAt = df.parse(updatedAtStr); } } catch (ParseException e) { Log.w(TAG, "Invalid date format for value: " + updatedAtStr); } ret.addNotification(new Notification(notification.getLong("id"), notification.getString("url"), subject.getString("title"), subject.getString("type"), subject.getString("url"), subject.getString("latest_comment_url"), repository.getString("full_name"), repository.getJSONObject("owner").getString("avatar_url"), updatedAt, notification.getString("reason"))); } } catch (Exception e) { throw new InvalidObjectException("JSON message is invalid: " + e.getMessage()); } return ret; }
From source file:com.appdynamics.monitors.nginx.statsExtractor.ServerZoneStatsExtractor.java
private Map<String, String> getServerZonesStats(JSONObject serverZones) { Map<String, String> serverZonesStats = new HashMap<String, String>(); Set<String> serverZoneNames = serverZones.keySet(); for (String serverZoneName : serverZoneNames) { JSONObject serverZone = serverZones.getJSONObject(serverZoneName); long processing = serverZone.getLong("processing"); serverZonesStats.put("server_zones|" + serverZoneName + "|processing", String.valueOf(processing)); long requests = serverZone.getLong("requests"); serverZonesStats.put("server_zones|" + serverZoneName + "|requests", String.valueOf(requests)); JSONObject responses = serverZone.getJSONObject("responses"); long resp1xx = responses.getLong("1xx"); serverZonesStats.put("server_zones|" + serverZoneName + "|responses|1xx", String.valueOf(resp1xx)); long resp2xx = responses.getLong("2xx"); serverZonesStats.put("server_zones|" + serverZoneName + "|responses|2xx", String.valueOf(resp2xx)); long resp3xx = responses.getLong("3xx"); serverZonesStats.put("server_zones|" + serverZoneName + "|responses|3xx", String.valueOf(resp3xx)); long resp4xx = responses.getLong("4xx"); serverZonesStats.put("server_zones|" + serverZoneName + "|responses|4xx", String.valueOf(resp4xx)); long resp5xx = responses.getLong("5xx"); serverZonesStats.put("server_zones|" + serverZoneName + "|responses|5xx", String.valueOf(resp5xx)); long respTotal = responses.getLong("total"); serverZonesStats.put("server_zones|" + serverZoneName + "|responses|total", String.valueOf(respTotal)); long received = serverZone.getLong("received"); serverZonesStats.put("server_zones|" + serverZoneName + "|received", String.valueOf(received)); long sent = serverZone.getLong("sent"); serverZonesStats.put("server_zones|" + serverZoneName + "|sent", String.valueOf(sent)); }//w w w . j av a 2 s.c o m return serverZonesStats; }
From source file:net.dv8tion.jda.core.handle.GuildMemberRemoveHandler.java
@Override protected Long handleInternally(JSONObject content) { final long id = content.getLong("guild_id"); if (api.getGuildLock().isLocked(id)) return id; GuildImpl guild = (GuildImpl) api.getGuildMap().get(id); if (guild == null) { //We probably just left the guild and this event is trying to remove us from the guild, therefore ignore return null; }/*from w w w .ja va 2 s .c o m*/ final long userId = content.getJSONObject("user").getLong("id"); MemberImpl member = (MemberImpl) guild.getMembersMap().remove(userId); if (member == null) { WebSocketClient.LOG .debug("Received GUILD_MEMBER_REMOVE for a Member that does not exist in the specified Guild."); return null; } if (member.getVoiceState().inVoiceChannel())//If this user was in a VoiceChannel, fire VoiceLeaveEvent. { GuildVoiceStateImpl vState = (GuildVoiceStateImpl) member.getVoiceState(); VoiceChannel channel = vState.getChannel(); vState.setConnectedChannel(null); ((VoiceChannelImpl) channel).getConnectedMembersMap().remove(member.getUser().getIdLong()); api.getEventManager().handle(new GuildVoiceLeaveEvent(api, responseNumber, member, channel)); } //The user is not in a different guild that we share // The user also is not a friend of this account in the case that the logged in account is a client account. if (userId != api.getSelfUser().getIdLong() // don't remove selfUser from cache && api.getGuildMap().valueCollection().stream() .noneMatch(g -> ((GuildImpl) g).getMembersMap().containsKey(userId)) && !(api.getAccountType() == AccountType.CLIENT && api.asClient().getFriendById(userId) != null)) { UserImpl user = (UserImpl) api.getUserMap().remove(userId); if (user.hasPrivateChannel()) { PrivateChannelImpl priv = (PrivateChannelImpl) user.getPrivateChannel(); user.setFake(true); priv.setFake(true); api.getFakeUserMap().put(user.getIdLong(), user); api.getFakePrivateChannelMap().put(priv.getIdLong(), priv); } else if (api.getAccountType() == AccountType.CLIENT) { //While the user might not have a private channel, if this is a client account then the user // could be in a Group, and if so we need to change the User object to be fake and // place it in the FakeUserMap for (Group grp : api.asClient().getGroups()) { if (grp.getNonFriendUsers().contains(user)) { user.setFake(true); api.getFakeUserMap().put(user.getIdLong(), user); break; //Breaks from groups loop } } } } api.getEventManager().handle(new GuildMemberLeaveEvent(api, responseNumber, guild, member)); return null; }
From source file:net.dv8tion.jda.core.handle.MessageUpdateHandler.java
private Long handleDefaultMessage(JSONObject content) { Message message;/*from w w w . j a v a 2 s. co m*/ try { message = api.getEntityBuilder().createMessage(content); } catch (IllegalArgumentException e) { switch (e.getMessage()) { case EntityBuilder.MISSING_CHANNEL: { final long channelId = content.getLong("channel_id"); api.getEventCache().cache(EventCache.Type.CHANNEL, channelId, () -> handle(responseNumber, allContent)); EventCache.LOG .debug("Received a message update for a channel that JDA does not currently have cached"); return null; } case EntityBuilder.MISSING_USER: { final long authorId = content.getJSONObject("author").getLong("id"); api.getEventCache().cache(EventCache.Type.USER, authorId, () -> handle(responseNumber, allContent)); EventCache.LOG .debug("Received a message update for a user that JDA does not currently have cached"); return null; } default: throw e; } } switch (message.getChannelType()) { case TEXT: { TextChannel channel = message.getTextChannel(); if (api.getGuildLock().isLocked(channel.getGuild().getIdLong())) { return channel.getGuild().getIdLong(); } api.getEventManager().handle(new GuildMessageUpdateEvent(api, responseNumber, message)); break; } case PRIVATE: { api.getEventManager().handle(new PrivateMessageUpdateEvent(api, responseNumber, message)); } case GROUP: { api.getEventManager().handle(new GroupMessageUpdateEvent(api, responseNumber, message)); break; } default: WebSocketClient.LOG .warn("Received a MESSAGE_UPDATE with a unknown MessageChannel ChannelType. JSON: " + content); return null; } //Combo event api.getEventManager().handle(new MessageUpdateEvent(api, responseNumber, message)); return null; }
From source file:net.dv8tion.jda.core.handle.MessageUpdateHandler.java
private Long handleMessageEmbed(JSONObject content) { EntityBuilder builder = api.getEntityBuilder(); final long messageId = content.getLong("id"); final long channelId = content.getLong("channel_id"); LinkedList<MessageEmbed> embeds = new LinkedList<>(); MessageChannel channel = api.getTextChannelMap().get(channelId); if (channel == null) channel = api.getPrivateChannelMap().get(channelId); if (channel == null) channel = api.getFakePrivateChannelMap().get(channelId); if (channel == null && api.getAccountType() == AccountType.CLIENT) channel = api.asClient().getGroupById(channelId); if (channel == null) { api.getEventCache().cache(EventCache.Type.CHANNEL, channelId, () -> { handle(responseNumber, allContent); });/* w w w .j a v a 2 s . c om*/ EventCache.LOG.debug( "Received message update for embeds for a channel/group that JDA does not have cached yet."); return null; } JSONArray embedsJson = content.getJSONArray("embeds"); for (int i = 0; i < embedsJson.length(); i++) { embeds.add(builder.createMessageEmbed(embedsJson.getJSONObject(i))); } if (channel instanceof TextChannel) { TextChannel tChannel = (TextChannel) channel; if (api.getGuildLock().isLocked(tChannel.getGuild().getIdLong())) { return tChannel.getGuild().getIdLong(); } api.getEventManager() .handle(new GuildMessageEmbedEvent(api, responseNumber, messageId, tChannel, embeds)); } else if (channel instanceof PrivateChannel) { api.getEventManager().handle( new PrivateMessageEmbedEvent(api, responseNumber, messageId, (PrivateChannel) channel, embeds)); } else { api.getEventManager() .handle(new GroupMessageEmbedEvent(api, responseNumber, messageId, (Group) channel, embeds)); } //Combo event api.getEventManager().handle(new MessageEmbedEvent(api, responseNumber, messageId, channel, embeds)); return null; }
From source file:com.mobile.system.db.abatis.AbatisService.java
/** * /* w w w . j a v a2 s.co m*/ * @param jsonStr * JSON String * @param beanClass * Bean class * @param basePackage * Base package name which includes all Bean classes * @return Object Bean * @throws Exception */ @SuppressWarnings({ "rawtypes", "unchecked" }) public Object parse(String jsonStr, Class beanClass, String basePackage) throws Exception { Object obj = null; JSONObject jsonObj = new JSONObject(jsonStr); // Check bean object if (beanClass == null) { Log.d(TAG, "Bean class is null"); return null; } // Read Class member fields Field[] props = beanClass.getDeclaredFields(); if (props == null || props.length == 0) { Log.d(TAG, "Class" + beanClass.getName() + " has no fields"); return null; } // Create instance of this Bean class obj = beanClass.newInstance(); // Set value of each member variable of this object for (int i = 0; i < props.length; i++) { String fieldName = props[i].getName(); // Skip public and static fields if (props[i].getModifiers() == (Modifier.PUBLIC | Modifier.STATIC)) { continue; } // Date Type of Field Class type = props[i].getType(); String typeName = type.getName(); // Check for Custom type if (typeName.equals("int")) { Class[] parms = { type }; Method m = beanClass.getDeclaredMethod(getBeanMethodName(fieldName, 1), parms); m.setAccessible(true); // Set value try { m.invoke(obj, jsonObj.getInt(fieldName)); } catch (Exception ex) { Log.d(TAG, ex.getMessage()); } } else if (typeName.equals("long")) { Class[] parms = { type }; Method m = beanClass.getDeclaredMethod(getBeanMethodName(fieldName, 1), parms); m.setAccessible(true); // Set value try { m.invoke(obj, jsonObj.getLong(fieldName)); } catch (Exception ex) { Log.d(TAG, ex.getMessage()); } } else if (typeName.equals("java.lang.String")) { Class[] parms = { type }; Method m = beanClass.getDeclaredMethod(getBeanMethodName(fieldName, 1), parms); m.setAccessible(true); // Set value try { m.invoke(obj, jsonObj.getString(fieldName)); } catch (Exception ex) { Log.d(TAG, ex.getMessage()); } } else if (typeName.equals("double")) { Class[] parms = { type }; Method m = beanClass.getDeclaredMethod(getBeanMethodName(fieldName, 1), parms); m.setAccessible(true); // Set value try { m.invoke(obj, jsonObj.getDouble(fieldName)); } catch (Exception ex) { Log.d(TAG, ex.getMessage()); } } else if (typeName.equals("java.util.Date")) { // modify Class[] parms = { type }; Method m = beanClass.getDeclaredMethod(getBeanMethodName(fieldName, 1), parms); m.setAccessible(true); String dateString = jsonObj.getString(fieldName); dateString = dateString.replace(" KST", ""); SimpleDateFormat genderFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy", Locale.KOREA); // Set value try { Date afterDate = genderFormat.parse(dateString); m.invoke(obj, afterDate); } catch (Exception e) { Log.d(TAG, e.getMessage()); } } else if (type.getName().equals(List.class.getName()) || type.getName().equals(ArrayList.class.getName())) { // Find out the Generic String generic = props[i].getGenericType().toString(); if (generic.indexOf("<") != -1) { String genericType = generic.substring(generic.lastIndexOf("<") + 1, generic.lastIndexOf(">")); if (genericType != null) { JSONArray array = null; try { array = jsonObj.getJSONArray(fieldName); } catch (Exception ex) { Log.d(TAG, ex.getMessage()); array = null; } if (array == null) { continue; } ArrayList arrayList = new ArrayList(); for (int j = 0; j < array.length(); j++) { arrayList.add(parse(array.getJSONObject(j).toString(), Class.forName(genericType), basePackage)); } // Set value Class[] parms = { type }; Method m = beanClass.getDeclaredMethod(getBeanMethodName(fieldName, 1), parms); m.setAccessible(true); m.invoke(obj, arrayList); } } else { // No generic defined generic = null; } } else if (typeName.startsWith(basePackage)) { Class[] parms = { type }; Method m = beanClass.getDeclaredMethod(getBeanMethodName(fieldName, 1), parms); m.setAccessible(true); // Set value try { JSONObject customObj = jsonObj.getJSONObject(fieldName); if (customObj != null) { m.invoke(obj, parse(customObj.toString(), type, basePackage)); } } catch (JSONException ex) { Log.d(TAG, ex.getMessage()); } } else { // Skip Log.d(TAG, "Field " + fieldName + "#" + typeName + " is skip"); } } return obj; }
From source file:com.nginious.http.serialize.JsonSerializerTestCase.java
public void testJsonSerializer() throws Exception { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssZ"); SerializableBean bean = new SerializableBean(); bean.setBooleanValue(true);/*from w ww . ja v a 2 s .co m*/ bean.setDoubleValue(0.451); bean.setFloatValue(1.34f); bean.setIntValue(3400100); bean.setLongValue(3400100200L); bean.setShortValue((short) 32767); bean.setStringValue("String"); bean.setDateValue(format.parse("2011-08-24T08:50:23+0200")); Date calDate = format.parse("2011-08-24T08:52:23+0200"); Calendar cal = Calendar.getInstance(); cal.setTime(calDate); bean.setCalendarValue(cal); bean.setObjectValue(TimeZone.getDefault()); InBean inBean = new InBean(); inBean.setFirst(true); inBean.setSecond(0.567d); inBean.setThird(0.342f); inBean.setFourth(100); inBean.setFifth(3400200100L); inBean.setSixth((short) 32767); inBean.setSeventh("String"); inBean.setEight(format.parse("2011-08-25T08:50:23+0200")); cal = Calendar.getInstance(); calDate = format.parse("2011-08-25T08:52:23+0200"); cal.setTime(calDate); inBean.setNinth(cal); bean.setBeanValue(inBean); List<InBean> beanList = new ArrayList<InBean>(); beanList.add(inBean); beanList.add(inBean); bean.setBeanListValue(beanList); List<String> stringList = new ArrayList<String>(); stringList.add("One"); stringList.add("Two"); stringList.add("Three"); bean.setStringListValue(stringList); ApplicationClassLoader classLoader = new ApplicationClassLoader( Thread.currentThread().getContextClassLoader()); SerializerFactoryImpl serializerFactory = new SerializerFactoryImpl(classLoader); Serializer<SerializableBean> serializer = serializerFactory.createSerializer(SerializableBean.class, "application/json"); assertEquals("application/json", serializer.getMimeType()); StringWriter strWriter = new StringWriter(); PrintWriter writer = new PrintWriter(strWriter); serializer.serialize(writer, bean); writer.flush(); JSONObject json = new JSONObject(strWriter.toString()); assertNotNull(json); assertTrue(json.has("serializableBean")); json = json.getJSONObject("serializableBean"); assertEquals(true, json.getBoolean("booleanValue")); assertEquals(0.451, json.getDouble("doubleValue")); assertEquals(1.34f, (float) json.getDouble("floatValue")); assertEquals(3400100, json.getInt("intValue")); assertEquals(3400100200L, json.getLong("longValue")); assertEquals(32767, (short) json.getInt("shortValue")); assertEquals("String", json.getString("stringValue")); assertEquals("2011-08-24T08:50:23+02:00", json.getString("dateValue")); assertEquals("2011-08-24T08:52:23+02:00", json.getString("calendarValue")); assertEquals(TimeZone.getDefault().toString(), json.getString("objectValue")); assertTrue(json.has("beanValue")); JSONObject inBeanJson = json.getJSONObject("beanValue"); assertTrue(inBeanJson.has("inBean")); inBeanJson = inBeanJson.getJSONObject("inBean"); assertEquals(true, inBeanJson.getBoolean("first")); assertEquals(0.567, inBeanJson.getDouble("second")); assertEquals(0.342f, (float) inBeanJson.getDouble("third")); assertEquals(100, inBeanJson.getInt("fourth")); assertEquals(3400200100L, inBeanJson.getLong("fifth")); assertEquals(32767, (short) inBeanJson.getInt("sixth")); assertEquals("String", inBeanJson.getString("seventh")); assertEquals("2011-08-25T08:50:23+02:00", inBeanJson.getString("eight")); assertEquals("2011-08-25T08:52:23+02:00", inBeanJson.getString("ninth")); assertTrue(json.has("stringListValue")); JSONArray stringListJson = json.getJSONArray("stringListValue"); assertEquals("One", stringListJson.get(0)); assertEquals("Two", stringListJson.get(1)); assertEquals("Three", stringListJson.get(2)); assertTrue(json.has("beanListValue")); JSONArray beanListJson = json.getJSONArray("beanListValue"); inBeanJson = beanListJson.getJSONObject(0); assertTrue(inBeanJson.has("inBean")); inBeanJson = inBeanJson.getJSONObject("inBean"); assertEquals(true, inBeanJson.getBoolean("first")); assertEquals(0.567, inBeanJson.getDouble("second")); assertEquals(0.342f, (float) inBeanJson.getDouble("third")); assertEquals(100, inBeanJson.getInt("fourth")); assertEquals(3400200100L, inBeanJson.getLong("fifth")); assertEquals(32767, (short) inBeanJson.getInt("sixth")); assertEquals("String", inBeanJson.getString("seventh")); assertEquals("2011-08-25T08:50:23+02:00", inBeanJson.getString("eight")); assertEquals("2011-08-25T08:52:23+02:00", inBeanJson.getString("ninth")); inBeanJson = beanListJson.getJSONObject(1); assertTrue(inBeanJson.has("inBean")); inBeanJson = inBeanJson.getJSONObject("inBean"); assertEquals(true, inBeanJson.getBoolean("first")); assertEquals(0.567, inBeanJson.getDouble("second")); assertEquals(0.342f, (float) inBeanJson.getDouble("third")); assertEquals(100, inBeanJson.getInt("fourth")); assertEquals(3400200100L, inBeanJson.getLong("fifth")); assertEquals(32767, (short) inBeanJson.getInt("sixth")); assertEquals("String", inBeanJson.getString("seventh")); assertEquals("2011-08-25T08:50:23+02:00", inBeanJson.getString("eight")); assertEquals("2011-08-25T08:52:23+02:00", inBeanJson.getString("ninth")); }
From source file:com.nginious.http.serialize.JsonSerializerTestCase.java
public void testNamedJsonSerializer() throws Exception { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssZ"); NamedBean bean = new NamedBean(); bean.setBooleanValue(true);//w w w. ja v a2s . c o m bean.setDoubleValue(0.451); bean.setFloatValue(1.34f); bean.setIntValue(3400100); bean.setLongValue(3400100200L); bean.setShortValue((short) 32767); bean.setStringValue("String"); bean.setDateValue(format.parse("2011-08-24T08:50:23+0200")); Date calDate = format.parse("2011-08-24T08:52:23+0200"); Calendar cal = Calendar.getInstance(); cal.setTime(calDate); bean.setCalendarValue(cal); bean.setObjectValue(TimeZone.getDefault()); InBean inBean = new InBean(); inBean.setFirst(true); inBean.setSecond(0.567d); inBean.setThird(0.342f); inBean.setFourth(100); inBean.setFifth(3400200100L); inBean.setSixth((short) 32767); inBean.setSeventh("String"); inBean.setEight(format.parse("2011-08-25T08:50:23+0200")); cal = Calendar.getInstance(); calDate = format.parse("2011-08-25T08:52:23+0200"); cal.setTime(calDate); inBean.setNinth(cal); bean.setBeanValue(inBean); List<InBean> beanList = new ArrayList<InBean>(); beanList.add(inBean); beanList.add(inBean); bean.setBeanListValue(beanList); List<String> stringList = new ArrayList<String>(); stringList.add("One"); stringList.add("Two"); stringList.add("Three"); bean.setStringListValue(stringList); ApplicationClassLoader classLoader = new ApplicationClassLoader( Thread.currentThread().getContextClassLoader()); SerializerFactoryImpl serializerFactory = new SerializerFactoryImpl(classLoader); Serializer<NamedBean> serializer = serializerFactory.createSerializer(NamedBean.class, "application/json"); assertEquals("application/json", serializer.getMimeType()); StringWriter strWriter = new StringWriter(); PrintWriter writer = new PrintWriter(strWriter); serializer.serialize(writer, bean); writer.flush(); JSONObject json = new JSONObject(strWriter.toString()); assertNotNull(json); assertTrue(json.has("testNamedBean")); json = json.getJSONObject("testNamedBean"); assertEquals(true, json.getBoolean("testBooleanValue")); assertEquals(0.451, json.getDouble("testDoubleValue")); assertEquals(1.34f, (float) json.getDouble("testFloatValue")); assertEquals(3400100, json.getInt("testIntValue")); assertEquals(3400100200L, json.getLong("testLongValue")); assertEquals(32767, (short) json.getInt("testShortValue")); assertEquals("String", json.getString("testStringValue")); assertEquals("2011-08-24T08:50:23+02:00", json.getString("testDateValue")); assertEquals("2011-08-24T08:52:23+02:00", json.getString("testCalendarValue")); assertEquals(TimeZone.getDefault().toString(), json.getString("testObjectValue")); assertTrue(json.has("testBeanValue")); JSONObject inBeanJson = json.getJSONObject("testBeanValue"); assertTrue(inBeanJson.has("inBean")); inBeanJson = inBeanJson.getJSONObject("inBean"); assertEquals(true, inBeanJson.getBoolean("first")); assertEquals(0.567, inBeanJson.getDouble("second")); assertEquals(0.342f, (float) inBeanJson.getDouble("third")); assertEquals(100, inBeanJson.getInt("fourth")); assertEquals(3400200100L, inBeanJson.getLong("fifth")); assertEquals(32767, (short) inBeanJson.getInt("sixth")); assertEquals("String", inBeanJson.getString("seventh")); assertEquals("2011-08-25T08:50:23+02:00", inBeanJson.getString("eight")); assertEquals("2011-08-25T08:52:23+02:00", inBeanJson.getString("ninth")); assertTrue(json.has("testStringListValue")); JSONArray stringListJson = json.getJSONArray("testStringListValue"); assertEquals("One", stringListJson.get(0)); assertEquals("Two", stringListJson.get(1)); assertEquals("Three", stringListJson.get(2)); assertTrue(json.has("testBeanListValue")); JSONArray beanListJson = json.getJSONArray("testBeanListValue"); inBeanJson = beanListJson.getJSONObject(0); assertTrue(inBeanJson.has("inBean")); inBeanJson = inBeanJson.getJSONObject("inBean"); assertEquals(true, inBeanJson.getBoolean("first")); assertEquals(0.567, inBeanJson.getDouble("second")); assertEquals(0.342f, (float) inBeanJson.getDouble("third")); assertEquals(100, inBeanJson.getInt("fourth")); assertEquals(3400200100L, inBeanJson.getLong("fifth")); assertEquals(32767, (short) inBeanJson.getInt("sixth")); assertEquals("String", inBeanJson.getString("seventh")); assertEquals("2011-08-25T08:50:23+02:00", inBeanJson.getString("eight")); assertEquals("2011-08-25T08:52:23+02:00", inBeanJson.getString("ninth")); inBeanJson = beanListJson.getJSONObject(1); assertTrue(inBeanJson.has("inBean")); inBeanJson = inBeanJson.getJSONObject("inBean"); assertEquals(true, inBeanJson.getBoolean("first")); assertEquals(0.567, inBeanJson.getDouble("second")); assertEquals(0.342f, (float) inBeanJson.getDouble("third")); assertEquals(100, inBeanJson.getInt("fourth")); assertEquals(3400200100L, inBeanJson.getLong("fifth")); assertEquals(32767, (short) inBeanJson.getInt("sixth")); assertEquals("String", inBeanJson.getString("seventh")); assertEquals("2011-08-25T08:50:23+02:00", inBeanJson.getString("eight")); assertEquals("2011-08-25T08:52:23+02:00", inBeanJson.getString("ninth")); }
From source file:com.nginious.http.serialize.JsonSerializerTestCase.java
public void testEmptyJsonSerializer() throws Exception { SerializableBean bean = new SerializableBean(); ApplicationClassLoader classLoader = new ApplicationClassLoader( Thread.currentThread().getContextClassLoader()); SerializerFactoryImpl serializerFactory = new SerializerFactoryImpl(classLoader); Serializer<SerializableBean> serializer = serializerFactory.createSerializer(SerializableBean.class, "application/json"); assertEquals("application/json", serializer.getMimeType()); StringWriter strWriter = new StringWriter(); PrintWriter writer = new PrintWriter(strWriter); serializer.serialize(writer, bean);//from ww w. ja v a 2 s. co m writer.flush(); JSONObject json = new JSONObject(strWriter.toString()); assertNotNull(json); assertTrue(json.has("serializableBean")); json = json.getJSONObject("serializableBean"); assertEquals(false, json.getBoolean("booleanValue")); assertEquals(0.0d, json.getDouble("doubleValue")); assertEquals(0.0f, (float) json.getDouble("floatValue")); assertEquals(0, json.getInt("intValue")); assertEquals(0L, json.getLong("longValue")); assertEquals(0, (short) json.getInt("shortValue")); assertFalse(json.has("stringValue")); assertFalse(json.has("dateValue")); assertFalse(json.has("calendarValue")); assertFalse(json.has("objectValue")); }
From source file:fi.kinetik.android.currencies.spi.openexchange.OpenExchangeRatesSpi.java
private void parseRates(ArrayList<ContentProviderOperation> operations, JSONObject jsonObj) throws JSONException { final long updated = jsonObj.getLong(Keys.TIMESTAMP); final String base = jsonObj.getString(Keys.BASE); final JSONObject rates = jsonObj.getJSONObject(Keys.RATES); final Iterator iter = rates.keys(); while (iter.hasNext()) { final String currency = (String) iter.next(); final double rate = rates.getDouble(currency); operations.add(ConversionRate.newUpdateOperation(currency, OpenExchangeRatesSpiFactory.PROVIDER_NAME, updated, rate));/*from w w w . ja v a2 s .c o m*/ } }