public static String toJson(Object value) { try { return OBJECT_MAPPER.writer().writeValueAsString(value); } catch (JsonProcessingException e) { throw new IllegalArgumentException("Unable to serialize to json", e); } }
public static byte[] toByteArray(Object value) { try { return OBJECT_MAPPER.writer().writeValueAsBytes(value); } catch (JsonProcessingException e) { throw new IllegalArgumentException("Unable to serialize to json", e); } }
public void testMapFilteringViaProps() throws Exception { FilterProvider prov = new SimpleFilterProvider() .addFilter("filterX", SimpleBeanPropertyFilter.filterOutAllExcept("b")); String json = MAPPER.writer(prov).writeValueAsString(new MapBean()); assertEquals(aposToQuotes("{'values':{'b':5}}"), json); }
public void testMapFilteringViaClass() throws Exception { FilteredBean bean = new FilteredBean(); bean.put("a", 4); bean.put("b", 3); FilterProvider prov = new SimpleFilterProvider() .addFilter("filterForMaps", SimpleBeanPropertyFilter.filterOutAllExcept("b")); String json = MAPPER.writer(prov).writeValueAsString(bean); assertEquals(aposToQuotes("{'b':3}"), json); }
// [JACKSON-212] public void testToStringEnum() throws Exception { ObjectMapper m = new ObjectMapper(); m.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true); assertEquals("\"b\"", m.writeValueAsString(LowerCaseEnum.B)); // [databind#749] but should also be able to dynamically disable assertEquals( "\"B\"", m.writer() .without(SerializationFeature.WRITE_ENUMS_USING_TO_STRING) .writeValueAsString(LowerCaseEnum.B)); }
// [Issue#522] public void testMapFilteringWithAnnotations() throws Exception { FilterProvider prov = new SimpleFilterProvider().addFilter("filterX", new MyMapFilter()); String json = MAPPER.writer(prov).writeValueAsString(new MapBean()); // a=1 should become a=2 assertEquals(aposToQuotes("{'values':{'a':2}}"), json); }