/** 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); }
// [JACKSON-684] public void testAsIndex() throws Exception { // By default, serialize using name ObjectMapper m = new ObjectMapper(); assertFalse(m.isEnabled(SerializationFeature.WRITE_ENUMS_USING_INDEX)); assertEquals(quote("B"), m.writeValueAsString(TestEnum.B)); // but we can change (dynamically, too!) it to be number-based m.enable(SerializationFeature.WRITE_ENUMS_USING_INDEX); assertEquals("1", m.writeValueAsString(TestEnum.B)); }
// [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")); }