@Test public void testGetDouble() { jsonObject.put("foo", 123d); assertEquals(Double.valueOf(123d), jsonObject.getDouble("foo")); jsonObject.put("bar", "hello"); try { jsonObject.getDouble("bar"); fail(); } catch (ClassCastException e) { // Ok } // Put as different Number types jsonObject.put("foo", 123); assertEquals(Double.valueOf(123d), jsonObject.getDouble("foo")); jsonObject.put("foo", 123l); assertEquals(Double.valueOf(123d), jsonObject.getDouble("foo")); jsonObject.put("foo", 123f); assertEquals(Double.valueOf(123d), jsonObject.getDouble("foo")); // Null and absent values jsonObject.putNull("foo"); assertNull(jsonObject.getDouble("foo")); assertNull(jsonObject.getDouble("absent")); try { jsonObject.getDouble(null); fail(); } catch (NullPointerException e) { // OK } }
@Test public void testGetDoubleDefault() { jsonObject.put("foo", 123d); assertEquals(Double.valueOf(123d), jsonObject.getDouble("foo", 321d)); assertEquals(Double.valueOf(123), jsonObject.getDouble("foo", null)); jsonObject.put("bar", "hello"); try { jsonObject.getDouble("bar", 123d); fail(); } catch (ClassCastException e) { assertEquals(Double.valueOf(123d), jsonObject.getDouble("foo", 321d)); jsonObject.put("foo", 123f); assertEquals(Double.valueOf(123d), jsonObject.getDouble("foo", 321d)); jsonObject.put("foo", 123l); assertEquals(Double.valueOf(123d), jsonObject.getDouble("foo", 321d)); assertNull(jsonObject.getDouble("foo", 321d)); assertEquals(Double.valueOf(321d), jsonObject.getDouble("absent", 321d)); assertNull(jsonObject.getDouble("foo", null)); assertNull(jsonObject.getDouble("absent", null)); jsonObject.getDouble(null, null); fail(); } catch (NullPointerException e) {
@Test public void testPutDouble() { assertSame(jsonObject, jsonObject.put("foo", 123d)); assertEquals(Double.valueOf(123d), jsonObject.getDouble("foo")); jsonObject.put("quux", 321d); assertEquals(Double.valueOf(321d), jsonObject.getDouble("quux")); assertEquals(Double.valueOf(123d), jsonObject.getDouble("foo")); jsonObject.put("foo", 456d); assertEquals(Double.valueOf(456d), jsonObject.getDouble("foo")); jsonObject.put("foo", (Double) null); assertTrue(jsonObject.containsKey("foo")); try { jsonObject.put(null, 1.23d); fail(); } catch (NullPointerException e) { // OK } }
assertEquals(Long.valueOf(123l), jsonObject.getLong("long")); assertEquals(Float.valueOf(1.23f), jsonObject.getFloat("float")); assertEquals(Double.valueOf(1.23d), jsonObject.getDouble("double")); assertArrayEquals(bytes, jsonObject.getBinary("binary")); assertEquals(Base64.getEncoder().encodeToString(bytes), jsonObject.getValue("binary"));
@Test public void testDecode() throws Exception { byte[] bytes = TestUtils.randomByteArray(10); String strBytes = Base64.getEncoder().encodeToString(bytes); Instant now = Instant.now(); String strInstant = ISO_INSTANT.format(now); String json = "{\"mystr\":\"foo\",\"myint\":123,\"mylong\":1234,\"myfloat\":1.23,\"mydouble\":2.34,\"" + "myboolean\":true,\"mybinary\":\"" + strBytes + "\",\"myinstant\":\"" + strInstant + "\",\"mynull\":null,\"myobj\":{\"foo\":\"bar\"},\"myarr\":[\"foo\",123]}"; JsonObject obj = new JsonObject(json); assertEquals(json, obj.encode()); assertEquals("foo", obj.getString("mystr")); assertEquals(Integer.valueOf(123), obj.getInteger("myint")); assertEquals(Long.valueOf(1234), obj.getLong("mylong")); assertEquals(Float.valueOf(1.23f), obj.getFloat("myfloat")); assertEquals(Double.valueOf(2.34d), obj.getDouble("mydouble")); assertTrue(obj.getBoolean("myboolean")); assertArrayEquals(bytes, obj.getBinary("mybinary")); assertEquals(Base64.getEncoder().encodeToString(bytes), obj.getValue("mybinary")); assertEquals(now, obj.getInstant("myinstant")); assertEquals(now.toString(), obj.getValue("myinstant")); assertTrue(obj.containsKey("mynull")); JsonObject nestedObj = obj.getJsonObject("myobj"); assertEquals("bar", nestedObj.getString("foo")); JsonArray nestedArr = obj.getJsonArray("myarr"); assertEquals("foo", nestedArr.getString(0)); assertEquals(Integer.valueOf(123), Integer.valueOf(nestedArr.getInteger(1))); }
@Test public void testGetDouble() { jsonObject.put("foo", 123d); assertEquals(Double.valueOf(123d), jsonObject.getDouble("foo")); jsonObject.put("bar", "hello"); try { jsonObject.getDouble("bar"); fail(); } catch (ClassCastException e) { // Ok } // Put as different Number types jsonObject.put("foo", 123); assertEquals(Double.valueOf(123d), jsonObject.getDouble("foo")); jsonObject.put("foo", 123l); assertEquals(Double.valueOf(123d), jsonObject.getDouble("foo")); jsonObject.put("foo", 123f); assertEquals(Double.valueOf(123d), jsonObject.getDouble("foo")); // Null and absent values jsonObject.putNull("foo"); assertNull(jsonObject.getDouble("foo")); assertNull(jsonObject.getDouble("absent")); try { jsonObject.getDouble(null); fail(); } catch (NullPointerException e) { // OK } }
@Test public void testGetDoubleDefault() { jsonObject.put("foo", 123d); assertEquals(Double.valueOf(123d), jsonObject.getDouble("foo", 321d)); assertEquals(Double.valueOf(123), jsonObject.getDouble("foo", null)); jsonObject.put("bar", "hello"); try { jsonObject.getDouble("bar", 123d); fail(); } catch (ClassCastException e) { assertEquals(Double.valueOf(123d), jsonObject.getDouble("foo", 321d)); jsonObject.put("foo", 123f); assertEquals(Double.valueOf(123d), jsonObject.getDouble("foo", 321d)); jsonObject.put("foo", 123l); assertEquals(Double.valueOf(123d), jsonObject.getDouble("foo", 321d)); assertNull(jsonObject.getDouble("foo", 321d)); assertEquals(Double.valueOf(321d), jsonObject.getDouble("absent", 321d)); assertNull(jsonObject.getDouble("foo", null)); assertNull(jsonObject.getDouble("absent", null)); jsonObject.getDouble(null, null); fail(); } catch (NullPointerException e) {
@Test public void testPutDouble() { assertSame(jsonObject, jsonObject.put("foo", 123d)); assertEquals(Double.valueOf(123d), jsonObject.getDouble("foo")); jsonObject.put("quux", 321d); assertEquals(Double.valueOf(321d), jsonObject.getDouble("quux")); assertEquals(Double.valueOf(123d), jsonObject.getDouble("foo")); jsonObject.put("foo", 456d); assertEquals(Double.valueOf(456d), jsonObject.getDouble("foo")); jsonObject.put("foo", (Double) null); assertTrue(jsonObject.containsKey("foo")); try { jsonObject.put(null, 1.23d); fail(); } catch (NullPointerException e) { // OK } }
assertEquals(Long.valueOf(123l), jsonObject.getLong("long")); assertEquals(Float.valueOf(1.23f), jsonObject.getFloat("float")); assertEquals(Double.valueOf(1.23d), jsonObject.getDouble("double")); assertArrayEquals(bytes, jsonObject.getBinary("binary")); assertEquals(Base64.getEncoder().encodeToString(bytes), jsonObject.getValue("binary"));
@Test public void testDecode() throws Exception { byte[] bytes = TestUtils.randomByteArray(10); String strBytes = Base64.getEncoder().encodeToString(bytes); Instant now = Instant.now(); String strInstant = ISO_INSTANT.format(now); String json = "{\"mystr\":\"foo\",\"myint\":123,\"mylong\":1234,\"myfloat\":1.23,\"mydouble\":2.34,\"" + "myboolean\":true,\"mybinary\":\"" + strBytes + "\",\"myinstant\":\"" + strInstant + "\",\"mynull\":null,\"myobj\":{\"foo\":\"bar\"},\"myarr\":[\"foo\",123]}"; JsonObject obj = new JsonObject(json); assertEquals(json, obj.encode()); assertEquals("foo", obj.getString("mystr")); assertEquals(Integer.valueOf(123), obj.getInteger("myint")); assertEquals(Long.valueOf(1234), obj.getLong("mylong")); assertEquals(Float.valueOf(1.23f), obj.getFloat("myfloat")); assertEquals(Double.valueOf(2.34d), obj.getDouble("mydouble")); assertTrue(obj.getBoolean("myboolean")); assertArrayEquals(bytes, obj.getBinary("mybinary")); assertEquals(Base64.getEncoder().encodeToString(bytes), obj.getValue("mybinary")); assertEquals(now, obj.getInstant("myinstant")); assertEquals(now.toString(), obj.getValue("myinstant")); assertTrue(obj.containsKey("mynull")); JsonObject nestedObj = obj.getJsonObject("myobj"); assertEquals("bar", nestedObj.getString("foo")); JsonArray nestedArr = obj.getJsonArray("myarr"); assertEquals("foo", nestedArr.getString(0)); assertEquals(Integer.valueOf(123), Integer.valueOf(nestedArr.getInteger(1))); }
@Override public void start() { EventBus bus = vertx.eventBus(); bus.<JsonObject>consumer("sensor.updates", msg -> { JsonObject body = msg.body(); String id = body.getString("id"); String temp = format.format(body.getDouble("temp")); logger.info("{} reports a temperature ~{}C", id, temp); }); } }
private Place createPlaceFromMapMe(JsonObject json, String tag, String category) { Place place = new Place(); return place .setAddress(json.getString("addressDisplay", "")) .setCategory(category) .setDescription(json.getString("description", "")) .setLatitude(json.getDouble("lat", -1.0)) .setLongitude(json.getDouble("lon", -1.0)) .setName(json.getString("companyName")) .setTags(Collections.singletonList(tag)); } }
@CodeTranslate public void getDoubleFromIdentifier() throws Exception { JsonObject obj = new JsonObject().put("port", 8080d); JsonTest.o = obj.getDouble("port"); }
public TestDataObject(JsonObject json) { this.foo = json.getString("foo", null); this.bar = json.getInteger("bar", 0); this.wibble = json.getDouble("wibble", 0d); }
public TestDataObject(JsonObject json) { this.foo = json.getString("foo", null); this.bar = json.getInteger("bar", 0); this.wibble = json.getDouble("wibble", 0d); }
public TestDataObject(JsonObject json) { this.foo = json.getString("foo", null); this.bar = json.getInteger("bar", 0); this.wibble = json.getDouble("wibble", 0d); }
public TestDataObject(JsonObject json) { this.foo = json.getString("foo", null); this.bar = json.getInteger("bar", 0); this.wibble = json.getDouble("wibble", 0d); }