Search in sources :

Example 16 with SerializationConfig

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();
}
Also used : StringWriter(java.io.StringWriter) SerializationConfig(org.codehaus.jackson.map.SerializationConfig)

Example 17 with SerializationConfig

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;
}
Also used : HelixException(org.apache.helix.HelixException) SerializationConfig(org.codehaus.jackson.map.SerializationConfig) List(java.util.List) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ZNRecord(org.apache.helix.ZNRecord) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) IOException(java.io.IOException) HelixException(org.apache.helix.HelixException)

Example 18 with SerializationConfig

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();
}
Also used : HelixException(org.apache.helix.HelixException) StringWriter(java.io.StringWriter) SerializationConfig(org.codehaus.jackson.map.SerializationConfig) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) HelixException(org.apache.helix.HelixException)

Example 19 with SerializationConfig

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;
}
Also used : SerializationConfig(org.codehaus.jackson.map.SerializationConfig) JaxbAnnotationIntrospector(org.codehaus.jackson.xc.JaxbAnnotationIntrospector) AnnotationIntrospector(org.codehaus.jackson.map.AnnotationIntrospector) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) JaxbAnnotationIntrospector(org.codehaus.jackson.xc.JaxbAnnotationIntrospector)

Aggregations

SerializationConfig (org.codehaus.jackson.map.SerializationConfig)19 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)16 StringWriter (java.io.StringWriter)13 ZNRecord (org.apache.helix.ZNRecord)8 Criteria (org.apache.helix.Criteria)7 HelixDataAccessor (org.apache.helix.HelixDataAccessor)7 HelixManager (org.apache.helix.HelixManager)7 PropertyKey (org.apache.helix.PropertyKey)7 Message (org.apache.helix.model.Message)7 Test (org.testng.annotations.Test)7 Builder (org.apache.helix.PropertyKey.Builder)4 PropertyPathBuilder (org.apache.helix.PropertyPathBuilder)4 JaxbAnnotationIntrospector (org.codehaus.jackson.xc.JaxbAnnotationIntrospector)4 HelixException (org.apache.helix.HelixException)3 IOException (java.io.IOException)2 TreeMap (java.util.TreeMap)2 ConstraintItem (org.apache.helix.model.ConstraintItem)2 AnnotationIntrospector (org.codehaus.jackson.map.AnnotationIntrospector)2 DeserializationConfig (org.codehaus.jackson.map.DeserializationConfig)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1