Skip to content

Commit 5333247

Browse files
committed
Minor tweaks to #374, update release notes
1 parent b2d692a commit 5333247

File tree

8 files changed

+131
-92
lines changed

8 files changed

+131
-92
lines changed

release-notes/CREDITS

+4
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,7 @@ Michael Sims (MichaelSims@github)
135135
* Reported, contributed fix for #372: JsonParserSequence#skipChildren() throws exception
136136
when current delegate is TokenBuffer.Parser with "incomplete" JSON
137137
(2.9.0)
138+
139+
Rafal Foltynski (rfoltyns@github)
140+
* Contributed #374: Minimal and DefaultPrettyPrinter with configurable separators
141+
(2.9.0)

release-notes/VERSION

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ JSON library.
3434
#372: JsonParserSequence#skipChildren() throws exception when current delegate is
3535
TokenBuffer.Parser with "incomplete" JSON
3636
(contributed by Michael S)
37+
#374: Minimal and DefaultPrettyPrinter with configurable separators
38+
(contributed by Rafal F)
3739

3840
2.8.8 (05-Apr-2017)
3941

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

+26-21
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
import java.io.IOException;
99

10+
import com.fasterxml.jackson.core.io.SerializedString;
11+
import com.fasterxml.jackson.core.util.Separators;
12+
1013
/**
1114
* Interface for objects that implement pretty printer functionality, such
1215
* as indentation.
@@ -17,11 +20,23 @@
1720
* Note: since Jackson 2.1, stateful implementations MUST implement
1821
* {@link com.fasterxml.jackson.core.util.Instantiatable} interface,
1922
* to allow for constructing per-generation instances and avoid
20-
* state corruption (see [JACKSON-851] for details).
23+
* state corruption.
2124
* Stateless implementations need not do this; but those are less common.
2225
*/
2326
public interface PrettyPrinter
2427
{
28+
/**
29+
* @since 2.9
30+
*/
31+
public final static Separators DEFAULT_SEPARATORS = Separators.createDefaultInstance();
32+
33+
/**
34+
* Default String used for separating root values is single space.
35+
*
36+
* @since 2.9
37+
*/
38+
public final static SerializedString DEFAULT_ROOT_VALUE_SEPARATOR = new SerializedString(" ");
39+
2540
/*
2641
/**********************************************************
2742
/* First methods that act both as events, and expect
@@ -42,8 +57,7 @@ public interface PrettyPrinter
4257
* to output some other suitable and nice-looking separator
4358
* (tab(s), space(s), linefeed(s) or any combination thereof).
4459
*/
45-
void writeRootValueSeparator(JsonGenerator jg)
46-
throws IOException, JsonGenerationException;
60+
void writeRootValueSeparator(JsonGenerator gen) throws IOException;
4761

4862
// // Object handling
4963

@@ -57,8 +71,7 @@ void writeRootValueSeparator(JsonGenerator jg)
5771
* to output a curly bracket as well, but can surround that
5872
* with other (white-space) decoration.
5973
*/
60-
void writeStartObject(JsonGenerator gen)
61-
throws IOException, JsonGenerationException;
74+
void writeStartObject(JsonGenerator gen) throws IOException;
6275

6376
/**
6477
* Method called after an Object value has been completely output
@@ -73,8 +86,7 @@ void writeStartObject(JsonGenerator gen)
7386
* @param nrOfEntries Number of direct members of the array that
7487
* have been output
7588
*/
76-
void writeEndObject(JsonGenerator gen, int nrOfEntries)
77-
throws IOException, JsonGenerationException;
89+
void writeEndObject(JsonGenerator gen, int nrOfEntries) throws IOException;
7890

7991
/**
8092
* Method called after an object entry (field:value) has been completely
@@ -85,8 +97,7 @@ void writeEndObject(JsonGenerator gen, int nrOfEntries)
8597
* to output a comma as well, but can surround that with other
8698
* (white-space) decoration.
8799
*/
88-
void writeObjectEntrySeparator(JsonGenerator gen)
89-
throws IOException, JsonGenerationException;
100+
void writeObjectEntrySeparator(JsonGenerator gen) throws IOException;
90101

91102
/**
92103
* Method called after an object field has been output, but
@@ -97,8 +108,7 @@ void writeObjectEntrySeparator(JsonGenerator gen)
97108
* to output a colon as well, but can surround that with other
98109
* (white-space) decoration.
99110
*/
100-
void writeObjectFieldValueSeparator(JsonGenerator gen)
101-
throws IOException, JsonGenerationException;
111+
void writeObjectFieldValueSeparator(JsonGenerator gen) throws IOException;
102112

103113
// // // Array handling
104114

@@ -112,8 +122,7 @@ void writeObjectFieldValueSeparator(JsonGenerator gen)
112122
* to output a bracket as well, but can surround that
113123
* with other (white-space) decoration.
114124
*/
115-
void writeStartArray(JsonGenerator gen)
116-
throws IOException, JsonGenerationException;
125+
void writeStartArray(JsonGenerator gen) throws IOException;
117126

118127
/**
119128
* Method called after an Array value has been completely output
@@ -128,8 +137,7 @@ void writeStartArray(JsonGenerator gen)
128137
* @param nrOfValues Number of direct members of the array that
129138
* have been output
130139
*/
131-
void writeEndArray(JsonGenerator gen, int nrOfValues)
132-
throws IOException, JsonGenerationException;
140+
void writeEndArray(JsonGenerator gen, int nrOfValues) throws IOException;
133141

134142
/**
135143
* Method called after an array value has been completely
@@ -140,8 +148,7 @@ void writeEndArray(JsonGenerator gen, int nrOfValues)
140148
* to output a comma as well, but can surround that with other
141149
* (white-space) decoration.
142150
*/
143-
void writeArrayValueSeparator(JsonGenerator gen)
144-
throws IOException, JsonGenerationException;
151+
void writeArrayValueSeparator(JsonGenerator gen) throws IOException;
145152

146153
/*
147154
/**********************************************************
@@ -159,8 +166,7 @@ void writeArrayValueSeparator(JsonGenerator gen)
159166
* Default handling does not output anything, but pretty-printer
160167
* is free to add any white space decoration.
161168
*/
162-
void beforeArrayValues(JsonGenerator gen)
163-
throws IOException, JsonGenerationException;
169+
void beforeArrayValues(JsonGenerator gen) throws IOException;
164170

165171
/**
166172
* Method called after object start marker has been output,
@@ -171,7 +177,6 @@ void beforeArrayValues(JsonGenerator gen)
171177
* Default handling does not output anything, but pretty-printer
172178
* is free to add any white space decoration.
173179
*/
174-
void beforeObjectEntries(JsonGenerator gen)
175-
throws IOException, JsonGenerationException;
180+
void beforeObjectEntries(JsonGenerator gen) throws IOException;
176181
}
177182

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

-43
This file was deleted.

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

+17-13
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,15 @@ public interface Indenter
8181
*/
8282
protected transient int _nesting;
8383

84-
protected Separators _separators = Separators.createDefaultInstance();
84+
/**
85+
* @since 2.9
86+
*/
87+
protected Separators _separators;
8588

86-
private String _objectFieldValueSeparatorWithSpaces = " " + _separators.getObjectFieldValueSeparator() + " ";
89+
/**
90+
* @since 2.9
91+
*/
92+
protected String _objectFieldValueSeparatorWithSpaces;
8793

8894
/*
8995
/**********************************************************
@@ -120,6 +126,7 @@ public DefaultPrettyPrinter(String rootSeparator) {
120126
*/
121127
public DefaultPrettyPrinter(SerializableString rootSeparator) {
122128
_rootSeparator = rootSeparator;
129+
withSeparators(DEFAULT_SEPARATORS);
123130
}
124131

125132
public DefaultPrettyPrinter(DefaultPrettyPrinter base) {
@@ -133,8 +140,9 @@ public DefaultPrettyPrinter(DefaultPrettyPrinter base,
133140
_objectIndenter = base._objectIndenter;
134141
_spacesInObjectEntries = base._spacesInObjectEntries;
135142
_nesting = base._nesting;
143+
136144
_separators = base._separators;
137-
_objectFieldValueSeparatorWithSpaces = " " + base._separators.getObjectFieldValueSeparator() + " ";
145+
_objectFieldValueSeparatorWithSpaces = base._objectFieldValueSeparatorWithSpaces;
138146

139147
_rootSeparator = rootSeparator;
140148
}
@@ -163,12 +171,6 @@ public void indentObjectsWith(Indenter i) {
163171
_objectIndenter = (i == null) ? NopIndenter.instance : i;
164172
}
165173

166-
/**
167-
* @deprecated Since 2.3 use {@link #withSpacesInObjectEntries} and {@link #withoutSpacesInObjectEntries()}
168-
*/
169-
@Deprecated
170-
public void spacesInObjectEntries(boolean b) { _spacesInObjectEntries = b; }
171-
172174
/**
173175
* @since 2.3
174176
*/
@@ -233,9 +235,12 @@ protected DefaultPrettyPrinter _withSpaces(boolean state)
233235
return pp;
234236
}
235237

236-
public DefaultPrettyPrinter withCustomSeparators(Separators separators) {
237-
this._separators = separators;
238-
this._objectFieldValueSeparatorWithSpaces = " " + separators.getObjectFieldValueSeparator() + " ";
238+
/**
239+
* @since 2.9
240+
*/
241+
public DefaultPrettyPrinter withSeparators(Separators separators) {
242+
_separators = separators;
243+
_objectFieldValueSeparatorWithSpaces = " " + separators.getObjectFieldValueSeparator() + " ";
239244
return this;
240245
}
241246

@@ -400,7 +405,6 @@ public void writeIndentation(JsonGenerator g, int level) throws IOException { }
400405
*/
401406
public static class FixedSpaceIndenter extends NopIndenter
402407
{
403-
@SuppressWarnings("hiding")
404408
public static final FixedSpaceIndenter instance = new FixedSpaceIndenter();
405409

406410
@Override

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

+15-12
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import com.fasterxml.jackson.core.JsonGenerator;
66
import com.fasterxml.jackson.core.PrettyPrinter;
7-
import com.fasterxml.jackson.core.Separators;
87

98
/**
109
* {@link PrettyPrinter} implementation that adds no indentation,
@@ -28,14 +27,12 @@ public class MinimalPrettyPrinter
2827
{
2928
private static final long serialVersionUID = 1L;
3029

30+
protected String _rootValueSeparator;
31+
3132
/**
32-
* Default String used for separating root values is single space.
33+
* @since 2.9
3334
*/
34-
public final static String DEFAULT_ROOT_VALUE_SEPARATOR = " ";
35-
36-
protected String _rootValueSeparator = DEFAULT_ROOT_VALUE_SEPARATOR;
37-
38-
protected Separators _separators = Separators.createDefaultInstance();
35+
protected Separators _separators;
3936

4037
/*
4138
/**********************************************************
@@ -44,20 +41,26 @@ public class MinimalPrettyPrinter
4441
*/
4542

4643
public MinimalPrettyPrinter() {
47-
this(DEFAULT_ROOT_VALUE_SEPARATOR);
44+
this(DEFAULT_ROOT_VALUE_SEPARATOR.toString());
4845
}
4946

5047
public MinimalPrettyPrinter(String rootValueSeparator) {
5148
_rootValueSeparator = rootValueSeparator;
49+
_separators = DEFAULT_SEPARATORS;
5250
}
53-
public MinimalPrettyPrinter withCustomSeparators(Separators separators) {
54-
this._separators = separators;
55-
return this;
56-
}
51+
5752
public void setRootValueSeparator(String sep) {
5853
_rootValueSeparator = sep;
5954
}
6055

56+
/**
57+
* @since 2.9
58+
*/
59+
public MinimalPrettyPrinter setSeparators(Separators separators) {
60+
_separators = separators;
61+
return this;
62+
}
63+
6164
/*
6265
/**********************************************************
6366
/* PrettyPrinter impl
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.fasterxml.jackson.core.util;
2+
3+
import java.io.Serializable;
4+
5+
/**
6+
* Value class used with some {@link com.fasterxml.jackson.core.PrettyPrinter}
7+
* implements
8+
*
9+
* @see com.fasterxml.jackson.core.util.DefaultPrettyPrinter
10+
* @see com.fasterxml.jackson.core.util.MinimalPrettyPrinter
11+
*
12+
* @since 2.9
13+
*/
14+
public class Separators implements Serializable
15+
{
16+
private static final long serialVersionUID = 1;
17+
18+
private final char objectFieldValueSeparator;
19+
private final char objectEntrySeparator;
20+
private final char arrayValueSeparator;
21+
22+
public static Separators createDefaultInstance() {
23+
return new Separators();
24+
}
25+
26+
public Separators() {
27+
this(':', ',', ',');
28+
}
29+
30+
public Separators(char objectFieldValueSeparator,
31+
char objectEntrySeparator, char arrayValueSeparator) {
32+
this.objectFieldValueSeparator = objectFieldValueSeparator;
33+
this.objectEntrySeparator = objectEntrySeparator;
34+
this.arrayValueSeparator = arrayValueSeparator;
35+
}
36+
37+
public Separators withObjectFieldValueSeparator(char sep) {
38+
return (objectFieldValueSeparator == sep) ? this
39+
: new Separators(sep, objectEntrySeparator, arrayValueSeparator);
40+
}
41+
42+
public Separators withObjectEntrySeparator(char sep) {
43+
return (objectEntrySeparator == sep) ? this
44+
: new Separators(objectFieldValueSeparator, sep, arrayValueSeparator);
45+
}
46+
47+
public Separators withArrayValueSeparator(char sep) {
48+
return (arrayValueSeparator == sep) ? this
49+
: new Separators(objectFieldValueSeparator, objectEntrySeparator, sep);
50+
}
51+
52+
public char getObjectFieldValueSeparator() {
53+
return objectFieldValueSeparator;
54+
}
55+
56+
public char getObjectEntrySeparator() {
57+
return objectEntrySeparator;
58+
}
59+
60+
public char getArrayValueSeparator() {
61+
return arrayValueSeparator;
62+
}
63+
}

0 commit comments

Comments
 (0)