diff --git a/json_serializable/lib/src/field_helpers.dart b/json_serializable/lib/src/field_helpers.dart index 4539c7e5..c2de4ca5 100644 --- a/json_serializable/lib/src/field_helpers.dart +++ b/json_serializable/lib/src/field_helpers.dart @@ -58,9 +58,9 @@ class _FieldSet implements Comparable<_FieldSet> { /// preference for the getter if it's defined. int offsetFor(FieldElement2 e) { if (e.isSynthetic) { - return (e.getter2 ?? e.setter2)!.firstFragment.nameOffset2!; + return (e.getter2 ?? e.setter2)!.firstFragment.nameOffset2 ?? 0; } - return e.firstFragment.nameOffset2!; + return e.firstFragment.nameOffset2 ?? 0; } return offsetFor(a).compareTo(offsetFor(b)); diff --git a/json_serializable/test/integration/json_test_example.dart b/json_serializable/test/integration/json_test_example.dart index 963cdc27..5b6a5581 100644 --- a/json_serializable/test/integration/json_test_example.dart +++ b/json_serializable/test/integration/json_test_example.dart @@ -266,3 +266,34 @@ class RegressionTestIssue1210 with RegressionTestIssue1210Mixin { Map toJson() => _$RegressionTestIssue1210ToJson(this); } + +@JsonSerializable() +class CustomList extends ListBase { + // Regression test for issue: + // https://github.com/google/json_serializable.dart/issues/1512 + + final List _innerList = []; + + CustomList(); + + @override + int get length => _innerList.length; + + @override + set length(int newLength) { + _innerList.length = newLength; + } + + @override + String operator [](int index) => _innerList[index]; + + @override + void operator []=(int index, String value) { + _innerList[index] = value; + } + + factory CustomList.fromJson(Map json) => + _$CustomListFromJson(json); + + Map toJson() => _$CustomListToJson(this); +} diff --git a/json_serializable/test/integration/json_test_example.g.dart b/json_serializable/test/integration/json_test_example.g.dart index 1f1b456e..66b12fa7 100644 --- a/json_serializable/test/integration/json_test_example.g.dart +++ b/json_serializable/test/integration/json_test_example.g.dart @@ -229,3 +229,15 @@ RegressionTestIssue1210 _$RegressionTestIssue1210FromJson( Map _$RegressionTestIssue1210ToJson( RegressionTestIssue1210 instance, ) => {'field': instance.field}; + +CustomList _$CustomListFromJson(Map json) => CustomList() + ..first = json['first'] as String + ..last = json['last'] as String + ..length = (json['length'] as num).toInt(); + +Map _$CustomListToJson(CustomList instance) => + { + 'first': instance.first, + 'last': instance.last, + 'length': instance.length, + }; diff --git a/json_serializable/test/integration/json_test_example.g_any_map.dart b/json_serializable/test/integration/json_test_example.g_any_map.dart index f499eb13..d4458416 100644 --- a/json_serializable/test/integration/json_test_example.g_any_map.dart +++ b/json_serializable/test/integration/json_test_example.g_any_map.dart @@ -266,3 +266,34 @@ class RegressionTestIssue1210 with RegressionTestIssue1210Mixin { Map toJson() => _$RegressionTestIssue1210ToJson(this); } + +@JsonSerializable(anyMap: true) +class CustomList extends ListBase { + // Regression test for issue: + // https://github.com/google/json_serializable.dart/issues/1512 + + final List _innerList = []; + + CustomList(); + + @override + int get length => _innerList.length; + + @override + set length(int newLength) { + _innerList.length = newLength; + } + + @override + String operator [](int index) => _innerList[index]; + + @override + void operator []=(int index, String value) { + _innerList[index] = value; + } + + factory CustomList.fromJson(Map json) => + _$CustomListFromJson(json); + + Map toJson() => _$CustomListToJson(this); +} diff --git a/json_serializable/test/integration/json_test_example.g_any_map.g.dart b/json_serializable/test/integration/json_test_example.g_any_map.g.dart index f25c7971..fdcd10b2 100644 --- a/json_serializable/test/integration/json_test_example.g_any_map.g.dart +++ b/json_serializable/test/integration/json_test_example.g_any_map.g.dart @@ -227,3 +227,15 @@ RegressionTestIssue1210 _$RegressionTestIssue1210FromJson(Map json) => Map _$RegressionTestIssue1210ToJson( RegressionTestIssue1210 instance, ) => {'field': instance.field}; + +CustomList _$CustomListFromJson(Map json) => CustomList() + ..first = json['first'] as String + ..last = json['last'] as String + ..length = (json['length'] as num).toInt(); + +Map _$CustomListToJson(CustomList instance) => + { + 'first': instance.first, + 'last': instance.last, + 'length': instance.length, + };