Add a `toJson` method for serialization.

This commit is contained in:
yohom 2019-03-14 17:42:58 +08:00
parent 8fe350219c
commit c13b0ff17f
2 changed files with 30 additions and 0 deletions

View File

@ -23,6 +23,15 @@ class TodoEntry {
category: intType.mapFromDatabaseResponse(data['category']),
);
}
Map<String, Object> toJson() {
return {
'id': id,
'content': content,
'targetDate': targetDate,
'category': category,
};
}
TodoEntry copyWith(
{int id, String content, DateTime targetDate, int category}) =>
TodoEntry(
@ -128,6 +137,13 @@ class Category {
description: stringType.mapFromDatabaseResponse(data['`desc`']),
);
}
Map<String, Object> toJson() {
return {
'id': id,
'description': description,
};
}
Category copyWith({int id, String description}) => Category(
id: id ?? this.id,
description: description ?? this.description,

View File

@ -26,6 +26,9 @@ class DataClassWriter {
// Also write parsing factory
_writeMappingConstructor(buffer);
// And a serializer method
_writeToJson(buffer);
// And a convenience method to copy data from this class.
_writeCopyWith(buffer);
@ -94,6 +97,17 @@ class DataClassWriter {
buffer.write(');}\n');
}
void _writeToJson(StringBuffer buffer) {
buffer.write('Map<String, Object> toJson() {\n return {');
for (var column in table.columns) {
final getter = column.dartGetterName;
buffer.write('\'$getter\': $getter,');
}
buffer.write('};}');
}
void _writeCopyWith(StringBuffer buffer) {
final dataClassName = table.dartTypeName;