google / google/json_serializable.dart
Consider option to define `Codec<Map<String, dynamic>, MyType>`
- Dominant language
- Dart
- Stars
- 1.6k
- Forks
- 461
- Avg merge
- 45m
- Merged PRs (30d)
- 1
Description
Might it not be nice to offer declaring a `Codec` to convert to/from JSON for `MyType`
```dart
import 'package:json_annotation/json_annotation.dart';
part 'example.g.dart';
@JsonCodec()
class Person {
String name;
@JsonKey(name: 'date-of-birth', nullable: false)
DateTime dateOfBirth;
...
}
```
Then a generated file a long the lines of:
```dart
import 'dart:convert';
part of 'example.dart';
Codec PersonJsonCodec = ...;
```
This way I could do:
```dart
var personCodec = PersonJsonCodec.fuse(JsonCode.fuse(Utf8Codec));
// Convert: bytes -> string -> json -> Person, in a single step
Person somePerson = personCodec.decode(List.from(bytes));
```
Further more, other libraries that accepts the `Codec` or `Converter` interfaces would be able to use this.. I could imagine packages that accepts a `Codec` prior to creating an abstraction for read/write files, cache, database entries, making RPCs.
Notably, it would also free me from writing constructors...
Contributor guide
Assessment
This issue has not been assessed yet.