google / google/json_serializable.dart

JsonKey.readValue having an error when trying to serialize complex objects

Open
#1,123 3 comments 9 reactions 0 assignees View on GitHub
Dominant language
Dart
Stars
1.6k
Forks
461
Avg merge
45m
Merged PRs (30d)
1

Description

### Reproducible steps

```
{
"id": "123",
"firstName": "John",
"lastName": "Doe"
"age": "27"
}
```

```
import 'package:json_annotation/json_annotation.dart';

@JsonSerializable()
class Person {
const Person (this.id, this.name, this.age);

final String id;

@JsonKey(readValue: _readName)
final Name name;
final int age;

static Name _readName(Map map, String key) => Name (map['firstName'], map['lastName']);
}

class Name {
const Name (this.firstName, this.lastName);

final String firstName;
final String lastName;
}
```
If you run a build for this class, it will have an error of:
```
Could not generate `fromJson` code for `name`.
To support the type `Name` you can:
* Use `JsonConverter`
https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonConverter-class.html
* Use `JsonKey` fields `fromJson` and `toJson`
https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonKey/fromJson.html
https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonKey/toJson.html
```

### Environment
Flutter: 2.10.3
json_serializable: 6.1.5
json_annotation: 4.4.0

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.