Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion json_annotation/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## 0.2.6

* `CheckedFromJsonException`
* The constructor is now public.
* Added a public constructor to support hand-written JSON decoding logic.
* The `message` property is now `String` (instead of `Object`).

* Added `JsonKey.defaultValue`.
Expand Down
19 changes: 14 additions & 5 deletions json_annotation/lib/src/checked_helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,16 @@ T $checkedNew<T>(String className, Map map, T constructor(),
try {
return constructor();
} on CheckedFromJsonException catch (e) {
if (e._className == null) {
if (identical(e.map, map) && e._className == null) {
e._className = className;
assert(identical(e.map, map));
}
rethrow;
} catch (error, stack) {
String key;
if (error is ArgumentError) {
key = fieldKeyMap[error.name] ?? error.name;
}
throw new CheckedFromJsonException(error, stack, map, key,
throw new CheckedFromJsonException._(error, stack, map, key,
className: className);
}
}
Expand All @@ -38,18 +37,22 @@ T $checkedConvert<T>(Map map, String key, T castFunc(Object value)) {
} on CheckedFromJsonException {
rethrow;
} catch (error, stack) {
throw new CheckedFromJsonException(error, stack, map, key);
throw new CheckedFromJsonException._(error, stack, map, key);
}
}

/// Exception thrown if there is a runtime exception in `fromJson`
/// code generated when `JsonSerializableGenerator.checked` is `true`
class CheckedFromJsonException implements Exception {
/// The [Error] or [Exception] that triggered this exception.
///
/// If this instance was created by user code, this field will be `null`.
final Object innerError;

/// The [StackTrace] for the [Error] or [Exception] that triggered this
/// exception.
///
/// If this instance was created by user code, this field will be `null`.
final StackTrace innerStack;

/// The key from [map] that corresponds to the thrown [innerError].
Expand All @@ -71,7 +74,13 @@ class CheckedFromJsonException implements Exception {
String _className;

/// Creates a new instance of [CheckedFromJsonException].
CheckedFromJsonException(this.innerError, this.innerStack, this.map, this.key,
CheckedFromJsonException(this.map, this.key, String className, this.message)
: _className = className,
innerError = null,
innerStack = null;

CheckedFromJsonException._(
this.innerError, this.innerStack, this.map, this.key,
{String className})
: _className = className,
message = _getMessage(innerError);
Expand Down