@SuppressWarnings("unchecked") protected THIS initWriter(ObjectWriter writer) { if (_activeView != null) { writer = writer.withView(_activeView); } if (_rootName != null) { writer = writer.withRootName(_rootName); } // Then features if (_serEnable != null) { writer = writer.withFeatures(_serEnable); } if (_serDisable != null) { writer = writer.withoutFeatures(_serDisable); } _writer = writer; return (THIS) this; }
public void setFormattedResponseData(String data) throws Exception { this.formattedResponseData = data; if (data != null && !data.equals("") && responseContentType != null && responseContentType.toLowerCase().indexOf("application/json") != -1) { // try to format it try { ObjectMapper objectMapper = new ObjectMapper(); ObjectWriter writer = objectMapper.writerWithDefaultPrettyPrinter(); Object json = objectMapper.readValue(data, Object.class); this.formattedResponseData = writer.withView(ViewFilters.Default.class).writeValueAsString(json); } catch (JsonParseException jpe) { // nothing to do here as this.formattedResponseData was already set to the appropriate data } } }
public void setFormattedOriginalResponseData(String originalResponseData) throws Exception { this.formattedOriginalResponseData = originalResponseData; if (originalResponseData != null && !originalResponseData.equals("") && originalResponseContentType != null && originalResponseContentType.toLowerCase().indexOf("application/json") != -1) { try { // try to format it ObjectMapper objectMapper = new ObjectMapper(); ObjectWriter writer = objectMapper.writerWithDefaultPrettyPrinter(); Object json = objectMapper.readValue(originalResponseData, Object.class); this.formattedOriginalResponseData = writer.withView(ViewFilters.Default.class).writeValueAsString(json); } catch (JsonParseException jpe) { // nothing to do here as this.formattedResponseData was already set to the appropriate data } } }
@Provides @Named(WRITER_NAME) public ObjectWriter objectWriter(@Named(FrontObjectMapperFactory.MAPPER_NAME) final ObjectMapper mapper, AppSettings appSettings) { ObjectWriter objectWriter = RestxContext.Modes.PROD.equals(appSettings.mode()) ? mapper.writer() : mapper.writerWithDefaultPrettyPrinter(); return objectWriter.withView(Views.Transient.class); }
@Provides @Named(WRITER_NAME) public ObjectWriter objectWriter(@Named(FrontObjectMapperFactory.MAPPER_NAME) final ObjectMapper mapper, AppSettings appSettings) { ObjectWriter objectWriter = RestxContext.Modes.PROD.equals(appSettings.mode()) ? mapper.writer() : mapper.writerWithDefaultPrettyPrinter(); return objectWriter.withView(Views.Transient.class); }
public static <T> String write(T object, Class<?> view) { try { ObjectMapper mapper = getObjectMapper(); ObjectWriter objectWriter = mapper.writerWithDefaultPrettyPrinter(); if (view != null) { objectWriter = objectWriter.withView(view); } return objectWriter.writeValueAsString(object); } catch (IOException ioe) { return throwUnchecked(ioe, String.class); } }
@Override public <T> Optional<? extends EntityResponseWriter<T>> mayBuildFor(Type valueType, String contentType) { if (!contentType.toLowerCase(Locale.ENGLISH).startsWith("application/json")) { return Optional.absent(); } Class<?> clazz = getCTJacksonViewClass(valueType, contentType, Views.Transient.class); ObjectWriter writer = objectWriter.withView(clazz); if (valueType instanceof ParameterizedType) { /* we set the type on writer only for parameterized types: * if we set it for regular types, jackson will build the serializer based on this type, and not the * instance type, making polymorphic returns broken. * For parameterized types it helps Jackson which otherwise tries to guess the serializer based on * value.getClass() which doesn't return type parameters information due to Java erasure. */ writer = writer.withType(TypeFactory.defaultInstance().constructType(valueType)); } return Optional.of(JsonEntityResponseWriter.<T>using(valueType, writer)); } };
@Override public <T> Optional<? extends EntityResponseWriter<T>> mayBuildFor(Type valueType, String contentType) { if (!contentType.toLowerCase(Locale.ENGLISH).startsWith("application/json")) { return Optional.absent(); } Class<?> clazz = getCTJacksonViewClass(valueType, contentType, Views.Transient.class); ObjectWriter writer = objectWriter.withView(clazz); if (valueType instanceof ParameterizedType) { /* we set the type on writer only for parameterized types: * if we set it for regular types, jackson will build the serializer based on this type, and not the * instance type, making polymorphic returns broken. * For parameterized types it helps Jackson which otherwise tries to guess the serializer based on * value.getClass() which doesn't return type parameters information due to Java erasure. */ writer = writer.withType(TypeFactory.defaultInstance().constructType(valueType)); } return Optional.of(JsonEntityResponseWriter.<T>using(valueType, writer)); } };
private ObjectWriter getObjectWriter(ResolvableType type) { ObjectWriter objectWriter = this.objectMapper.writerFor(getType(type)); if (this.view != null) { return objectWriter.withView(this.view); } return objectWriter; }
@SuppressWarnings("unchecked") protected THIS initWriter(ObjectWriter writer) { if (_activeView != null) { writer = writer.withView(_activeView); } if (_rootName != null) { writer = writer.withRootName(_rootName); } // Then features if (_serEnable != null) { writer = writer.withFeatures(_serEnable); } if (_serDisable != null) { writer = writer.withoutFeatures(_serDisable); } _writer = writer; return (THIS) this; }
@SuppressWarnings("unchecked") protected THIS initWriter(ObjectWriter writer) { if (_activeView != null) { writer = writer.withView(_activeView); } if (_rootName != null) { writer = writer.withRootName(_rootName); } // Then features if (_serEnable != null) { writer = writer.withFeatures(_serEnable); } if (_serDisable != null) { writer = writer.withoutFeatures(_serDisable); } _writer = writer; return (THIS) this; }
@SuppressWarnings("unchecked") protected THIS initWriter(ObjectWriter writer) { if (_activeView != null) { writer = writer.withView(_activeView); } if (_rootName != null) { writer = writer.withRootName(_rootName); } // Then features if (_serEnable != null) { writer = writer.withFeatures(_serEnable); } if (_serDisable != null) { writer = writer.withoutFeatures(_serDisable); } _writer = writer; return (THIS) this; }
@SuppressWarnings("unchecked") protected THIS initWriter(ObjectWriter writer) { if (_activeView != null) { writer = writer.withView(_activeView); } if (_rootName != null) { writer = writer.withRootName(_rootName); } // Then features if (_serEnable != null) { writer = writer.withFeatures(_serEnable); } if (_serDisable != null) { writer = writer.withoutFeatures(_serDisable); } _writer = writer; return (THIS) this; }
@SuppressWarnings("unchecked") protected THIS initWriter(ObjectWriter writer) { if (_activeView != null) { writer = writer.withView(_activeView); } if (_rootName != null) { writer = writer.withRootName(_rootName); } // Then features if (_serEnable != null) { writer = writer.withFeatures(_serEnable); } if (_serDisable != null) { writer = writer.withoutFeatures(_serDisable); } _writer = writer; return (THIS) this; }
Object rawContent = ((DocumentRequest) request).getRawContent(); if (rawContent instanceof String) { writer = writer.withView(DocumentRequestMixin.Views.Content.class); } else if (rawContent != null) { notPlainText = true;
@Override public void render(Context ctx, JsonRender object) throws Exception { ObjectWriter writer = object.getObjectWriter(); if (writer == null) { writer = ctx.maybeGet(ObjectWriter.class) .orElseGet(() -> ctx.get(ObjectMapper.class).writer()); } Class<?> viewClass = object.getViewClass(); if (viewClass != null) { writer = writer.withView(viewClass); } ByteBuf buffer = ctx.get(ByteBufAllocator.class).buffer(); OutputStream outputStream = new ByteBufOutputStream(buffer); try { writer.writeValue(outputStream, object.getObject()); } catch (JsonProcessingException e) { buffer.release(); ctx.error(e); return; } ctx.getResponse() .contentTypeIfNotSet(HttpHeaderConstants.JSON) .send(buffer); }
if (outputConfig.isDebug()) { configObjectString = mapper.writerWithDefaultPrettyPrinter() .withView(jsonView).writeValueAsString(modelObject);
public ObjectWriter modify(EndpointConfigBase<?> endpoint, MultivaluedMap<String, Object> responseHeaders, Object valueToWrite, ObjectWriter w, JsonGenerator g) throws IOException { return w.withView(view);