Java Code Examples for com.fasterxml.jackson.core.JsonGenerator#disable()

The following examples show how to use com.fasterxml.jackson.core.JsonGenerator#disable() . 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: ClassUtil.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Helper method that encapsulate logic in trying to close output generator
 * in case of failure; useful mostly in forcing flush()ing as otherwise
 * error conditions tend to be hard to diagnose. However, it is often the
 * case that output state may be corrupt so we need to be prepared for
 * secondary exception without masking original one.
 *
 * @since 2.8
 */
public static void closeOnFailAndThrowAsIOE(JsonGenerator g, Exception fail)
    throws IOException
{
    /* 04-Mar-2014, tatu: Let's try to prevent auto-closing of
     *    structures, which typically causes more damage.
     */
    g.disable(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT);
    try {
        g.close();
    } catch (Exception e) {
        fail.addSuppressed(e);
    }
    throwIfIOE(fail);
    throwIfRTE(fail);
    throw new RuntimeException(fail);
}
 
Example 2
Source File: PropertyFilteringMessageBodyWriter.java    From jackson-jaxrs-propertyfiltering with Apache License 2.0 6 votes vote down vote up
private void writeValue(ObjectWriter writer, PropertyFilter filter, Object value, OutputStream outputStream) throws IOException {
  JsonGenerator generator = writer.getFactory().createGenerator(outputStream);
  // Important: we are NOT to close the underlying stream after
  // mapping, so we need to instruct generator
  generator.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
  generator = new PropertyFilteringJsonGenerator(generator, filter);

  boolean ok = false;

  try {
    writer.writeValue(generator, value);
    ok = true;
  } finally {
    if (ok) {
      generator.close();
    } else {
      try {
        generator.close();
      } catch (Exception ignored) {}
    }
  }
}
 
Example 3
Source File: JsonJacksonFormat.java    From jigsaw-payment with Apache License 2.0 4 votes vote down vote up
protected JsonGenerator createGenerator(OutputStream output) throws IOException {
	JsonGenerator generator = jsonFactory.createJsonGenerator(output, JsonEncoding.UTF8);
	generator.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
	return generator;
}