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 example/example.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 28 additions & 15 deletions lib/generators/json_serializable_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ class JsonSerializableGenerator
// write fields
fields.forEach((name, field) {
//TODO - handle aliased imports
//TODO - write generic types. Now `List<int>` turns into `List`
buffer.writeln(' ${field.type.name} get $name;');
buffer.writeln(' ${field.type} get $name;');
});

// write toJson method
Expand Down Expand Up @@ -204,26 +203,33 @@ class JsonSerializableGenerator
}
}

if (_coreListChecker.isAssignableFromType(fieldType)) {
var indexVal = "i${depth}";

var substitute = '${expression}[$indexVal]';
var isList = false;
if (_coreIterableChecker.isAssignableFromType(fieldType)) {
if (_coreListChecker.isAssignableFromType(fieldType)) {
isList = true;
}
var substitute = "v$depth";
var subFieldValue = _fieldToJsonMapValue(substitute,
_getIterableGenericType(fieldType as InterfaceType), depth + 1);

// If we're dealing with `List<T>` where `T` must be serialized, then
// generate a value that does the equivalent of .map(...).toList(), but
// Does so efficiently by creating a known-length List.
//TODO(kevmoo) this might be overkill. I think .map on iterable returns
// an efficient-length iterable, so .map(...).toList() might be fine. :-/
// In the case of trivial JSON types (int, String, etc), `subFieldValue`
// will be identical to `substitute` – so no explicit mapping is needed.
// If they are not equal, then we to write out the substitution.
if (subFieldValue != substitute) {
// TODO: the type could be imported from a library with a prefix!
return "${expression} == null ? null : "
"new List.generate(${expression}.length, "
"(int $indexVal) => $subFieldValue)";
expression = "${expression}?.map(($substitute) => $subFieldValue)";

// expression now represents an Iterable (even if it started as a List
// ...resetting `isList` to `false`.
isList = false;
}
}

if (!isList && _coreIterableChecker.isAssignableFromType(fieldType)) {
// Then we need to add `?.toList()
expression += "?.toList()";
}

return expression;
}

Expand Down Expand Up @@ -252,6 +258,13 @@ class JsonSerializableGenerator
_getIterableGenericType(searchType as InterfaceType);

var itemVal = "v$depth";
var itemSubVal =
_writeAccessToVar(itemVal, iterableGenericType, depth: depth + 1);

// If `itemSubVal` is the same, then we don't need to do anything fancy
if (itemVal == itemSubVal) {
return varExpression;
}

var output = "($varExpression as List)?.map(($itemVal) => "
"${_writeAccessToVar(itemVal, iterableGenericType, depth: depth+1)}"
Expand All @@ -264,7 +277,7 @@ class JsonSerializableGenerator
return output;
}

if (!searchType.isDynamic) {
if (!searchType.isDynamic && !searchType.isObject) {
return "$varExpression as $searchType";
}

Expand Down
2 changes: 1 addition & 1 deletion test/json_serializable_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ void main() {
await _getClassForCodeString('ParentObjectWithDynamicChildren');
var output = await _generator.generate(element, null);

expect(output, contains('(json[\'children\'] as List)?.map('));
expect(output, contains('children = json[\'children\'];'));
});

test('class with list of int is cast for strong mode', () async {
Expand Down
20 changes: 20 additions & 0 deletions test/test_files/json_test_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,26 @@ import 'package:source_gen/generators/json_serializable.dart';

part 'json_test_example.g.dart';

@JsonSerializable()
class ListGenericTests extends Object with _$ListGenericTestsSerializerMixin {
ListGenericTests();

factory ListGenericTests.fromJson(Map<String, Object> json) =>
_$ListGenericTestsFromJson(json);

Iterable iterable;
Iterable<dynamic> dynamicIterable;
Iterable<Object> objectIterable;
Iterable<int> intIterable;
Iterable<DateTime> dateTimeIterable;

List list;
List<dynamic> dynamicList;
List<Object> objectList;
List<int> intList;
List<DateTime> dateTimeList;
}

@JsonSerializable()
class Person extends Object with _$PersonSerializerMixin {
final String firstName, middleName, lastName;
Expand Down
58 changes: 51 additions & 7 deletions test/test_files/json_test_example.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.