Class JavaEscaper

java.lang.Object
org.cthing.escapers.JavaEscaper

public final class JavaEscaper extends Object
Escapes strings and character arrays according to the Escape Sequences section of the Java Language Specification. Characters are escaped according to the following table:
Character Escaping
Character Escape Description
0x00 - 0x07 \uXXXX ASCII control characters
0x08 \b Backspace
0x09 \t Tab
0x0A \n Line feed
0x0B \uXXXX ASCII control character
0x0C \f Form feed
0x0D \r Carriage return
0x0E - 0x1F \uXXXX ASCII control characters
0x20 Unchanged or \s if JavaEscaper.Option.ESCAPE_SPACE specified Space character
0x21 Unchanged Printable ASCII characters
0x22 \" Double quote
0x23 - 0x5B Unchanged Printable ASCII characters
0x5C \\ Backslash
0x5D - 0x7E Unchanged Printable ASCII characters
0x7F \uXXXX ASCII control character
0x80 - 0x10FFFF Unchanged by default or \uXXXX[\uXXXX] if JavaEscaper.Option.ESCAPE_NON_ASCII specified ISO Latin-1, Unicode BMP and surrogate pairs
  • Method Details

    • escape

      public static String escape(CharSequence charSequence, JavaEscaper.Option... options)
      Applies JSON escaping to the specified string.
      Parameters:
      charSequence - String to escape
      options - Escaping options
      Returns:
      Escaped string or null if null was passed in.
    • escape

      public static String escape(CharSequence charSequence, Set<JavaEscaper.Option> options)
      Applies JSON escaping to the specified string.
      Parameters:
      charSequence - String to escape
      options - Escaping options
      Returns:
      Escaped string or null if null was passed in.
    • escape

      @WillNotClose public static void escape(CharSequence charSequence, Writer writer, JavaEscaper.Option... options) throws IOException
      Applies JSON escaping to the specified string and writes the result to the specified writer.
      Parameters:
      charSequence - String to escape
      writer - Writer to which the escaped string is written
      options - Escaping options
      Throws:
      IOException - if there was a problem writing the escaped string
      IllegalArgumentException - if the writer is null
    • escape

      @WillNotClose public static void escape(CharSequence charSequence, Writer writer, Set<JavaEscaper.Option> options) throws IOException
      Applies JSON escaping to the specified string and writes the result to the specified writer.
      Parameters:
      charSequence - String to escape
      writer - Writer to which the escaped string is written
      options - Escaping options
      Throws:
      IOException - if there was a problem writing the escaped string
      IllegalArgumentException - if the writer is null
    • escape

      public static String escape(char[] charArr, JavaEscaper.Option... options)
      Applies JSON escaping to the specified character array.
      Parameters:
      charArr - Character array to escape
      options - Escaping options
      Returns:
      Escaped string or null if null was passed in.
    • escape

      public static String escape(char[] charArr, int offset, int length, JavaEscaper.Option... options)
      Applies JSON escaping to the specified character array.
      Parameters:
      charArr - Character array to escape
      offset - Start index in array
      length - Number of characters in array
      options - Escaping options
      Returns:
      Escaped string or null if null was passed in.
    • escape

      public static String escape(char[] charArr, Set<JavaEscaper.Option> options)
      Applies JSON escaping to the specified character array.
      Parameters:
      charArr - Character array to escape
      options - Escaping options
      Returns:
      Escaped string or null if null was passed in.
    • escape

      public static String escape(char[] charArr, int offset, int length, Set<JavaEscaper.Option> options)
      Applies JSON escaping to the specified character array.
      Parameters:
      charArr - Character array to escape
      offset - Start index in array
      length - Number of characters in array
      options - Escaping options
      Returns:
      Escaped string or null if null was passed in.
    • escape

      @WillNotClose public static void escape(char[] charArr, Writer writer, JavaEscaper.Option... options) throws IOException
      Applies JSON escaping to the specified character array and writes the result to the specified writer.
      Parameters:
      charArr - Character array to escape
      writer - Writer to which the escaped string is written
      options - Escaping options
      Throws:
      IOException - if there was a problem writing the escaped string
      IllegalArgumentException - if the writer is null
    • escape

      @WillNotClose public static void escape(char[] charArr, int offset, int length, Writer writer, JavaEscaper.Option... options) throws IOException
      Applies JSON escaping to the specified character array and writes the result to the specified writer.
      Parameters:
      charArr - Character array to escape
      offset - Start index in array
      length - Number of characters in array
      writer - Writer to which the escaped string is written
      options - Escaping options
      Throws:
      IOException - if there was a problem writing the escaped string
      IllegalArgumentException - if the writer is null
    • escape

      @WillNotClose public static void escape(char[] charArr, Writer writer, Set<JavaEscaper.Option> options) throws IOException
      Applies JSON escaping to the specified character array and writes the result to the specified writer.
      Parameters:
      charArr - Character array to escape
      writer - Writer to which the escaped string is written
      options - Escaping options
      Throws:
      IOException - if there was a problem writing the escaped string
      IllegalArgumentException - if the writer is null
    • escape

      @WillNotClose public static void escape(char[] charArr, int offset, int length, Writer writer, Set<JavaEscaper.Option> options) throws IOException
      Applies JSON escaping to the specified character array and writes the result to the specified writer.
      Parameters:
      charArr - Character array to escape
      offset - Start index in array
      length - Number of characters in array
      writer - Writer to which the escaped string is written
      options - Escaping options
      Throws:
      IOException - if there was a problem writing the escaped string
      IllegalArgumentException - if the writer is null