@@ -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
0 commit comments