use of org.apache.cassandra.serializers.MarshalException in project cassandra by apache.
the class UserType method fromJSONObject.
@Override
public Term fromJSONObject(Object parsed) throws MarshalException {
if (parsed instanceof String)
parsed = Json.decodeJson((String) parsed);
if (!(parsed instanceof Map))
throw new MarshalException(String.format("Expected a map, but got a %s: %s", parsed.getClass().getSimpleName(), parsed));
Map<String, Object> map = (Map<String, Object>) parsed;
Json.handleCaseSensitivity(map);
List<Term> terms = new ArrayList<>(types.size());
Set keys = map.keySet();
assert keys.isEmpty() || keys.iterator().next() instanceof String;
int foundValues = 0;
for (int i = 0; i < types.size(); i++) {
Object value = map.get(stringFieldNames.get(i));
if (value == null) {
terms.add(Constants.NULL_VALUE);
} else {
terms.add(types.get(i).fromJSONObject(value));
foundValues += 1;
}
}
// check for extra, unrecognized fields
if (foundValues != map.size()) {
for (Object fieldName : keys) {
if (!stringFieldNames.contains(fieldName))
throw new MarshalException(String.format("Unknown field '%s' in value of user defined type %s", fieldName, getNameAsString()));
}
}
return new UserTypes.DelayedValue(this, terms);
}
use of org.apache.cassandra.serializers.MarshalException in project cassandra by apache.
the class ListType method fromJSONObject.
@Override
public Term fromJSONObject(Object parsed) throws MarshalException {
if (parsed instanceof String)
parsed = Json.decodeJson((String) parsed);
if (!(parsed instanceof List))
throw new MarshalException(String.format("Expected a list, but got a %s: %s", parsed.getClass().getSimpleName(), parsed));
List list = (List) parsed;
List<Term> terms = new ArrayList<>(list.size());
for (Object element : list) {
if (element == null)
throw new MarshalException("Invalid null element in list");
terms.add(elements.fromJSONObject(element));
}
return new Lists.DelayedValue(terms);
}
use of org.apache.cassandra.serializers.MarshalException in project cassandra by apache.
the class MapType method fromJSONObject.
@Override
public Term fromJSONObject(Object parsed) throws MarshalException {
if (parsed instanceof String)
parsed = Json.decodeJson((String) parsed);
if (!(parsed instanceof Map))
throw new MarshalException(String.format("Expected a map, but got a %s: %s", parsed.getClass().getSimpleName(), parsed));
Map<Object, Object> map = (Map<Object, Object>) parsed;
Map<Term, Term> terms = new HashMap<>(map.size());
for (Map.Entry<Object, Object> entry : map.entrySet()) {
if (entry.getKey() == null)
throw new MarshalException("Invalid null key in map");
if (entry.getValue() == null)
throw new MarshalException("Invalid null value in map");
terms.put(keys.fromJSONObject(entry.getKey()), values.fromJSONObject(entry.getValue()));
}
return new Maps.DelayedValue(keys, terms);
}
use of org.apache.cassandra.serializers.MarshalException in project cassandra by apache.
the class DynamicCompositeType method validateComparator.
protected <V> AbstractType<?> validateComparator(int i, V input, ValueAccessor<V> accessor, int offset) throws MarshalException {
AbstractType<?> comparator = null;
if (accessor.sizeFromOffset(input, offset) < 2)
throw new MarshalException("Not enough bytes to header of the comparator part of component " + i);
int header = accessor.getShort(input, offset);
offset += TypeSizes.SHORT_SIZE;
if ((header & 0x8000) == 0) {
if (accessor.sizeFromOffset(input, offset) < header)
throw new MarshalException("Not enough bytes to read comparator name of component " + i);
V value = accessor.slice(input, offset, header);
String valueStr = null;
try {
valueStr = accessor.toString(value);
comparator = TypeParser.parse(valueStr);
} catch (CharacterCodingException ce) {
// ByteBufferUtil.string failed.
// Log it here and we'll further throw an exception below since comparator == null
logger.error("Failed when decoding the byte buffer in ByteBufferUtil.string()", ce);
} catch (Exception e) {
// parse failed.
// Log it here and we'll further throw an exception below since comparator == null
logger.error("Failed to parse value string \"{}\" with exception:", valueStr, e);
}
} else {
comparator = aliases.get((byte) (header & 0xFF));
}
if (comparator == null)
throw new MarshalException("Cannot find comparator for component " + i);
else
return comparator;
}
use of org.apache.cassandra.serializers.MarshalException in project cassandra by apache.
the class NonTokenizingAnalyzer method hasNext.
public boolean hasNext() {
// check that we know how to handle the input, otherwise bail
if (!VALID_ANALYZABLE_TYPES.contains(validator))
return false;
if (hasNext) {
String inputStr;
try {
inputStr = validator.getString(input);
if (inputStr == null)
throw new MarshalException(String.format("'null' deserialized value for %s with %s", ByteBufferUtil.bytesToHex(input), validator));
Object pipelineRes = FilterPipelineExecutor.execute(filterPipeline, inputStr);
if (pipelineRes == null)
return false;
next = validator.fromString(normalize((String) pipelineRes));
return true;
} catch (MarshalException e) {
logger.error("Failed to deserialize value with " + validator, e);
return false;
} finally {
hasNext = false;
}
}
return false;
}
Aggregations