@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 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()); }
/** tests getting serializer/deserializer instances. */ public void testSerializeDeserializeWithJaxbAnnotations() throws Exception { ObjectMapper mapper = getJaxbMapper(); // test expects that wrapper name be used... mapper.enable(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME); mapper.enable(SerializationFeature.INDENT_OUTPUT); JaxbExample ex = new JaxbExample(); QName qname = new QName("urn:hi", "hello"); ex.setQname(qname); QName qname1 = new QName("urn:hi", "hello1"); ex.setQname1(qname1); ex.setAttributeProperty("attributeValue"); ex.setElementProperty("elementValue"); ex.setWrappedElementProperty(Arrays.asList("wrappedElementValue")); ex.setEnumProperty(EnumExample.VALUE1); ex.setPropertyToIgnore("ignored"); String json = mapper.writeValueAsString(ex); // uncomment to see what the JSON looks like. // System.out.println(json); // make sure the json is written out correctly. JsonNode node = mapper.readValue(json, JsonNode.class); assertEquals(qname.toString(), node.get("qname").asText()); JsonNode attr = node.get("myattribute"); assertNotNull(attr); assertEquals("attributeValue", attr.asText()); assertEquals("elementValue", node.get("myelement").asText()); assertTrue(node.has("mywrapped")); assertEquals(1, node.get("mywrapped").size()); assertEquals("wrappedElementValue", node.get("mywrapped").get(0).asText()); assertEquals("Value One", node.get("enumProperty").asText()); assertNull(node.get("propertyToIgnore")); // now make sure it gets deserialized correctly. JaxbExample readEx = mapper.readValue(json, JaxbExample.class); assertEquals(ex.qname, readEx.qname); assertEquals(ex.qname1, readEx.qname1); assertEquals(ex.attributeProperty, readEx.attributeProperty); assertEquals(ex.elementProperty, readEx.elementProperty); assertEquals(ex.wrappedElementProperty, readEx.wrappedElementProperty); assertEquals(ex.enumProperty, readEx.enumProperty); assertNull(readEx.propertyToIgnore); }