public final class JsonEscape extends Object
Utility class for performing JSON escape/unescape operations.
Configuration of escape/unescape operationsEscape operations can be (optionally) configured by means of:
JsonEscapeLevel
enum.JsonEscapeType
enum.Unescape operations need no configuration parameters. Unescape operations will always perform complete unescape of SECs (\n) and u-based (\u00E1) hexadecimal escapes.
FeaturesSpecific features of the JSON escape/unescape operations performed by means of this class:
There are two different input/output modes that can be used in escape/unescape operations:
The following references apply:
Modifier and Type | Method and Description |
---|---|
static void |
escapeJson(char[] text,
int offset,
int len,
Writer writer)
Perform a JSON level 2 (basic set and all non-ASCII chars) escape operation
on a char[] input.
|
static void |
escapeJson(char[] text,
int offset,
int len,
Writer writer,
JsonEscapeType type,
JsonEscapeLevel level)
Perform a (configurable) JSON escape operation on a char[] input.
|
static String |
escapeJson(String text)
Perform a JSON level 2 (basic set and all non-ASCII chars) escape operation
on a String input.
|
static String |
escapeJson(String text,
JsonEscapeType type,
JsonEscapeLevel level)
Perform a (configurable) JSON escape operation on a String input.
|
static void |
escapeJsonMinimal(char[] text,
int offset,
int len,
Writer writer)
Perform a JSON level 1 (only basic set) escape operation
on a char[] input.
|
static String |
escapeJsonMinimal(String text)
Perform a JSON level 1 (only basic set) escape operation
on a String input.
|
static void |
unescapeJson(char[] text,
int offset,
int len,
Writer writer)
Perform a JSON unescape operation on a char[] input.
|
static String |
unescapeJson(String text)
Perform a JSON unescape operation on a String input.
|
public static String escapeJsonMinimal(String text)
Perform a JSON level 1 (only basic set) escape operation on a String input.
Level 1 means this method will only escape the JSON basic escape set:
This method calls escapeJson(String, JsonEscapeType, JsonEscapeLevel)
with the following preconfigured values:
JsonEscapeType.SINGLE_ESCAPE_CHARS_DEFAULT_TO_UHEXA
JsonEscapeLevel.LEVEL_1_BASIC_ESCAPE_SET
This method is thread-safe.
text
- the String to be escaped.public static String escapeJson(String text)
Perform a JSON level 2 (basic set and all non-ASCII chars) escape operation on a String input.
Level 2 means this method will escape:
This escape will be performed by using the Single Escape Chars whenever possible. For escaped characters that do not have an associated SEC, default to \uFFFF Hexadecimal Escapes.
This method calls escapeJson(String, JsonEscapeType, JsonEscapeLevel)
with the following preconfigured values:
JsonEscapeType.SINGLE_ESCAPE_CHARS_DEFAULT_TO_UHEXA
JsonEscapeLevel.LEVEL_2_ALL_NON_ASCII_PLUS_BASIC_ESCAPE_SET
This method is thread-safe.
text
- the String to be escaped.public static String escapeJson(String text, JsonEscapeType type, JsonEscapeLevel level)
Perform a (configurable) JSON escape operation on a String input.
This method will perform an escape operation according to the specified
JsonEscapeType
and
JsonEscapeLevel
argument values.
All other String-based escapeJson*(...) methods call this one with preconfigured type and level values.
This method is thread-safe.
text
- the String to be escaped.type
- the type of escape operation to be performed, see
JsonEscapeType
.level
- the escape level to be applied, see JsonEscapeLevel
.public static void escapeJsonMinimal(char[] text, int offset, int len, Writer writer) throws IOException
Perform a JSON level 1 (only basic set) escape operation on a char[] input.
Level 1 means this method will only escape the JSON basic escape set:
This method calls
escapeJson(char[], int, int, java.io.Writer, JsonEscapeType, JsonEscapeLevel)
with the following preconfigured values:
JsonEscapeType.SINGLE_ESCAPE_CHARS_DEFAULT_TO_UHEXA
JsonEscapeLevel.LEVEL_1_BASIC_ESCAPE_SET
This method is thread-safe.
text
- the char[] to be escaped.offset
- the position in text at which the escape operation should start.len
- the number of characters in text that should be escaped.writer
- the java.io.Writer to which the escaped result will be written. Nothing will
be written at all to this writer if text is null.IOException
- if an input/output exception occurspublic static void escapeJson(char[] text, int offset, int len, Writer writer) throws IOException
Perform a JSON level 2 (basic set and all non-ASCII chars) escape operation on a char[] input.
Level 2 means this method will escape:
This escape will be performed by using the Single Escape Chars whenever possible. For escaped characters that do not have an associated SEC, default to \uFFFF Hexadecimal Escapes.
This method calls
escapeJson(char[], int, int, java.io.Writer, JsonEscapeType, JsonEscapeLevel)
with the following preconfigured values:
JsonEscapeType.SINGLE_ESCAPE_CHARS_DEFAULT_TO_UHEXA
JsonEscapeLevel.LEVEL_2_ALL_NON_ASCII_PLUS_BASIC_ESCAPE_SET
This method is thread-safe.
text
- the char[] to be escaped.offset
- the position in text at which the escape operation should start.len
- the number of characters in text that should be escaped.writer
- the java.io.Writer to which the escaped result will be written. Nothing will
be written at all to this writer if text is null.IOException
- if an input/output exception occurspublic static void escapeJson(char[] text, int offset, int len, Writer writer, JsonEscapeType type, JsonEscapeLevel level) throws IOException
Perform a (configurable) JSON escape operation on a char[] input.
This method will perform an escape operation according to the specified
JsonEscapeType
and
JsonEscapeLevel
argument values.
All other char[]-based escapeJson*(...) methods call this one with preconfigured type and level values.
This method is thread-safe.
text
- the char[] to be escaped.offset
- the position in text at which the escape operation should start.len
- the number of characters in text that should be escaped.writer
- the java.io.Writer to which the escaped result will be written. Nothing will
be written at all to this writer if text is null.type
- the type of escape operation to be performed, see
JsonEscapeType
.level
- the escape level to be applied, see JsonEscapeLevel
.IOException
- if an input/output exception occurspublic static String unescapeJson(String text)
Perform a JSON unescape operation on a String input.
No additional configuration arguments are required. Unescape operations will always perform complete JSON unescape of SECs and u-based escapes.
This method is thread-safe.
text
- the String to be unescaped.public static void unescapeJson(char[] text, int offset, int len, Writer writer) throws IOException
Perform a JSON unescape operation on a char[] input.
No additional configuration arguments are required. Unescape operations will always perform complete JSON unescape of SECs and u-based escapes.
This method is thread-safe.
text
- the char[] to be unescaped.offset
- the position in text at which the unescape operation should start.len
- the number of characters in text that should be unescaped.writer
- the java.io.Writer to which the unescaped result will be written. Nothing will
be written at all to this writer if text is null.IOException
- if an input/output exception occursCopyright © 2015 The UNBESCAPE team. All rights reserved.