/** Unit test to check for regression of [JACKSON-18]. */ public void testSmallNumbers() throws Exception { ObjectMapper mapper = new ObjectMapper(); ArrayNode root = mapper.createArrayNode(); for (int i = -20; i <= 20; ++i) { JsonNode n = root.numberNode(i); root.add(n); // Hmmh. Not sure why toString() won't be triggered otherwise... assertEquals(String.valueOf(i), n.toString()); } // Loop over 2 different serialization methods for (int type = 0; type < 2; ++type) { StringWriter sw = new StringWriter(); if (type == 0) { JsonGenerator gen = new JsonFactory().createGenerator(sw); root.serialize(gen, null); gen.close(); } else { mapper.writeValue(sw, root); } String doc = sw.toString(); JsonParser p = new JsonFactory().createParser(new StringReader(doc)); assertEquals(JsonToken.START_ARRAY, p.nextToken()); for (int i = -20; i <= 20; ++i) { assertEquals(JsonToken.VALUE_NUMBER_INT, p.nextToken()); assertEquals(i, p.getIntValue()); assertEquals("" + i, p.getText()); } assertEquals(JsonToken.END_ARRAY, p.nextToken()); p.close(); } }
public void testBinary() throws Exception { ObjectMapper mapper = new ObjectMapper(); final int LENGTH = 13045; byte[] data = new byte[LENGTH]; for (int i = 0; i < LENGTH; ++i) { data[i] = (byte) i; } StringWriter sw = new StringWriter(); mapper.writeValue(sw, BinaryNode.valueOf(data)); JsonParser p = new JsonFactory().createParser(sw.toString()); // note: can't determine it's binary from json alone: assertToken(JsonToken.VALUE_STRING, p.nextToken()); assertArrayEquals(data, p.getBinaryValue()); p.close(); }
@Test public void testJSONToRGB() { InputStream is = this.getClass().getResourceAsStream("/json_test/user_profile_json_RGB.txt"); Profile profile = JsonParser.parseProfile(is); assertNotNull(profile); assertNotNull(profile.dashBoards); assertEquals(1, profile.dashBoards.length); assertNotNull(profile.dashBoards[0]); assertNotNull(profile.dashBoards[0].widgets); assertEquals(1, profile.dashBoards[0].widgets.length); RGB rgb = (RGB) profile.dashBoards[0].widgets[0]; assertNotNull(rgb.pins); assertEquals(2, rgb.pins.length); Pin pin1 = rgb.pins[0]; Pin pin2 = rgb.pins[1]; assertNotNull(pin1); assertNotNull(pin2); assertEquals(1, pin1.pin); assertEquals(2, pin2.pin); assertEquals("1", pin1.value); assertEquals("2", pin2.value); assertEquals(PinType.DIGITAL, pin1.pinType); assertEquals(PinType.DIGITAL, pin2.pinType); assertFalse(pin1.pwmMode); assertTrue(pin2.pwmMode); }
@Test public void testParseUserProfile() { InputStream is = this.getClass().getResourceAsStream("/json_test/user_profile_json.txt"); Profile profile = JsonParser.parseProfile(is); assertNotNull(profile); assertNotNull(profile.dashBoards); assertEquals(profile.dashBoards.length, 1); DashBoard dashBoard = profile.dashBoards[0]; assertNotNull(dashBoard); assertEquals(1, dashBoard.id); assertEquals("My Dashboard", dashBoard.name); assertNotNull(dashBoard.widgets); assertEquals(dashBoard.widgets.length, 8); assertNotNull(dashBoard.boardType); assertEquals("UNO", dashBoard.boardType); for (Widget widget : dashBoard.widgets) { assertNotNull(widget); assertEquals(1, widget.x); assertEquals(1, widget.y); assertEquals(1, widget.id); assertEquals("Some Text", widget.label); } }
@Override public Leaf deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { JsonNode tree = (JsonNode) jp.readValueAsTree(); Leaf leaf = new Leaf(); leaf.value = tree.get("value").intValue(); return leaf; }
@Test public void testUserProfileToJson() { InputStream is = this.getClass().getResourceAsStream("/json_test/user_profile_json.txt"); Profile profile = JsonParser.parseProfile(is); String userProfileString = profile.toString(); assertNotNull(userProfileString); assertTrue(userProfileString.contains("dashBoards")); }
@Test public void testUserProfileToJsonWithTimer() { InputStream is = this.getClass().getResourceAsStream("/json_test/user_profile_with_timer.txt"); Profile profile = JsonParser.parseProfile(is); String userProfileString = profile.toString(); assertNotNull(userProfileString); assertTrue(userProfileString.contains("dashBoards")); List<Timer> timers = getActiveTimerWidgets(profile); assertNotNull(timers); assertEquals(1, timers.size()); }
@Test public void testParseIOSProfile() { InputStream is = this.getClass().getResourceAsStream("/json_test/user_ios_profile_json.txt"); Profile profile = JsonParser.parseProfile(is); assertNotNull(profile); assertNotNull(profile.dashBoards); assertEquals(1, profile.dashBoards.length); assertNotNull(profile.dashBoards[0].widgets); assertNotNull(profile.dashBoards[0].widgets[0]); assertNotNull(profile.dashBoards[0].widgets[1]); assertTrue(((Button) profile.dashBoards[0].widgets[0]).pushMode); assertFalse(((Button) profile.dashBoards[0].widgets[1]).pushMode); }
@Test public void testCreateFailures() { // Delayed invalid JSON JsonArray array = null; try { array = JsonParser.parseArray("[{]", true); } catch (JsonParseException e) { fail("This shouldn't happen!"); } // get(int) try { array.get(0); fail("Didn't throw an exception on delayed invalid JSON"); } catch (JsonParseException e) { } catch (Exception e) { e.printStackTrace(); fail("Wrong exception!"); } // getObject(int) try { array.getObject(0); fail("Didn't throw an exception on delayed invalid JSON"); } catch (JsonParseException e) { } catch (Exception e) { fail("Wrong exception!"); } // getArray(int) try { array.getArray(0); fail("Didn't throw an exception on delayed invalid JSON"); } catch (JsonParseException e) { } catch (Exception e) { fail("Wrong exception!"); } // getString(int) try { array.getString(0); fail("Didn't throw an exception on delayed invalid JSON"); } catch (JsonParseException e) { } catch (Exception e) { fail("Wrong exception!"); } // getLong(int) try { array.getLong(0); fail("Didn't throw an exception on delayed invalid JSON"); } catch (JsonParseException e) { } catch (Exception e) { fail("Wrong exception!"); } // getDouble(int) try { array.getDouble(0); fail("Didn't throw an exception on delayed invalid JSON"); } catch (JsonParseException e) { } catch (Exception e) { fail("Wrong exception!"); } // getBoolean(int) try { array.getBoolean(0); fail("Didn't throw an exception on delayed invalid JSON"); } catch (JsonParseException e) { } catch (Exception e) { fail("Wrong exception!"); } }
private void verifyFromMap(String input) throws Exception { JsonParser p = new JsonFactory().createParser(input); assertEquals(JsonToken.START_OBJECT, p.nextToken()); assertEquals(JsonToken.FIELD_NAME, p.nextToken()); assertEquals(FIELD4, getAndVerifyText(p)); assertEquals(JsonToken.VALUE_STRING, p.nextToken()); assertEquals(TEXT2, getAndVerifyText(p)); assertEquals(JsonToken.FIELD_NAME, p.nextToken()); assertEquals(FIELD3, getAndVerifyText(p)); assertEquals(JsonToken.VALUE_NUMBER_INT, p.nextToken()); assertEquals(-1, p.getIntValue()); assertEquals(JsonToken.FIELD_NAME, p.nextToken()); assertEquals(FIELD2, getAndVerifyText(p)); assertEquals(JsonToken.START_ARRAY, p.nextToken()); assertEquals(JsonToken.END_ARRAY, p.nextToken()); assertEquals(JsonToken.FIELD_NAME, p.nextToken()); assertEquals(FIELD1, getAndVerifyText(p)); assertEquals(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken()); assertEquals(DOUBLE_VALUE, p.getDoubleValue(), 0); assertEquals(JsonToken.END_OBJECT, p.nextToken()); assertNull(p.nextToken()); p.close(); }
private void verifyFromArray(String input) throws Exception { JsonParser p = new JsonFactory().createParser(new StringReader(input)); assertEquals(JsonToken.START_ARRAY, p.nextToken()); assertEquals(JsonToken.VALUE_STRING, p.nextToken()); assertEquals(TEXT1, getAndVerifyText(p)); assertEquals(JsonToken.VALUE_NUMBER_INT, p.nextToken()); assertEquals(3, p.getIntValue()); assertEquals(JsonToken.START_OBJECT, p.nextToken()); assertEquals(JsonToken.FIELD_NAME, p.nextToken()); assertEquals(FIELD1, getAndVerifyText(p)); assertEquals(JsonToken.VALUE_TRUE, p.nextToken()); assertEquals(JsonToken.FIELD_NAME, p.nextToken()); assertEquals(FIELD2, getAndVerifyText(p)); assertEquals(JsonToken.START_ARRAY, p.nextToken()); assertEquals(JsonToken.END_ARRAY, p.nextToken()); assertEquals(JsonToken.END_OBJECT, p.nextToken()); assertEquals(JsonToken.VALUE_FALSE, p.nextToken()); assertEquals(JsonToken.END_ARRAY, p.nextToken()); assertNull(p.nextToken()); p.close(); }