Search in sources :

Example 1 with SerializableString

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

the class WritableObjectId method writeAsField.

/**
     * Method called to output Object Id as specified.
     */
public void writeAsField(JsonGenerator gen, SerializerProvider provider, ObjectIdWriter w) throws IOException {
    idWritten = true;
    // 03-Aug-2013, tatu: Prefer Native Object Ids if available
    if (gen.canWriteObjectId()) {
        // Need to assume String(ified) ids, for now... could add 'long' variant?
        gen.writeObjectId(String.valueOf(id));
        return;
    }
    SerializableString name = w.propertyName;
    if (name != null) {
        gen.writeFieldName(name);
        w.serializer.serialize(id, gen, provider);
    }
}
Also used : SerializableString(com.fasterxml.jackson.core.SerializableString)

Example 2 with SerializableString

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

the class EnumValues method constructFromName.

public static EnumValues constructFromName(MapperConfig<?> config, Class<Enum<?>> enumClass) {
    // Enum types with per-instance sub-classes need special handling
    Class<? extends Enum<?>> enumCls = ClassUtil.findEnumType(enumClass);
    Enum<?>[] enumValues = enumCls.getEnumConstants();
    if (enumValues == null) {
        throw new IllegalArgumentException("Can not determine enum constants for Class " + enumClass.getName());
    }
    String[] names = config.getAnnotationIntrospector().findEnumValues(enumCls, enumValues, new String[enumValues.length]);
    SerializableString[] textual = new SerializableString[enumValues.length];
    for (int i = 0, len = enumValues.length; i < len; ++i) {
        Enum<?> en = enumValues[i];
        String name = names[i];
        if (name == null) {
            name = en.name();
        }
        textual[en.ordinal()] = config.compileString(name);
    }
    return new EnumValues(enumClass, textual);
}
Also used : SerializableString(com.fasterxml.jackson.core.SerializableString) SerializableString(com.fasterxml.jackson.core.SerializableString)

Example 3 with SerializableString

use of com.fasterxml.jackson.core.SerializableString 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 4 with SerializableString

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

the class TestSerializedString method testFailedAccess.

public void testFailedAccess() throws IOException {
    final String INPUT = "Bit longer text";
    SerializableString sstr = new SerializedString(INPUT);
    final byte[] buffer = new byte[INPUT.length() - 2];
    final char[] ch = new char[INPUT.length() - 2];
    final ByteBuffer bbuf = ByteBuffer.allocate(INPUT.length() - 2);
    assertEquals(-1, sstr.appendQuotedUTF8(buffer, 0));
    assertEquals(-1, sstr.appendQuoted(ch, 0));
    assertEquals(-1, sstr.putQuotedUTF8(bbuf));
    bbuf.rewind();
    assertEquals(-1, sstr.appendUnquotedUTF8(buffer, 0));
    assertEquals(-1, sstr.appendUnquoted(ch, 0));
    assertEquals(-1, sstr.putUnquotedUTF8(bbuf));
}
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) ByteBuffer(java.nio.ByteBuffer)

Example 5 with SerializableString

use of 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)

Aggregations

SerializableString (com.fasterxml.jackson.core.SerializableString)6 SerializedString (com.fasterxml.jackson.core.io.SerializedString)3 CharacterEscapes (com.fasterxml.jackson.core.io.CharacterEscapes)1 ByteBuffer (java.nio.ByteBuffer)1