mirror of https://github.com/AMT-Cheif/drift.git
Merge pull request #3 from yohom/master
Add a `toJson` method for serialization.
This commit is contained in:
commit
e7cb0cb2e5
|
@ -23,6 +23,15 @@ class TodoEntry {
|
|||
category: intType.mapFromDatabaseResponse(data['category']),
|
||||
);
|
||||
}
|
||||
Map<String, dynamic> 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, dynamic> toJson() {
|
||||
return {
|
||||
'id': id,
|
||||
'description': description,
|
||||
};
|
||||
}
|
||||
|
||||
Category copyWith({int id, String description}) => Category(
|
||||
id: id ?? this.id,
|
||||
description: description ?? this.description,
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import 'package:recase/recase.dart';
|
||||
import 'package:moor_generator/src/model/specified_table.dart';
|
||||
import 'package:recase/recase.dart';
|
||||
|
||||
class DataClassWriter {
|
||||
final SpecifiedTable table;
|
||||
|
@ -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, dynamic> 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;
|
||||
|
||||
|
|
Loading…
Reference in New Issue