Skip to content

Commit

Permalink
Fix #1195
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Apr 14, 2016
1 parent 9e2f76e commit eae6759
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 3 deletions.
2 changes: 2 additions & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Project: jackson-databind
(reported by carrino@github)
#1191: Non-matching quotes used in error message for date parsing
#1194: Incorrect signature for generic type via `JavaType.getGenericSignature
#1195: `JsonMappingException` not Serializable due to 2.7 reference to source (parser)
(reported by mjustin@github)
#1198: Problem with `@JsonTypeInfo.As.EXTERNAL_PROPERTY`, `defaultImpl`, missing type id, NPE
- Improve handling of custom content (de)serializers for `AtomicReference`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ public Reference(Object from, int index) {
_index = index;
}

private Reference(Reference src, Object newFrom) {
_from = newFrom;
_fieldName = src._fieldName;
_index = src._index;
}

public void setFrom(Object o) { _from = o; }
public void setFieldName(String n) { _fieldName = n; }
public void setIndex(int ix) { _index = ix; }
Expand Down Expand Up @@ -124,7 +130,24 @@ public Reference(Object from, int index) {
sb.append(']');
return sb.toString();
}
}

/**
* May need some cleaning here, given that `from` may or may not be serializable.
*
* since 2.7.4
*/
Object writeReplace() {
// as per [databind#1195], reference may cause trouble, if non-serializable
// instance. What to replace it with is trickier; Class is most natural, but
// would recipient have that available? Assume this is the case, for now, because
//
if ((_from != null) && !(_from instanceof Serializable)) {
Object from = _from.getClass();
return new Reference(this, from);
}
return this;
}
}

/*
/**********************************************************
Expand All @@ -141,10 +164,12 @@ public Reference(Object from, int index) {
/**
* Underlying processor ({@link JsonParser} or {@link JsonGenerator}),
* if known.
*<p>
* NOTE: typically not serializable hence <code>transient</code>
*
* @since 2.7
*/
protected Closeable _processor;
protected transient Closeable _processor;

/*
/**********************************************************
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,5 +194,4 @@ protected <T> T jdkDeserialize(byte[] raw) throws IOException
objIn.close();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.fasterxml.jackson.databind.interop;

import java.io.*;

import com.fasterxml.jackson.databind.*;

public class ExceptionSerializableTest1195 extends BaseMapTest
{
abstract static class ClassToRead {
public int x;
}

static class ContainerClassToRead {
public ClassToRead classToRead;
}

public void testExceptionSerializability() throws Exception
{
final ObjectMapper mapper = new ObjectMapper();
try {
mapper.readValue("{\"type\": \"B\"}", ClassToRead.class);
fail("Should not have passed");
} catch (JsonMappingException e) {
ObjectOutputStream stream = new ObjectOutputStream(new ByteArrayOutputStream());
try {
stream.writeObject(e);
stream.close();
} catch (Exception e2) {
fail("Failed to serialize "+e.getClass().getName()+": "+e2);
}
}
try {
mapper.readValue("{\"classToRead\": {\"type\": \"B\"}}", ContainerClassToRead.class);
fail("Should not have passed");
} catch (JsonMappingException e) {
ObjectOutputStream stream = new ObjectOutputStream(new ByteArrayOutputStream());
try {
stream.writeObject(e);
stream.close();
} catch (Exception e2) {
fail("Failed to serialize "+e.getClass().getName()+": "+e2);
}
}
}
}

0 comments on commit eae6759

Please sign in to comment.