Skip to content

Commit a76a6d0

Browse files
authored
Simplify public constructor for CheckedFromJsonException (#206)
Don't require an innerError/innerStack
1 parent 5e7b70c commit a76a6d0

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

json_annotation/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
## 0.2.6
22

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

77
* Added `JsonKey.defaultValue`.

json_annotation/lib/src/checked_helpers.dart

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,16 @@ T $checkedNew<T>(String className, Map map, T constructor(),
1313
try {
1414
return constructor();
1515
} on CheckedFromJsonException catch (e) {
16-
if (e._className == null) {
16+
if (identical(e.map, map) && e._className == null) {
1717
e._className = className;
18-
assert(identical(e.map, map));
1918
}
2019
rethrow;
2120
} catch (error, stack) {
2221
String key;
2322
if (error is ArgumentError) {
2423
key = fieldKeyMap[error.name] ?? error.name;
2524
}
26-
throw new CheckedFromJsonException(error, stack, map, key,
25+
throw new CheckedFromJsonException._(error, stack, map, key,
2726
className: className);
2827
}
2928
}
@@ -38,18 +37,22 @@ T $checkedConvert<T>(Map map, String key, T castFunc(Object value)) {
3837
} on CheckedFromJsonException {
3938
rethrow;
4039
} catch (error, stack) {
41-
throw new CheckedFromJsonException(error, stack, map, key);
40+
throw new CheckedFromJsonException._(error, stack, map, key);
4241
}
4342
}
4443

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

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

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

7376
/// Creates a new instance of [CheckedFromJsonException].
74-
CheckedFromJsonException(this.innerError, this.innerStack, this.map, this.key,
77+
CheckedFromJsonException(this.map, this.key, String className, this.message)
78+
: _className = className,
79+
innerError = null,
80+
innerStack = null;
81+
82+
CheckedFromJsonException._(
83+
this.innerError, this.innerStack, this.map, this.key,
7584
{String className})
7685
: _className = className,
7786
message = _getMessage(innerError);

0 commit comments

Comments
 (0)