/** 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(); } }
@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; }
public void testEmbeddedObject() throws Exception { TokenBuffer buf = new TokenBuffer(MAPPER); buf.writeObject(new byte[3]); JsonNode node = MAPPER.readTree(buf.asParser()); buf.close(); assertTrue(node.isBinary()); byte[] data = node.binaryValue(); assertNotNull(data); assertEquals(3, data.length); }
// [Issue#232] public void testBigDecimalAsPlainStringTreeConversion() throws Exception { ObjectMapper mapper = new ObjectMapper(); mapper.enable(SerializationFeature.WRITE_BIGDECIMAL_AS_PLAIN); Map<String, Object> map = new HashMap<String, Object>(); String PI_STR = "3.00000000"; map.put("pi", new BigDecimal(PI_STR)); JsonNode tree = mapper.valueToTree(map); assertNotNull(tree); assertEquals(1, tree.size()); assertTrue(tree.has("pi")); }
/** * Method for locating node specified by given JSON pointer instances. Method will never return * null; if no matching node exists, will return a node for which {@link #isMissingNode()} returns * true. * * @return Node that matches given JSON Pointer: if no match exists, will return a node for which * {@link #isMissingNode()} returns true. * @since 2.3 */ @Override public final JsonNode at(JsonPointer ptr) { // Basically: value nodes only match if we have "empty" path left if (ptr.matches()) { return this; } JsonNode n = _at(ptr); if (n == null) { return MissingNode.getInstance(); } return n.at(ptr.tail()); }
public void testEmbeddedObjectInArray() throws Exception { TokenBuffer buf = new TokenBuffer(MAPPER); buf.writeStartArray(); buf.writeObject(MARKER); buf.writeEndArray(); JsonNode node = MAPPER.readTree(buf.asParser()); buf.close(); assertTrue(node.isArray()); assertEquals(1, node.size()); JsonNode n = node.get(0); assertTrue(n.isPojo()); assertSame(MARKER, ((POJONode) n).getPojo()); }
/** * Method that is similar to {@link #has(int)}, but that will return <code>false</code> for * explicitly added nulls. * * <p>This method is equivalent to: * * <pre> * node.get(index) != null << !node.get(index).isNull() * </pre> * * @since 2.1 */ public boolean hasNonNull(int index) { JsonNode n = get(index); return (n != null) && !n.isNull(); }
/** * Method that is similar to {@link #has(String)}, but that will return <code>false</code> for * explicitly added nulls. * * <p>This method is functionally equivalent to: * * <pre> * node.get(fieldName) != null << !node.get(fieldName).isNull() * </pre> * * @since 2.1 */ public boolean hasNonNull(String fieldName) { JsonNode n = get(fieldName); return (n != null) && !n.isNull(); }