Search in sources :

Example 6 with CharacterEscapes

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

the class CharEscapeUtil method _writeStringCustom.

private void _writeStringCustom(char[] text, int offset, int len) throws IOException, JsonGenerationException {
    // -> len marks the end from now on
    len += offset;
    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);
    }
}
Also used : CharacterEscapes(com.fasterxml.jackson.core.io.CharacterEscapes)

Example 7 with CharacterEscapes

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

the class CharEscapeUtil 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 8 with CharacterEscapes

use of com.fasterxml.jackson.core.io.CharacterEscapes in project bson4jackson by michel-kraemer.

the class BsonGeneratorTest method characterEscapes.

/**
 * Test if {@link CharacterEscapes} are supported
 * @throws Exception if something goes wrong
 */
@Test
public void characterEscapes() throws Exception {
    JsonNode node = new ObjectMapper().readTree("{ \"some.field\": \"some.val\", " + "\"another\": \"field\", " + "\".some\": \".field\", " + "\"some.\": \"field.\" }");
    ObjectMapper bsonMapper = new ObjectMapper(new BsonFactory());
    bsonMapper.getFactory().setCharacterEscapes(new CharacterEscapes() {

        private static final long serialVersionUID = 283833498358662446L;

        @Override
        public int[] getEscapeCodesForAscii() {
            int[] escapes = CharacterEscapes.standardAsciiEscapesForJSON();
            escapes['.'] = CharacterEscapes.ESCAPE_CUSTOM;
            return escapes;
        }

        @Override
        public SerializableString getEscapeSequence(int ch) {
            switch(ch) {
                case '.':
                    return new SerializedString("\uff0e");
            }
            return null;
        }
    });
    byte[] bsonBytes = bsonMapper.writeValueAsBytes(node);
    byte[] sBytes = new byte[] { // document length
    0x61, 0x00, 0x00, 0x00, // type = string
    0x02, // 's'   'o'   'm'   'e'
    0x73, 0x6f, 0x6d, 0x65, // escape sequence
    (byte) 0xef, (byte) 0xbc, (byte) 0x8e, // 'f'   'i'   'e'   'l'   'd'
    0x66, 0x69, 0x65, 0x6c, 0x64, // end of string
    0x00, // string length (0x0b = 10 characters + trailing 0x00)
    0x0b, 0x00, 0x00, 0x00, // 's'   'o'   'm'   'e'
    0x73, 0x6f, 0x6d, 0x65, // escape sequence
    (byte) 0xef, (byte) 0xbc, (byte) 0x8e, // 'v'   'a'   'l'
    0x76, 0x61, 0x6c, // end of string
    0x00, // type = string
    0x02, // 'a'   'n'   'o'   't'   'h'   'e'   'r'
    0x61, 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, // end of string
    0x00, // string length (5 characters + trailing 0x00)
    0x06, 0x00, 0x00, 0x00, // 'f'   'i'   'e'   'l'   'd'
    0x66, 0x69, 0x65, 0x6c, 0x64, // end of string
    0x00, // type = string
    0x02, // escape sequence
    (byte) 0xef, (byte) 0xbc, (byte) 0x8e, // 's'   'o'   'm'   'e'
    0x73, 0x6f, 0x6d, 0x65, // end of string
    0x00, // string length (8 characters + trailing 0x00)
    0x09, 0x00, 0x00, 0x00, // escape sequence
    (byte) 0xef, (byte) 0xbc, (byte) 0x8e, // 'f'   'i'   'e'   'l'   'd'
    0x66, 0x69, 0x65, 0x6c, 0x64, // end of string
    0x00, // type = string
    0x02, // 's'   'o'   'm'   'e'
    0x73, 0x6f, 0x6d, 0x65, // escape sequence
    (byte) 0xef, (byte) 0xbc, (byte) 0x8e, // end of string
    0x00, // string length (8 characters + trailing 0x00)
    0x09, 0x00, 0x00, 0x00, // 'f'   'i'   'e'   'l'   'd'
    0x66, 0x69, 0x65, 0x6c, 0x64, // escape sequence
    (byte) 0xef, (byte) 0xbc, (byte) 0x8e, // end of string
    0x00, // end of document
    0x00 };
    assertArrayEquals(sBytes, bsonBytes);
}
Also used : SerializedString(com.fasterxml.jackson.core.io.SerializedString) SerializableString(com.fasterxml.jackson.core.SerializableString) CharacterEscapes(com.fasterxml.jackson.core.io.CharacterEscapes) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 9 with CharacterEscapes

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

the class WriterBasedJsonGenerator method _writeStringCustom.

private void _writeStringCustom(char[] text, int offset, int len) throws IOException, JsonGenerationException {
    // -> len marks the end from now on
    len += offset;
    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);
    }
}
Also used : CharacterEscapes(com.fasterxml.jackson.core.io.CharacterEscapes)

Example 10 with CharacterEscapes

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

the class UTF8JsonGenerator method _writeCustomStringSegment2.

/*
    /**********************************************************
    /* Internal methods, low-level writing, text segment
    /* with fully custom escaping (and possibly escaping of non-ASCII
    /**********************************************************
     */
/**
 * Same as <code>_writeStringSegmentASCII2(char[], ...)</code., but with
 * additional checking for completely custom escapes
 */
private final void _writeCustomStringSegment2(final char[] cbuf, 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 = cbuf[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)

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