Search in sources :

Example 1 with CharacterEscapes

use of com.fasterxml.jackson.core.io.CharacterEscapes in project jackson-core by FasterXML.

the class WriterBasedJsonGenerator method _writeSegmentCustom.

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);
    }
}
Also used : CharacterEscapes(com.fasterxml.jackson.core.io.CharacterEscapes)

Example 2 with CharacterEscapes

use of com.fasterxml.jackson.core.io.CharacterEscapes in project jackson-core by FasterXML.

the class WriterBasedJsonGenerator method _writeStringCustom.

/*
    /**********************************************************
    /* Internal methods, low-level writing, text segment
    /* with custom escaping (possibly coupling with ASCII limits)
    /**********************************************************
     */
/* Same as "_writeString2()", except needs additional escaping
     * for subset of characters
     */
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);
    }
}
Also used : CharacterEscapes(com.fasterxml.jackson.core.io.CharacterEscapes)

Example 3 with CharacterEscapes

use of com.fasterxml.jackson.core.io.CharacterEscapes in project jackson-core by FasterXML.

the class UTF8JsonGenerator method _writeCustomStringSegment2.

private final void _writeCustomStringSegment2(final String text, int offset, final int end) throws IOException {
    // Ok: caller guarantees buffer can have room; but that may require flushing:
    if ((_outputTail + 6 * (end - offset)) > _outputEnd) {
        _flushBuffer();
    }
    int outputPtr = _outputTail;
    final byte[] outputBuffer = _outputBuffer;
    final int[] escCodes = _outputEscapes;
    // may or may not have this limit
    final int maxUnescaped = (_maximumNonEscapedChar <= 0) ? 0xFFFF : _maximumNonEscapedChar;
    // non-null
    final CharacterEscapes customEscapes = _characterEscapes;
    while (offset < end) {
        int ch = text.charAt(offset++);
        if (ch <= 0x7F) {
            if (escCodes[ch] == 0) {
                outputBuffer[outputPtr++] = (byte) ch;
                continue;
            }
            int escape = escCodes[ch];
            if (escape > 0) {
                // 2-char escape, fine
                outputBuffer[outputPtr++] = BYTE_BACKSLASH;
                outputBuffer[outputPtr++] = (byte) escape;
            } else if (escape == CharacterEscapes.ESCAPE_CUSTOM) {
                SerializableString esc = customEscapes.getEscapeSequence(ch);
                if (esc == null) {
                    _reportError("Invalid custom escape definitions; custom escape not found for character code 0x" + Integer.toHexString(ch) + ", although was supposed to have one");
                }
                outputPtr = _writeCustomEscape(outputBuffer, outputPtr, esc, end - offset);
            } else {
                // ctrl-char, 6-byte escape...
                outputPtr = _writeGenericEscape(ch, outputPtr);
            }
            continue;
        }
        if (ch > maxUnescaped) {
            // [JACKSON-102] Allow forced escaping if non-ASCII (etc) chars:
            outputPtr = _writeGenericEscape(ch, outputPtr);
            continue;
        }
        SerializableString esc = customEscapes.getEscapeSequence(ch);
        if (esc != null) {
            outputPtr = _writeCustomEscape(outputBuffer, outputPtr, esc, end - offset);
            continue;
        }
        if (ch <= 0x7FF) {
            // fine, just needs 2 byte output
            outputBuffer[outputPtr++] = (byte) (0xc0 | (ch >> 6));
            outputBuffer[outputPtr++] = (byte) (0x80 | (ch & 0x3f));
        } else {
            outputPtr = _outputMultiByteChar(ch, outputPtr);
        }
    }
    _outputTail = outputPtr;
}
Also used : CharacterEscapes(com.fasterxml.jackson.core.io.CharacterEscapes)

Example 4 with CharacterEscapes

use of com.fasterxml.jackson.core.io.CharacterEscapes in project jackson-databind by FasterXML.

the class TestGeneratorUsingMapper method testIssue820.

public void testIssue820() throws IOException {
    StringBuffer sb = new StringBuffer();
    while (sb.length() <= 5000) {
        sb.append("Yet another line of text...\n");
    }
    String sampleText = sb.toString();
    assertTrue("Sanity check so I don't mess up the sample text later.", sampleText.contains("\n"));
    final ObjectMapper mapper = new ObjectMapper();
    final CharacterEscapes defaultCharacterEscapes = new CharacterEscapes() {

        private static final long serialVersionUID = 1L;

        @Override
        public int[] getEscapeCodesForAscii() {
            return standardAsciiEscapesForJSON();
        }

        @Override
        public SerializableString getEscapeSequence(final int ch) {
            return null;
        }
    };
    mapper.getFactory().setCharacterEscapes(defaultCharacterEscapes);
    String jacksonJson = mapper.writeValueAsString(sampleText);
    boolean hasLFs = jacksonJson.indexOf('\n') > 0;
    assertFalse("Should NOT contain linefeeds, should have been escaped", hasLFs);
}
Also used : CharacterEscapes(com.fasterxml.jackson.core.io.CharacterEscapes) SerializableString(com.fasterxml.jackson.core.SerializableString)

Example 5 with CharacterEscapes

use of com.fasterxml.jackson.core.io.CharacterEscapes in project bboss-elastic by bbossgroups.

the class CharEscapeUtil method _writeStringCustom.

/*
    /**********************************************************
    /* Internal methods, low-level writing, text segment
    /* with custom escaping (possibly coupling with ASCII limits)
    /**********************************************************
     */
/* Same as "_writeString2()", except needs additional escaping
	 * for subset of characters
	 */
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);
    }
}
Also used : CharacterEscapes(com.fasterxml.jackson.core.io.CharacterEscapes)

Aggregations

CharacterEscapes (com.fasterxml.jackson.core.io.CharacterEscapes)10 SerializableString (com.fasterxml.jackson.core.SerializableString)2 SerializedString (com.fasterxml.jackson.core.io.SerializedString)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Test (org.junit.Test)1