Implement fromJson factory in data classes.

This commit is contained in:
Simon Binder 2019-03-15 17:56:39 +01:00
parent e7ece27528
commit 3e1cbee3c6
No known key found for this signature in database
GPG Key ID: B807FDF954BA00CF
3 changed files with 77 additions and 2 deletions

View File

@ -18,6 +18,12 @@ class Category {
description: stringType.mapFromDatabaseResponse(data['description']),
);
}
factory Category.fromJson(Map<String, dynamic> json) {
return Category(
id: json['id'] as int,
description: json['description'] as String,
);
}
Map<String, dynamic> toJson() {
return {
'id': id,
@ -106,6 +112,14 @@ class Recipe {
category: intType.mapFromDatabaseResponse(data['category']),
);
}
factory Recipe.fromJson(Map<String, dynamic> json) {
return Recipe(
id: json['id'] as int,
title: json['title'] as String,
instructions: json['instructions'] as String,
category: json['category'] as int,
);
}
Map<String, dynamic> toJson() {
return {
'id': id,
@ -223,6 +237,13 @@ class Ingredient {
caloriesPer100g: intType.mapFromDatabaseResponse(data['calories']),
);
}
factory Ingredient.fromJson(Map<String, dynamic> json) {
return Ingredient(
id: json['id'] as int,
name: json['name'] as String,
caloriesPer100g: json['caloriesPer100g'] as int,
);
}
Map<String, dynamic> toJson() {
return {
'id': id,
@ -327,6 +348,13 @@ class IngredientInRecipe {
amountInGrams: intType.mapFromDatabaseResponse(data['amount']),
);
}
factory IngredientInRecipe.fromJson(Map<String, dynamic> json) {
return IngredientInRecipe(
recipe: json['recipe'] as int,
ingredient: json['ingredient'] as int,
amountInGrams: json['amountInGrams'] as int,
);
}
Map<String, dynamic> toJson() {
return {
'recipe': recipe,

View File

@ -26,6 +26,15 @@ class TodoEntry {
category: intType.mapFromDatabaseResponse(data['category']),
);
}
factory TodoEntry.fromJson(Map<String, dynamic> 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<String, dynamic> toJson() {
return {
'id': id,
@ -164,6 +173,12 @@ class Category {
description: stringType.mapFromDatabaseResponse(data['`desc`']),
);
}
factory Category.fromJson(Map<String, dynamic> json) {
return Category(
id: json['id'] as int,
description: json['description'] as String,
);
}
Map<String, dynamic> toJson() {
return {
'id': id,
@ -255,6 +270,14 @@ class User {
uint8ListType.mapFromDatabaseResponse(data['profile_picture']),
);
}
factory User.fromJson(Map<String, dynamic> json) {
return User(
id: json['id'] as int,
name: json['name'] as String,
isAwesome: json['isAwesome'] as bool,
profilePicture: json['profilePicture'] as Uint8List,
);
}
Map<String, dynamic> toJson() {
return {
'id': id,
@ -370,6 +393,12 @@ class SharedTodo {
user: intType.mapFromDatabaseResponse(data['user']),
);
}
factory SharedTodo.fromJson(Map<String, dynamic> json) {
return SharedTodo(
todo: json['todo'] as int,
user: json['user'] as int,
);
}
Map<String, dynamic> toJson() {
return {
'todo': todo,

View File

@ -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<String, dynamic> 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<String, dynamic> toJson() {\n return {');
for (var column in table.columns) {
final getter = column.dartGetterName;
buffer.write('\'$getter\': $getter,');
buffer.write("'$getter': $getter,");
}
buffer.write('};}');