Java Code Examples for com.fasterxml.jackson.core.io.CharacterEscapes#ESCAPE_CUSTOM

The following examples show how to use com.fasterxml.jackson.core.io.CharacterEscapes#ESCAPE_CUSTOM . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MessageMarshaller.java    From curiostack with MIT License 5 votes vote down vote up
@Override
public int[] getEscapeCodesForAscii() {
  int[] escapes = CharacterEscapes.standardAsciiEscapesForJSON();
  // From
  // https://github.com/google/gson/blob/bac26b8e429150d4cbf807e8692f207b7ce7d40d/gson/src/main/java/com/google/gson/stream/JsonWriter.java#L158
  escapes['<'] = CharacterEscapes.ESCAPE_CUSTOM;
  escapes['>'] = CharacterEscapes.ESCAPE_CUSTOM;
  escapes['&'] = CharacterEscapes.ESCAPE_STANDARD;
  escapes['='] = CharacterEscapes.ESCAPE_STANDARD;
  escapes['\''] = CharacterEscapes.ESCAPE_STANDARD;
  return escapes;
}
 
Example 2
Source File: ObjectMapperFactory.java    From etf-webapp with European Union Public License 1.2 5 votes vote down vote up
public HTMLCharacterEscapes() {
    asciiEscapes = CharacterEscapes.standardAsciiEscapesForJSON();
    asciiEscapes['<'] = CharacterEscapes.ESCAPE_CUSTOM;
    asciiEscapes['>'] = CharacterEscapes.ESCAPE_CUSTOM;
    asciiEscapes['&'] = CharacterEscapes.ESCAPE_CUSTOM;
    asciiEscapes['"'] = CharacterEscapes.ESCAPE_CUSTOM;
    asciiEscapes['\''] = CharacterEscapes.ESCAPE_CUSTOM;
}
 
Example 3
Source File: CharEscapeUtil.java    From bboss-elasticsearch with Apache License 2.0 4 votes vote down vote up
private void _writeSegmentCustom(int end)
		throws IOException, JsonGenerationException
{
	final int[] escCodes = _outputEscapes;
	final int maxNonEscaped = (_maximumNonEscapedChar < 1) ? 0xFFFF : _maximumNonEscapedChar;
	final int escLimit = Math.min(escCodes.length, maxNonEscaped+1);
	final CharacterEscapes customEscapes = _characterEscapes;

	int ptr = 0;
	int escCode = 0;
	int start = ptr;

	output_loop:
	while (ptr < end) {
		// Fast loop for chars not needing escaping
		char c;
		while (true) {
			c = _outputBuffer[ptr];
			if (c < escLimit) {
				escCode = escCodes[c];
				if (escCode != 0) {
					break;
				}
			} else if (c > maxNonEscaped) {
				escCode = CharacterEscapes.ESCAPE_STANDARD;
				break;
			} else {
				if ((_currentEscape = customEscapes.getEscapeSequence(c)) != null) {
					escCode = CharacterEscapes.ESCAPE_CUSTOM;
					break;
				}
			}
			if (++ptr >= end) {
				break;
			}
		}
		int flushLen = (ptr - start);
		if (flushLen > 0) {
			_writer.write(_outputBuffer, start, flushLen);
			if (ptr >= end) {
				break output_loop;
			}
		}
		++ptr;
		start = _prependOrWriteCharacterEscape(_outputBuffer, ptr, end, c, escCode);
	}
}
 
Example 4
Source File: CharEscapeUtil.java    From bboss-elasticsearch with Apache License 2.0 4 votes vote down vote up
private void _writeStringCustom(final int len)
		throws IOException, JsonGenerationException
{
	// And then we'll need to verify need for escaping etc:
	int end = _outputTail + len;
	final int[] escCodes = _outputEscapes;
	final int maxNonEscaped = (_maximumNonEscapedChar < 1) ? 0xFFFF : _maximumNonEscapedChar;
	final int escLimit = Math.min(escCodes.length, maxNonEscaped+1);
	int escCode = 0;
	final CharacterEscapes customEscapes = _characterEscapes;

	output_loop:
	while (_outputTail < end) {
		char c;
		// Fast loop for chars not needing escaping
		escape_loop:
		while (true) {
			c = _outputBuffer[_outputTail];
			if (c < escLimit) {
				escCode = escCodes[c];
				if (escCode != 0) {
					break escape_loop;
				}
			} else if (c > maxNonEscaped) {
				escCode = CharacterEscapes.ESCAPE_STANDARD;
				break escape_loop;
			} else {
				if ((_currentEscape = customEscapes.getEscapeSequence(c)) != null) {
					escCode = CharacterEscapes.ESCAPE_CUSTOM;
					break escape_loop;
				}
			}
			if (++_outputTail >= end) {
				break output_loop;
			}
		}
		int flushLen = (_outputTail - _outputHead);
		if (flushLen > 0) {
			_writer.write(_outputBuffer, _outputHead, flushLen);
		}
		++_outputTail;
		_prependOrWriteCharacterEscape(c, escCode);
	}
}
 
Example 5
Source File: CharEscapeUtil.java    From bboss-elasticsearch with Apache License 2.0 4 votes vote down vote up
private void _writeStringCustom(char[] text, int offset, int len)
		throws IOException, JsonGenerationException
{
	len += offset; // -> len marks the end from now on
	final int[] escCodes = _outputEscapes;
	final int maxNonEscaped = (_maximumNonEscapedChar < 1) ? 0xFFFF : _maximumNonEscapedChar;
	final int escLimit = Math.min(escCodes.length, maxNonEscaped+1);
	final CharacterEscapes customEscapes = _characterEscapes;

	int escCode = 0;

	while (offset < len) {
		int start = offset;
		char c;

		while (true) {
			c = text[offset];
			if (c < escLimit) {
				escCode = escCodes[c];
				if (escCode != 0) {
					break;
				}
			} else if (c > maxNonEscaped) {
				escCode = CharacterEscapes.ESCAPE_STANDARD;
				break;
			} else {
				if ((_currentEscape = customEscapes.getEscapeSequence(c)) != null) {
					escCode = CharacterEscapes.ESCAPE_CUSTOM;
					break;
				}
			}
			if (++offset >= len) {
				break;
			}
		}

		// Short span? Better just copy it to buffer first:
		int newAmount = offset - start;
		if (newAmount < SHORT_WRITE) {
			// Note: let's reserve room for escaped char (up to 6 chars)
			if ((_outputTail + newAmount) > _outputEnd) {
				_flushBuffer();
			}
			if (newAmount > 0) {
				System.arraycopy(text, start, _outputBuffer, _outputTail, newAmount);
				_outputTail += newAmount;
			}
		} else { // Nope: better just write through
			_flushBuffer();
			_writer.write(text, start, newAmount);
		}
		// Was this the end?
		if (offset >= len) { // yup
			break;
		}
		// Nope, need to escape the char.
		++offset;
		_appendCharacterEscape(c, escCode);
	}
}
 
Example 6
Source File: CharEscapeUtil.java    From bboss-elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * Method called to append escape sequence for given character, at the
 * end of standard output buffer; or if not possible, write out directly.
 */
private void _appendCharacterEscape(char ch, int escCode)
		throws IOException, JsonGenerationException
{
	if (escCode >= 0) { // \\N (2 char)
		if ((_outputTail + 2) > _outputEnd) {
			_flushBuffer();
		}
		_outputBuffer[_outputTail++] = '\\';
		_outputBuffer[_outputTail++] = (char) escCode;
		return;
	}
	if (escCode != CharacterEscapes.ESCAPE_CUSTOM) { // std, \\uXXXX
		if ((_outputTail + 5) >= _outputEnd) {
			_flushBuffer();
		}
		int ptr = _outputTail;
		char[] buf = _outputBuffer;
		buf[ptr++] = '\\';
		buf[ptr++] = 'u';
		// We know it's a control char, so only the last 2 chars are non-0
		if (ch > 0xFF) { // beyond 8 bytes
			int hi = (ch >> 8) & 0xFF;
			buf[ptr++] = HEX_CHARS[hi >> 4];
			buf[ptr++] = HEX_CHARS[hi & 0xF];
			ch &= 0xFF;
		} else {
			buf[ptr++] = '0';
			buf[ptr++] = '0';
		}
		buf[ptr++] = HEX_CHARS[ch >> 4];
		buf[ptr++] = HEX_CHARS[ch & 0xF];
		_outputTail = ptr;
		return;
	}
	String escape;
	if (_currentEscape == null) {
		escape = _characterEscapes.getEscapeSequence(ch).getValue();
	} else {
		escape = _currentEscape.getValue();
		_currentEscape = null;
	}
	int len = escape.length();
	if ((_outputTail + len) > _outputEnd) {
		_flushBuffer();
		if (len > _outputEnd) { // very very long escape; unlikely but theoretically possible
			_writer.write(escape);
			return;
		}
	}
	escape.getChars(0, len, _outputBuffer, _outputTail);
	_outputTail += len;
}
 
Example 7
Source File: WriterBasedJsonGenerator.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private void _writeStringCustom(final int len)
    throws IOException, JsonGenerationException
{
    // And then we'll need to verify need for escaping etc:
    int end = _outputTail + len;
    final int[] escCodes = _outputEscapes;
    final int maxNonEscaped = (_maximumNonEscapedChar < 1) ? 0xFFFF : _maximumNonEscapedChar;
    final int escLimit = Math.min(escCodes.length, maxNonEscaped+1);
    int escCode = 0;
    final CharacterEscapes customEscapes = _characterEscapes;

    output_loop:
    while (_outputTail < end) {
        char c;
        // Fast loop for chars not needing escaping
        escape_loop:
        while (true) {
            c = _outputBuffer[_outputTail];
            if (c < escLimit) {
                escCode = escCodes[c];
                if (escCode != 0) {
                    break escape_loop;
                }
            } else if (c > maxNonEscaped) {
                escCode = CharacterEscapes.ESCAPE_STANDARD;
                break escape_loop;
            } else {
                if ((_currentEscape = customEscapes.getEscapeSequence(c)) != null) {
                    escCode = CharacterEscapes.ESCAPE_CUSTOM;
                    break escape_loop;
                }
            }
            if (++_outputTail >= end) {
                break output_loop;
            }
        }
        int flushLen = (_outputTail - _outputHead);
        if (flushLen > 0) {
            _writer.write(_outputBuffer, _outputHead, flushLen);
        }
        ++_outputTail;
        _prependOrWriteCharacterEscape(c, escCode);
    }
}
 
Example 8
Source File: WriterBasedJsonGenerator.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private void _writeSegmentCustom(int end)
    throws IOException, JsonGenerationException
{
    final int[] escCodes = _outputEscapes;
    final int maxNonEscaped = (_maximumNonEscapedChar < 1) ? 0xFFFF : _maximumNonEscapedChar;
    final int escLimit = Math.min(escCodes.length, maxNonEscaped+1);
    final CharacterEscapes customEscapes = _characterEscapes;

    int ptr = 0;
    int escCode = 0;
    int start = ptr;

    output_loop:
    while (ptr < end) {
        // Fast loop for chars not needing escaping
        char c;
        while (true) {
            c = _outputBuffer[ptr];
            if (c < escLimit) {
                escCode = escCodes[c];
                if (escCode != 0) {
                    break;
                }
            } else if (c > maxNonEscaped) {
                escCode = CharacterEscapes.ESCAPE_STANDARD;
                break;
            } else {
                if ((_currentEscape = customEscapes.getEscapeSequence(c)) != null) {
                    escCode = CharacterEscapes.ESCAPE_CUSTOM;
                    break;
                }
            }
            if (++ptr >= end) {
                break;
            }
        }
        int flushLen = (ptr - start);
        if (flushLen > 0) {
            _writer.write(_outputBuffer, start, flushLen);
            if (ptr >= end) {
                break output_loop;
            }
        }
        ++ptr;
        start = _prependOrWriteCharacterEscape(_outputBuffer, ptr, end, c, escCode);
    }
}
 
Example 9
Source File: WriterBasedJsonGenerator.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private void _writeStringCustom(char[] text, int offset, int len)
    throws IOException, JsonGenerationException
{
    len += offset; // -> len marks the end from now on
    final int[] escCodes = _outputEscapes;
    final int maxNonEscaped = (_maximumNonEscapedChar < 1) ? 0xFFFF : _maximumNonEscapedChar;
    final int escLimit = Math.min(escCodes.length, maxNonEscaped+1);
    final CharacterEscapes customEscapes = _characterEscapes;

    int escCode = 0;
    
    while (offset < len) {
        int start = offset;
        char c;
        
        while (true) {
            c = text[offset];
            if (c < escLimit) {
                escCode = escCodes[c];
                if (escCode != 0) {
                    break;
                }
            } else if (c > maxNonEscaped) {
                escCode = CharacterEscapes.ESCAPE_STANDARD;
                break;
            } else {
                if ((_currentEscape = customEscapes.getEscapeSequence(c)) != null) {
                    escCode = CharacterEscapes.ESCAPE_CUSTOM;
                    break;
                }
            }
            if (++offset >= len) {
                break;
            }
        }

        // Short span? Better just copy it to buffer first:
        int newAmount = offset - start;
        if (newAmount < SHORT_WRITE) {
            // Note: let's reserve room for escaped char (up to 6 chars)
            if ((_outputTail + newAmount) > _outputEnd) {
                _flushBuffer();
            }
            if (newAmount > 0) {
                System.arraycopy(text, start, _outputBuffer, _outputTail, newAmount);
                _outputTail += newAmount;
            }
        } else { // Nope: better just write through
            _flushBuffer();
            _writer.write(text, start, newAmount);
        }
        // Was this the end?
        if (offset >= len) { // yup
            break;
        }
        // Nope, need to escape the char.
        ++offset;
        _appendCharacterEscape(c, escCode);
    }
}
 
Example 10
Source File: WriterBasedJsonGenerator.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Method called to append escape sequence for given character, at the
 * end of standard output buffer; or if not possible, write out directly.
 */
private void _appendCharacterEscape(char ch, int escCode)
    throws IOException, JsonGenerationException
{
    if (escCode >= 0) { // \\N (2 char)
        if ((_outputTail + 2) > _outputEnd) {
            _flushBuffer();
        }
        _outputBuffer[_outputTail++] = '\\';
        _outputBuffer[_outputTail++] = (char) escCode;
        return;
    }
    if (escCode != CharacterEscapes.ESCAPE_CUSTOM) { // std, \\uXXXX
        if ((_outputTail + 5) >= _outputEnd) {
            _flushBuffer();
        }
        int ptr = _outputTail;
        char[] buf = _outputBuffer;
        buf[ptr++] = '\\';
        buf[ptr++] = 'u';
        // We know it's a control char, so only the last 2 chars are non-0
        if (ch > 0xFF) { // beyond 8 bytes
            int hi = (ch >> 8) & 0xFF;
            buf[ptr++] = HEX_CHARS[hi >> 4];
            buf[ptr++] = HEX_CHARS[hi & 0xF];
            ch &= 0xFF;
        } else {
            buf[ptr++] = '0';
            buf[ptr++] = '0';
        }
        buf[ptr++] = HEX_CHARS[ch >> 4];
        buf[ptr++] = HEX_CHARS[ch & 0xF];
        _outputTail = ptr;
        return;
    }
    String escape;
    if (_currentEscape == null) {
        escape = _characterEscapes.getEscapeSequence(ch).getValue();
    } else {
        escape = _currentEscape.getValue();
        _currentEscape = null;
    }
    int len = escape.length();
    if ((_outputTail + len) > _outputEnd) {
        _flushBuffer();
        if (len > _outputEnd) { // very very long escape; unlikely but theoretically possible
            _writer.write(escape);
            return;
        }
    }
    escape.getChars(0, len, _outputBuffer, _outputTail);
    _outputTail += len;
}