tryToJson function
dynamic
tryToJson(
- dynamic object
Returns object.toJson() if it exists, otherwise the object itself.
Only swallows NoSuchMethodError from a missing toJson member — any
exception thrown by a present toJson propagates to the caller so
data-integrity bugs are not hidden.
Implementation
dynamic tryToJson(dynamic object) {
if (object == null) return null;
try {
return object.toJson();
} on NoSuchMethodError {
return object;
}
}