Skip to content

Commit 39467a3

Browse files
authored
Fix support for static functions with JsonKey.readValue (#1058)
and prepare to release v6.1.1 Fixes #1055
1 parent 1a8c00b commit 39467a3

15 files changed

+76
-40
lines changed

json_serializable/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 6.1.1
2+
3+
- Fix `JsonKey.readValue` support to allow static functions.
4+
15
## 6.1.0
26

37
- Support `JsonKey.readValue` to allow customized reading of values from source

json_serializable/lib/src/json_key_utils.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,8 @@ KeyConfig _from(FieldElement element, ClassConfig classAnnotation) {
212212
String? readValueFunctionName;
213213
final readValue = obj.read('readValue');
214214
if (!readValue.isNull) {
215-
final objValue = readValue.objectValue.toFunctionValue()!;
216-
readValueFunctionName = objValue.name;
215+
readValueFunctionName =
216+
readValue.objectValue.toFunctionValue()!.qualifiedName;
217217
}
218218

219219
return _populateJsonKey(

json_serializable/lib/src/type_helper_ctx.dart

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,5 @@ ConvertData? _convertData(DartObject obj, FieldElement element, bool isFrom) {
176176
}
177177
}
178178

179-
var name = executableElement.name;
180-
181-
if (executableElement is MethodElement) {
182-
name = '${executableElement.enclosingElement.name}.$name';
183-
}
184-
185-
return ConvertData(name, argType);
179+
return ConvertData(executableElement.qualifiedName, argType);
186180
}

json_serializable/lib/src/utils.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,21 @@ String typeToCode(
207207
}
208208
throw UnimplementedError('(${type.runtimeType}) $type');
209209
}
210+
211+
extension ExecutableElementExtension on ExecutableElement {
212+
/// Returns the name of `this` qualified with the class name if it's a
213+
/// [MethodElement].
214+
String get qualifiedName {
215+
if (this is FunctionElement) {
216+
return name;
217+
}
218+
219+
if (this is MethodElement) {
220+
return '${enclosingElement.name}.$name';
221+
}
222+
223+
throw UnsupportedError(
224+
'Not sure how to support typeof $runtimeType',
225+
);
226+
}
227+
}

json_serializable/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: json_serializable
2-
version: 6.1.0
2+
version: 6.1.1
33
description: >-
44
Automatically generate code for converting to and from JSON by annotating
55
Dart classes.

json_serializable/test/kitchen_sink/kitchen_sink.dart

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,6 @@ class _Factory implements k.KitchenSinkFactory<String, dynamic> {
7676
}
7777

7878
Object? _valueAccessor(Map json, String key) {
79-
if (key == k.trickyKeyName) {
80-
return json[k.trickyKeyName] ?? json['STRING'];
81-
}
82-
8379
if (key == 'iterable') {
8480
return json['iterable'] ?? json['theIterable'];
8581
}
@@ -165,7 +161,7 @@ class KitchenSink implements k.KitchenSink {
165161
// Handle fields with names that collide with helper names
166162
Map<String, bool> val = _defaultMap();
167163
bool? writeNotNull;
168-
@JsonKey(name: k.trickyKeyName, readValue: _valueAccessor)
164+
@JsonKey(name: k.trickyKeyName, readValue: _trickyValueAccessor)
169165
String? string;
170166

171167
SimpleObject simpleObject = _defaultSimpleObject();
@@ -184,6 +180,14 @@ class KitchenSink implements k.KitchenSink {
184180
}
185181

186182
bool operator ==(Object other) => k.sinkEquals(this, other);
183+
184+
static Object? _trickyValueAccessor(Map json, String key) {
185+
if (key == k.trickyKeyName) {
186+
return json[k.trickyKeyName] ?? json['STRING'];
187+
}
188+
189+
return json[key];
190+
}
187191
}
188192

189193
@JsonSerializable()

json_serializable/test/kitchen_sink/kitchen_sink.g.dart

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

json_serializable/test/kitchen_sink/kitchen_sink.g_any_map.dart

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,6 @@ class _Factory implements k.KitchenSinkFactory<dynamic, dynamic> {
7575
}
7676

7777
Object? _valueAccessor(Map json, String key) {
78-
if (key == k.trickyKeyName) {
79-
return json[k.trickyKeyName] ?? json['STRING'];
80-
}
81-
8278
if (key == 'iterable') {
8379
return json['iterable'] ?? json['theIterable'];
8480
}
@@ -165,7 +161,7 @@ class KitchenSink implements k.KitchenSink {
165161
// Handle fields with names that collide with helper names
166162
Map<String, bool> val = _defaultMap();
167163
bool? writeNotNull;
168-
@JsonKey(name: k.trickyKeyName, readValue: _valueAccessor)
164+
@JsonKey(name: k.trickyKeyName, readValue: _trickyValueAccessor)
169165
String? string;
170166

171167
SimpleObject simpleObject = _defaultSimpleObject();
@@ -184,6 +180,14 @@ class KitchenSink implements k.KitchenSink {
184180
}
185181

186182
bool operator ==(Object other) => k.sinkEquals(this, other);
183+
184+
static Object? _trickyValueAccessor(Map json, String key) {
185+
if (key == k.trickyKeyName) {
186+
return json[k.trickyKeyName] ?? json['STRING'];
187+
}
188+
189+
return json[key];
190+
}
187191
}
188192

189193
@JsonSerializable(

json_serializable/test/kitchen_sink/kitchen_sink.g_any_map.g.dart

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

json_serializable/test/kitchen_sink/kitchen_sink.g_any_map__checked.dart

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,6 @@ class _Factory implements k.KitchenSinkFactory<dynamic, dynamic> {
7575
}
7676

7777
Object? _valueAccessor(Map json, String key) {
78-
if (key == k.trickyKeyName) {
79-
return json[k.trickyKeyName] ?? json['STRING'];
80-
}
81-
8278
if (key == 'iterable') {
8379
return json['iterable'] ?? json['theIterable'];
8480
}
@@ -166,7 +162,7 @@ class KitchenSink implements k.KitchenSink {
166162
// Handle fields with names that collide with helper names
167163
Map<String, bool> val = _defaultMap();
168164
bool? writeNotNull;
169-
@JsonKey(name: k.trickyKeyName, readValue: _valueAccessor)
165+
@JsonKey(name: k.trickyKeyName, readValue: _trickyValueAccessor)
170166
String? string;
171167

172168
SimpleObject simpleObject = _defaultSimpleObject();
@@ -185,6 +181,14 @@ class KitchenSink implements k.KitchenSink {
185181
}
186182

187183
bool operator ==(Object other) => k.sinkEquals(this, other);
184+
185+
static Object? _trickyValueAccessor(Map json, String key) {
186+
if (key == k.trickyKeyName) {
187+
return json[k.trickyKeyName] ?? json['STRING'];
188+
}
189+
190+
return json[key];
191+
}
188192
}
189193

190194
@JsonSerializable(

0 commit comments

Comments
 (0)