@@ -76,27 +76,14 @@ public class ObjectWriter
76
76
* @since 2.1
77
77
*/
78
78
protected final JsonSerializer <Object > _rootSerializer ;
79
-
80
- /**
81
- * To allow for dynamic enabling/disabling of pretty printing,
82
- * pretty printer can be optionally configured for writer
83
- * as well
84
- */
85
- protected final PrettyPrinter _prettyPrinter ;
86
-
87
- /**
88
- * When using data format that uses a schema, schema is passed
89
- * to generator.
90
- */
91
- protected final FormatSchema _schema ;
92
-
79
+
93
80
/**
94
- * Caller may want to specify character escaping details, either as
95
- * defaults, or on call-by-call basis .
96
- *
97
- * @since 2.3
81
+ * Container for settings that need to be passed to {@link JsonGenerator}
82
+ * constructed for serializing values .
83
+ *
84
+ * @since 2.5
98
85
*/
99
- protected final CharacterEscapes _characterEscapes ;
86
+ protected final GeneratorSettings _generatorSettings ;
100
87
101
88
/*
102
89
/**********************************************************
@@ -114,9 +101,8 @@ protected ObjectWriter(ObjectMapper mapper, SerializationConfig config,
114
101
_serializerProvider = mapper ._serializerProvider ;
115
102
_serializerFactory = mapper ._serializerFactory ;
116
103
_generatorFactory = mapper ._jsonFactory ;
117
- _prettyPrinter = pp ;
118
- _schema = null ;
119
- _characterEscapes = null ;
104
+ _generatorSettings = (pp == null ) ? GeneratorSettings .empty
105
+ : new GeneratorSettings (pp , null , null );
120
106
121
107
// 29-Apr-2014, tatu: There is no "untyped serializer", so:
122
108
if (rootType == null || rootType .hasRawClass (Object .class )) {
@@ -140,9 +126,7 @@ protected ObjectWriter(ObjectMapper mapper, SerializationConfig config)
140
126
141
127
_rootType = null ;
142
128
_rootSerializer = null ;
143
- _prettyPrinter = null ;
144
- _schema = null ;
145
- _characterEscapes = null ;
129
+ _generatorSettings = GeneratorSettings .empty ;
146
130
}
147
131
148
132
/**
@@ -159,17 +143,16 @@ protected ObjectWriter(ObjectMapper mapper, SerializationConfig config,
159
143
160
144
_rootType = null ;
161
145
_rootSerializer = null ;
162
- _prettyPrinter = null ;
163
- _schema = s ;
164
- _characterEscapes = null ;
146
+ _generatorSettings = (s == null ) ? GeneratorSettings .empty
147
+ : new GeneratorSettings (null , s , null );
165
148
}
166
149
167
150
/**
168
151
* Copy constructor used for building variations.
169
152
*/
170
153
protected ObjectWriter (ObjectWriter base , SerializationConfig config ,
171
154
JavaType rootType , JsonSerializer <Object > rootSer ,
172
- PrettyPrinter pp , FormatSchema s , CharacterEscapes escapes )
155
+ GeneratorSettings genSettings )
173
156
{
174
157
_config = config ;
175
158
@@ -179,9 +162,7 @@ protected ObjectWriter(ObjectWriter base, SerializationConfig config,
179
162
180
163
_rootType = rootType ;
181
164
_rootSerializer = rootSer ;
182
- _prettyPrinter = pp ;
183
- _schema = s ;
184
- _characterEscapes = escapes ;
165
+ _generatorSettings = genSettings ;
185
166
}
186
167
187
168
/**
@@ -194,12 +175,10 @@ protected ObjectWriter(ObjectWriter base, SerializationConfig config)
194
175
_serializerProvider = base ._serializerProvider ;
195
176
_serializerFactory = base ._serializerFactory ;
196
177
_generatorFactory = base ._generatorFactory ;
197
- _schema = base ._schema ;
198
- _characterEscapes = base ._characterEscapes ;
178
+ _generatorSettings = base ._generatorSettings ;
199
179
200
180
_rootType = base ._rootType ;
201
181
_rootSerializer = base ._rootSerializer ;
202
- _prettyPrinter = base ._prettyPrinter ;
203
182
}
204
183
205
184
/**
@@ -214,12 +193,10 @@ protected ObjectWriter(ObjectWriter base, JsonFactory f)
214
193
_serializerProvider = base ._serializerProvider ;
215
194
_serializerFactory = base ._serializerFactory ;
216
195
_generatorFactory = base ._generatorFactory ;
217
- _schema = base ._schema ;
218
- _characterEscapes = base ._characterEscapes ;
196
+ _generatorSettings = base ._generatorSettings ;
219
197
220
198
_rootType = base ._rootType ;
221
199
_rootSerializer = base ._rootSerializer ;
222
- _prettyPrinter = base ._prettyPrinter ;
223
200
}
224
201
225
202
/**
@@ -258,14 +235,15 @@ protected ObjectWriter _new(ObjectWriter base, SerializationConfig config) {
258
235
}
259
236
260
237
/**
261
- * Overridable factory method called by various "withXxx()" methods
238
+ * Overridable factory method called by various "withXxx()" methods.
239
+ * It assumes `this` as base for settings other than those directly
240
+ * passed in.
262
241
*
263
242
* @since 2.5
264
243
*/
265
- protected ObjectWriter _new (ObjectWriter base , SerializationConfig config ,
266
- JavaType rootType , JsonSerializer <Object > rootSer ,
267
- PrettyPrinter pp , FormatSchema s , CharacterEscapes escapes ) {
268
- return new ObjectWriter (base , config , rootType , rootSer , pp , s , escapes );
244
+ protected ObjectWriter _new (JavaType rootType , JsonSerializer <Object > rootSer ,
245
+ GeneratorSettings genSettings ) {
246
+ return new ObjectWriter (this , _config , rootType , rootSer , genSettings );
269
247
}
270
248
271
249
/**
@@ -423,15 +401,11 @@ public ObjectWriter with(FilterProvider filterProvider) {
423
401
* printer (or, if null, will not do any pretty-printing)
424
402
*/
425
403
public ObjectWriter with (PrettyPrinter pp ) {
426
- if (pp == _prettyPrinter ) {
404
+ GeneratorSettings genSet = _generatorSettings .with (pp );
405
+ if (genSet == _generatorSettings ) {
427
406
return this ;
428
407
}
429
- // since null would mean "don't care", need to use placeholder to indicate "disable"
430
- if (pp == null ) {
431
- pp = NULL_PRETTY_PRINTER ;
432
- }
433
- return _new (this , _config , _rootType , _rootSerializer ,
434
- pp , _schema , _characterEscapes );
408
+ return _new (_rootType , _rootSerializer , genSet );
435
409
}
436
410
437
411
/**
@@ -455,12 +429,12 @@ public ObjectWriter withRootName(String rootName) {
455
429
* rather construct and returns a newly configured instance.
456
430
*/
457
431
public ObjectWriter with (FormatSchema schema ) {
458
- if (_schema == schema ) {
432
+ GeneratorSettings genSet = _generatorSettings .with (schema );
433
+ if (genSet == _generatorSettings ) {
459
434
return this ;
460
435
}
461
436
_verifySchemaType (schema );
462
- return _new (this , _config , _rootType , _rootSerializer ,
463
- _prettyPrinter , schema , _characterEscapes );
437
+ return _new (_rootType , _rootSerializer , genSet );
464
438
}
465
439
466
440
/**
@@ -492,8 +466,7 @@ public ObjectWriter forType(JavaType rootType)
492
466
rootType = rootType .withStaticTyping ();
493
467
rootSer = _prefetchRootSerializer (_config , rootType );
494
468
}
495
- return _new (this , _config , rootType , rootSer ,
496
- _prettyPrinter , _schema , _characterEscapes );
469
+ return _new (rootType , rootSer , _generatorSettings );
497
470
}
498
471
499
472
/**
@@ -576,11 +549,11 @@ public ObjectWriter with(Base64Variant b64variant) {
576
549
* @since 2.3
577
550
*/
578
551
public ObjectWriter with (CharacterEscapes escapes ) {
579
- if (_characterEscapes == escapes ) {
552
+ GeneratorSettings genSet = _generatorSettings .with (escapes );
553
+ if (genSet == _generatorSettings ) {
580
554
return this ;
581
555
}
582
- return _new (this , _config , _rootType , _rootSerializer ,
583
- _prettyPrinter , _schema , escapes );
556
+ return _new (_rootType , _rootSerializer , genSet );
584
557
}
585
558
586
559
/**
@@ -1011,21 +984,21 @@ public boolean canSerialize(Class<?> type) {
1011
984
public boolean canSerialize (Class <?> type , AtomicReference <Throwable > cause ) {
1012
985
return _serializerProvider (_config ).hasSerializerFor (type , cause );
1013
986
}
1014
-
987
+
1015
988
/*
1016
989
/**********************************************************
1017
990
/* Overridable helper methods
1018
991
/**********************************************************
1019
992
*/
1020
-
993
+
1021
994
/**
1022
995
* Overridable helper method used for constructing
1023
996
* {@link SerializerProvider} to use for serialization.
1024
997
*/
1025
998
protected DefaultSerializerProvider _serializerProvider (SerializationConfig config ) {
1026
999
return _serializerProvider .createInstance (config , _serializerFactory );
1027
1000
}
1028
-
1001
+
1029
1002
/*
1030
1003
/**********************************************************
1031
1004
/* Internal methods
@@ -1192,8 +1165,9 @@ protected void _configureJsonGenerator(JsonGenerator gen) {
1192
1165
*/
1193
1166
protected JsonGenerator _configureGenerator (JsonGenerator gen )
1194
1167
{
1195
- if (_prettyPrinter != null ) {
1196
- PrettyPrinter pp = _prettyPrinter ;
1168
+ GeneratorSettings genSet = _generatorSettings ;
1169
+ PrettyPrinter pp = genSet .prettyPrinter ;
1170
+ if (pp != null ) {
1197
1171
if (pp == NULL_PRETTY_PRINTER ) {
1198
1172
gen .setPrettyPrinter (null );
1199
1173
} else {
@@ -1208,14 +1182,81 @@ protected JsonGenerator _configureGenerator(JsonGenerator gen)
1208
1182
} else if (_config .isEnabled (SerializationFeature .INDENT_OUTPUT )) {
1209
1183
gen .useDefaultPrettyPrinter ();
1210
1184
}
1211
- if (_characterEscapes != null ) {
1212
- gen .setCharacterEscapes (_characterEscapes );
1185
+ CharacterEscapes esc = genSet .characterEscapes ;
1186
+ if (esc != null ) {
1187
+ gen .setCharacterEscapes (esc );
1213
1188
}
1214
1189
// [JACKSON-520]: add support for pass-through schema:
1215
- if (_schema != null ) {
1216
- gen .setSchema (_schema );
1190
+ FormatSchema sch = genSet .schema ;
1191
+ if (sch != null ) {
1192
+ gen .setSchema (sch );
1217
1193
}
1218
1194
_config .initialize (gen ); // since 2.5
1219
1195
return gen ;
1220
1196
}
1197
+
1198
+ /*
1199
+ /**********************************************************
1200
+ /* Helper classes for configuration
1201
+ /**********************************************************
1202
+ */
1203
+
1204
+ /**
1205
+ * Helper class used for containing settings specifically related
1206
+ * to (re)configuring {@link JsonGenerator} constructed for
1207
+ * writing output.
1208
+ *
1209
+ * @since 2.5
1210
+ */
1211
+ public final static class GeneratorSettings
1212
+ implements java .io .Serializable
1213
+ {
1214
+ private static final long serialVersionUID = 1L ;
1215
+
1216
+ public final static GeneratorSettings empty = new GeneratorSettings (null , null , null );
1217
+
1218
+ /**
1219
+ * To allow for dynamic enabling/disabling of pretty printing,
1220
+ * pretty printer can be optionally configured for writer
1221
+ * as well
1222
+ */
1223
+ public final PrettyPrinter prettyPrinter ;
1224
+
1225
+ /**
1226
+ * When using data format that uses a schema, schema is passed
1227
+ * to generator.
1228
+ */
1229
+ public final FormatSchema schema ;
1230
+
1231
+ /**
1232
+ * Caller may want to specify character escaping details, either as
1233
+ * defaults, or on call-by-call basis.
1234
+ */
1235
+ public final CharacterEscapes characterEscapes ;
1236
+
1237
+ public GeneratorSettings (PrettyPrinter pp , FormatSchema sch , CharacterEscapes esc ) {
1238
+ prettyPrinter = pp ;
1239
+ schema = sch ;
1240
+ characterEscapes = esc ;
1241
+ }
1242
+
1243
+ public GeneratorSettings with (PrettyPrinter pp ) {
1244
+ // since null would mean "don't care", need to use placeholder to indicate "disable"
1245
+ if (pp == null ) {
1246
+ pp = NULL_PRETTY_PRINTER ;
1247
+ }
1248
+ return (pp == prettyPrinter ) ? this
1249
+ : new GeneratorSettings (pp , schema , characterEscapes );
1250
+ }
1251
+
1252
+ public GeneratorSettings with (FormatSchema sch ) {
1253
+ return (schema == sch ) ? this
1254
+ : new GeneratorSettings (prettyPrinter , sch , characterEscapes );
1255
+ }
1256
+
1257
+ public GeneratorSettings with (CharacterEscapes esc ) {
1258
+ return (characterEscapes == esc ) ? this
1259
+ : new GeneratorSettings (prettyPrinter , schema , esc );
1260
+ }
1261
+ }
1221
1262
}
0 commit comments