@@ -258,8 +258,12 @@ public boolean useForType(JavaType t)
258
258
259
259
protected final static VisibilityChecker <?> STD_VISIBILITY_CHECKER = VisibilityChecker .Std .defaultInstance ();
260
260
261
+ /**
262
+ * @deprecated Since 2.6, do not use: will be removed in 2.7 or later
263
+ */
264
+ @ Deprecated
261
265
protected final static PrettyPrinter _defaultPrettyPrinter = new DefaultPrettyPrinter ();
262
-
266
+
263
267
/**
264
268
* Base settings contain defaults used for all {@link ObjectMapper}
265
269
* instances.
@@ -309,7 +313,7 @@ public boolean useForType(JavaType t)
309
313
* Cache for root names used when root-wrapping is enabled.
310
314
*/
311
315
protected final RootNameLookup _rootNames ;
312
-
316
+
313
317
/*
314
318
/**********************************************************
315
319
/* Configuration settings: mix-in annotations
@@ -481,7 +485,7 @@ protected ObjectMapper(ObjectMapper src)
481
485
// Default serializer factory is stateless, can just assign
482
486
_serializerFactory = src ._serializerFactory ;
483
487
}
484
-
488
+
485
489
/**
486
490
* Constructs instance that uses specified {@link JsonFactory}
487
491
* for constructing necessary {@link JsonParser}s and/or
@@ -548,7 +552,7 @@ public ObjectMapper(JsonFactory jf,
548
552
protected ClassIntrospector defaultClassIntrospector () {
549
553
return new BasicClassIntrospector ();
550
554
}
551
-
555
+
552
556
/*
553
557
/**********************************************************
554
558
/* Methods sub-classes MUST override
@@ -1254,13 +1258,28 @@ public PropertyNamingStrategy getPropertyNamingStrategy() {
1254
1258
}
1255
1259
1256
1260
/**
1257
- * Method for setting defalt POJO property inclusion strategy for serialization.
1261
+ * Method for setting default POJO property inclusion strategy for serialization.
1258
1262
*/
1259
1263
public ObjectMapper setSerializationInclusion (JsonInclude .Include incl ) {
1260
1264
_serializationConfig = _serializationConfig .withSerializationInclusion (incl );
1261
1265
return this ;
1262
1266
}
1263
-
1267
+
1268
+ /**
1269
+ * Method for specifying {@link PrettyPrinter} to use when "default pretty-printing"
1270
+ * is enabled (by enabling {@link SerializationFeature#INDENT_OUTPUT})
1271
+ *
1272
+ * @param pp
1273
+ *
1274
+ * @return This mapper, useful for call-chaining
1275
+ *
1276
+ * @since 2.6
1277
+ */
1278
+ public ObjectMapper setDefaultPrettyPrinter (PrettyPrinter pp ) {
1279
+ _serializationConfig = _serializationConfig .withDefaultPrettyPrinter (pp );
1280
+ return this ;
1281
+ }
1282
+
1264
1283
/*
1265
1284
/**********************************************************
1266
1285
/* Type information configuration (1.5+)
@@ -2328,20 +2347,28 @@ public JsonNode readTree(URL source)
2328
2347
* JSON output, using provided {@link JsonGenerator}.
2329
2348
*/
2330
2349
@ Override
2331
- public void writeValue (JsonGenerator jgen , Object value )
2350
+ public void writeValue (JsonGenerator g , Object value )
2332
2351
throws IOException , JsonGenerationException , JsonMappingException
2333
2352
{
2334
2353
SerializationConfig config = getSerializationConfig ();
2354
+
2355
+ /* 12-May-2015/2.6, tatu: Looks like we do NOT want to call the usual
2356
+ * 'config.initialize(g)` here, since it is assumed that generator
2357
+ * has been configured by caller. But for some reason we don't
2358
+ * trust indentation settings...
2359
+ */
2335
2360
// 10-Aug-2012, tatu: as per [Issue#12], must handle indentation:
2336
2361
if (config .isEnabled (SerializationFeature .INDENT_OUTPUT )) {
2337
- jgen .useDefaultPrettyPrinter ();
2362
+ if (g .getPrettyPrinter () == null ) {
2363
+ g .setPrettyPrinter (config .constructDefaultPrettyPrinter ());
2364
+ }
2338
2365
}
2339
2366
if (config .isEnabled (SerializationFeature .CLOSE_CLOSEABLE ) && (value instanceof Closeable )) {
2340
- _writeCloseableValue (jgen , value , config );
2367
+ _writeCloseableValue (g , value , config );
2341
2368
} else {
2342
- _serializerProvider (config ).serializeValue (jgen , value );
2369
+ _serializerProvider (config ).serializeValue (g , value );
2343
2370
if (config .isEnabled (SerializationFeature .FLUSH_AFTER_WRITE_VALUE )) {
2344
- jgen .flush ();
2371
+ g .flush ();
2345
2372
}
2346
2373
}
2347
2374
}
@@ -3032,8 +3059,9 @@ public ObjectWriter writer(PrettyPrinter pp) {
3032
3059
* serialize objects using the default pretty printer for indentation
3033
3060
*/
3034
3061
public ObjectWriter writerWithDefaultPrettyPrinter () {
3035
- return _newWriter (getSerializationConfig (),
3036
- /*root type*/ null , _defaultPrettyPrinter ());
3062
+ SerializationConfig config = getSerializationConfig ();
3063
+ return _newWriter (config ,
3064
+ /*root type*/ null , config .getDefaultPrettyPrinter ());
3037
3065
}
3038
3066
3039
3067
/**
@@ -3465,32 +3493,31 @@ protected DefaultSerializerProvider _serializerProvider(SerializationConfig conf
3465
3493
}
3466
3494
3467
3495
/**
3468
- * Helper method that should return default pretty-printer to
3469
- * use for generators constructed by this mapper, when instructed
3470
- * to use default pretty printer.
3496
+ * @deprecated Since 2.6, use {@link SerializationConfig#constructDefaultPrettyPrinter()} directly
3471
3497
*/
3498
+ @ Deprecated
3472
3499
protected PrettyPrinter _defaultPrettyPrinter () {
3473
- return _defaultPrettyPrinter ;
3500
+ return _serializationConfig . constructDefaultPrettyPrinter () ;
3474
3501
}
3475
-
3502
+
3476
3503
/**
3477
3504
* Method called to configure the generator as necessary and then
3478
3505
* call write functionality
3479
3506
*/
3480
- protected final void _configAndWriteValue (JsonGenerator jgen , Object value )
3507
+ protected final void _configAndWriteValue (JsonGenerator g , Object value )
3481
3508
throws IOException
3482
3509
{
3483
3510
SerializationConfig cfg = getSerializationConfig ();
3484
- cfg .initialize (jgen ); // since 2.5
3511
+ cfg .initialize (g ); // since 2.5
3485
3512
if (cfg .isEnabled (SerializationFeature .CLOSE_CLOSEABLE ) && (value instanceof Closeable )) {
3486
- _configAndWriteCloseable (jgen , value , cfg );
3513
+ _configAndWriteCloseable (g , value , cfg );
3487
3514
return ;
3488
3515
}
3489
3516
boolean closed = false ;
3490
3517
try {
3491
- _serializerProvider (cfg ).serializeValue (jgen , value );
3518
+ _serializerProvider (cfg ).serializeValue (g , value );
3492
3519
closed = true ;
3493
- jgen .close ();
3520
+ g .close ();
3494
3521
} finally {
3495
3522
/* won't try to close twice; also, must catch exception (so it
3496
3523
* will not mask exception that is pending)
@@ -3499,37 +3526,37 @@ protected final void _configAndWriteValue(JsonGenerator jgen, Object value)
3499
3526
/* 04-Mar-2014, tatu: But! Let's try to prevent auto-closing of
3500
3527
* structures, which typically causes more damage.
3501
3528
*/
3502
- jgen .disable (JsonGenerator .Feature .AUTO_CLOSE_JSON_CONTENT );
3529
+ g .disable (JsonGenerator .Feature .AUTO_CLOSE_JSON_CONTENT );
3503
3530
try {
3504
- jgen .close ();
3531
+ g .close ();
3505
3532
} catch (IOException ioe ) { }
3506
3533
}
3507
3534
}
3508
3535
}
3509
3536
3510
- protected final void _configAndWriteValue (JsonGenerator jgen , Object value , Class <?> viewClass )
3537
+ protected final void _configAndWriteValue (JsonGenerator g , Object value , Class <?> viewClass )
3511
3538
throws IOException
3512
3539
{
3513
3540
SerializationConfig cfg = getSerializationConfig ().withView (viewClass );
3514
- cfg .initialize (jgen ); // since 2.5
3541
+ cfg .initialize (g ); // since 2.5
3515
3542
3516
3543
// [JACKSON-282]: consider Closeable
3517
3544
if (cfg .isEnabled (SerializationFeature .CLOSE_CLOSEABLE ) && (value instanceof Closeable )) {
3518
- _configAndWriteCloseable (jgen , value , cfg );
3545
+ _configAndWriteCloseable (g , value , cfg );
3519
3546
return ;
3520
3547
}
3521
3548
boolean closed = false ;
3522
3549
try {
3523
- _serializerProvider (cfg ).serializeValue (jgen , value );
3550
+ _serializerProvider (cfg ).serializeValue (g , value );
3524
3551
closed = true ;
3525
- jgen .close ();
3552
+ g .close ();
3526
3553
} finally {
3527
3554
if (!closed ) {
3528
3555
// 04-Mar-2014, tatu: But! Let's try to prevent auto-closing of
3529
3556
// structures, which typically causes more damage.
3530
- jgen .disable (JsonGenerator .Feature .AUTO_CLOSE_JSON_CONTENT );
3557
+ g .disable (JsonGenerator .Feature .AUTO_CLOSE_JSON_CONTENT );
3531
3558
try {
3532
- jgen .close ();
3559
+ g .close ();
3533
3560
} catch (IOException ioe ) { }
3534
3561
}
3535
3562
}
@@ -3539,28 +3566,28 @@ protected final void _configAndWriteValue(JsonGenerator jgen, Object value, Clas
3539
3566
* Helper method used when value to serialize is {@link Closeable} and its <code>close()</code>
3540
3567
* method is to be called right after serialization has been called
3541
3568
*/
3542
- private final void _configAndWriteCloseable (JsonGenerator jgen , Object value , SerializationConfig cfg )
3569
+ private final void _configAndWriteCloseable (JsonGenerator g , Object value , SerializationConfig cfg )
3543
3570
throws IOException , JsonGenerationException , JsonMappingException
3544
3571
{
3545
3572
Closeable toClose = (Closeable ) value ;
3546
3573
try {
3547
- _serializerProvider (cfg ).serializeValue (jgen , value );
3548
- JsonGenerator tmpJgen = jgen ;
3549
- jgen = null ;
3550
- tmpJgen .close ();
3574
+ _serializerProvider (cfg ).serializeValue (g , value );
3575
+ JsonGenerator tmpGen = g ;
3576
+ g = null ;
3577
+ tmpGen .close ();
3551
3578
Closeable tmpToClose = toClose ;
3552
3579
toClose = null ;
3553
3580
tmpToClose .close ();
3554
3581
} finally {
3555
3582
/* Need to close both generator and value, as long as they haven't yet
3556
3583
* been closed
3557
3584
*/
3558
- if (jgen != null ) {
3585
+ if (g != null ) {
3559
3586
// 04-Mar-2014, tatu: But! Let's try to prevent auto-closing of
3560
3587
// structures, which typically causes more damage.
3561
- jgen .disable (JsonGenerator .Feature .AUTO_CLOSE_JSON_CONTENT );
3588
+ g .disable (JsonGenerator .Feature .AUTO_CLOSE_JSON_CONTENT );
3562
3589
try {
3563
- jgen .close ();
3590
+ g .close ();
3564
3591
} catch (IOException ioe ) { }
3565
3592
}
3566
3593
if (toClose != null ) {
@@ -3575,14 +3602,14 @@ private final void _configAndWriteCloseable(JsonGenerator jgen, Object value, Se
3575
3602
* Helper method used when value to serialize is {@link Closeable} and its <code>close()</code>
3576
3603
* method is to be called right after serialization has been called
3577
3604
*/
3578
- private final void _writeCloseableValue (JsonGenerator jgen , Object value , SerializationConfig cfg )
3605
+ private final void _writeCloseableValue (JsonGenerator g , Object value , SerializationConfig cfg )
3579
3606
throws IOException , JsonGenerationException , JsonMappingException
3580
3607
{
3581
3608
Closeable toClose = (Closeable ) value ;
3582
3609
try {
3583
- _serializerProvider (cfg ).serializeValue (jgen , value );
3610
+ _serializerProvider (cfg ).serializeValue (g , value );
3584
3611
if (cfg .isEnabled (SerializationFeature .FLUSH_AFTER_WRITE_VALUE )) {
3585
- jgen .flush ();
3612
+ g .flush ();
3586
3613
}
3587
3614
Closeable tmpToClose = toClose ;
3588
3615
toClose = null ;
0 commit comments