} catch (NoSuchMethodException | IllegalAccessException ex) {
throw new IllegalStateException("Attempted to decode enum type", ex);
}
return (T) json;
}
CodableClassInfo classInfo = Fields.getClassFieldMap(type);
// json config is "unexpectedly" an array; if the base class has registered a handler, use it
if (json instanceof JSONArray) {
Class<?> arrarySugar = classInfo.getArraySugar();
if (arrarySugar != null) {
LineNumberInfo infoCopy = ((JSONArray) json).getMyLineNumberInfo();
JSONObject magicWrapper = new JSONObject();
magicWrapper.put(classInfo.getPluginMap().aliasDefaults("_array").toConfig().getString("_primary"), json, infoCopy, infoCopy);
classInfo = Fields.getClassFieldMap(arrarySugar);
json = magicWrapper;
type = (Class<T>) arrarySugar;
}
}
if (!(json instanceof JSONObject)) {
return (T) json;
}
JSONObject jsonObj = (JSONObject) json;
if (info == LineNumberInfo.MissingInfo) {
info = jsonObj.getLineNumberInfo();
}
String classField = classInfo.getClassField();
String stype = jsonObj.optString(classField, null);
if ((stype == null) && Modifier.isAbstract(type.getModifiers()) &&
(jsonObj.length() == 1)) {
// if otherwise doomed to fail, try supporting "type-value : {...}" syntax
stype = jsonObj.keySet().iterator().next();
jsonObj = jsonObj.getJSONObject(stype);
}
try {
if (stype != null) {
Class<?> atype = classInfo.getClass(stype);
classInfo = Fields.getClassFieldMap(atype);
type = (Class<T>) atype;
jsonObj.remove(classField);
}
} catch (ClassNotFoundException ex) {
String helpMessage = Plugins.classNameSuggestions(
PluginRegistry.defaultRegistry(), classInfo.getPluginMap(), stype);
throw new CodecExceptionLineNumber(new ClassNotFoundException(helpMessage, ex),
jsonObj.getValLineNumber(classField));
} catch (Exception ex) {
throw new CodecExceptionLineNumber(ex, jsonObj.getValLineNumber(classField));
}