Skip to content

Commit

Permalink
Start working on #1221
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed May 4, 2016
1 parent 12e884a commit 504464d
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 99 deletions.
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@
<!-- and for testing we need a few libraries
libs for which we use reflection for code, but direct dep for testing
-->
<!-- Mock -->

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
Expand Down
60 changes: 15 additions & 45 deletions src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.fasterxml.jackson.databind.node.*;
import com.fasterxml.jackson.databind.ser.*;
import com.fasterxml.jackson.databind.type.*;
import com.fasterxml.jackson.databind.util.ClassUtil;
import com.fasterxml.jackson.databind.util.RootNameLookup;
import com.fasterxml.jackson.databind.util.StdDateFormat;
import com.fasterxml.jackson.databind.util.TokenBuffer;
Expand Down Expand Up @@ -3560,7 +3561,7 @@ protected Object _convert(Object fromValue, JavaType toValueType)
} else { // pointing to event other than null
DeserializationContext ctxt = createDeserializationContext(jp, deserConfig);
JsonDeserializer<Object> deser = _findRootDeserializer(ctxt, toValueType);
// note: no handling of unwarpping
// note: no handling of unwrapping
result = deser.deserialize(jp, ctxt);
}
jp.close();
Expand Down Expand Up @@ -3657,25 +3658,13 @@ protected final void _configAndWriteValue(JsonGenerator g, Object value)
_configAndWriteCloseable(g, value, cfg);
return;
}
boolean closed = false;
try {
_serializerProvider(cfg).serializeValue(g, value);
closed = true;
g.close();
} finally {
/* won't try to close twice; also, must catch exception (so it
* will not mask exception that is pending)
*/
if (!closed) {
/* 04-Mar-2014, tatu: But! 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 (IOException ioe) { }
}
} catch (Exception e) {
ClassUtil.closeOnFailAndThrowAsIAE(g, e);
return;
}
g.close();
}

/**
Expand All @@ -3688,30 +3677,16 @@ private final void _configAndWriteCloseable(JsonGenerator g, Object value, Seria
Closeable toClose = (Closeable) value;
try {
_serializerProvider(cfg).serializeValue(g, value);
JsonGenerator tmpGen = g;
g = null;
tmpGen.close();
Closeable tmpToClose = toClose;
toClose = null;
tmpToClose.close();
} finally {
// Need to close both generator and value, as long as they haven't yet been closed
if (g != null) {
// 04-Mar-2014, tatu: But! 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 (IOException ioe) { }
}
if (toClose != null) {
try {
toClose.close();
} catch (IOException ioe) { }
}
} catch (Exception e) {
ClassUtil.closeOnFailAndThrowAsIAE(g, toClose, e);
return;
}
g.close();
}

/**
* Helper method used when value to serialize is {@link Closeable} and its <code>close()</code>
* method is to be called right after serialization has been called
Expand All @@ -3725,16 +3700,11 @@ private final void _writeCloseableValue(JsonGenerator g, Object value, Serializa
if (cfg.isEnabled(SerializationFeature.FLUSH_AFTER_WRITE_VALUE)) {
g.flush();
}
Closeable tmpToClose = toClose;
toClose = null;
tmpToClose.close();
} finally {
if (toClose != null) {
try {
toClose.close();
} catch (IOException ioe) { }
}
} catch (Exception e) {
ClassUtil.closeOnFailAndThrowAsIAE(null, toClose, e);
return;
}
toClose.close();
}

/*
Expand Down
61 changes: 14 additions & 47 deletions src/main/java/com/fasterxml/jackson/databind/ObjectWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.fasterxml.jackson.databind.ser.*;
import com.fasterxml.jackson.databind.ser.impl.TypeWrappedSerializer;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.databind.util.ClassUtil;

/**
* Builder object that can be used for per-serialization configuration of
Expand Down Expand Up @@ -903,8 +904,7 @@ public ContextAttributes getAttributes() {
* Method that can be used to serialize any Java value as
* JSON output, using provided {@link JsonGenerator}.
*/
public void writeValue(JsonGenerator gen, Object value)
throws IOException, JsonGenerationException, JsonMappingException
public void writeValue(JsonGenerator gen, Object value) throws IOException
{
_configureGenerator(gen);
if (_config.isEnabled(SerializationFeature.CLOSE_CLOSEABLE)
Expand All @@ -916,16 +916,11 @@ public void writeValue(JsonGenerator gen, Object value)
if (_config.isEnabled(SerializationFeature.FLUSH_AFTER_WRITE_VALUE)) {
gen.flush();
}
Closeable tmpToClose = toClose;
toClose = null;
tmpToClose.close();
} finally {
if (toClose != null) {
try {
toClose.close();
} catch (IOException ioe) { }
}
} catch (Exception e) {
ClassUtil.closeOnFailAndThrowAsIAE(null, toClose, e);
return;
}
toClose.close();
} else {
_prefetch.serialize(gen, value, _serializerProvider());
if (_config.isEnabled(SerializationFeature.FLUSH_AFTER_WRITE_VALUE)) {
Expand Down Expand Up @@ -1124,24 +1119,13 @@ protected final void _configAndWriteValue(JsonGenerator gen, Object value) throw
_writeCloseable(gen, value);
return;
}
boolean closed = false;
try {
_prefetch.serialize(gen, value, _serializerProvider());
closed = true;
gen.close();
} finally {
// won't try to close twice; also, must catch exception (so it
// will not mask exception that is pending)
if (!closed) {
/* 04-Mar-2014, tatu: But! Let's try to prevent auto-closing of
* structures, which typically causes more damage.
*/
gen.disable(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT);
try {
gen.close();
} catch (IOException ioe) { }
}
} catch (Exception e) {
ClassUtil.closeOnFailAndThrowAsIAE(gen, e);
return;
}
gen.close();
}

/**
Expand All @@ -1154,31 +1138,14 @@ private final void _writeCloseable(JsonGenerator gen, Object value)
Closeable toClose = (Closeable) value;
try {
_prefetch.serialize(gen, value, _serializerProvider());
JsonGenerator tmpGen = gen;
gen = null;
tmpGen.close();
Closeable tmpToClose = toClose;
toClose = null;
tmpToClose.close();
} finally {
/* Need to close both generator and value, as long as they haven't yet
* been closed
*/
if (gen != null) {
/* 04-Mar-2014, tatu: But! Let's try to prevent auto-closing of
* structures, which typically causes more damage.
*/
gen.disable(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT);
try {
gen.close();
} catch (IOException ioe) { }
}
if (toClose != null) {
try {
toClose.close();
} catch (IOException ioe) { }
}
} catch (Exception e) {
ClassUtil.closeOnFailAndThrowAsIAE(gen, toClose, e);
return;
}
gen.close();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.fasterxml.jackson.databind.PropertyName;
import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.introspect.AnnotatedParameter;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;

/**
* To support Java7-incomplete platforms, we will offer support for JDK 7
Expand All @@ -24,7 +23,7 @@ public abstract class Java7Support
impl = (Java7Support) cls.newInstance();
} catch (Throwable t) {
// 24-Nov-2015, tatu: Should we log or not?
java.util.logging.Logger.getLogger(JacksonAnnotationIntrospector.class.getName())
java.util.logging.Logger.getLogger(Java7Support.class.getName())
.warning("Unable to load JDK7 types (annotations, java.nio.file.Path): no Java7 support added");
}
IMPL = impl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import com.fasterxml.jackson.databind.introspect.AnnotatedParameter;
import com.fasterxml.jackson.databind.introspect.AnnotatedWithParams;

/**
* @since 2.8
*/
public class Java7SupportImpl extends Java7Support
{
@SuppressWarnings("unused") // compiler warns, just needed side-effects
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ public JsonSerializer<?> createContextual(SerializerProvider serializers,
BeanProperty property) throws JsonMappingException
{
if (property != null) {
JsonFormat.Value format = findFormatOverrides(serializers, property,
handledType());
JsonFormat.Value format = findFormatOverrides(serializers, property, handledType());
if (format != null) {
// Simple case first: serialize as numeric timestamp?
JsonFormat.Shape shape = format.getShape();
Expand Down
70 changes: 70 additions & 0 deletions src/main/java/com/fasterxml/jackson/databind/util/ClassUtil.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.fasterxml.jackson.databind.util;

import java.io.Closeable;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.*;
import java.util.*;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;

Expand Down Expand Up @@ -567,6 +570,73 @@ public static void unwrapAndThrowAsIAE(Throwable t, String msg)
throwAsIAE(getRootCause(t), msg);
}

/**
* 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 closeOnFailAndThrowAsIAE(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);
}
if (fail instanceof IOException) {
throw (IOException) fail;
}
if (fail instanceof RuntimeException) {
throw (RuntimeException) fail;
}
throw new RuntimeException(fail);
}

/**
* Helper method that encapsulate logic in trying to close given {@link Closeable}
* 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 closeOnFailAndThrowAsIAE(JsonGenerator g,
Closeable toClose, Exception fail)
throws IOException
{
if (g != null) {
g.disable(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT);
try {
g.close();
} catch (Exception e) {
fail.addSuppressed(e);
}
}
if (toClose != null) {
try {
toClose.close();
} catch (Exception e) {
fail.addSuppressed(e);
}
}
if (fail instanceof IOException) {
throw (IOException) fail;
}
if (fail instanceof RuntimeException) {
throw (RuntimeException) fail;
}
throw new RuntimeException(fail);
}

/*
/**********************************************************
/* Instantiation
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.fasterxml.jackson.databind.creators;

import java.awt.Point; // just for convenience
import java.math.BigDecimal;
import java.util.*;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.BaseMapTest.Point;

public class IgnorePropsForSerTest
extends BaseMapTest
Expand Down

0 comments on commit 504464d

Please sign in to comment.