Skip to content

Commit 23aee01

Browse files
authored
Allow configuring spaces before and/or after the colon in DefaultPrettyPrinter (#1042)
1 parent b275152 commit 23aee01

File tree

7 files changed

+431
-86
lines changed

7 files changed

+431
-86
lines changed

src/main/java/com/fasterxml/jackson/core/JsonFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
import com.fasterxml.jackson.core.sym.CharsToNameCanonicalizer;
2020
import com.fasterxml.jackson.core.util.BufferRecycler;
2121
import com.fasterxml.jackson.core.util.BufferRecyclers;
22-
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
2322
import com.fasterxml.jackson.core.util.JacksonFeature;
23+
import com.fasterxml.jackson.core.util.Separators;
2424

2525
/**
2626
* The main factory class of Jackson package, used to configure and
@@ -197,7 +197,7 @@ public static int collectDefaults() {
197197
*/
198198
protected final static int DEFAULT_GENERATOR_FEATURE_FLAGS = JsonGenerator.Feature.collectDefaults();
199199

200-
public final static SerializableString DEFAULT_ROOT_VALUE_SEPARATOR = DefaultPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR;
200+
public final static SerializableString DEFAULT_ROOT_VALUE_SEPARATOR = new SerializedString(Separators.DEFAULT_ROOT_VALUE_SEPARATOR);
201201

202202
/**
203203
* @since 2.10

src/main/java/com/fasterxml/jackson/core/json/JsonGeneratorImpl.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import com.fasterxml.jackson.core.io.CharTypes;
88
import com.fasterxml.jackson.core.io.CharacterEscapes;
99
import com.fasterxml.jackson.core.io.IOContext;
10-
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
1110
import com.fasterxml.jackson.core.util.JacksonFeatureSet;
1211
import com.fasterxml.jackson.core.util.VersionUtil;
1312

@@ -92,7 +91,7 @@ public abstract class JsonGeneratorImpl extends GeneratorBase
9291
* @since 2.1
9392
*/
9493
protected SerializableString _rootValueSeparator
95-
= DefaultPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR;
94+
= JsonFactory.DEFAULT_ROOT_VALUE_SEPARATOR;
9695

9796
/**
9897
* Flag that is set if quoting is not to be added around

src/main/java/com/fasterxml/jackson/core/util/DefaultPrettyPrinter.java

Lines changed: 94 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ public class DefaultPrettyPrinter
3333
* root values: a single space character.
3434
*
3535
* @since 2.1
36+
* @deprecated in 2.16. Use the Separators API instead.
3637
*/
38+
@Deprecated
3739
public final static SerializedString DEFAULT_ROOT_VALUE_SEPARATOR = new SerializedString(" ");
3840

3941
/**
@@ -70,16 +72,21 @@ public interface Indenter
7072

7173
/**
7274
* String printed between root-level values, if any.
75+
*
76+
* @deprecated in 2.16. Use Separators API instead.
7377
*/
74-
protected final SerializableString _rootSeparator;
78+
protected SerializableString _rootSeparator;
7579

7680
// // // Config, other white space configuration
7781

7882
/**
7983
* By default we will add spaces around colons used to
8084
* separate object fields and values.
8185
* If disabled, will not use spaces around colon.
86+
*
87+
* @deprecated in 2.16. Use Separators API instead.
8288
*/
89+
@Deprecated
8390
protected boolean _spacesInObjectEntries = true;
8491

8592
// // // State:
@@ -99,15 +106,25 @@ public interface Indenter
99106
* @since 2.9
100107
*/
101108
protected String _objectFieldValueSeparatorWithSpaces;
109+
110+
/**
111+
* @since 2.16
112+
*/
113+
protected String _objectEntrySeparator;
102114

115+
/**
116+
* @since 2.16
117+
*/
118+
protected String _arrayValueSeparator;
119+
103120
/*
104121
/**********************************************************
105122
/* Life-cycle (construct, configure)
106123
/**********************************************************
107124
*/
108125

109126
public DefaultPrettyPrinter() {
110-
this(DEFAULT_ROOT_VALUE_SEPARATOR);
127+
this(DEFAULT_SEPARATORS);
111128
}
112129

113130
/**
@@ -118,7 +135,9 @@ public DefaultPrettyPrinter() {
118135
* calls {@link #DefaultPrettyPrinter(SerializableString)}
119136
*
120137
* @param rootSeparator String to use as root value separator
138+
* @deprecated in 2.16. Use the Separators API instead.
121139
*/
140+
@Deprecated
122141
public DefaultPrettyPrinter(String rootSeparator) {
123142
this((rootSeparator == null) ? null : new SerializedString(rootSeparator));
124143
}
@@ -128,16 +147,17 @@ public DefaultPrettyPrinter(String rootSeparator) {
128147
* if null, no separator is printed.
129148
*
130149
* @param rootSeparator String to use as root value separator
150+
* @deprecated in 2.16. Use the Separators API instead.
131151
*/
152+
@Deprecated
132153
public DefaultPrettyPrinter(SerializableString rootSeparator) {
133-
_rootSeparator = rootSeparator;
134-
withSeparators(DEFAULT_SEPARATORS);
135-
}
136-
137-
public DefaultPrettyPrinter(DefaultPrettyPrinter base) {
138-
this(base, base._rootSeparator);
154+
this(DEFAULT_SEPARATORS.withRootSeparator(rootSeparator.getValue()));
139155
}
140156

157+
/**
158+
* @deprecated in 2.16. Use the Separators API instead.
159+
*/
160+
@Deprecated
141161
public DefaultPrettyPrinter(DefaultPrettyPrinter base,
142162
SerializableString rootSeparator)
143163
{
@@ -148,17 +168,58 @@ public DefaultPrettyPrinter(DefaultPrettyPrinter base,
148168

149169
_separators = base._separators;
150170
_objectFieldValueSeparatorWithSpaces = base._objectFieldValueSeparatorWithSpaces;
171+
_objectEntrySeparator = base._objectEntrySeparator;
172+
_arrayValueSeparator = base._arrayValueSeparator;
151173

152174
_rootSeparator = rootSeparator;
153175
}
154176

177+
/**
178+
* @since 2.16
179+
*/
180+
public DefaultPrettyPrinter(Separators separators)
181+
{
182+
_separators = separators;
183+
184+
_rootSeparator = separators.getRootSeparator() == null ? null : new SerializedString(separators.getRootSeparator());
185+
_objectFieldValueSeparatorWithSpaces = separators.getObjectFieldValueSpacing().apply(
186+
separators.getObjectFieldValueSeparator());
187+
_objectEntrySeparator = separators.getObjectEntrySpacing().apply(separators.getObjectEntrySeparator());
188+
_arrayValueSeparator = separators.getArrayValueSpacing().apply(separators.getArrayValueSeparator());
189+
}
190+
191+
/**
192+
* Copy constructor
193+
*
194+
* @since 2.16
195+
*/
196+
public DefaultPrettyPrinter(DefaultPrettyPrinter base) {
197+
_rootSeparator = base._rootSeparator;
198+
199+
_arrayIndenter = base._arrayIndenter;
200+
_objectIndenter = base._objectIndenter;
201+
_spacesInObjectEntries = base._spacesInObjectEntries;
202+
_nesting = base._nesting;
203+
204+
_separators = base._separators;
205+
_objectFieldValueSeparatorWithSpaces = base._objectFieldValueSeparatorWithSpaces;
206+
_objectEntrySeparator = base._objectEntrySeparator;
207+
_arrayValueSeparator = base._arrayValueSeparator;
208+
}
209+
210+
/**
211+
* @deprecated in 2.16. Use the Separators API instead.
212+
*/
213+
@Deprecated
155214
public DefaultPrettyPrinter withRootSeparator(SerializableString rootSeparator)
156215
{
157216
if (_rootSeparator == rootSeparator ||
158217
(rootSeparator != null && rootSeparator.equals(_rootSeparator))) {
159218
return this;
160219
}
161-
return new DefaultPrettyPrinter(this, rootSeparator);
220+
Separators separators = _separators.withRootSeparator(rootSeparator == null ? null : rootSeparator.getValue());
221+
return new DefaultPrettyPrinter(this)
222+
.withSeparators(separators);
162223
}
163224

164225
/**
@@ -167,7 +228,9 @@ public DefaultPrettyPrinter withRootSeparator(SerializableString rootSeparator)
167228
* @return This pretty-printer instance (for call chaining)
168229
*
169230
* @since 2.6
231+
* @deprecated in 2.16. Use the Separators API instead.
170232
*/
233+
@Deprecated
171234
public DefaultPrettyPrinter withRootSeparator(String rootSeparator) {
172235
return withRootSeparator((rootSeparator == null) ? null : new SerializedString(rootSeparator));
173236
}
@@ -180,7 +243,7 @@ public void indentObjectsWith(Indenter i) {
180243
_objectIndenter = (i == null) ? NopIndenter.instance : i;
181244
}
182245

183-
// @since 2.3
246+
/** @since 2.3 */
184247
public DefaultPrettyPrinter withArrayIndenter(Indenter i) {
185248
if (i == null) {
186249
i = NopIndenter.instance;
@@ -193,7 +256,7 @@ public DefaultPrettyPrinter withArrayIndenter(Indenter i) {
193256
return pp;
194257
}
195258

196-
// @since 2.3
259+
/** @since 2.3 */
197260
public DefaultPrettyPrinter withObjectIndenter(Indenter i) {
198261
if (i == null) {
199262
i = NopIndenter.instance;
@@ -215,7 +278,9 @@ public DefaultPrettyPrinter withObjectIndenter(Indenter i) {
215278
* @return This pretty-printer instance (for call chaining)
216279
*
217280
* @since 2.3
281+
* @deprecated in 2.16. Use the Separators API instead.
218282
*/
283+
@Deprecated
219284
public DefaultPrettyPrinter withSpacesInObjectEntries() {
220285
return _withSpaces(true);
221286
}
@@ -229,7 +294,9 @@ public DefaultPrettyPrinter withSpacesInObjectEntries() {
229294
* @return This pretty-printer instance (for call chaining)
230295
*
231296
* @since 2.3
297+
* @deprecated in 2.16. Use the Separators API instead.
232298
*/
299+
@Deprecated
233300
public DefaultPrettyPrinter withoutSpacesInObjectEntries() {
234301
return _withSpaces(false);
235302
}
@@ -239,7 +306,9 @@ protected DefaultPrettyPrinter _withSpaces(boolean state)
239306
if (_spacesInObjectEntries == state) {
240307
return this;
241308
}
242-
DefaultPrettyPrinter pp = new DefaultPrettyPrinter(this);
309+
310+
Separators copy = _separators.withObjectFieldValueSpacing(state ? Separators.Spacing.BOTH : Separators.Spacing.NONE);
311+
DefaultPrettyPrinter pp = withSeparators(copy);
243312
pp._spacesInObjectEntries = state;
244313
return pp;
245314
}
@@ -254,9 +323,16 @@ protected DefaultPrettyPrinter _withSpaces(boolean state)
254323
* @since 2.9
255324
*/
256325
public DefaultPrettyPrinter withSeparators(Separators separators) {
257-
_separators = separators;
258-
_objectFieldValueSeparatorWithSpaces = " " + separators.getObjectFieldValueSeparator() + " ";
259-
return this;
326+
DefaultPrettyPrinter result = new DefaultPrettyPrinter(this);
327+
result._separators = separators;
328+
329+
result._rootSeparator = separators.getRootSeparator() == null ? null : new SerializedString(separators.getRootSeparator());
330+
result._objectFieldValueSeparatorWithSpaces = separators.getObjectFieldValueSpacing().apply(
331+
separators.getObjectFieldValueSeparator());
332+
result._objectEntrySeparator = separators.getObjectEntrySpacing().apply(separators.getObjectEntrySeparator());
333+
result._arrayValueSeparator = separators.getArrayValueSpacing().apply(separators.getArrayValueSeparator());
334+
335+
return result;
260336
}
261337

262338
/*
@@ -315,11 +391,7 @@ public void beforeObjectEntries(JsonGenerator g) throws IOException
315391
@Override
316392
public void writeObjectFieldValueSeparator(JsonGenerator g) throws IOException
317393
{
318-
if (_spacesInObjectEntries) {
319-
g.writeRaw(_objectFieldValueSeparatorWithSpaces);
320-
} else {
321-
g.writeRaw(_separators.getObjectFieldValueSeparator());
322-
}
394+
g.writeRaw(_objectFieldValueSeparatorWithSpaces);
323395
}
324396

325397
/**
@@ -334,7 +406,7 @@ public void writeObjectFieldValueSeparator(JsonGenerator g) throws IOException
334406
@Override
335407
public void writeObjectEntrySeparator(JsonGenerator g) throws IOException
336408
{
337-
g.writeRaw(_separators.getObjectEntrySeparator());
409+
g.writeRaw(_objectEntrySeparator);
338410
_objectIndenter.writeIndentation(g, _nesting);
339411
}
340412

@@ -378,7 +450,7 @@ public void beforeArrayValues(JsonGenerator g) throws IOException {
378450
@Override
379451
public void writeArrayValueSeparator(JsonGenerator g) throws IOException
380452
{
381-
g.writeRaw(_separators.getArrayValueSeparator());
453+
g.writeRaw(_arrayValueSeparator);
382454
_arrayIndenter.writeIndentation(g, _nesting);
383455
}
384456

src/main/java/com/fasterxml/jackson/core/util/MinimalPrettyPrinter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import com.fasterxml.jackson.core.JsonGenerator;
66
import com.fasterxml.jackson.core.PrettyPrinter;
7+
import com.fasterxml.jackson.core.util.Separators.Spacing;
78

89
/**
910
* {@link PrettyPrinter} implementation that adds no indentation,
@@ -46,7 +47,7 @@ public MinimalPrettyPrinter() {
4647

4748
public MinimalPrettyPrinter(String rootValueSeparator) {
4849
_rootValueSeparator = rootValueSeparator;
49-
_separators = DEFAULT_SEPARATORS;
50+
_separators = DEFAULT_SEPARATORS.withObjectFieldValueSpacing(Spacing.NONE);
5051
}
5152

5253
public void setRootValueSeparator(String sep) {

0 commit comments

Comments
 (0)