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);
}
}
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);
}
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);
}
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));
}
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()));
}
Aggregations