Java Code Examples for com.fasterxml.jackson.databind.ObjectWriter#with()

The following examples show how to use com.fasterxml.jackson.databind.ObjectWriter#with() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractJacksonFactory.java    From curiostack with MIT License 7 votes vote down vote up
public ObjectWriter newWriter(
    final boolean locationInfo, final boolean properties, final boolean compact) {
  final SimpleFilterProvider filters = new SimpleFilterProvider();
  final Set<String> except = new HashSet<>(4);
  if (!locationInfo) {
    except.add(this.getPropertyNameForSource());
  }
  if (!properties) {
    except.add(this.getPropertyNameForContextMap());
  }
  if (!includeStacktrace) {
    except.add(this.getPropertyNameForStackTrace());
  }
  except.add(this.getPropertyNameForNanoTime());
  filters.addFilter(
      Log4jLogEvent.class.getName(), SimpleBeanPropertyFilter.serializeAllExcept(except));
  final ObjectWriter writer =
      this.newObjectMapper().writer(compact ? this.newCompactPrinter() : this.newPrettyPrinter());
  return writer.with(filters);
}
 
Example 2
Source File: AbstractJackson2View.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Write the actual JSON content to the stream.
 * @param stream the output stream to use
 * @param object the value to be rendered, as returned from {@link #filterModel}
 * @throws IOException if writing failed
 */
protected void writeContent(OutputStream stream, Object object) throws IOException {
	JsonGenerator generator = this.objectMapper.getFactory().createGenerator(stream, this.encoding);
	writePrefix(generator, object);

	Object value = object;
	Class<?> serializationView = null;
	FilterProvider filters = null;

	if (value instanceof MappingJacksonValue) {
		MappingJacksonValue container = (MappingJacksonValue) value;
		value = container.getValue();
		serializationView = container.getSerializationView();
		filters = container.getFilters();
	}

	ObjectWriter objectWriter = (serializationView != null ?
			this.objectMapper.writerWithView(serializationView) : this.objectMapper.writer());
	if (filters != null) {
		objectWriter = objectWriter.with(filters);
	}
	objectWriter.writeValue(generator, value);

	writeSuffix(generator, object);
	generator.flush();
}
 
Example 3
Source File: AbstractJackson2View.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Write the actual JSON content to the stream.
 * @param stream the output stream to use
 * @param object the value to be rendered, as returned from {@link #filterModel}
 * @throws IOException if writing failed
 */
protected void writeContent(OutputStream stream, Object object) throws IOException {
	JsonGenerator generator = this.objectMapper.getFactory().createGenerator(stream, this.encoding);
	writePrefix(generator, object);

	Object value = object;
	Class<?> serializationView = null;
	FilterProvider filters = null;

	if (value instanceof MappingJacksonValue) {
		MappingJacksonValue container = (MappingJacksonValue) value;
		value = container.getValue();
		serializationView = container.getSerializationView();
		filters = container.getFilters();
	}

	ObjectWriter objectWriter = (serializationView != null ?
			this.objectMapper.writerWithView(serializationView) : this.objectMapper.writer());
	if (filters != null) {
		objectWriter = objectWriter.with(filters);
	}
	objectWriter.writeValue(generator, value);

	writeSuffix(generator, object);
	generator.flush();
}
 
Example 4
Source File: ServletResponseResultWriter.java    From endpoints-java with Apache License 2.0 6 votes vote down vote up
public ServletResponseResultWriter(
    HttpServletResponse servletResponse, ApiSerializationConfig serializationConfig,
    boolean prettyPrint, boolean addContentLength) {
  this.servletResponse = servletResponse;
  Set<SimpleModule> modules = new LinkedHashSet<>();
  modules.addAll(WRITER_MODULES);
  ObjectWriter objectWriter = ConfiguredObjectMapper.builder()
      .apiSerializationConfig(serializationConfig)
      .addRegisteredModules(modules)
      .build()
      .writer();

  if (prettyPrint) {
    objectWriter = objectWriter.with(new EndpointsPrettyPrinter());
  }
  this.objectWriter = configureWriter(objectWriter);
  this.addContentLength = addContentLength;
}
 
Example 5
Source File: JacksonRepresentationImpl.java    From ontopia with Apache License 2.0 6 votes vote down vote up
@Override
protected ObjectWriter createObjectWriter() {
	ObjectWriter writer = super.createObjectWriter();

	for (SerializationFeature feature : SerializationFeature.values()) {
		boolean hasDefault = DEFAULT_SERIALIZATION_FEATURES.contains(feature);
		Parameter parameter = ContextUtils.getParameter(ContextUtils.getCurrentApplicationContext(), SERIALIZATION_FEATURE + feature.name());
		if ((parameter != null) || hasDefault) {
			if (ContextUtils.getParameterAsBoolean(parameter, feature.enabledByDefault() || hasDefault)) {
				writer = writer.with(feature);
			} else {
				writer = writer.without(feature);
			}
		}
	}
	return writer;
}
 
Example 6
Source File: AbstractJacksonFactory.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
public ObjectWriter newWriter(final boolean locationInfo, final boolean properties, final boolean compact,
        final boolean includeMillis) {
    final SimpleFilterProvider filters = new SimpleFilterProvider();
    final Set<String> except = new HashSet<>(5);
    if (!locationInfo) {
        except.add(this.getPropertyNameForSource());
    }
    if (!properties) {
        except.add(this.getPropertyNameForContextMap());
    }
    if (!includeStacktrace) {
        except.add(this.getPropertyNameForStackTrace());
    }
    if (includeMillis) {
        except.add(getPropertyNameForInstant());
    } else {
        except.add(getPropertyNameForTimeMillis());
    }
    except.add(this.getPropertyNameForNanoTime());
    filters.addFilter(Log4jLogEvent.class.getName(), SimpleBeanPropertyFilter.serializeAllExcept(except));
    final ObjectWriter writer = this.newObjectMapper()
            .writer(compact ? this.newCompactPrinter() : this.newPrettyPrinter());
    return writer.with(filters);
}
 
Example 7
Source File: Jackson2JsonEncoder.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
protected ObjectWriter customizeWriter(ObjectWriter writer, @Nullable MimeType mimeType,
		ResolvableType elementType, @Nullable Map<String, Object> hints) {

	return (this.ssePrettyPrinter != null &&
			MediaType.TEXT_EVENT_STREAM.isCompatibleWith(mimeType) &&
			writer.getConfig().isEnabled(SerializationFeature.INDENT_OUTPUT) ?
			writer.with(this.ssePrettyPrinter) : writer);
}
 
Example 8
Source File: Jackson2JsonEncoder.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected ObjectWriter customizeWriter(ObjectWriter writer, @Nullable MimeType mimeType,
		ResolvableType elementType, @Nullable Map<String, Object> hints) {

	return (this.ssePrettyPrinter != null &&
			MediaType.TEXT_EVENT_STREAM.isCompatibleWith(mimeType) &&
			writer.getConfig().isEnabled(SerializationFeature.INDENT_OUTPUT) ?
			writer.with(this.ssePrettyPrinter) : writer);
}
 
Example 9
Source File: JacksonFactory.java    From summerframework with Apache License 2.0 5 votes vote down vote up
ObjectWriter newWriter(final boolean locationInfo, final boolean properties, final boolean compact) {
    final SimpleFilterProvider filters = new SimpleFilterProvider();
    final Set<String> except = new HashSet<>(2);
    if (!locationInfo) {
        except.add(this.getPropertNameForSource());
    }
    if (!properties) {
        except.add(this.getPropertNameForContextMap());
    }
    except.add(this.getPropertNameForNanoTime());
    filters.addFilter(Log4jLogEvent.class.getName(), SimpleBeanPropertyFilter.serializeAllExcept(except));
    final ObjectWriter writer =
        this.newObjectMapper().writer(compact ? this.newCompactPrinter() : this.newPrettyPrinter());
    return writer.with(filters);
}
 
Example 10
Source File: ResponseTransformationFilter.java    From minnal with Apache License 2.0 5 votes vote down vote up
@Override
public ObjectWriter modify(EndpointConfigBase<?> endpoint, MultivaluedMap<String, Object> responseHeaders, Object valueToWrite,
		ObjectWriter w, JsonGenerator g) throws IOException {
	SimpleBeanPropertyFilter filter = null;
	if (includes != null && !includes.isEmpty()) {
		filter = new SimpleBeanPropertyFilter.FilterExceptFilter(includes);
	} else if (excludes != null && !excludes.isEmpty()) {
		filter = SimpleBeanPropertyFilter.serializeAllExcept(excludes);
	} else {
		filter = SimpleBeanPropertyFilter.serializeAllExcept(new HashSet<String>());
	}
	FilterProvider provider = new SimpleFilterProvider().addFilter("property_filter", filter);
	return w.with(provider);
}
 
Example 11
Source File: AbstractJackson2HttpMessageConverter.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected void writeInternal(Object object, Type type, HttpOutputMessage outputMessage)
		throws IOException, HttpMessageNotWritableException {

	MediaType contentType = outputMessage.getHeaders().getContentType();
	JsonEncoding encoding = getJsonEncoding(contentType);
	JsonGenerator generator = this.objectMapper.getFactory().createGenerator(outputMessage.getBody(), encoding);
	try {
		writePrefix(generator, object);

		Class<?> serializationView = null;
		FilterProvider filters = null;
		Object value = object;
		JavaType javaType = null;
		if (object instanceof MappingJacksonValue) {
			MappingJacksonValue container = (MappingJacksonValue) object;
			value = container.getValue();
			serializationView = container.getSerializationView();
			filters = container.getFilters();
		}
		if (type != null && value != null && TypeUtils.isAssignable(type, value.getClass())) {
			javaType = getJavaType(type, null);
		}
		ObjectWriter objectWriter;
		if (serializationView != null) {
			objectWriter = this.objectMapper.writerWithView(serializationView);
		}
		else if (filters != null) {
			objectWriter = this.objectMapper.writer(filters);
		}
		else {
			objectWriter = this.objectMapper.writer();
		}
		if (javaType != null && javaType.isContainerType()) {
			objectWriter = objectWriter.forType(javaType);
		}
		SerializationConfig config = objectWriter.getConfig();
		if (contentType != null && contentType.isCompatibleWith(TEXT_EVENT_STREAM) &&
				config.isEnabled(SerializationFeature.INDENT_OUTPUT)) {
			objectWriter = objectWriter.with(this.ssePrettyPrinter);
		}
		objectWriter.writeValue(generator, value);

		writeSuffix(generator, object);
		generator.flush();

	}
	catch (JsonProcessingException ex) {
		throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getOriginalMessage(), ex);
	}
}