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