diff --git a/moor/example/example.g.dart b/moor/example/example.g.dart index 5c81c7d5..11fc9ff1 100644 --- a/moor/example/example.g.dart +++ b/moor/example/example.g.dart @@ -18,6 +18,12 @@ class Category { description: stringType.mapFromDatabaseResponse(data['description']), ); } + factory Category.fromJson(Map json) { + return Category( + id: json['id'] as int, + description: json['description'] as String, + ); + } Map toJson() { return { 'id': id, @@ -106,6 +112,14 @@ class Recipe { category: intType.mapFromDatabaseResponse(data['category']), ); } + factory Recipe.fromJson(Map json) { + return Recipe( + id: json['id'] as int, + title: json['title'] as String, + instructions: json['instructions'] as String, + category: json['category'] as int, + ); + } Map toJson() { return { 'id': id, @@ -223,6 +237,13 @@ class Ingredient { caloriesPer100g: intType.mapFromDatabaseResponse(data['calories']), ); } + factory Ingredient.fromJson(Map json) { + return Ingredient( + id: json['id'] as int, + name: json['name'] as String, + caloriesPer100g: json['caloriesPer100g'] as int, + ); + } Map toJson() { return { 'id': id, @@ -327,6 +348,13 @@ class IngredientInRecipe { amountInGrams: intType.mapFromDatabaseResponse(data['amount']), ); } + factory IngredientInRecipe.fromJson(Map json) { + return IngredientInRecipe( + recipe: json['recipe'] as int, + ingredient: json['ingredient'] as int, + amountInGrams: json['amountInGrams'] as int, + ); + } Map toJson() { return { 'recipe': recipe, diff --git a/moor/test/data/tables/todos.g.dart b/moor/test/data/tables/todos.g.dart index 060538e8..ecd7d6fd 100644 --- a/moor/test/data/tables/todos.g.dart +++ b/moor/test/data/tables/todos.g.dart @@ -26,6 +26,15 @@ class TodoEntry { category: intType.mapFromDatabaseResponse(data['category']), ); } + factory TodoEntry.fromJson(Map json) { + return TodoEntry( + id: json['id'] as int, + title: json['title'] as String, + content: json['content'] as String, + targetDate: json['targetDate'] as DateTime, + category: json['category'] as int, + ); + } Map toJson() { return { 'id': id, @@ -164,6 +173,12 @@ class Category { description: stringType.mapFromDatabaseResponse(data['`desc`']), ); } + factory Category.fromJson(Map json) { + return Category( + id: json['id'] as int, + description: json['description'] as String, + ); + } Map toJson() { return { 'id': id, @@ -255,6 +270,14 @@ class User { uint8ListType.mapFromDatabaseResponse(data['profile_picture']), ); } + factory User.fromJson(Map json) { + return User( + id: json['id'] as int, + name: json['name'] as String, + isAwesome: json['isAwesome'] as bool, + profilePicture: json['profilePicture'] as Uint8List, + ); + } Map toJson() { return { 'id': id, @@ -370,6 +393,12 @@ class SharedTodo { user: intType.mapFromDatabaseResponse(data['user']), ); } + factory SharedTodo.fromJson(Map json) { + return SharedTodo( + todo: json['todo'] as int, + user: json['user'] as int, + ); + } Map toJson() { return { 'todo': todo, diff --git a/moor_generator/lib/src/writer/data_class_writer.dart b/moor_generator/lib/src/writer/data_class_writer.dart index 897c209a..0f083d81 100644 --- a/moor_generator/lib/src/writer/data_class_writer.dart +++ b/moor_generator/lib/src/writer/data_class_writer.dart @@ -26,7 +26,8 @@ class DataClassWriter { // Also write parsing factory _writeMappingConstructor(buffer); - // And a serializer method + // And a serializer and deserializer method + _writeFromJson(buffer); _writeToJson(buffer); // And a convenience method to copy data from this class. @@ -97,12 +98,29 @@ class DataClassWriter { buffer.write(');}\n'); } + void _writeFromJson(StringBuffer buffer) { + final dataClassName = table.dartTypeName; + + buffer + ..write('factory $dataClassName.fromJson(Map json) {\n') + ..write('return $dataClassName('); + + for (var column in table.columns) { + final getter = column.dartGetterName; + final type = column.dartTypeName; + + buffer.write("$getter: json['$getter'] as $type,"); + } + + buffer.write(');}\n'); + } + void _writeToJson(StringBuffer buffer) { buffer.write('Map toJson() {\n return {'); for (var column in table.columns) { final getter = column.dartGetterName; - buffer.write('\'$getter\': $getter,'); + buffer.write("'$getter': $getter,"); } buffer.write('};}');