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; }
@Test public void jsonview_persistence_should_manage_all_properties() throws Exception { Page myPage = createAFilledPage(); // We serialize and deserialize our object Page pageAfterJsonProcessing = objectMapper.readValue( objectMapper.writerWithView(JsonViewPersistence.class).writeValueAsString(myPage), Page.class); assertThat(pageAfterJsonProcessing.getName()).isEqualTo(myPage.getName()); assertThat(pageAfterJsonProcessing.getId()).isEqualTo(myPage.getId()); assertThat(pageAfterJsonProcessing.getData()).isNotEmpty(); assertThat(pageAfterJsonProcessing.getRows()).isNotEmpty(); // A rows contains a list of elements. We verify the first Element element = pageAfterJsonProcessing.getRows().get(0).get(0); assertThat(element.getPropertyValues()).isNotNull(); assertThat(element.getDimension().get("xs")).isEqualTo(12); }
@Test public void jsonview_light_should_only_manage_id_and_name() throws Exception { String json = objectMapper.writerWithView(JsonViewLight.class).writeValueAsString(createAFilledPage()); assertThat(json).isEqualTo("{\"id\":\"UUID\",\"name\":\"myPage\",\"type\":\"page\"}"); }
@SuppressWarnings("resource") public void writeJsonResponse( HttpServletResponse response, Object responseObject, Class<?> jsonView, boolean streamResponse, boolean isMultipart) throws IOException, JsonGenerationException, JsonMappingException { ObjectMapper objectMapper = configurationService.getJsonHandler().getMapper(); if (isMultipart) { response.setContentType(RouterController.TEXT_HTML.toString()); response.setCharacterEncoding(RouterController.TEXT_HTML.getCharSet().name()); ByteArrayOutputStream bos = new ByteArrayOutputStream(1024); bos.write("<html><body><textarea>".getBytes(ExtDirectSpringUtil.UTF8_CHARSET)); String responseJson; if (jsonView == null) { responseJson = objectMapper.writeValueAsString(responseObject); } else { responseJson = objectMapper.writerWithView(jsonView).writeValueAsString(responseObject); } responseJson = responseJson.replace(""", "\\""); bos.write(responseJson.getBytes(ExtDirectSpringUtil.UTF8_CHARSET)); bos.write("</textarea></body></html>".getBytes(ExtDirectSpringUtil.UTF8_CHARSET)); response.setContentLength(bos.size()); FileCopyUtils.copy(bos.toByteArray(), response.getOutputStream()); } else { response.setContentType(APPLICATION_JSON.toString()); response.setCharacterEncoding(APPLICATION_JSON.getCharSet().name()); ServletOutputStream outputStream = response.getOutputStream(); if (!streamResponse) { ByteArrayOutputStream bos = new ByteArrayOutputStream(1024); JsonGenerator jsonGenerator = objectMapper.getFactory().createJsonGenerator(bos, JsonEncoding.UTF8); if (jsonView == null) { objectMapper.writeValue(jsonGenerator, responseObject); } else { objectMapper.writerWithView(jsonView).writeValue(jsonGenerator, responseObject); } response.setContentLength(bos.size()); outputStream.write(bos.toByteArray()); jsonGenerator.close(); } else { JsonGenerator jsonGenerator = objectMapper.getFactory().createJsonGenerator(outputStream, JsonEncoding.UTF8); if (jsonView == null) { objectMapper.writeValue(jsonGenerator, responseObject); } else { objectMapper.writerWithView(jsonView).writeValue(jsonGenerator, responseObject); } jsonGenerator.close(); } outputStream.flush(); } }