Search in sources :

Example 1 with Persister

use of com.palantir.atlasdb.persist.api.Persister in project atlasdb by palantir.

the class ColumnValueDescription method persistJsonToBytes.

@SuppressWarnings("unchecked")
public byte[] persistJsonToBytes(ClassLoader classLoader, String str) throws ParseException {
    final byte[] bytes;
    if (format == Format.PERSISTABLE) {
        throw new SafeIllegalArgumentException("Tried to pass json into a persistable type.");
    } else if (format == Format.PERSISTER) {
        Persister<?> persister = getPersister();
        if (JsonNode.class == persister.getPersistingClassType()) {
            try {
                JsonNode jsonNode = new ObjectMapper().readValue(str, JsonNode.class);
                return ((Persister<JsonNode>) persister).persistToBytes(jsonNode);
            } catch (IOException e) {
                throw Throwables.throwUncheckedException(e);
            }
        } else {
            throw new SafeIllegalArgumentException("Tried to write json to a Persister that isn't for JsonNode.");
        }
    } else if (format == Format.PROTO) {
        Message.Builder builder = createBuilder(classLoader);
        // This will have issues with base64 blobs
        JsonFormat.merge(str, builder);
        bytes = builder.build().toByteArray();
    } else {
        bytes = type.convertFromString(str);
    }
    return CompressionUtils.compress(bytes, compression);
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) Message(com.google.protobuf.Message) JsonNode(com.fasterxml.jackson.databind.JsonNode) Persister(com.palantir.atlasdb.persist.api.Persister) IOException(java.io.IOException) SafeIllegalArgumentException(com.palantir.logsafe.exceptions.SafeIllegalArgumentException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 AbstractMessage (com.google.protobuf.AbstractMessage)1 Message (com.google.protobuf.Message)1 Persister (com.palantir.atlasdb.persist.api.Persister)1 SafeIllegalArgumentException (com.palantir.logsafe.exceptions.SafeIllegalArgumentException)1 IOException (java.io.IOException)1