Skip to content

Commit cee73b4

Browse files
committed
More tweaking of JDK serializability
1 parent c8a233c commit cee73b4

File tree

3 files changed

+63
-6
lines changed

3 files changed

+63
-6
lines changed

src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,29 @@ public ObjectMapper build() {
170170

171171
@Override
172172
protected MapperBuilderState _saveState() {
173-
return new MapperBuilderState(this);
173+
return new StateImpl(this);
174174
}
175175

176176
public Builder(MapperBuilderState state) {
177177
super(state);
178178
}
179+
180+
/**
181+
* We also need actual instance of state as base class can not implement logic
182+
* for reinstating mapper (via mapper builder) from state.
183+
*/
184+
static class StateImpl extends MapperBuilderState {
185+
private static final long serialVersionUID = 3L;
186+
187+
public StateImpl(Builder b) {
188+
super(b);
189+
}
190+
191+
@Override
192+
protected Object readResolve() {
193+
return new Builder(this).build();
194+
}
195+
}
179196
}
180197

181198
/*
@@ -341,7 +358,7 @@ public Builder(MapperBuilderState state) {
341358

342359
/*
343360
/**********************************************************************
344-
/* Life-cycle: constructing instance
361+
/* Life-cycle: legacy constructors
345362
/**********************************************************************
346363
*/
347364

@@ -363,6 +380,12 @@ public ObjectMapper(TokenStreamFactory streamFactory) {
363380
this(new Builder(streamFactory));
364381
}
365382

383+
/*
384+
/**********************************************************************
385+
/* Life-cycle: builder-style construction
386+
/**********************************************************************
387+
*/
388+
366389
/**
367390
* Constructor usually called either by {@link MapperBuilder#build} or
368391
* by sub-class constructor: will get all the settings through passed-in
@@ -443,6 +466,27 @@ public <M extends ObjectMapper, B extends MapperBuilder<M,B>> MapperBuilder<M,B>
443466
return (MapperBuilder<M,B>) new ObjectMapper.Builder(_savedBuilderState);
444467
}
445468

469+
/*
470+
/**********************************************************************
471+
/* Life-cycle: JDK serialization support
472+
/**********************************************************************
473+
*/
474+
475+
// Logic here is simple: instead of serializing mapper via its contents,
476+
// we have pre-packaged `MapperBuilderState` in a way that makes serialization
477+
// easier, and we go with that.
478+
// But note that return direction has to be supported, then, by that state object
479+
// and NOT anything in here.
480+
481+
Object writeReplace() {
482+
return _savedBuilderState;
483+
}
484+
485+
// Just as a sanity check verify there is no attempt at directly instantiating mapper here
486+
Object readResolve() {
487+
throw new IllegalStateException("Should never deserialize `ObjectMapper` directly");
488+
}
489+
446490
/*
447491
/**********************************************************************
448492
/* Versioned impl

src/main/java/com/fasterxml/jackson/databind/cfg/MapperBuilderState.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@
2323
* {@link MapperBuilder} before modules are configured and resulting
2424
* {@link com.fasterxml.jackson.databind.ObjectMapper} isn't constructed.
2525
* It is passed to mapper to allow "re-building" via newly created builder.
26+
*<p>
27+
* Note that JDK serialization is supported by switching this object in place
28+
* of mapper. This requires some acrobatics on return direction.
2629
*/
27-
public class MapperBuilderState
30+
public abstract class MapperBuilderState
2831
implements java.io.Serializable // important!
2932
{
3033
private static final long serialVersionUID = 3L;
@@ -105,7 +108,7 @@ public class MapperBuilderState
105108

106109
/*
107110
/**********************************************************************
108-
/* Life-cycle
111+
/* Construction
109112
/**********************************************************************
110113
*/
111114

@@ -161,4 +164,16 @@ private static com.fasterxml.jackson.databind.Module[] _toArray(Collection<?> co
161164
}
162165
return coll.toArray(new com.fasterxml.jackson.databind.Module[coll.size()]);
163166
}
167+
168+
/*
169+
/**********************************************************************
170+
/* JDK deserialization support
171+
/**********************************************************************
172+
*/
173+
174+
/**
175+
* Method required to support JDK deserialization; made `abstract` here to ensure
176+
* sub-classes must implement it.
177+
*/
178+
protected abstract Object readResolve();
164179
}

src/main/java/com/fasterxml/jackson/databind/type/TypeBindings.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ public class TypeBindings
3636

3737
/**
3838
* Names of potentially unresolved type variables.
39-
*
40-
* @since 2.3
4139
*/
4240
private final String[] _unboundVariables;
4341

0 commit comments

Comments
 (0)