Search in sources :

Example 11 with InvalidTypeException

use of com.datastax.driver.core.exceptions.InvalidTypeException in project java-driver by datastax.

the class Jsr353JsonCodec method serialize.

@Override
public ByteBuffer serialize(JsonStructure value, ProtocolVersion protocolVersion) throws InvalidTypeException {
    if (value == null)
        return null;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        JsonWriter writer = writerFactory.createWriter(baos);
        writer.write(value);
        return ByteBuffer.wrap(baos.toByteArray());
    } catch (JsonException e) {
        throw new InvalidTypeException(e.getMessage(), e);
    } finally {
        try {
            baos.close();
        } catch (IOException e) {
            // cannot happen
            throw new InvalidTypeException(e.getMessage(), e);
        }
    }
}
Also used : InvalidTypeException(com.datastax.driver.core.exceptions.InvalidTypeException)

Example 12 with InvalidTypeException

use of com.datastax.driver.core.exceptions.InvalidTypeException in project java-driver by datastax.

the class Jsr353JsonCodec method parse.

@Override
@SuppressWarnings("unchecked")
public JsonStructure parse(String value) throws InvalidTypeException {
    if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL"))
        return null;
    if (!ParseUtils.isQuoted(value))
        throw new InvalidTypeException("JSON strings must be enclosed by single quotes");
    String json = ParseUtils.unquote(value);
    StringReader sr = new StringReader(json);
    try {
        JsonReader reader = readerFactory.createReader(sr);
        return reader.read();
    } catch (JsonException e) {
        throw new InvalidTypeException(e.getMessage(), e);
    } finally {
        sr.close();
    }
}
Also used : InvalidTypeException(com.datastax.driver.core.exceptions.InvalidTypeException)

Example 13 with InvalidTypeException

use of com.datastax.driver.core.exceptions.InvalidTypeException in project java-driver by datastax.

the class ObjectArrayCodec method deserialize.

@Override
public E[] deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) {
    if (bytes == null || bytes.remaining() == 0)
        return newInstance(0);
    try {
        ByteBuffer input = bytes.duplicate();
        int n = CodecUtils.readSize(input, protocolVersion);
        E[] array = newInstance(n);
        for (int i = 0; i < n; i++) {
            ByteBuffer databb = CodecUtils.readValue(input, protocolVersion);
            array[i] = eltCodec.deserialize(databb, protocolVersion);
        }
        return array;
    } catch (BufferUnderflowException e) {
        throw new InvalidTypeException("Not enough bytes to deserialize list");
    }
}
Also used : ByteBuffer(java.nio.ByteBuffer) BufferUnderflowException(java.nio.BufferUnderflowException) InvalidTypeException(com.datastax.driver.core.exceptions.InvalidTypeException)

Example 14 with InvalidTypeException

use of com.datastax.driver.core.exceptions.InvalidTypeException in project java-driver by datastax.

the class SimpleStatement method convert.

private static Map<String, ByteBuffer> convert(Map<String, Object> values, ProtocolVersion protocolVersion, CodecRegistry codecRegistry) {
    Map<String, ByteBuffer> serializedValues = new HashMap<String, ByteBuffer>();
    for (Map.Entry<String, Object> entry : values.entrySet()) {
        String name = entry.getKey();
        Object value = entry.getValue();
        if (value == null) {
            // impossible to locate the right codec when object is null,
            // so forcing the result to null
            serializedValues.put(name, null);
        } else {
            if (value instanceof Token) {
                // bypass CodecRegistry for Token instances
                serializedValues.put(name, ((Token) value).serialize(protocolVersion));
            } else {
                try {
                    TypeCodec<Object> codec = codecRegistry.codecFor(value);
                    serializedValues.put(name, codec.serialize(value, protocolVersion));
                } catch (Exception e) {
                    // Catch and rethrow to provide a more helpful error message (one that include which value is bad)
                    throw new InvalidTypeException(String.format("Value '%s' of type %s does not correspond to any CQL3 type", name, value.getClass()), e);
                }
            }
        }
    }
    return serializedValues;
}
Also used : HashMap(java.util.HashMap) ByteBuffer(java.nio.ByteBuffer) Map(java.util.Map) HashMap(java.util.HashMap) InvalidTypeException(com.datastax.driver.core.exceptions.InvalidTypeException) InvalidTypeException(com.datastax.driver.core.exceptions.InvalidTypeException)

Aggregations

InvalidTypeException (com.datastax.driver.core.exceptions.InvalidTypeException)14 ByteBuffer (java.nio.ByteBuffer)7 Map (java.util.Map)2 BoundStatement (com.datastax.driver.core.BoundStatement)1 PreparedStatement (com.datastax.driver.core.PreparedStatement)1 ResultSetFuture (com.datastax.driver.core.ResultSetFuture)1 Session (com.datastax.driver.core.Session)1 NoHostAvailableException (com.datastax.driver.core.exceptions.NoHostAvailableException)1 QueryExecutionException (com.datastax.driver.core.exceptions.QueryExecutionException)1 QueryValidationException (com.datastax.driver.core.exceptions.QueryValidationException)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 BufferUnderflowException (java.nio.BufferUnderflowException)1 Charset (java.nio.charset.Charset)1 HashMap (java.util.HashMap)1 TimeoutException (java.util.concurrent.TimeoutException)1 Matcher (java.util.regex.Matcher)1 FlowFile (org.apache.nifi.flowfile.FlowFile)1 ComponentLog (org.apache.nifi.logging.ComponentLog)1 ProcessSession (org.apache.nifi.processor.ProcessSession)1