1 package com.fasterxml.jackson.core.io; 2 3 import java.util.Arrays; 4 5 import com.fasterxml.jackson.core.SerializableString; 6 7 /** 8 * Abstract base class that defines interface for customizing character 9 * escaping aspects for String values, for formats that use escaping. 10 * For JSON this applies to both property names and String values. 11 */ 12 @SuppressWarnings("serial") 13 public abstract class CharacterEscapes 14 implements java.io.Serializable // since 2.1 15 { 16 /** 17 * Value used for lookup tables to indicate that matching characters 18 * do not need to be escaped. 19 */ 20 public final static int ESCAPE_NONE = 0; 21 22 /** 23 * Value used for lookup tables to indicate that matching characters 24 * are to be escaped using standard escaping; for JSON this means 25 * (for example) using "backslash - u" escape method. 26 */ 27 public final static int ESCAPE_STANDARD = -1; 28 29 /** 30 * Value used for lookup tables to indicate that matching characters 31 * will need custom escapes; and that another call 32 * to {@link #getEscapeSequence} is needed to figure out exact escape 33 * sequence to output. 34 */ 35 public final static int ESCAPE_CUSTOM = -2; 36 37 /** 38 * Method generators can call to get lookup table for determining 39 * escape handling for first 128 characters of Unicode (ASCII 40 * characters. Caller is not to modify contents of this array, since 41 * this is expected to be a shared copy. 42 * 43 * @return Array with size of at least 128, where first 128 entries 44 * have either one of <code>ESCAPE_xxx</code> constants, or non-zero positive 45 * integer (meaning of which is data format specific; for JSON it means 46 * that combination of backslash and character with that value is to be used) 47 * to indicate that specific escape sequence is to be used. 48 */ getEscapeCodesForAscii()49 public abstract int[] getEscapeCodesForAscii(); 50 51 /** 52 * Method generators can call to get lookup table for determining 53 * exact escape sequence to use for given character. 54 * It can be called for any character, but typically is called for 55 * either for ASCII characters for which custom escape 56 * sequence is needed; or for any non-ASCII character. 57 */ getEscapeSequence(int ch)58 public abstract SerializableString getEscapeSequence(int ch); 59 60 /** 61 * Helper method that can be used to get a copy of standard JSON 62 * escape definitions; this is useful when just wanting to slightly 63 * customize definitions. Caller can modify this array as it sees 64 * fit and usually returns modified instance via {@link #getEscapeCodesForAscii} 65 */ standardAsciiEscapesForJSON()66 public static int[] standardAsciiEscapesForJSON() 67 { 68 int[] esc = CharTypes.get7BitOutputEscapes(); 69 return Arrays.copyOf(esc, esc.length); 70 } 71 } 72