A Dart build_runner to generate toDynamoJson
and fromDynamoJson
support methods, similar to json_serializable, specifically supporting the DynamoDB attribute values.
Add dynamo_json
to your pubspec.yaml
as shown below or run dart pub add dynamo_json
dependencies:
dynamo_json: ^0.0.1
Import it in your Dart code and use it with your objects like in the example below. You can see the generated code example if you like.
import 'dart:convert'; // You'll need this if you use `dynamic` fields
import 'package:dynamo_json/dynamo_json.dart';
part 'example.g.dart';
@DynamoJson()
class Person {
final String firstName, lastName;
final DateTime? dateOfBirth;
final List<Person> relatives;
final dynamic stateBucket;
// Explicitly ignore because it's explicitly set.
// In general, `late` fields are supported.
@DynamoIgnore()
late final bool hasState;
Person({
required this.firstName,
required this.lastName,
this.dateOfBirth,
this.relatives = const [],
this.stateBucket,
}) {
hasState = stateBucket != null;
}
factory Person.fromDynamoJson(Map<String, dynamic> json) =>
_$PersonFromDynamoJson(json);
Map<String, dynamic> toDynamoJson() => _$PersonToDynamoJson(this);
}
Can only be used on classes to indicate the need for (de)serialization support.
Can only be used on fields to indicate they should not be considered by the generated code.
For details, see the documentation.