@@ -173,6 +173,16 @@ public final class CharTypes
173
173
sOutputEscapes128 = table ;
174
174
}
175
175
176
+ /**
177
+ * Lookup table same as {@link #sOutputEscapes128} except that
178
+ * forward slash ('/') is also escaped
179
+ */
180
+ protected final static int [] sOutputEscapes128WithSlash ;
181
+ static {
182
+ sOutputEscapes128WithSlash = Arrays .copyOf (sOutputEscapes128 , sOutputEscapes128 .length );
183
+ sOutputEscapes128WithSlash ['/' ] = '/' ;
184
+ }
185
+
176
186
/**
177
187
* Lookup table for the first 256 Unicode characters (ASCII / UTF-8)
178
188
* range. For actual hex digits, contains corresponding value;
@@ -228,6 +238,28 @@ public static int[] get7BitOutputEscapes(int quoteChar) {
228
238
return AltEscapes .instance .escapesFor (quoteChar );
229
239
}
230
240
241
+ /**
242
+ * Alternative to {@link #get7BitOutputEscapes()} when either a non-standard
243
+ * quote character is used, or forward slash is to be escaped.
244
+ *
245
+ * @param quoteChar Character used for quoting textual values and property names;
246
+ * usually double-quote but sometimes changed to single-quote (apostrophe)
247
+ * @param escapeSlash
248
+ *
249
+ * @return 128-entry {@code int[]} that contains escape definitions
250
+ *
251
+ * @since 2.17
252
+ */
253
+ public static int [] get7BitOutputEscapes (int quoteChar , boolean escapeSlash ) {
254
+ if (quoteChar == '"' ) {
255
+ if (escapeSlash ) {
256
+ return sOutputEscapes128WithSlash ;
257
+ }
258
+ return sOutputEscapes128 ;
259
+ }
260
+ return AltEscapes .instance .escapesFor (quoteChar , escapeSlash );
261
+ }
262
+
231
263
public static int charToHex (int ch )
232
264
{
233
265
// 08-Nov-2019, tatu: As per [core#540] and [core#578], changed to
@@ -241,7 +273,6 @@ public static char hexToChar(int ch)
241
273
return HC [ch ];
242
274
}
243
275
244
-
245
276
/**
246
277
* Helper method for appending JSON-escaped version of contents
247
278
* into specific {@link StringBuilder}, using default JSON specification
@@ -301,6 +332,9 @@ private static class AltEscapes {
301
332
302
333
private int [][] _altEscapes = new int [128 ][];
303
334
335
+ // @since 2.17
336
+ private int [][] _altEscapesWithSlash = new int [128 ][];
337
+
304
338
public int [] escapesFor (int quoteChar ) {
305
339
int [] esc = _altEscapes [quoteChar ];
306
340
if (esc == null ) {
@@ -313,6 +347,20 @@ public int[] escapesFor(int quoteChar) {
313
347
}
314
348
return esc ;
315
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
+ }
363
+ return esc ;
364
+ }
316
365
}
317
366
}
318
-
0 commit comments