use of com.datastax.driver.core.exceptions.InvalidTypeException in project java-driver by datastax.
the class DataTypeTest method serializeDeserializeTest.
public void serializeDeserializeTest(ProtocolVersion version) {
for (DataType dt : DataType.allPrimitiveTypes()) {
if (exclude(dt))
continue;
Object value = TestUtils.getFixedValue(dt);
TypeCodec<Object> codec = codecRegistry.codecFor(dt);
assertEquals(codec.deserialize(codec.serialize(value, version), version), value);
}
TypeCodec<Long> codec = codecRegistry.codecFor(DataType.bigint());
try {
ByteBuffer badValue = ByteBuffer.allocate(4);
codec.deserialize(badValue, version);
fail("This should not have worked");
} catch (InvalidTypeException e) {
/* That's what we want */
}
}
use of com.datastax.driver.core.exceptions.InvalidTypeException in project java-driver by datastax.
the class SimpleStatement method convert.
/*
* This method performs a best-effort heuristic to guess which codec to use.
* Note that this is not particularly efficient as the codec registry needs to iterate over
* the registered codecs until it finds a suitable one.
*/
private static ByteBuffer[] convert(Object[] values, ProtocolVersion protocolVersion, CodecRegistry codecRegistry) {
ByteBuffer[] serializedValues = new ByteBuffer[values.length];
for (int i = 0; i < values.length; i++) {
Object value = values[i];
if (value == null) {
// impossible to locate the right codec when object is null,
// so forcing the result to null
serializedValues[i] = null;
} else {
if (value instanceof Token) {
// bypass CodecRegistry for Token instances
serializedValues[i] = ((Token) value).serialize(protocolVersion);
} else {
try {
TypeCodec<Object> codec = codecRegistry.codecFor(value);
serializedValues[i] = 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 %d of type %s does not correspond to any CQL3 type", i, value.getClass()), e);
}
}
}
}
return serializedValues;
}
use of com.datastax.driver.core.exceptions.InvalidTypeException in project java-driver by datastax.
the class Utils method convert.
/**
* Utility method to serialize user-provided values.
* <p/>
* This method is a copy of the one declared in {@link com.datastax.driver.core.SimpleStatement}, it was duplicated
* to avoid having to make it public.
* <p/>
* It is useful in situations where there is no metadata available and the underlying CQL
* type for the values is not known.
* <p/>
* This situation happens when a {@link com.datastax.driver.core.SimpleStatement}
* or a {@link com.datastax.driver.core.querybuilder.BuiltStatement} (Query Builder) contain values;
* in these places, the driver has no way to determine the right CQL type to use.
* <p/>
* This method performs a best-effort heuristic to guess which codec to use.
* Note that this is not particularly efficient as the codec registry needs to iterate over
* the registered codecs until it finds a suitable one.
*
* @param values The values to convert.
* @param protocolVersion The protocol version to use.
* @param codecRegistry The {@link CodecRegistry} to use.
* @return The converted values.
*/
static ByteBuffer[] convert(Object[] values, ProtocolVersion protocolVersion, CodecRegistry codecRegistry) {
ByteBuffer[] serializedValues = new ByteBuffer[values.length];
for (int i = 0; i < values.length; i++) {
Object value = values[i];
if (value == null) {
// impossible to locate the right codec when object is null,
// so forcing the result to null
serializedValues[i] = null;
} else {
if (value instanceof Token) {
// bypass CodecRegistry for Token instances
serializedValues[i] = ((Token) value).serialize(protocolVersion);
} else {
try {
TypeCodec<Object> codec = codecRegistry.codecFor(value);
serializedValues[i] = 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 %d of type %s does not correspond to any CQL3 type", i, value.getClass()), e);
}
}
}
}
return serializedValues;
}
use of com.datastax.driver.core.exceptions.InvalidTypeException in project java-driver by datastax.
the class LocalDateCodec method parse.
@Override
public LocalDate parse(String value) {
if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL"))
return null;
// strip enclosing single quotes, if any
if (isQuoted(value)) {
value = unquote(value);
}
if (isLongLiteral(value)) {
long raw;
try {
raw = parseLong(value);
} catch (NumberFormatException e) {
throw new InvalidTypeException(String.format("Cannot parse date value from \"%s\"", value));
}
int days;
try {
days = fromCqlDateToDaysSinceEpoch(raw);
} catch (IllegalArgumentException e) {
throw new InvalidTypeException(String.format("Cannot parse date value from \"%s\"", value));
}
return EPOCH.plusDays(days);
}
try {
return LocalDate.parse(value, FORMATTER);
} catch (RuntimeException e) {
throw new InvalidTypeException(String.format("Cannot parse date value from \"%s\"", value));
}
}
use of com.datastax.driver.core.exceptions.InvalidTypeException in project java-driver by datastax.
the class Jsr353JsonCodec method format.
@Override
public String format(JsonStructure value) throws InvalidTypeException {
if (value == null)
return "NULL";
String json;
StringWriter sw = new StringWriter();
try {
JsonWriter writer = writerFactory.createWriter(sw);
writer.write(value);
json = sw.toString();
} catch (JsonException e) {
throw new InvalidTypeException(e.getMessage(), e);
} finally {
try {
sw.close();
} catch (IOException e) {
// cannot happen
throw new InvalidTypeException(e.getMessage(), e);
}
}
return ParseUtils.quote(json);
}
Aggregations