Skip to content

Commit c0b9552

Browse files
committed
Implement #689: Add ObjectMapper.setDefaultPrettyPrinter(...)
1 parent 4265e1d commit c0b9552

File tree

7 files changed

+210
-72
lines changed

7 files changed

+210
-72
lines changed

release-notes/CREDITS

+3-1
Original file line numberDiff line numberDiff line change
@@ -263,4 +263,6 @@ Laird Nelson (ljnelson@github)
263263
classes on demand
264264
(2.6.0)
265265

266-
266+
Derk Norton (derknorton@github)
267+
* Suggested #689: Add `ObjectMapper.setDefaultPrettyPrinter(PrettyPrinter)`
268+
(2.6.0)

release-notes/VERSION

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Project: jackson-databind
1616
#679: Add `isEmpty()` implementation for `JsonNode` serializers
1717
#688: Provide a means for an ObjectMapper to discover mixin annotation classes on demand
1818
(requested by Laird N)
19+
#689: Add `ObjectMapper.setDefaultPrettyPrinter(PrettyPrinter)`
20+
(requested by derknorton@github)
1921
#696: Copy constructor does not preserve `_injectableValues`
2022
(reported by Charles A)
2123
#698: Add support for referential types (ReferenceType)

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

+70-43
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,12 @@ public boolean useForType(JavaType t)
258258

259259
protected final static VisibilityChecker<?> STD_VISIBILITY_CHECKER = VisibilityChecker.Std.defaultInstance();
260260

261+
/**
262+
* @deprecated Since 2.6, do not use: will be removed in 2.7 or later
263+
*/
264+
@Deprecated
261265
protected final static PrettyPrinter _defaultPrettyPrinter = new DefaultPrettyPrinter();
262-
266+
263267
/**
264268
* Base settings contain defaults used for all {@link ObjectMapper}
265269
* instances.
@@ -309,7 +313,7 @@ public boolean useForType(JavaType t)
309313
* Cache for root names used when root-wrapping is enabled.
310314
*/
311315
protected final RootNameLookup _rootNames;
312-
316+
313317
/*
314318
/**********************************************************
315319
/* Configuration settings: mix-in annotations
@@ -481,7 +485,7 @@ protected ObjectMapper(ObjectMapper src)
481485
// Default serializer factory is stateless, can just assign
482486
_serializerFactory = src._serializerFactory;
483487
}
484-
488+
485489
/**
486490
* Constructs instance that uses specified {@link JsonFactory}
487491
* for constructing necessary {@link JsonParser}s and/or
@@ -548,7 +552,7 @@ public ObjectMapper(JsonFactory jf,
548552
protected ClassIntrospector defaultClassIntrospector() {
549553
return new BasicClassIntrospector();
550554
}
551-
555+
552556
/*
553557
/**********************************************************
554558
/* Methods sub-classes MUST override
@@ -1254,13 +1258,28 @@ public PropertyNamingStrategy getPropertyNamingStrategy() {
12541258
}
12551259

12561260
/**
1257-
* Method for setting defalt POJO property inclusion strategy for serialization.
1261+
* Method for setting default POJO property inclusion strategy for serialization.
12581262
*/
12591263
public ObjectMapper setSerializationInclusion(JsonInclude.Include incl) {
12601264
_serializationConfig = _serializationConfig.withSerializationInclusion(incl);
12611265
return this;
12621266
}
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+
12641283
/*
12651284
/**********************************************************
12661285
/* Type information configuration (1.5+)
@@ -2328,20 +2347,28 @@ public JsonNode readTree(URL source)
23282347
* JSON output, using provided {@link JsonGenerator}.
23292348
*/
23302349
@Override
2331-
public void writeValue(JsonGenerator jgen, Object value)
2350+
public void writeValue(JsonGenerator g, Object value)
23322351
throws IOException, JsonGenerationException, JsonMappingException
23332352
{
23342353
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+
*/
23352360
// 10-Aug-2012, tatu: as per [Issue#12], must handle indentation:
23362361
if (config.isEnabled(SerializationFeature.INDENT_OUTPUT)) {
2337-
jgen.useDefaultPrettyPrinter();
2362+
if (g.getPrettyPrinter() == null) {
2363+
g.setPrettyPrinter(config.constructDefaultPrettyPrinter());
2364+
}
23382365
}
23392366
if (config.isEnabled(SerializationFeature.CLOSE_CLOSEABLE) && (value instanceof Closeable)) {
2340-
_writeCloseableValue(jgen, value, config);
2367+
_writeCloseableValue(g, value, config);
23412368
} else {
2342-
_serializerProvider(config).serializeValue(jgen, value);
2369+
_serializerProvider(config).serializeValue(g, value);
23432370
if (config.isEnabled(SerializationFeature.FLUSH_AFTER_WRITE_VALUE)) {
2344-
jgen.flush();
2371+
g.flush();
23452372
}
23462373
}
23472374
}
@@ -3032,8 +3059,9 @@ public ObjectWriter writer(PrettyPrinter pp) {
30323059
* serialize objects using the default pretty printer for indentation
30333060
*/
30343061
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());
30373065
}
30383066

30393067
/**
@@ -3465,32 +3493,31 @@ protected DefaultSerializerProvider _serializerProvider(SerializationConfig conf
34653493
}
34663494

34673495
/**
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
34713497
*/
3498+
@Deprecated
34723499
protected PrettyPrinter _defaultPrettyPrinter() {
3473-
return _defaultPrettyPrinter;
3500+
return _serializationConfig.constructDefaultPrettyPrinter();
34743501
}
3475-
3502+
34763503
/**
34773504
* Method called to configure the generator as necessary and then
34783505
* call write functionality
34793506
*/
3480-
protected final void _configAndWriteValue(JsonGenerator jgen, Object value)
3507+
protected final void _configAndWriteValue(JsonGenerator g, Object value)
34813508
throws IOException
34823509
{
34833510
SerializationConfig cfg = getSerializationConfig();
3484-
cfg.initialize(jgen); // since 2.5
3511+
cfg.initialize(g); // since 2.5
34853512
if (cfg.isEnabled(SerializationFeature.CLOSE_CLOSEABLE) && (value instanceof Closeable)) {
3486-
_configAndWriteCloseable(jgen, value, cfg);
3513+
_configAndWriteCloseable(g, value, cfg);
34873514
return;
34883515
}
34893516
boolean closed = false;
34903517
try {
3491-
_serializerProvider(cfg).serializeValue(jgen, value);
3518+
_serializerProvider(cfg).serializeValue(g, value);
34923519
closed = true;
3493-
jgen.close();
3520+
g.close();
34943521
} finally {
34953522
/* won't try to close twice; also, must catch exception (so it
34963523
* will not mask exception that is pending)
@@ -3499,37 +3526,37 @@ protected final void _configAndWriteValue(JsonGenerator jgen, Object value)
34993526
/* 04-Mar-2014, tatu: But! Let's try to prevent auto-closing of
35003527
* structures, which typically causes more damage.
35013528
*/
3502-
jgen.disable(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT);
3529+
g.disable(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT);
35033530
try {
3504-
jgen.close();
3531+
g.close();
35053532
} catch (IOException ioe) { }
35063533
}
35073534
}
35083535
}
35093536

3510-
protected final void _configAndWriteValue(JsonGenerator jgen, Object value, Class<?> viewClass)
3537+
protected final void _configAndWriteValue(JsonGenerator g, Object value, Class<?> viewClass)
35113538
throws IOException
35123539
{
35133540
SerializationConfig cfg = getSerializationConfig().withView(viewClass);
3514-
cfg.initialize(jgen); // since 2.5
3541+
cfg.initialize(g); // since 2.5
35153542

35163543
// [JACKSON-282]: consider Closeable
35173544
if (cfg.isEnabled(SerializationFeature.CLOSE_CLOSEABLE) && (value instanceof Closeable)) {
3518-
_configAndWriteCloseable(jgen, value, cfg);
3545+
_configAndWriteCloseable(g, value, cfg);
35193546
return;
35203547
}
35213548
boolean closed = false;
35223549
try {
3523-
_serializerProvider(cfg).serializeValue(jgen, value);
3550+
_serializerProvider(cfg).serializeValue(g, value);
35243551
closed = true;
3525-
jgen.close();
3552+
g.close();
35263553
} finally {
35273554
if (!closed) {
35283555
// 04-Mar-2014, tatu: But! Let's try to prevent auto-closing of
35293556
// structures, which typically causes more damage.
3530-
jgen.disable(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT);
3557+
g.disable(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT);
35313558
try {
3532-
jgen.close();
3559+
g.close();
35333560
} catch (IOException ioe) { }
35343561
}
35353562
}
@@ -3539,28 +3566,28 @@ protected final void _configAndWriteValue(JsonGenerator jgen, Object value, Clas
35393566
* Helper method used when value to serialize is {@link Closeable} and its <code>close()</code>
35403567
* method is to be called right after serialization has been called
35413568
*/
3542-
private final void _configAndWriteCloseable(JsonGenerator jgen, Object value, SerializationConfig cfg)
3569+
private final void _configAndWriteCloseable(JsonGenerator g, Object value, SerializationConfig cfg)
35433570
throws IOException, JsonGenerationException, JsonMappingException
35443571
{
35453572
Closeable toClose = (Closeable) value;
35463573
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();
35513578
Closeable tmpToClose = toClose;
35523579
toClose = null;
35533580
tmpToClose.close();
35543581
} finally {
35553582
/* Need to close both generator and value, as long as they haven't yet
35563583
* been closed
35573584
*/
3558-
if (jgen != null) {
3585+
if (g != null) {
35593586
// 04-Mar-2014, tatu: But! Let's try to prevent auto-closing of
35603587
// structures, which typically causes more damage.
3561-
jgen.disable(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT);
3588+
g.disable(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT);
35623589
try {
3563-
jgen.close();
3590+
g.close();
35643591
} catch (IOException ioe) { }
35653592
}
35663593
if (toClose != null) {
@@ -3575,14 +3602,14 @@ private final void _configAndWriteCloseable(JsonGenerator jgen, Object value, Se
35753602
* Helper method used when value to serialize is {@link Closeable} and its <code>close()</code>
35763603
* method is to be called right after serialization has been called
35773604
*/
3578-
private final void _writeCloseableValue(JsonGenerator jgen, Object value, SerializationConfig cfg)
3605+
private final void _writeCloseableValue(JsonGenerator g, Object value, SerializationConfig cfg)
35793606
throws IOException, JsonGenerationException, JsonMappingException
35803607
{
35813608
Closeable toClose = (Closeable) value;
35823609
try {
3583-
_serializerProvider(cfg).serializeValue(jgen, value);
3610+
_serializerProvider(cfg).serializeValue(g, value);
35843611
if (cfg.isEnabled(SerializationFeature.FLUSH_AFTER_WRITE_VALUE)) {
3585-
jgen.flush();
3612+
g.flush();
35863613
}
35873614
Closeable tmpToClose = toClose;
35883615
toClose = null;

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ protected ObjectWriter(ObjectMapper mapper, SerializationConfig config)
120120
_serializerFactory = mapper._serializerFactory;
121121
_generatorFactory = mapper._jsonFactory;
122122

123-
_prefetch = Prefetch.empty;
124123
_generatorSettings = GeneratorSettings.empty;
124+
_prefetch = Prefetch.empty;
125125
}
126126

127127
/**
@@ -136,9 +136,9 @@ protected ObjectWriter(ObjectMapper mapper, SerializationConfig config,
136136
_serializerFactory = mapper._serializerFactory;
137137
_generatorFactory = mapper._jsonFactory;
138138

139-
_prefetch = Prefetch.empty;
140139
_generatorSettings = (s == null) ? GeneratorSettings.empty
141140
: new GeneratorSettings(null, s, null, null);
141+
_prefetch = Prefetch.empty;
142142
}
143143

144144
/**
@@ -167,6 +167,7 @@ protected ObjectWriter(ObjectWriter base, SerializationConfig config)
167167
_serializerProvider = base._serializerProvider;
168168
_serializerFactory = base._serializerFactory;
169169
_generatorFactory = base._generatorFactory;
170+
170171
_generatorSettings = base._generatorSettings;
171172
_prefetch = base._prefetch;
172173
}
@@ -183,6 +184,7 @@ protected ObjectWriter(ObjectWriter base, JsonFactory f)
183184
_serializerProvider = base._serializerProvider;
184185
_serializerFactory = base._serializerFactory;
185186
_generatorFactory = base._generatorFactory;
187+
186188
_generatorSettings = base._generatorSettings;
187189
_prefetch = base._prefetch;
188190
}
@@ -371,7 +373,7 @@ public ObjectWriter with(DateFormat df) {
371373
* pretty printer for serialization.
372374
*/
373375
public ObjectWriter withDefaultPrettyPrinter() {
374-
return with(new DefaultPrettyPrinter());
376+
return with(_config.getDefaultPrettyPrinter());
375377
}
376378

377379
/**
@@ -1237,8 +1239,6 @@ protected JsonGenerator _configureGenerator(JsonGenerator gen)
12371239
}
12381240
gen.setPrettyPrinter(pp);
12391241
}
1240-
} else if (_config.isEnabled(SerializationFeature.INDENT_OUTPUT)) {
1241-
gen.useDefaultPrettyPrinter();
12421242
}
12431243
CharacterEscapes esc = genSet.characterEscapes;
12441244
if (esc != null) {

0 commit comments

Comments
 (0)