To generate toJson and fromJson code for a class, annotate it with @JsonSerializable. You must also include a part directive pointing to the generated file (e.g., part 'filename.g.dart';) and manually connect the generated functions to your class's factory constructor and toJson method.
You can customize individual fields using the @JsonKey annotation.
import 'package:json_annotation/json_annotation.dart';
part 'example.g.dart';
@JsonSerializable(createJsonSchema: true)
class Person {
final String firstName, lastName;
final DateTime? dateOfBirth;
Person({required this.firstName, required this.lastName, this.dateOfBirth});
factory Person.fromJson(Map<String, dynamic> json) => _$PersonFromJson(json);
Map<String, dynamic> toJson() => _$PersonToJson(this);
static const jsonSchema = _$PersonJsonSchema;
}