jsonField<T> function
dynamic
jsonField<T>(})
Implementation
dynamic jsonField <T>(dynamic json, List<String> field, {bool nullable = true, T? defaultValue, bool printUnknownException = false}) {
dynamic retval = json;
try {
for (String f in field){
retval = retval [f];
if (retval == null && nullable) {
break;
}
}
if (retval == null) {
if (!nullable) {
throw BodyException (type: BodyExceptionType.isNull, fieldName: field.join("-"));
} else {
return defaultValue;
}
} else {
assert (retval is T);
}
return retval;
} on BodyException {
rethrow;
} on AssertionError catch (_) {
throw BodyException(
type: BodyExceptionType.isNotType,
fieldName: field.join ("-"),
failedType: T,
currentType: retval.runtimeType
);
} on NoSuchMethodError catch (_) {
throw BodyException(
type: BodyExceptionType.isNull,
fieldName: field.join ("_"),
failedType: T,
currentType: retval.runtimeType
);
} catch (error, bt) {
if (printUnknownException) {
Completer ().completeError(error, bt);
}
throw BodyException(
type: BodyExceptionType.undefined,
fieldName: field.join ("_")
);
}
}