Search in sources :

Example 1 with FSTClazzInfo

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;
}
Also used : Arrays(java.util.Arrays) FSTClazzInfo(org.nustaq.serialization.FSTClazzInfo) Map(java.util.Map) JsonObject(com.eclipsesource.json.JsonObject) FSTConfiguration(org.nustaq.serialization.FSTConfiguration) Set(java.util.Set) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) JsonValue(com.eclipsesource.json.JsonValue) JsonValue(com.eclipsesource.json.JsonValue) JsonObject(com.eclipsesource.json.JsonObject) FSTClazzInfo(org.nustaq.serialization.FSTClazzInfo)

Aggregations

JsonObject (com.eclipsesource.json.JsonObject)1 JsonValue (com.eclipsesource.json.JsonValue)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 FSTClazzInfo (org.nustaq.serialization.FSTClazzInfo)1 FSTConfiguration (org.nustaq.serialization.FSTConfiguration)1