Skip to content

Commit 2ea6e40

Browse files
committed
Fix #899
1 parent eda66cc commit 2ea6e40

12 files changed

+124
-57
lines changed

release-notes/VERSION

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ Project: jackson-databind
44
=== Releases ===
55
------------------------------------------------------------------------
66

7+
2.6.2 (not yet released)
8+
9+
#899: Problem serializing `ObjectReader` (and possibly `ObjectMapper`)
10+
711
2.6.1 (09-Aug-2015)
812

913
#873: Add missing OSGi import

src/main/java/com/fasterxml/jackson/databind/ser/BeanPropertyWriter.java

+87-50
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,12 @@
3636
*/
3737
@JacksonStdImpl // since 2.6. NOTE: sub-classes typically are not
3838
public class BeanPropertyWriter extends PropertyWriter
39-
implements BeanProperty
39+
implements BeanProperty,
40+
java.io.Serializable // since 2.6.2
4041
{
42+
// as of 2.6.2
43+
private static final long serialVersionUID = 4603296144163950020L;
44+
4145
/**
4246
* Marker object used to indicate "do not serialize if empty"
4347
*/
@@ -50,54 +54,7 @@ public class BeanPropertyWriter extends PropertyWriter
5054
* @since 2.6
5155
*/
5256
protected final static JsonFormat.Value NO_FORMAT = new JsonFormat.Value();
53-
54-
/*
55-
/**********************************************************
56-
/* Settings for accessing property value to serialize
57-
/**********************************************************
58-
*/
59-
60-
/**
61-
* Member (field, method) that represents property and allows access
62-
* to associated annotations.
63-
*/
64-
protected final AnnotatedMember _member;
65-
66-
/**
67-
* Annotations from context (most often, class that declares property,
68-
* or in case of sub-class serializer, from that sub-class)
69-
*/
70-
protected final Annotations _contextAnnotations;
71-
72-
/**
73-
* Type property is declared to have, either in class definition
74-
* or associated annotations.
75-
*/
76-
protected final JavaType _declaredType;
77-
78-
/**
79-
* Accessor method used to get property value, for
80-
* method-accessible properties.
81-
* Null if and only if {@link #_field} is null.
82-
*/
83-
protected final Method _accessorMethod;
84-
85-
/**
86-
* Field that contains the property value for field-accessible
87-
* properties.
88-
* Null if and only if {@link #_accessorMethod} is null.
89-
*/
90-
protected final Field _field;
91-
92-
/*
93-
/**********************************************************
94-
/* Opaque internal data that bean serializer factory and
95-
/* bean serializers can add.
96-
/**********************************************************
97-
*/
9857

99-
protected HashMap<Object,Object> _internalSettings;
100-
10158
/*
10259
/**********************************************************
10360
/* Basic property metadata: name, type, other
@@ -122,6 +79,12 @@ public class BeanPropertyWriter extends PropertyWriter
12279
*/
12380
protected final PropertyName _wrapperName;
12481

82+
/**
83+
* Type property is declared to have, either in class definition
84+
* or associated annotations.
85+
*/
86+
protected final JavaType _declaredType;
87+
12588
/**
12689
* Type to use for locating serializer; normally same as return
12790
* type of the accessor method, but may be overridden by annotations.
@@ -137,6 +100,15 @@ public class BeanPropertyWriter extends PropertyWriter
137100
*/
138101
protected JavaType _nonTrivialBaseType;
139102

103+
/**
104+
* Annotations from context (most often, class that declares property,
105+
* or in case of sub-class serializer, from that sub-class)
106+
*<p>
107+
* NOTE: transient just to support JDK serializability; Annotations
108+
* do not serialize. At all.
109+
*/
110+
protected final transient Annotations _contextAnnotations;
111+
140112
/**
141113
* Additional information about property
142114
*
@@ -151,6 +123,36 @@ public class BeanPropertyWriter extends PropertyWriter
151123
*/
152124
protected transient JsonFormat.Value _format;
153125

126+
/*
127+
/**********************************************************
128+
/* Settings for accessing property value to serialize
129+
/**********************************************************
130+
*/
131+
132+
/**
133+
* Member (field, method) that represents property and allows access
134+
* to associated annotations.
135+
*/
136+
protected final AnnotatedMember _member;
137+
138+
/**
139+
* Accessor method used to get property value, for
140+
* method-accessible properties.
141+
* Null if and only if {@link #_field} is null.
142+
*<p>
143+
* `transient` (and non-final) only to support JDK serializability.
144+
*/
145+
protected transient Method _accessorMethod;
146+
147+
/**
148+
* Field that contains the property value for field-accessible
149+
* properties.
150+
* Null if and only if {@link #_accessorMethod} is null.
151+
*<p>
152+
* `transient` (and non-final) only to support JDK serializability.
153+
*/
154+
protected transient Field _field;
155+
154156
/*
155157
/**********************************************************
156158
/* Serializers needed
@@ -211,6 +213,15 @@ public class BeanPropertyWriter extends PropertyWriter
211213
*/
212214
protected final Class<?>[] _includeInViews;
213215

216+
/*
217+
/**********************************************************
218+
/* Opaque internal data that bean serializer factory and
219+
/* bean serializers can add.
220+
/**********************************************************
221+
*/
222+
223+
protected transient HashMap<Object,Object> _internalSettings;
224+
214225
/*
215226
/**********************************************************
216227
/* Construction, configuration
@@ -301,16 +312,18 @@ protected BeanPropertyWriter(BeanPropertyWriter base, PropertyName name)
301312
/* 02-Dec-2014, tatu: This is a big mess, alas, what with dependency
302313
* to MapperConfig to encode, and Afterburner having heartburn
303314
* for SerializableString (vs SerializedString).
304-
* Hope it can be resolved/reworker in 2.6 timeframe, if not for 2.5
315+
* Hope it can be resolved/reworked in 2.6 timeframe, if not for 2.5
305316
*/
306317
_name = new SerializedString(name.getSimpleName());
307318
_wrapperName = base._wrapperName;
308319

309-
_member = base._member;
310320
_contextAnnotations = base._contextAnnotations;
311321
_declaredType = base._declaredType;
322+
323+
_member = base._member;
312324
_accessorMethod = base._accessorMethod;
313325
_field = base._field;
326+
314327
_serializer = base._serializer;
315328
_nullSerializer = base._nullSerializer;
316329
// one more thing: copy internal settings, if any (since 1.7)
@@ -421,6 +434,30 @@ public void setNonTrivialBaseType(JavaType t) {
421434
_nonTrivialBaseType = t;
422435
}
423436

437+
/*
438+
/**********************************************************
439+
/* JDK Serializability
440+
/**********************************************************
441+
*/
442+
443+
/* Ideally would not require mutable state, and instead would re-create with
444+
* final settings. However, as things are, with sub-types and all, simplest
445+
* to just change Field/Method value directly.
446+
*/
447+
Object readResolve() {
448+
if (_member instanceof AnnotatedField) {
449+
_accessorMethod = null;
450+
_field = (Field) _member.getMember();
451+
} else if (_member instanceof AnnotatedMethod) {
452+
_accessorMethod = (Method) _member.getMember();
453+
_field = null;
454+
}
455+
if (_serializer == null) {
456+
_dynamicSerializers = PropertySerializerMap.emptyForProperties();
457+
}
458+
return this;
459+
}
460+
424461
/*
425462
/**********************************************************
426463
/* BeanProperty impl

src/main/java/com/fasterxml/jackson/databind/ser/BeanSerializer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
public class BeanSerializer
2727
extends BeanSerializerBase
2828
{
29-
private static final long serialVersionUID = -4536893235025590367L;
29+
private static final long serialVersionUID = -3618164443537292758L;
3030

3131
/*
3232
/**********************************************************

src/main/java/com/fasterxml/jackson/databind/ser/VirtualBeanPropertyWriter.java

+3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
*/
2323
public abstract class VirtualBeanPropertyWriter
2424
extends BeanPropertyWriter
25+
implements java.io.Serializable
2526
{
27+
private static final long serialVersionUID = 1L;
28+
2629
/**
2730
* Constructor used by most sub-types.
2831
*/

src/main/java/com/fasterxml/jackson/databind/ser/impl/AttributePropertyWriter.java

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
public class AttributePropertyWriter
2121
extends VirtualBeanPropertyWriter
2222
{
23+
private static final long serialVersionUID = 1;
24+
2325
protected final String _attrName;
2426

2527
/*

src/main/java/com/fasterxml/jackson/databind/ser/impl/FilteredBeanPropertyWriter.java

+6
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ public static BeanPropertyWriter constructViewBased(BeanPropertyWriter base, Cla
2828

2929
private final static class SingleView
3030
extends BeanPropertyWriter
31+
implements java.io.Serializable
3132
{
33+
private static final long serialVersionUID = 1L;
34+
3235
protected final BeanPropertyWriter _delegate;
3336

3437
protected final Class<?> _view;
@@ -82,7 +85,10 @@ public void serializeAsElement(Object bean, JsonGenerator jgen, SerializerProvid
8285

8386
private final static class MultiView
8487
extends BeanPropertyWriter
88+
implements java.io.Serializable
8589
{
90+
private static final long serialVersionUID = 1L;
91+
8692
protected final BeanPropertyWriter _delegate;
8793

8894
protected final Class<?>[] _views;

src/main/java/com/fasterxml/jackson/databind/ser/impl/UnwrappingBeanPropertyWriter.java

+3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323
*/
2424
public class UnwrappingBeanPropertyWriter
2525
extends BeanPropertyWriter
26+
implements java.io.Serializable
2627
{
28+
private static final long serialVersionUID = 1L;
29+
2730
/**
2831
* Transformer used to add prefix and/or suffix for properties
2932
* of unwrapped POJO.

src/main/java/com/fasterxml/jackson/databind/ser/impl/UnwrappingBeanSerializer.java

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
public class UnwrappingBeanSerializer
1414
extends BeanSerializerBase
15+
implements java.io.Serializable
1516
{
1617
private static final long serialVersionUID = 1L;
1718

src/main/java/com/fasterxml/jackson/databind/util/EnumValues.java

+3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
* enumerations.
1212
*/
1313
public final class EnumValues
14+
implements java.io.Serializable
1415
{
16+
private static final long serialVersionUID = 1;
17+
1518
private final Class<Enum<?>> _enumClass;
1619

1720
private final Enum<?>[] _values;

src/main/java/com/fasterxml/jackson/databind/util/NameTransformer.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@ public abstract class NameTransformer
1111
* Singleton "no-operation" transformer which simply returns given
1212
* name as is. Used commonly as placeholder or marker.
1313
*/
14-
public final static NameTransformer NOP = new NameTransformer() {
14+
public final static NameTransformer NOP = new NopTransformer();
15+
16+
protected final static class NopTransformer
17+
extends NameTransformer
18+
implements java.io.Serializable
19+
{
20+
private static final long serialVersionUID = 1L;
21+
1522
@Override
1623
public String transform(String name) {
1724
return name;
@@ -21,7 +28,7 @@ public String reverse(String transformed) {
2128
// identity transformation is always reversible:
2229
return transformed;
2330
}
24-
};
31+
}
2532

2633
protected NameTransformer() { }
2734

@@ -107,7 +114,10 @@ public static NameTransformer chainedTransformer(NameTransformer t1, NameTransfo
107114
public abstract String reverse(String transformed);
108115

109116
public static class Chained extends NameTransformer
117+
implements java.io.Serializable
110118
{
119+
private static final long serialVersionUID = 1L;
120+
111121
protected final NameTransformer _t1, _t2;
112122

113123
public Chained(NameTransformer t1, NameTransformer t2) {

src/test/java/com/fasterxml/jackson/databind/TestJDKSerialization.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,7 @@ public void testEnumHandlers() throws IOException
9090
/* 14-Aug-2015, tatu: Looks like pre-loading JsonSerializer is problematic
9191
* at this point; comment out for now. Try to fix later on.
9292
*/
93-
94-
// bytes = jdkSerialize(mapper.writerFor(EnumPOJO.class));
95-
bytes = jdkSerialize(mapper.writer());
93+
bytes = jdkSerialize(mapper.writerFor(EnumPOJO.class));
9694
ObjectWriter w = jdkDeserialize(bytes);
9795
assertNotNull(w);
9896

src/test/java/com/fasterxml/jackson/databind/ser/TestVirtualProperties.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import com.fasterxml.jackson.annotation.JsonInclude;
66
import com.fasterxml.jackson.core.JsonGenerator;
7-
87
import com.fasterxml.jackson.databind.*;
98
import com.fasterxml.jackson.databind.annotation.JsonAppend;
109
import com.fasterxml.jackson.databind.cfg.MapperConfig;
@@ -45,6 +44,7 @@ static class OptionalsBean
4544
public int value = 28;
4645
}
4746

47+
@SuppressWarnings("serial")
4848
static class CustomVProperty
4949
extends VirtualBeanPropertyWriter
5050
{

0 commit comments

Comments
 (0)