/** * 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>")); }
/** * 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); }
/** * Helper, converts an logData object into Json for POSTs */ private static String getLogDataAppJson(LogData logData) { try{ ObjectMapper mapper = new ObjectMapper(); SimpleFilterProvider filters = new SimpleFilterProvider().addFilter("privateView",new ManagedAppTestFilter()); // and then serialize using that filter provider: return mapper.writer(filters).writeValueAsString(logData); } catch(Exception e) { fail("JSON Serialization failed." + e); return null; } }
/** * Helper, Get PiProfile as JSON. * @param profile * @return */ private static String getProfileJson(PiProfile profile) { try{ ObjectMapper mapper = new ObjectMapper(); SimpleFilterProvider filters = new SimpleFilterProvider().addFilter("privateView",new PiProfileTestFilter()); // and then serialize using that filter provider: return mapper.writer(filters).writeValueAsString(profile); } catch(Exception e) { fail("JSON Serialization failed." + e); return null; } }
private static String getManagedAppJson(ManagedApp ma) { try{ ObjectMapper mapper = new ObjectMapper(); SimpleFilterProvider filters = new SimpleFilterProvider().addFilter("privateView",new ManagedAppTestFilter()); // and then serialize using that filter provider: return mapper.writer(filters).writeValueAsString(ma); } catch(Exception e) { fail("JSON Serialization failed." + e); return null; } }
/** * Helper, Get HomePiUser as JSON. * @param user * @return */ private static String getUserJson(HomePiUser user) { try{ ObjectMapper mapper = new ObjectMapper(); SimpleFilterProvider filters = new SimpleFilterProvider().addFilter("privateView",new HomePiUserTestFilter()); // and then serialize using that filter provider: return mapper.writer(filters).writeValueAsString(user); } catch(Exception e) { fail("JSON Serialization failed." + e); return null; } }
private ObjectMapper getConfiguredMapper(){ ObjectMapper mapper = new ObjectMapper(); SerializationConfig serConfig = mapper.getSerializationConfig(); SimpleFilterProvider fp = new SimpleFilterProvider(); fp.addFilter("privateView", new HomePiPrivateFilter()); mapper.setSerializationConfig(serConfig.withFilters(fp)); mapper.setFilters(fp); return mapper; }
public JacksonConfigurator() { SerializationConfig serConfig = mapper.getSerializationConfig(); SimpleFilterProvider fp = new SimpleFilterProvider(); //Add filters here! TODO: Extract to properties and inject fp.addFilter("privateView", new HomePiPrivateFilter()); mapper.setSerializationConfig(serConfig.withFilters(fp)); mapper.setFilters(fp); }