1 package com.fasterxml.jackson.core; 2 3 import com.fasterxml.jackson.core.io.CharacterEscapes; 4 import com.fasterxml.jackson.core.io.SerializedString; 5 6 /** 7 * Convenience {@link CharacterEscapes} implementation that escapes 8 * Unicode characters `0x2028` and `0x2029` (in addition to characters 9 * escaped otherwise), which are apparently considered linefeeds as 10 * per newer Javascript specifications, and consequently problematic 11 * when using JSONP (see https://en.wikipedia.org/wiki/JSONP). 12 * 13 * @since 2.8 14 */ 15 public class JsonpCharacterEscapes extends CharacterEscapes 16 { 17 private static final long serialVersionUID = 1L; 18 19 private static final int[] asciiEscapes = CharacterEscapes.standardAsciiEscapesForJSON(); 20 private static final SerializedString escapeFor2028 = new SerializedString("\\u2028"); 21 private static final SerializedString escapeFor2029 = new SerializedString("\\u2029"); 22 23 private static final JsonpCharacterEscapes sInstance = new JsonpCharacterEscapes(); 24 instance()25 public static JsonpCharacterEscapes instance() { 26 return sInstance; 27 } 28 29 @Override getEscapeSequence(int ch)30 public SerializableString getEscapeSequence(int ch) 31 { 32 switch (ch) { 33 case 0x2028: 34 return escapeFor2028; 35 case 0x2029: 36 return escapeFor2029; 37 default: 38 return null; 39 } 40 } 41 42 @Override getEscapeCodesForAscii()43 public int[] getEscapeCodesForAscii() { 44 return asciiEscapes; 45 } 46 } 47