下面列出了com.fasterxml.jackson.core.JsonGenerator#disable ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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);
}
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) {}
}
}
}
protected JsonGenerator createGenerator(OutputStream output) throws IOException {
JsonGenerator generator = jsonFactory.createJsonGenerator(output, JsonEncoding.UTF8);
generator.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
return generator;
}