Skip to content

Commit d4e9457

Browse files
committed
Fixes #1200: change default for "Escape slash" to true for 3.0
1 parent 86faf57 commit d4e9457

File tree

3 files changed

+38
-51
lines changed

3 files changed

+38
-51
lines changed

src/main/java/tools/jackson/core/io/CharTypes.java

+20-44
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public final class CharTypes
153153
* Lookup table used for determining which output characters in
154154
* 7-bit ASCII range need to be quoted.
155155
*/
156-
protected final static int[] sOutputEscapes128;
156+
protected final static int[] sOutputEscapes128NoSlash;
157157
static {
158158
int[] table = new int[128];
159159
// Control chars need generic escape sequence
@@ -170,16 +170,16 @@ public final class CharTypes
170170
table[0x0C] = 'f';
171171
table[0x0A] = 'n';
172172
table[0x0D] = 'r';
173-
sOutputEscapes128 = table;
173+
sOutputEscapes128NoSlash = table;
174174
}
175175

176176
/**
177-
* Lookup table same as {@link #sOutputEscapes128} except that
177+
* Lookup table same as {@link #sOutputEscapes128NoSlash} except that
178178
* forward slash ('/') is also escaped
179179
*/
180180
protected final static int[] sOutputEscapes128WithSlash;
181181
static {
182-
sOutputEscapes128WithSlash = Arrays.copyOf(sOutputEscapes128, sOutputEscapes128.length);
182+
sOutputEscapes128WithSlash = Arrays.copyOf(sOutputEscapes128NoSlash, sOutputEscapes128NoSlash.length);
183183
sOutputEscapes128WithSlash['/'] = '/';
184184
}
185185

@@ -220,23 +220,7 @@ public final class CharTypes
220220
*
221221
* @return 128-entry {@code int[]} that contains escape definitions
222222
*/
223-
public static int[] get7BitOutputEscapes() { return sOutputEscapes128; }
224-
225-
/**
226-
* Alternative to {@link #get7BitOutputEscapes()} when a non-standard quote character
227-
* is used.
228-
*
229-
* @param quoteChar Character used for quoting textual values and property names;
230-
* usually double-quote but sometimes changed to single-quote (apostrophe)
231-
*
232-
* @return 128-entry {@code int[]} that contains escape definitions
233-
*/
234-
public static int[] get7BitOutputEscapes(int quoteChar) {
235-
if (quoteChar == '"') {
236-
return sOutputEscapes128;
237-
}
238-
return AltEscapes.instance.escapesFor(quoteChar);
239-
}
223+
public static int[] get7BitOutputEscapes() { return sOutputEscapes128WithSlash; }
240224

241225
/**
242226
* Alternative to {@link #get7BitOutputEscapes()} when either a non-standard
@@ -255,7 +239,7 @@ public static int[] get7BitOutputEscapes(int quoteChar, boolean escapeSlash) {
255239
if (escapeSlash) {
256240
return sOutputEscapes128WithSlash;
257241
}
258-
return sOutputEscapes128;
242+
return sOutputEscapes128NoSlash;
259243
}
260244
return AltEscapes.instance.escapesFor(quoteChar, escapeSlash);
261245
}
@@ -283,7 +267,7 @@ public static char hexToChar(int ch)
283267
* @param content Unescaped String value to append with escaping applied
284268
*/
285269
public static void appendQuoted(StringBuilder sb, String content) {
286-
final int[] escCodes = sOutputEscapes128;
270+
final int[] escCodes = sOutputEscapes128WithSlash;
287271
final int escLen = escCodes.length;
288272
for (int i = 0, len = content.length(); i < len; ++i) {
289273
char c = content.charAt(i);
@@ -330,35 +314,27 @@ public static byte[] copyHexBytes(boolean uppercase) {
330314
private static class AltEscapes {
331315
public final static AltEscapes instance = new AltEscapes();
332316

333-
private int[][] _altEscapes = new int[128][];
317+
private int[][] _altEscapesNoSlash = new int[128][];
334318

335319
// @since 2.17
336320
private int[][] _altEscapesWithSlash = new int[128][];
337321

338-
public int[] escapesFor(int quoteChar) {
339-
int[] esc = _altEscapes[quoteChar];
340-
if (esc == null) {
341-
esc = Arrays.copyOf(sOutputEscapes128, 128);
342-
// Only add escape setting if character does not already have it
343-
if (esc[quoteChar] == 0) {
344-
esc[quoteChar] = CharacterEscapes.ESCAPE_STANDARD;
345-
}
346-
_altEscapes[quoteChar] = esc;
347-
}
348-
return esc;
349-
}
350-
351-
// @since 2.17
352322
public int[] escapesFor(int quoteChar, boolean escapeSlash)
353323
{
354-
if (!escapeSlash) {
355-
return escapesFor(quoteChar);
356-
}
357-
int[] esc = _altEscapesWithSlash[quoteChar];
324+
int[] esc = escapeSlash
325+
? _altEscapesWithSlash[quoteChar]
326+
: _altEscapesNoSlash[quoteChar];
358327
if (esc == null) {
359-
esc = escapesFor(quoteChar);
360-
esc['/'] = '/';
328+
esc = escapeSlash
329+
? sOutputEscapes128WithSlash
330+
: sOutputEscapes128NoSlash;
331+
esc = Arrays.copyOf(esc, esc.length);
361332
_altEscapesWithSlash[quoteChar] = esc;
333+
if (escapeSlash) {
334+
_altEscapesWithSlash[quoteChar] = esc;
335+
} else {
336+
_altEscapesNoSlash[quoteChar] = esc;
337+
}
362338
}
363339
return esc;
364340
}

src/main/java/tools/jackson/core/json/JsonWriteFeature.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,9 @@ public enum JsonWriteFeature
103103
/**
104104
* Feature that specifies whether {@link JsonGenerator} should escape forward slashes.
105105
* <p>
106-
* Feature is disabled by default for Jackson 2.x version, and enabled by default in Jackson 3.0.
107-
*
108-
* @since 2.17
106+
* Feature is enabled by default in Jackson 3.0 (was disabled in 2.x).
109107
*/
110-
ESCAPE_FORWARD_SLASHES(false),
108+
ESCAPE_FORWARD_SLASHES(true),
111109

112110
;
113111

src/test/java/tools/jackson/core/write/JsonWriteFeatureEscapeForwardSlashTest.java

+16-3
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,28 @@
77
import tools.jackson.core.JsonGenerator;
88
import tools.jackson.core.ObjectWriteContext;
99
import tools.jackson.core.json.JsonFactory;
10+
import tools.jackson.core.json.JsonGeneratorBase;
1011
import tools.jackson.core.json.JsonWriteFeature;
1112

1213
import static org.junit.jupiter.api.Assertions.assertEquals;
14+
import static org.junit.jupiter.api.Assertions.assertTrue;
1315

14-
/**
15-
* @since 2.17
16-
*/
1716
public class JsonWriteFeatureEscapeForwardSlashTest
1817
{
18+
@Test
19+
public void testDefaultSettings() {
20+
JsonFactory jsonF = new JsonFactory();
21+
assertTrue(jsonF.isEnabled(JsonWriteFeature.ESCAPE_FORWARD_SLASHES));
22+
try (JsonGeneratorBase g = (JsonGeneratorBase) jsonF.createGenerator(ObjectWriteContext.empty(),
23+
new StringWriter())) {
24+
assertTrue(g.isEnabled(JsonWriteFeature.ESCAPE_FORWARD_SLASHES));
25+
}
26+
try (JsonGeneratorBase g = (JsonGeneratorBase) jsonF.createGenerator(ObjectWriteContext.empty(),
27+
new ByteArrayOutputStream())) {
28+
assertTrue(g.isEnabled(JsonWriteFeature.ESCAPE_FORWARD_SLASHES));
29+
}
30+
}
31+
1932
@Test
2033
public void testDontEscapeForwardSlash() throws Exception {
2134
final JsonFactory jsonF = JsonFactory.builder()

0 commit comments

Comments
 (0)