@@ -153,7 +153,7 @@ public final class CharTypes
153
153
* Lookup table used for determining which output characters in
154
154
* 7-bit ASCII range need to be quoted.
155
155
*/
156
- protected final static int [] sOutputEscapes128 ;
156
+ protected final static int [] sOutputEscapes128NoSlash ;
157
157
static {
158
158
int [] table = new int [128 ];
159
159
// Control chars need generic escape sequence
@@ -170,16 +170,16 @@ public final class CharTypes
170
170
table [0x0C ] = 'f' ;
171
171
table [0x0A ] = 'n' ;
172
172
table [0x0D ] = 'r' ;
173
- sOutputEscapes128 = table ;
173
+ sOutputEscapes128NoSlash = table ;
174
174
}
175
175
176
176
/**
177
- * Lookup table same as {@link #sOutputEscapes128 } except that
177
+ * Lookup table same as {@link #sOutputEscapes128NoSlash } except that
178
178
* forward slash ('/') is also escaped
179
179
*/
180
180
protected final static int [] sOutputEscapes128WithSlash ;
181
181
static {
182
- sOutputEscapes128WithSlash = Arrays .copyOf (sOutputEscapes128 , sOutputEscapes128 .length );
182
+ sOutputEscapes128WithSlash = Arrays .copyOf (sOutputEscapes128NoSlash , sOutputEscapes128NoSlash .length );
183
183
sOutputEscapes128WithSlash ['/' ] = '/' ;
184
184
}
185
185
@@ -217,25 +217,13 @@ public final class CharTypes
217
217
* Value of 0 means "no escaping"; other positive values that value is character
218
218
* to use after backslash; and negative values that generic (backslash - u)
219
219
* escaping is to be used.
220
+ *<p>
221
+ * NOTE: as of Jackson 3.0, forward slash ({@code "/"}) is escaped by default.
220
222
*
221
223
* @return 128-entry {@code int[]} that contains escape definitions
222
224
*/
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 );
225
+ public static int [] get7BitOutputEscapes () {
226
+ return get7BitOutputEscapes ('"' , true );
239
227
}
240
228
241
229
/**
@@ -244,7 +232,8 @@ public static int[] get7BitOutputEscapes(int quoteChar) {
244
232
*
245
233
* @param quoteChar Character used for quoting textual values and property names;
246
234
* usually double-quote but sometimes changed to single-quote (apostrophe)
247
- * @param escapeSlash
235
+ * @param escapeSlash Whether forward slash ({@code "/"}) is escaped by default
236
+ * or not.
248
237
*
249
238
* @return 128-entry {@code int[]} that contains escape definitions
250
239
*
@@ -255,9 +244,9 @@ public static int[] get7BitOutputEscapes(int quoteChar, boolean escapeSlash) {
255
244
if (escapeSlash ) {
256
245
return sOutputEscapes128WithSlash ;
257
246
}
258
- return sOutputEscapes128 ;
247
+ return sOutputEscapes128NoSlash ;
259
248
}
260
- return AltEscapes .instance .escapesFor (quoteChar , escapeSlash );
249
+ return AltQuoteEscapes .instance .altEscapesFor (quoteChar , escapeSlash );
261
250
}
262
251
263
252
public static int charToHex (int ch )
@@ -283,7 +272,7 @@ public static char hexToChar(int ch)
283
272
* @param content Unescaped String value to append with escaping applied
284
273
*/
285
274
public static void appendQuoted (StringBuilder sb , String content ) {
286
- final int [] escCodes = sOutputEscapes128 ;
275
+ final int [] escCodes = sOutputEscapes128WithSlash ;
287
276
final int escLen = escCodes .length ;
288
277
for (int i = 0 , len = content .length (); i < len ; ++i ) {
289
278
char c = content .charAt (i );
@@ -327,38 +316,43 @@ public static byte[] copyHexBytes(boolean uppercase) {
327
316
* table, used for escaping content that uses non-standard quote
328
317
* character (usually apostrophe).
329
318
*/
330
- private static class AltEscapes {
331
- public final static AltEscapes instance = new AltEscapes ();
319
+ private static class AltQuoteEscapes {
320
+ public final static AltQuoteEscapes instance = new AltQuoteEscapes ();
332
321
333
- private int [][] _altEscapes = new int [128 ][];
322
+ private int [][] _altEscapesNoSlash = new int [128 ][];
334
323
335
324
// @since 2.17
336
325
private int [][] _altEscapesWithSlash = new int [128 ][];
337
326
338
- public int [] escapesFor (int quoteChar ) {
339
- int [] esc = _altEscapes [quoteChar ];
327
+ public int [] altEscapesFor (int quoteChar , boolean escapeSlash )
328
+ {
329
+ int [] esc = escapeSlash
330
+ ? _altEscapesWithSlash [quoteChar ]
331
+ : _altEscapesNoSlash [quoteChar ];
340
332
if (esc == null ) {
341
- esc = Arrays .copyOf (sOutputEscapes128 , 128 );
333
+ esc = escapeSlash
334
+ ? sOutputEscapes128WithSlash
335
+ : sOutputEscapes128NoSlash ;
336
+ esc = Arrays .copyOf (esc , esc .length );
342
337
// Only add escape setting if character does not already have it
343
338
if (esc [quoteChar ] == 0 ) {
344
- esc [quoteChar ] = CharacterEscapes .ESCAPE_STANDARD ;
339
+ // And try to use "natural" escape for apos
340
+ int quoteStyle ;
341
+ switch (quoteChar ) {
342
+ case '\'' :
343
+ case '"' :
344
+ quoteStyle = quoteChar ;
345
+ break ;
346
+ default :
347
+ quoteStyle = CharacterEscapes .ESCAPE_STANDARD ;
348
+ }
349
+ esc [quoteChar ] = quoteStyle ;
350
+ }
351
+ if (escapeSlash ) {
352
+ _altEscapesWithSlash [quoteChar ] = esc ;
353
+ } else {
354
+ _altEscapesNoSlash [quoteChar ] = esc ;
345
355
}
346
- _altEscapes [quoteChar ] = esc ;
347
- }
348
- return esc ;
349
- }
350
-
351
- // @since 2.17
352
- public int [] escapesFor (int quoteChar , boolean escapeSlash )
353
- {
354
- if (!escapeSlash ) {
355
- return escapesFor (quoteChar );
356
- }
357
- int [] esc = _altEscapesWithSlash [quoteChar ];
358
- if (esc == null ) {
359
- esc = escapesFor (quoteChar );
360
- esc ['/' ] = '/' ;
361
- _altEscapesWithSlash [quoteChar ] = esc ;
362
356
}
363
357
return esc ;
364
358
}
0 commit comments