use of org.nustaq.serialization.FSTClazzInfo in project kontraktor by RuedigerMoeller.
the class MessageValidator method read.
public <T> Object read(Class<T> targetType, JsonObject parsed) throws ValidationException, ClassNotFoundException, IllegalAccessException, InstantiationException {
if (targetType == null) {
JsonValue jtype = parsed.get("type");
if (jtype == null || !jtype.isString()) {
throw new ValidationException("missing type information " + parsed.toString());
}
String jtstring = jtype.asString();
if (type2Clazz.containsKey(jtstring)) {
targetType = type2Clazz.get(jtstring);
} else {
targetType = (Class<T>) Class.forName(jtstring);
}
}
Object target = targetType.newInstance();
FSTClazzInfo classInfo = conf.getClassInfo(target.getClass());
FSTClazzInfo.FSTFieldInfo[] fields = classInfo.getFieldInfo();
for (int i = 0; i < fields.length; i++) {
FSTClazzInfo.FSTFieldInfo field = fields[i];
if (field.isPrimitive() || field.getType() == String.class || field.getType().isEnum()) {
JsonValue jsonValue = parsed.get(field.getName());
correlatePrimitiveValue(target, field, jsonValue);
} else if (field.getField().getAnnotation(JsonOption.class) == null)
throw new ValidationException("unhandled field '" + field.getName() + "' on " + target.getClass());
}
Set<String> names = Arrays.stream(fields).map(x -> x.getName()).collect(Collectors.toSet());
names.add("type");
String unknown = parsed.names().stream().filter(name -> !names.contains(name)).collect(Collectors.joining(","));
if (unknown.length() > 0)
throw new ValidationException("unknown fields in message:" + unknown);
return target;
}
Aggregations