Search in sources :

Example 6 with SerializableString

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.SerializableString 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 7 with SerializableString

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.SerializableString in project jackson-core by FasterXML.

the class TestSerializedString method testAppending.

public void testAppending() throws IOException {
    final String INPUT = "\"quo\\ted\"";
    final String QUOTED = "\\\"quo\\\\ted\\\"";
    SerializableString sstr = new SerializedString(INPUT);
    // sanity checks first:
    assertEquals(sstr.getValue(), INPUT);
    assertEquals(QUOTED, new String(sstr.asQuotedChars()));
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    assertEquals(QUOTED.length(), sstr.writeQuotedUTF8(bytes));
    assertEquals(QUOTED, bytes.toString("UTF-8"));
    bytes.reset();
    assertEquals(INPUT.length(), sstr.writeUnquotedUTF8(bytes));
    assertEquals(INPUT, bytes.toString("UTF-8"));
    byte[] buffer = new byte[100];
    assertEquals(QUOTED.length(), sstr.appendQuotedUTF8(buffer, 3));
    assertEquals(QUOTED, new String(buffer, 3, QUOTED.length()));
    Arrays.fill(buffer, (byte) 0);
    assertEquals(INPUT.length(), sstr.appendUnquotedUTF8(buffer, 5));
    assertEquals(INPUT, new String(buffer, 5, INPUT.length()));
}
Also used : SerializedString(com.fasterxml.jackson.core.io.SerializedString) SerializableString(com.fasterxml.jackson.core.SerializableString) SerializedString(com.fasterxml.jackson.core.io.SerializedString) SerializableString(com.fasterxml.jackson.core.SerializableString)

Example 8 with SerializableString

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.SerializableString in project jackson-databind by FasterXML.

the class ObjectIdWriter method construct.

/**
     * Factory method called by {@link com.fasterxml.jackson.databind.ser.std.BeanSerializerBase}
     * with the initial information based on standard settings for the type
     * for which serializer is being built.
     * 
     * @since 2.3
     */
public static ObjectIdWriter construct(JavaType idType, PropertyName propName, ObjectIdGenerator<?> generator, boolean alwaysAsId) {
    String simpleName = (propName == null) ? null : propName.getSimpleName();
    SerializableString serName = (simpleName == null) ? null : new SerializedString(simpleName);
    return new ObjectIdWriter(idType, serName, generator, null, alwaysAsId);
}
Also used : SerializedString(com.fasterxml.jackson.core.io.SerializedString) SerializableString(com.fasterxml.jackson.core.SerializableString) SerializedString(com.fasterxml.jackson.core.io.SerializedString) SerializableString(com.fasterxml.jackson.core.SerializableString)

Aggregations

SerializableString (com.fasterxml.jackson.core.SerializableString)8 SerializedString (com.fasterxml.jackson.core.io.SerializedString)4 CharacterEscapes (com.fasterxml.jackson.core.io.CharacterEscapes)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ByteBuffer (java.nio.ByteBuffer)1 Test (org.junit.Test)1