/** * jsonToString exclude null data end edit fields * * @param model - model data to String * @return - model data with json format */ public static String jsonToStringIgnoreSpecialFields(RestModel model) { String[] ExcludedFieldsFromView = getExcludedFields(model); ObjectMapper specialMapper = new ObjectMapper(); specialMapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL); String data = null; FilterProvider filters = new SimpleFilterProvider() .addFilter("exclude fields", SimpleBeanPropertyFilter.serializeAllExcept( (ExcludedFieldsFromView))); ObjectWriter writer = specialMapper.writer(filters); try { data = writer.writeValueAsString(model); } catch (IOException e) { log.debug(e.getMessage()); } return data; }
@Test public void testSerializeComment() throws IOException { final Comment aComment = new Comment(); aComment.setContent("<b>There it is</b>"); ByteArrayOutputStream out = new ByteArrayOutputStream(); jsonHelper.withWriter(out, new Writer() { @Override public void writeContents(JsonGenerator generator, ObjectMapper objectMapper) throws JsonGenerationException, JsonMappingException, IOException { FilterProvider fp = new SimpleFilterProvider().addFilter( JacksonHelper.DEFAULT_FILTER_NAME, new ReturnAllBeanProperties()); objectMapper.writer(fp).writeValue(generator, aComment); } }); assertTrue(out.toString().contains("{\"content\":\"<b>There it is</b>\"")); }
@Test public void testNullInComment() throws IOException { final Comment aComment = new Comment(); aComment.setContent(null); ByteArrayOutputStream out = new ByteArrayOutputStream(); jsonHelper.withWriter(out, new Writer() { @Override public void writeContents(JsonGenerator generator, ObjectMapper objectMapper) throws JsonGenerationException, JsonMappingException, IOException { FilterProvider fp = new SimpleFilterProvider().addFilter( JacksonHelper.DEFAULT_FILTER_NAME, new ReturnAllBeanProperties()); objectMapper.writer(fp).writeValue(generator, aComment); } }); assertEquals("Null values should not be output.", "{\"canEdit\":false,\"canDelete\":false}", out.toString()); }
/** * Get an inclusive jackson object writer for writing a set of named fields of an object * * @param includeFieldNames fields that should be serialized * @param filterName filter name. This needs to match the @JsonFilter("filterName") annotation for a bean * * @return a Jackson ObjectWriter * @throws Exception */ public static ObjectWriter getInclusiveObjectWriter(String[] includeFieldNames, String filterName) throws Exception { if (includeFieldNames == null) throw new Exception("includeFieldNames can't be null"); if (filterName == null) throw new Exception("filterName can't be null"); ObjectMapper mapper = new ObjectMapper(); FilterProvider filters = new SimpleFilterProvider() .addFilter(filterName, SimpleBeanPropertyFilter.filterOutAllExcept(includeFieldNames)); return mapper.writer(filters); }
/** * Get an exclusive jackson object writer for serializing an object without a set of named fields * * @param excludeFieldNames fields that should be excluded for serialization * @param filterName filter name. This needs to match the @JsonFilter("filterName") annotation for a bean * * @return a Jackson ObjectWriter * @throws Exception */ public static ObjectWriter getExclusivObjectWriter(String[] excludeFieldNames, String filterName) throws Exception { if (excludeFieldNames == null) throw new Exception("includeFieldNames can't be null"); if (filterName == null) throw new Exception("filterName can't be null"); ObjectMapper mapper = new ObjectMapper(); FilterProvider filters = new SimpleFilterProvider() .addFilter(filterName, SimpleBeanPropertyFilter.serializeAllExcept(excludeFieldNames)); return mapper.writer(filters); }
/** * Serialize to a JSON representation using * {@link ObjectMapper#writeValue(java.io.Writer, Object)} * * @return a JSON representation * @throws IOException */ public String serialize() throws IOException { ObjectMapper mapper = new ObjectMapper(); FilterProvider filters = new SimpleFilterProvider().addFilter( "savedForm1Properties", SimpleBeanPropertyFilter.filterOutAllExcept( "plates", "assayPlateSize", "outputFileName", "outputFormat", "assayPositiveControls", "assayNegativeControls", "assayOtherControls", "libraryControls")); return mapper.writer(filters).writeValueAsString(this); }
@Test public void testSerializeMultipleObjects() throws IOException { final Collection<Comment> allComments = new ArrayList<Comment>(); Comment aComment = new Comment(); aComment.setContent("<b>There it is</b>"); allComments.add(aComment); aComment = new Comment(); aComment.setContent("<p>I agree with the author</p>"); allComments.add(aComment); ByteArrayOutputStream out = new ByteArrayOutputStream(); jsonHelper.withWriter(out, new Writer() { @Override public void writeContents(JsonGenerator generator, ObjectMapper objectMapper) throws JsonGenerationException, JsonMappingException, IOException { FilterProvider fp = new SimpleFilterProvider().addFilter( JacksonHelper.DEFAULT_FILTER_NAME, new ReturnAllBeanProperties()); objectMapper.writer(fp).writeValue(generator, allComments); } }); assertTrue(out.toString().contains("content\":\"<b>There it is</b>")); assertTrue(out.toString().contains("content\":\"<p>I agree with the author</p>")); }
/** * Helper method used to locate filter that is needed, based on filter id * this serializer was constructed with. * * @since 1.7 */ protected BeanPropertyFilter findFilter(SerializerProvider provider) throws JsonMappingException { final Object filterId = _propertyFilterId; FilterProvider filters = provider.getFilterProvider(); // Not ok to miss the provider, if a filter is declared to be needed. if (filters == null) { throw new JsonMappingException("Can not resolve BeanPropertyFilter with id '"+filterId+"'; no FilterProvider configured"); } BeanPropertyFilter filter = filters.findFilter(filterId); // But whether unknown ids are ok just depends on filter provider; if we get null that's fine return filter; }
protected SerializationConfig(SerializationConfig paramSerializationConfig, FilterProvider paramFilterProvider) { super(paramSerializationConfig); this._featureFlags = paramSerializationConfig._featureFlags; this._serializationInclusion = paramSerializationConfig._serializationInclusion; this._serializationView = paramSerializationConfig._serializationView; this._filterProvider = paramFilterProvider; }
/** * Serialize to a JSON representation using * {@link ObjectMapper#writeValue(java.io.Writer, Object)} * * @return a JSON representation * @throws IOException */ public String serialize() throws IOException { ObjectMapper mapper = new ObjectMapper(); FilterProvider filters = new SimpleFilterProvider().addFilter( "savedForm2Properties", SimpleBeanPropertyFilter.filterOutAllExcept("uploadedFilename", "conditions", "replicates", "readoutTypeSelection", "readouts", "collationOrderOrdering")); return mapper.writer(filters).writeValueAsString(this); }
protected BeanPropertyFilter findFilter(SerializerProvider paramSerializerProvider) { Object localObject = this._propertyFilterId; FilterProvider localFilterProvider = paramSerializerProvider.getFilterProvider(); if (localFilterProvider == null) throw new JsonMappingException("Can not resolve BeanPropertyFilter with id '" + localObject + "'; no FilterProvider configured"); return localFilterProvider.findFilter(localObject); }
public FilterProvider getFilterProvider() { return this._filterProvider; }
public SerializationConfig withFilters(FilterProvider paramFilterProvider) { return new SerializationConfig(this, paramFilterProvider); }
public ObjectWriter filteredWriter(FilterProvider paramFilterProvider) { return new ObjectWriter(this, copySerializationConfig().withFilters(paramFilterProvider)); }
public void setFilters(FilterProvider paramFilterProvider) { this._serializationConfig = this._serializationConfig.withFilters(paramFilterProvider); }
public final FilterProvider getFilterProvider() { return this._config.getFilterProvider(); }
public ObjectWriter withFilters(FilterProvider paramFilterProvider) { if (paramFilterProvider == this._config.getFilterProvider()) return this; return new ObjectWriter(this, this._config.withFilters(paramFilterProvider)); }