Merge pull request #3 from yohom/master

Add a `toJson` method for serialization.
This commit is contained in:
Simon Binder 2019-03-15 07:23:03 +01:00 committed by GitHub
commit e7cb0cb2e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 1 deletions

View File

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

View File

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