Skip to content

Commit b2d692a

Browse files
authored
Merge pull request #374 from rfoltyns/feature/pp_configurable_separators
Minimal and DefaultPrettyPrinter with configurable separators
2 parents 97db1f8 + 8e367b5 commit b2d692a

File tree

4 files changed

+156
-41
lines changed

4 files changed

+156
-41
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.fasterxml.jackson.core;
2+
3+
import java.io.Serializable;
4+
5+
public class Separators implements Serializable {
6+
7+
private static final long serialVersionUID = 1;
8+
9+
private char objectFieldValueSeparator = ':';
10+
private char objectEntrySeparator = ',';
11+
private char arrayValueSeparator = ',';
12+
13+
public static Separators createDefaultInstance() {
14+
return new Separators();
15+
}
16+
17+
public Separators withObjectFieldValueSeparator(char objectFieldValueSeparator) {
18+
this.objectFieldValueSeparator = objectFieldValueSeparator;
19+
return this;
20+
}
21+
22+
public Separators withObjectEntrySeparator(char objectEntrySeparator) {
23+
this.objectEntrySeparator = objectEntrySeparator;
24+
return this;
25+
}
26+
27+
public Separators withArrayValueSeparator(char arrayValueSeparator) {
28+
this.arrayValueSeparator = arrayValueSeparator;
29+
return this;
30+
}
31+
32+
public char getObjectFieldValueSeparator() {
33+
return objectFieldValueSeparator;
34+
}
35+
36+
public char getObjectEntrySeparator() {
37+
return objectEntrySeparator;
38+
}
39+
40+
public char getArrayValueSeparator() {
41+
return arrayValueSeparator;
42+
}
43+
}

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

+29-17
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class DefaultPrettyPrinter
2626
* @since 2.1
2727
*/
2828
public final static SerializedString DEFAULT_ROOT_VALUE_SEPARATOR = new SerializedString(" ");
29-
29+
3030
/**
3131
* Interface that defines objects that can produce indentation used
3232
* to separate object entries and array values. Indentation in this
@@ -43,7 +43,7 @@ public interface Indenter
4343
*/
4444
boolean isInline();
4545
}
46-
46+
4747
// // // Config, indentation
4848

4949
/**
@@ -63,7 +63,7 @@ public interface Indenter
6363
* String printed between root-level values, if any.
6464
*/
6565
protected final SerializableString _rootSeparator;
66-
66+
6767
// // // Config, other white space configuration
6868

6969
/**
@@ -81,6 +81,10 @@ public interface Indenter
8181
*/
8282
protected transient int _nesting;
8383

84+
protected Separators _separators = Separators.createDefaultInstance();
85+
86+
private String _objectFieldValueSeparatorWithSpaces = " " + _separators.getObjectFieldValueSeparator() + " ";
87+
8488
/*
8589
/**********************************************************
8690
/* Life-cycle (construct, configure)
@@ -129,6 +133,8 @@ public DefaultPrettyPrinter(DefaultPrettyPrinter base,
129133
_objectIndenter = base._objectIndenter;
130134
_spacesInObjectEntries = base._spacesInObjectEntries;
131135
_nesting = base._nesting;
136+
_separators = base._separators;
137+
_objectFieldValueSeparatorWithSpaces = " " + base._separators.getObjectFieldValueSeparator() + " ";
132138

133139
_rootSeparator = rootSeparator;
134140
}
@@ -148,7 +154,7 @@ public DefaultPrettyPrinter withRootSeparator(SerializableString rootSeparator)
148154
public DefaultPrettyPrinter withRootSeparator(String rootSeparator) {
149155
return withRootSeparator((rootSeparator == null) ? null : new SerializedString(rootSeparator));
150156
}
151-
157+
152158
public void indentArraysWith(Indenter i) {
153159
_arrayIndenter = (i == null) ? NopIndenter.instance : i;
154160
}
@@ -210,7 +216,7 @@ public DefaultPrettyPrinter withSpacesInObjectEntries() {
210216
* that does not use spaces inside object entries; if 'this' instance already
211217
* does this, it is returned; if not, a new instance will be constructed
212218
* and returned.
213-
*
219+
*
214220
* @since 2.3
215221
*/
216222
public DefaultPrettyPrinter withoutSpacesInObjectEntries() {
@@ -226,13 +232,19 @@ protected DefaultPrettyPrinter _withSpaces(boolean state)
226232
pp._spacesInObjectEntries = state;
227233
return pp;
228234
}
229-
235+
236+
public DefaultPrettyPrinter withCustomSeparators(Separators separators) {
237+
this._separators = separators;
238+
this._objectFieldValueSeparatorWithSpaces = " " + separators.getObjectFieldValueSeparator() + " ";
239+
return this;
240+
}
241+
230242
/*
231243
/**********************************************************
232244
/* Instantiatable impl
233245
/**********************************************************
234246
*/
235-
247+
236248
@Override
237249
public DefaultPrettyPrinter createInstance() {
238250
return new DefaultPrettyPrinter(this);
@@ -280,9 +292,9 @@ public void beforeObjectEntries(JsonGenerator g) throws IOException
280292
public void writeObjectFieldValueSeparator(JsonGenerator g) throws IOException
281293
{
282294
if (_spacesInObjectEntries) {
283-
g.writeRaw(" : ");
295+
g.writeRaw(_objectFieldValueSeparatorWithSpaces);
284296
} else {
285-
g.writeRaw(':');
297+
g.writeRaw(_separators.getObjectFieldValueSeparator());
286298
}
287299
}
288300

@@ -298,7 +310,7 @@ public void writeObjectFieldValueSeparator(JsonGenerator g) throws IOException
298310
@Override
299311
public void writeObjectEntrySeparator(JsonGenerator g) throws IOException
300312
{
301-
g.writeRaw(',');
313+
g.writeRaw(_separators.getObjectEntrySeparator());
302314
_objectIndenter.writeIndentation(g, _nesting);
303315
}
304316

@@ -340,24 +352,24 @@ public void beforeArrayValues(JsonGenerator g) throws IOException {
340352
* (white-space) decoration.
341353
*/
342354
@Override
343-
public void writeArrayValueSeparator(JsonGenerator gen) throws IOException
355+
public void writeArrayValueSeparator(JsonGenerator g) throws IOException
344356
{
345-
gen.writeRaw(',');
346-
_arrayIndenter.writeIndentation(gen, _nesting);
357+
g.writeRaw(_separators.getArrayValueSeparator());
358+
_arrayIndenter.writeIndentation(g, _nesting);
347359
}
348360

349361
@Override
350-
public void writeEndArray(JsonGenerator gen, int nrOfValues) throws IOException
362+
public void writeEndArray(JsonGenerator g, int nrOfValues) throws IOException
351363
{
352364
if (!_arrayIndenter.isInline()) {
353365
--_nesting;
354366
}
355367
if (nrOfValues > 0) {
356-
_arrayIndenter.writeIndentation(gen, _nesting);
368+
_arrayIndenter.writeIndentation(g, _nesting);
357369
} else {
358-
gen.writeRaw(' ');
370+
g.writeRaw(' ');
359371
}
360-
gen.writeRaw(']');
372+
g.writeRaw(']');
361373
}
362374

363375
/*

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

+11-5
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.Separators;
78

89
/**
910
* {@link PrettyPrinter} implementation that adds no indentation,
@@ -34,6 +35,8 @@ public class MinimalPrettyPrinter
3435

3536
protected String _rootValueSeparator = DEFAULT_ROOT_VALUE_SEPARATOR;
3637

38+
protected Separators _separators = Separators.createDefaultInstance();
39+
3740
/*
3841
/**********************************************************
3942
/* Life-cycle, construction, configuration
@@ -47,7 +50,10 @@ public MinimalPrettyPrinter() {
4750
public MinimalPrettyPrinter(String rootValueSeparator) {
4851
_rootValueSeparator = rootValueSeparator;
4952
}
50-
53+
public MinimalPrettyPrinter withCustomSeparators(Separators separators) {
54+
this._separators = separators;
55+
return this;
56+
}
5157
public void setRootValueSeparator(String sep) {
5258
_rootValueSeparator = sep;
5359
}
@@ -62,7 +68,7 @@ public void setRootValueSeparator(String sep) {
6268
public void writeRootValueSeparator(JsonGenerator g) throws IOException
6369
{
6470
if (_rootValueSeparator != null) {
65-
g.writeRaw(_rootValueSeparator);
71+
g.writeRaw(_rootValueSeparator);
6672
}
6773
}
6874

@@ -88,7 +94,7 @@ public void beforeObjectEntries(JsonGenerator g) throws IOException
8894
@Override
8995
public void writeObjectFieldValueSeparator(JsonGenerator g) throws IOException
9096
{
91-
g.writeRaw(':');
97+
g.writeRaw(_separators.getObjectFieldValueSeparator());
9298
}
9399

94100
/**
@@ -101,7 +107,7 @@ public void writeObjectFieldValueSeparator(JsonGenerator g) throws IOException
101107
@Override
102108
public void writeObjectEntrySeparator(JsonGenerator g) throws IOException
103109
{
104-
g.writeRaw(',');
110+
g.writeRaw(_separators.getObjectEntrySeparator());
105111
}
106112

107113
@Override
@@ -132,7 +138,7 @@ public void beforeArrayValues(JsonGenerator g) throws IOException
132138
@Override
133139
public void writeArrayValueSeparator(JsonGenerator g) throws IOException
134140
{
135-
g.writeRaw(',');
141+
g.writeRaw(_separators.getArrayValueSeparator());
136142
}
137143

138144
@Override

src/test/java/com/fasterxml/jackson/core/main/TestPrettyPrinter.java

+73-19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.fasterxml.jackson.core.main;
22

33
import com.fasterxml.jackson.core.*;
4+
import com.fasterxml.jackson.core.util.DefaultIndenter;
45
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
56
import com.fasterxml.jackson.core.util.MinimalPrettyPrinter;
67

@@ -153,32 +154,65 @@ public void testCustomRootSeparatorWithFactory() throws Exception
153154
gen.close();
154155
assertEquals("13##false##null", sw.toString());
155156
}
156-
157+
158+
public void testCustomSeparatorsWithMinimal() throws Exception
159+
{
160+
StringWriter sw = new StringWriter();
161+
JsonGenerator gen = new JsonFactory().createGenerator(sw);
162+
gen.setPrettyPrinter(new MinimalPrettyPrinter().withCustomSeparators(Separators.createDefaultInstance()
163+
.withObjectFieldValueSeparator('=')
164+
.withObjectEntrySeparator(';')
165+
.withArrayValueSeparator('|')));
166+
167+
_writeTestDocument(gen);
168+
169+
assertEquals("[3|\"abc\"|[true]|{\"f\"=null;\"f2\"=null}]", sw.toString());
170+
}
171+
172+
public void testCustomSeparatorsWithPP() throws Exception
173+
{
174+
StringWriter sw = new StringWriter();
175+
JsonGenerator gen = new JsonFactory().createGenerator(sw);
176+
gen.setPrettyPrinter(new DefaultPrettyPrinter().withCustomSeparators(Separators.createDefaultInstance()
177+
.withObjectFieldValueSeparator('=')
178+
.withObjectEntrySeparator(';')
179+
.withArrayValueSeparator('|')));
180+
181+
_writeTestDocument(gen);
182+
183+
assertEquals("[ 3| \"abc\"| [ true ]| {" + DefaultIndenter.SYS_LF +
184+
" \"f\" = null;" + DefaultIndenter.SYS_LF +
185+
" \"f2\" = null" + DefaultIndenter.SYS_LF +
186+
"} ]", sw.toString());
187+
}
188+
189+
public void testCustomSeparatorsWithPPWithoutSpaces() throws Exception
190+
{
191+
StringWriter sw = new StringWriter();
192+
JsonGenerator gen = new JsonFactory().createGenerator(sw);
193+
gen.setPrettyPrinter(new DefaultPrettyPrinter().withCustomSeparators(Separators.createDefaultInstance()
194+
.withObjectFieldValueSeparator('=')
195+
.withObjectEntrySeparator(';')
196+
.withArrayValueSeparator('|'))
197+
.withoutSpacesInObjectEntries());
198+
199+
_writeTestDocument(gen);
200+
201+
assertEquals("[ 3| \"abc\"| [ true ]| {" + DefaultIndenter.SYS_LF +
202+
" \"f\"=null;" + DefaultIndenter.SYS_LF +
203+
" \"f2\"=null" + DefaultIndenter.SYS_LF +
204+
"} ]", sw.toString());
205+
}
206+
157207
/*
158208
/**********************************************************
159209
/* Helper methods
160210
/**********************************************************
161211
*/
162212

163213
private String _verifyPrettyPrinter(JsonGenerator gen, StringWriter sw) throws Exception
164-
{
165-
gen.writeStartArray();
166-
gen.writeNumber(3);
167-
gen.writeString("abc");
168-
169-
gen.writeStartArray();
170-
gen.writeBoolean(true);
171-
gen.writeEndArray();
172-
173-
gen.writeStartObject();
174-
gen.writeFieldName("f");
175-
gen.writeNull();
176-
gen.writeFieldName("f2");
177-
gen.writeNull();
178-
gen.writeEndObject();
179-
180-
gen.writeEndArray();
181-
gen.close();
214+
{
215+
_writeTestDocument(gen);
182216

183217
String docStr = sw.toString();
184218
JsonParser jp = createParserUsingReader(docStr);
@@ -210,6 +244,26 @@ private String _verifyPrettyPrinter(JsonGenerator gen, StringWriter sw) throws E
210244
return docStr;
211245
}
212246

247+
private void _writeTestDocument(JsonGenerator gen) throws IOException {
248+
gen.writeStartArray();
249+
gen.writeNumber(3);
250+
gen.writeString("abc");
251+
252+
gen.writeStartArray();
253+
gen.writeBoolean(true);
254+
gen.writeEndArray();
255+
256+
gen.writeStartObject();
257+
gen.writeFieldName("f");
258+
gen.writeNull();
259+
gen.writeFieldName("f2");
260+
gen.writeNull();
261+
gen.writeEndObject();
262+
263+
gen.writeEndArray();
264+
gen.close();
265+
}
266+
213267
protected String _generateRoot(JsonFactory jf, PrettyPrinter pp) throws IOException
214268
{
215269
StringWriter sw = new StringWriter();

0 commit comments

Comments
 (0)