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 static String toJson(Object value) { try { return OBJECT_MAPPER.writer().writeValueAsString(value); } catch (JsonProcessingException e) { throw new IllegalArgumentException("Unable to serialize to json", e); } }
protected EndpointConfig initWriter(ObjectMapper mapper) { // first common config if (_activeView != null) { _writer = mapper.writerWithView(_activeView); } else { _writer = mapper.writer(); } if (_rootName != null) { _writer = _writer.withRootName(_rootName); } // Then features if (_serEnable != null) { _writer = _writer.withFeatures(_serEnable); } if (_serDisable != null) { _writer = _writer.withoutFeatures(_serDisable); } // then others // Finally: couple of features we always set /* Important: we are NOT to close the underlying stream after * mapping, so we need to instruct parser: */ _writer.getJsonFactory().disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET); return this; }
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); }