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); }
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()); }
@SuppressWarnings("unchecked") public <T extends JsonNode> T valueToTree(ObjectWriter writer, Object fromValue) throws IllegalArgumentException { if (fromValue == null) return null; TokenBuffer buf = new TokenBuffer(mapper); JsonNode result; try { writer.writeValue(buf, fromValue); JsonParser jp = buf.asParser(); result = mapper.readTree(jp); jp.close(); } catch (IOException e) { // should not occur, no real i/o... throw new IllegalArgumentException(e.getMessage(), e); } return (T) result; }