use of org.codehaus.jackson.map.SerializationConfig in project helix by apache.
the class AbstractResource method toJson.
protected static String toJson(Object object) throws IOException {
SerializationConfig serializationConfig = OBJECT_MAPPER.getSerializationConfig();
serializationConfig.set(SerializationConfig.Feature.INDENT_OUTPUT, true);
StringWriter sw = new StringWriter();
OBJECT_MAPPER.writeValue(sw, object);
sw.append('\n');
return sw.toString();
}
use of org.codehaus.jackson.map.SerializationConfig in project helix by apache.
the class ZNRecordSerializer method serialize.
@Override
public byte[] serialize(Object data) {
if (!(data instanceof ZNRecord)) {
// null is NOT an instance of any class
logger.error("Input object must be of type ZNRecord but it is " + data + ". Will not write to zk");
throw new HelixException("Input object is not of type ZNRecord (was " + data + ")");
}
ZNRecord record = (ZNRecord) data;
// apply retention policy
int max = getListFieldBound(record);
if (max < Integer.MAX_VALUE) {
Map<String, List<String>> listMap = record.getListFields();
for (String key : listMap.keySet()) {
List<String> list = listMap.get(key);
if (list.size() > max) {
listMap.put(key, list.subList(0, max));
}
}
}
// do serialization
ObjectMapper mapper = new ObjectMapper();
SerializationConfig serializationConfig = mapper.getSerializationConfig();
serializationConfig.set(SerializationConfig.Feature.INDENT_OUTPUT, true);
serializationConfig.set(SerializationConfig.Feature.AUTO_DETECT_FIELDS, true);
serializationConfig.set(SerializationConfig.Feature.CAN_OVERRIDE_ACCESS_MODIFIERS, true);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] serializedBytes = null;
try {
mapper.writeValue(baos, data);
serializedBytes = baos.toByteArray();
// apply compression if needed
if (record.getBooleanField("enableCompression", false) || serializedBytes.length > ZNRecord.SIZE_LIMIT) {
serializedBytes = GZipCompressionUtil.compress(serializedBytes);
}
} catch (Exception e) {
logger.error("Exception during data serialization. Will not write to zk. Data (first 1k): " + new String(baos.toByteArray()).substring(0, 1024), e);
throw new HelixException(e);
}
if (serializedBytes.length > ZNRecord.SIZE_LIMIT) {
logger.error("Data size larger than 1M, ZNRecord.id: " + record.getId() + ". Will not write to zk. Data (first 1k): " + new String(serializedBytes).substring(0, 1024));
throw new HelixException("Data size larger than 1M, ZNRecord.id: " + record.getId());
}
return serializedBytes;
}
use of org.codehaus.jackson.map.SerializationConfig in project helix by apache.
the class JacksonPayloadSerializer method serialize.
@Override
public <T> byte[] serialize(final T data) {
if (data == null) {
return null;
}
ObjectMapper mapper = new ObjectMapper();
SerializationConfig serializationConfig = mapper.getSerializationConfig();
serializationConfig.set(SerializationConfig.Feature.INDENT_OUTPUT, true);
serializationConfig.set(SerializationConfig.Feature.AUTO_DETECT_FIELDS, true);
serializationConfig.set(SerializationConfig.Feature.CAN_OVERRIDE_ACCESS_MODIFIERS, true);
StringWriter sw = new StringWriter();
try {
mapper.writeValue(sw, data);
} catch (Exception e) {
logger.error("Exception during payload data serialization.", e);
throw new HelixException(e);
}
return sw.toString().getBytes();
}
use of org.codehaus.jackson.map.SerializationConfig in project onebusaway-application-modules by camsys.
the class SiriJsonSerializerV2 method getJson.
public String getJson(Siri siri, String callback) throws Exception {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(Inclusion.NON_NULL);
mapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, false);
mapper.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
mapper.setDateFormat(new RFC822SimpleDateFormat());
AnnotationIntrospector introspector = new JaxbAnnotationIntrospector();
SerializationConfig config = mapper.getSerializationConfig().withAnnotationIntrospector(introspector);
mapper.setSerializationConfig(config);
mapper.registerModule(new JacksonModule());
String output = "";
if (callback != null)
output = callback + "(";
output += mapper.writeValueAsString(siri);
if (callback != null)
output += ")";
return output;
}
Aggregations