mirror of https://github.com/AMT-Cheif/drift.git
parent
927fbf561b
commit
e9cada5e54
|
@ -2,6 +2,12 @@
|
||||||
|
|
||||||
- New `clientDefault` method for columns. It can be used for dynamic defaults that might be different for
|
- New `clientDefault` method for columns. It can be used for dynamic defaults that might be different for
|
||||||
each row. For instance, you can generate a uuid for each row with `text().clientDefault(() => Uuid().v4()();`
|
each row. For instance, you can generate a uuid for each row with `text().clientDefault(() => Uuid().v4()();`
|
||||||
|
- Ability to override the default `ValueSerializer` globally by using `moorRuntimeOptions.valueSerializer`.
|
||||||
|
- Moor files: You can now explicitly declare column types in those cases that the analyzer can't
|
||||||
|
infer it:
|
||||||
|
```
|
||||||
|
selectVariable(:variable AS TEXT): SELECT :variable;
|
||||||
|
```
|
||||||
|
|
||||||
## 2.2.0
|
## 2.2.0
|
||||||
|
|
||||||
|
|
|
@ -23,20 +23,21 @@ class Category extends DataClass implements Insertable<Category> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
factory Category.fromJson(Map<String, dynamic> json,
|
factory Category.fromJson(Map<String, dynamic> json,
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
{ValueSerializer serializer}) {
|
||||||
|
serializer ??= moorRuntimeOptions.defaultSerializer;
|
||||||
return Category(
|
return Category(
|
||||||
id: serializer.fromJson<int>(json['id']),
|
id: serializer.fromJson<int>(json['id']),
|
||||||
description: serializer.fromJson<String>(json['description']),
|
description: serializer.fromJson<String>(json['description']),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
factory Category.fromJsonString(String encodedJson,
|
factory Category.fromJsonString(String encodedJson,
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) =>
|
{ValueSerializer serializer}) =>
|
||||||
Category.fromJson(
|
Category.fromJson(
|
||||||
DataClass.parseJson(encodedJson) as Map<String, dynamic>,
|
DataClass.parseJson(encodedJson) as Map<String, dynamic>,
|
||||||
serializer: serializer);
|
serializer: serializer);
|
||||||
@override
|
@override
|
||||||
Map<String, dynamic> toJson(
|
Map<String, dynamic> toJson({ValueSerializer serializer}) {
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
serializer ??= moorRuntimeOptions.defaultSerializer;
|
||||||
return <String, dynamic>{
|
return <String, dynamic>{
|
||||||
'id': serializer.toJson<int>(id),
|
'id': serializer.toJson<int>(id),
|
||||||
'description': serializer.toJson<String>(description),
|
'description': serializer.toJson<String>(description),
|
||||||
|
@ -197,7 +198,8 @@ class Recipe extends DataClass implements Insertable<Recipe> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
factory Recipe.fromJson(Map<String, dynamic> json,
|
factory Recipe.fromJson(Map<String, dynamic> json,
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
{ValueSerializer serializer}) {
|
||||||
|
serializer ??= moorRuntimeOptions.defaultSerializer;
|
||||||
return Recipe(
|
return Recipe(
|
||||||
id: serializer.fromJson<int>(json['id']),
|
id: serializer.fromJson<int>(json['id']),
|
||||||
title: serializer.fromJson<String>(json['title']),
|
title: serializer.fromJson<String>(json['title']),
|
||||||
|
@ -206,12 +208,12 @@ class Recipe extends DataClass implements Insertable<Recipe> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
factory Recipe.fromJsonString(String encodedJson,
|
factory Recipe.fromJsonString(String encodedJson,
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) =>
|
{ValueSerializer serializer}) =>
|
||||||
Recipe.fromJson(DataClass.parseJson(encodedJson) as Map<String, dynamic>,
|
Recipe.fromJson(DataClass.parseJson(encodedJson) as Map<String, dynamic>,
|
||||||
serializer: serializer);
|
serializer: serializer);
|
||||||
@override
|
@override
|
||||||
Map<String, dynamic> toJson(
|
Map<String, dynamic> toJson({ValueSerializer serializer}) {
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
serializer ??= moorRuntimeOptions.defaultSerializer;
|
||||||
return <String, dynamic>{
|
return <String, dynamic>{
|
||||||
'id': serializer.toJson<int>(id),
|
'id': serializer.toJson<int>(id),
|
||||||
'title': serializer.toJson<String>(title),
|
'title': serializer.toJson<String>(title),
|
||||||
|
@ -432,7 +434,8 @@ class Ingredient extends DataClass implements Insertable<Ingredient> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
factory Ingredient.fromJson(Map<String, dynamic> json,
|
factory Ingredient.fromJson(Map<String, dynamic> json,
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
{ValueSerializer serializer}) {
|
||||||
|
serializer ??= moorRuntimeOptions.defaultSerializer;
|
||||||
return Ingredient(
|
return Ingredient(
|
||||||
id: serializer.fromJson<int>(json['id']),
|
id: serializer.fromJson<int>(json['id']),
|
||||||
name: serializer.fromJson<String>(json['name']),
|
name: serializer.fromJson<String>(json['name']),
|
||||||
|
@ -440,13 +443,13 @@ class Ingredient extends DataClass implements Insertable<Ingredient> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
factory Ingredient.fromJsonString(String encodedJson,
|
factory Ingredient.fromJsonString(String encodedJson,
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) =>
|
{ValueSerializer serializer}) =>
|
||||||
Ingredient.fromJson(
|
Ingredient.fromJson(
|
||||||
DataClass.parseJson(encodedJson) as Map<String, dynamic>,
|
DataClass.parseJson(encodedJson) as Map<String, dynamic>,
|
||||||
serializer: serializer);
|
serializer: serializer);
|
||||||
@override
|
@override
|
||||||
Map<String, dynamic> toJson(
|
Map<String, dynamic> toJson({ValueSerializer serializer}) {
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
serializer ??= moorRuntimeOptions.defaultSerializer;
|
||||||
return <String, dynamic>{
|
return <String, dynamic>{
|
||||||
'id': serializer.toJson<int>(id),
|
'id': serializer.toJson<int>(id),
|
||||||
'name': serializer.toJson<String>(name),
|
'name': serializer.toJson<String>(name),
|
||||||
|
@ -641,7 +644,8 @@ class IngredientInRecipe extends DataClass
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
factory IngredientInRecipe.fromJson(Map<String, dynamic> json,
|
factory IngredientInRecipe.fromJson(Map<String, dynamic> json,
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
{ValueSerializer serializer}) {
|
||||||
|
serializer ??= moorRuntimeOptions.defaultSerializer;
|
||||||
return IngredientInRecipe(
|
return IngredientInRecipe(
|
||||||
recipe: serializer.fromJson<int>(json['recipe']),
|
recipe: serializer.fromJson<int>(json['recipe']),
|
||||||
ingredient: serializer.fromJson<int>(json['ingredient']),
|
ingredient: serializer.fromJson<int>(json['ingredient']),
|
||||||
|
@ -649,13 +653,13 @@ class IngredientInRecipe extends DataClass
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
factory IngredientInRecipe.fromJsonString(String encodedJson,
|
factory IngredientInRecipe.fromJsonString(String encodedJson,
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) =>
|
{ValueSerializer serializer}) =>
|
||||||
IngredientInRecipe.fromJson(
|
IngredientInRecipe.fromJson(
|
||||||
DataClass.parseJson(encodedJson) as Map<String, dynamic>,
|
DataClass.parseJson(encodedJson) as Map<String, dynamic>,
|
||||||
serializer: serializer);
|
serializer: serializer);
|
||||||
@override
|
@override
|
||||||
Map<String, dynamic> toJson(
|
Map<String, dynamic> toJson({ValueSerializer serializer}) {
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
serializer ??= moorRuntimeOptions.defaultSerializer;
|
||||||
return <String, dynamic>{
|
return <String, dynamic>{
|
||||||
'recipe': serializer.toJson<int>(recipe),
|
'recipe': serializer.toJson<int>(recipe),
|
||||||
'ingredient': serializer.toJson<int>(ingredient),
|
'ingredient': serializer.toJson<int>(ingredient),
|
||||||
|
|
|
@ -15,6 +15,9 @@ part 'query_engine.dart';
|
||||||
class MoorRuntimeOptions {
|
class MoorRuntimeOptions {
|
||||||
/// Don't warn when a database class isn't used as singleton.
|
/// Don't warn when a database class isn't used as singleton.
|
||||||
bool dontWarnAboutMultipleDatabases = false;
|
bool dontWarnAboutMultipleDatabases = false;
|
||||||
|
|
||||||
|
/// The [ValueSerializer] that will be used by default in [DataClass.toJson].
|
||||||
|
ValueSerializer defaultSerializer = const ValueSerializer.defaults();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Stores the [MoorRuntimeOptions] describing global moor behavior across
|
/// Stores the [MoorRuntimeOptions] describing global moor behavior across
|
||||||
|
|
|
@ -24,14 +24,15 @@ abstract class DataClass {
|
||||||
|
|
||||||
/// Converts this object into a representation that can be encoded with
|
/// Converts this object into a representation that can be encoded with
|
||||||
/// [json]. The [serializer] can be used to configure how individual values
|
/// [json]. The [serializer] can be used to configure how individual values
|
||||||
/// will be encoded.
|
/// will be encoded. By default, [MoorRuntimeOptions.defaultSerializer] will
|
||||||
Map<String, dynamic> toJson(
|
/// be used. See [ValueSerializer.defaults()] for details.
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()});
|
Map<String, dynamic> toJson({ValueSerializer serializer});
|
||||||
|
|
||||||
/// Converts this object into a json representation. The [serializer] can be
|
/// Converts this object into a json representation. The [serializer] can be
|
||||||
/// used to configure how individual values will be encoded.
|
/// used to configure how individual values will be encoded. By default,
|
||||||
String toJsonString(
|
/// [MoorRuntimeOptions.defaultSerializer] will be used. See
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
/// [ValueSerializer.defaults()] for details.
|
||||||
|
String toJsonString({ValueSerializer serializer}) {
|
||||||
return json.encode(toJson(serializer: serializer));
|
return json.encode(toJson(serializer: serializer));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,18 +20,19 @@ class NoId extends DataClass implements Insertable<NoId> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
factory NoId.fromJson(Map<String, dynamic> json,
|
factory NoId.fromJson(Map<String, dynamic> json,
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
{ValueSerializer serializer}) {
|
||||||
|
serializer ??= moorRuntimeOptions.defaultSerializer;
|
||||||
return NoId(
|
return NoId(
|
||||||
payload: serializer.fromJson<Uint8List>(json['payload']),
|
payload: serializer.fromJson<Uint8List>(json['payload']),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
factory NoId.fromJsonString(String encodedJson,
|
factory NoId.fromJsonString(String encodedJson,
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) =>
|
{ValueSerializer serializer}) =>
|
||||||
NoId.fromJson(DataClass.parseJson(encodedJson) as Map<String, dynamic>,
|
NoId.fromJson(DataClass.parseJson(encodedJson) as Map<String, dynamic>,
|
||||||
serializer: serializer);
|
serializer: serializer);
|
||||||
@override
|
@override
|
||||||
Map<String, dynamic> toJson(
|
Map<String, dynamic> toJson({ValueSerializer serializer}) {
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
serializer ??= moorRuntimeOptions.defaultSerializer;
|
||||||
return <String, dynamic>{
|
return <String, dynamic>{
|
||||||
'payload': serializer.toJson<Uint8List>(payload),
|
'payload': serializer.toJson<Uint8List>(payload),
|
||||||
};
|
};
|
||||||
|
@ -154,20 +155,21 @@ class WithDefault extends DataClass implements Insertable<WithDefault> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
factory WithDefault.fromJson(Map<String, dynamic> json,
|
factory WithDefault.fromJson(Map<String, dynamic> json,
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
{ValueSerializer serializer}) {
|
||||||
|
serializer ??= moorRuntimeOptions.defaultSerializer;
|
||||||
return WithDefault(
|
return WithDefault(
|
||||||
a: serializer.fromJson<String>(json['a']),
|
a: serializer.fromJson<String>(json['a']),
|
||||||
b: serializer.fromJson<int>(json['b']),
|
b: serializer.fromJson<int>(json['b']),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
factory WithDefault.fromJsonString(String encodedJson,
|
factory WithDefault.fromJsonString(String encodedJson,
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) =>
|
{ValueSerializer serializer}) =>
|
||||||
WithDefault.fromJson(
|
WithDefault.fromJson(
|
||||||
DataClass.parseJson(encodedJson) as Map<String, dynamic>,
|
DataClass.parseJson(encodedJson) as Map<String, dynamic>,
|
||||||
serializer: serializer);
|
serializer: serializer);
|
||||||
@override
|
@override
|
||||||
Map<String, dynamic> toJson(
|
Map<String, dynamic> toJson({ValueSerializer serializer}) {
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
serializer ??= moorRuntimeOptions.defaultSerializer;
|
||||||
return <String, dynamic>{
|
return <String, dynamic>{
|
||||||
'a': serializer.toJson<String>(a),
|
'a': serializer.toJson<String>(a),
|
||||||
'b': serializer.toJson<int>(b),
|
'b': serializer.toJson<int>(b),
|
||||||
|
@ -313,7 +315,8 @@ class WithConstraint extends DataClass implements Insertable<WithConstraint> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
factory WithConstraint.fromJson(Map<String, dynamic> json,
|
factory WithConstraint.fromJson(Map<String, dynamic> json,
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
{ValueSerializer serializer}) {
|
||||||
|
serializer ??= moorRuntimeOptions.defaultSerializer;
|
||||||
return WithConstraint(
|
return WithConstraint(
|
||||||
a: serializer.fromJson<String>(json['a']),
|
a: serializer.fromJson<String>(json['a']),
|
||||||
b: serializer.fromJson<int>(json['b']),
|
b: serializer.fromJson<int>(json['b']),
|
||||||
|
@ -321,13 +324,13 @@ class WithConstraint extends DataClass implements Insertable<WithConstraint> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
factory WithConstraint.fromJsonString(String encodedJson,
|
factory WithConstraint.fromJsonString(String encodedJson,
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) =>
|
{ValueSerializer serializer}) =>
|
||||||
WithConstraint.fromJson(
|
WithConstraint.fromJson(
|
||||||
DataClass.parseJson(encodedJson) as Map<String, dynamic>,
|
DataClass.parseJson(encodedJson) as Map<String, dynamic>,
|
||||||
serializer: serializer);
|
serializer: serializer);
|
||||||
@override
|
@override
|
||||||
Map<String, dynamic> toJson(
|
Map<String, dynamic> toJson({ValueSerializer serializer}) {
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
serializer ??= moorRuntimeOptions.defaultSerializer;
|
||||||
return <String, dynamic>{
|
return <String, dynamic>{
|
||||||
'a': serializer.toJson<String>(a),
|
'a': serializer.toJson<String>(a),
|
||||||
'b': serializer.toJson<int>(b),
|
'b': serializer.toJson<int>(b),
|
||||||
|
@ -498,19 +501,20 @@ class Config extends DataClass implements Insertable<Config> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
factory Config.fromJson(Map<String, dynamic> json,
|
factory Config.fromJson(Map<String, dynamic> json,
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
{ValueSerializer serializer}) {
|
||||||
|
serializer ??= moorRuntimeOptions.defaultSerializer;
|
||||||
return Config(
|
return Config(
|
||||||
configKey: serializer.fromJson<String>(json['config_key']),
|
configKey: serializer.fromJson<String>(json['config_key']),
|
||||||
configValue: serializer.fromJson<String>(json['config_value']),
|
configValue: serializer.fromJson<String>(json['config_value']),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
factory Config.fromJsonString(String encodedJson,
|
factory Config.fromJsonString(String encodedJson,
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) =>
|
{ValueSerializer serializer}) =>
|
||||||
Config.fromJson(DataClass.parseJson(encodedJson) as Map<String, dynamic>,
|
Config.fromJson(DataClass.parseJson(encodedJson) as Map<String, dynamic>,
|
||||||
serializer: serializer);
|
serializer: serializer);
|
||||||
@override
|
@override
|
||||||
Map<String, dynamic> toJson(
|
Map<String, dynamic> toJson({ValueSerializer serializer}) {
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
serializer ??= moorRuntimeOptions.defaultSerializer;
|
||||||
return <String, dynamic>{
|
return <String, dynamic>{
|
||||||
'config_key': serializer.toJson<String>(configKey),
|
'config_key': serializer.toJson<String>(configKey),
|
||||||
'config_value': serializer.toJson<String>(configValue),
|
'config_value': serializer.toJson<String>(configValue),
|
||||||
|
@ -673,7 +677,8 @@ class MytableData extends DataClass implements Insertable<MytableData> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
factory MytableData.fromJson(Map<String, dynamic> json,
|
factory MytableData.fromJson(Map<String, dynamic> json,
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
{ValueSerializer serializer}) {
|
||||||
|
serializer ??= moorRuntimeOptions.defaultSerializer;
|
||||||
return MytableData(
|
return MytableData(
|
||||||
someid: serializer.fromJson<int>(json['someid']),
|
someid: serializer.fromJson<int>(json['someid']),
|
||||||
sometext: serializer.fromJson<String>(json['sometext']),
|
sometext: serializer.fromJson<String>(json['sometext']),
|
||||||
|
@ -682,13 +687,13 @@ class MytableData extends DataClass implements Insertable<MytableData> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
factory MytableData.fromJsonString(String encodedJson,
|
factory MytableData.fromJsonString(String encodedJson,
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) =>
|
{ValueSerializer serializer}) =>
|
||||||
MytableData.fromJson(
|
MytableData.fromJson(
|
||||||
DataClass.parseJson(encodedJson) as Map<String, dynamic>,
|
DataClass.parseJson(encodedJson) as Map<String, dynamic>,
|
||||||
serializer: serializer);
|
serializer: serializer);
|
||||||
@override
|
@override
|
||||||
Map<String, dynamic> toJson(
|
Map<String, dynamic> toJson({ValueSerializer serializer}) {
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
serializer ??= moorRuntimeOptions.defaultSerializer;
|
||||||
return <String, dynamic>{
|
return <String, dynamic>{
|
||||||
'someid': serializer.toJson<int>(someid),
|
'someid': serializer.toJson<int>(someid),
|
||||||
'sometext': serializer.toJson<String>(sometext),
|
'sometext': serializer.toJson<String>(sometext),
|
||||||
|
@ -897,7 +902,8 @@ class EMail extends DataClass implements Insertable<EMail> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
factory EMail.fromJson(Map<String, dynamic> json,
|
factory EMail.fromJson(Map<String, dynamic> json,
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
{ValueSerializer serializer}) {
|
||||||
|
serializer ??= moorRuntimeOptions.defaultSerializer;
|
||||||
return EMail(
|
return EMail(
|
||||||
sender: serializer.fromJson<String>(json['sender']),
|
sender: serializer.fromJson<String>(json['sender']),
|
||||||
title: serializer.fromJson<String>(json['title']),
|
title: serializer.fromJson<String>(json['title']),
|
||||||
|
@ -905,12 +911,12 @@ class EMail extends DataClass implements Insertable<EMail> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
factory EMail.fromJsonString(String encodedJson,
|
factory EMail.fromJsonString(String encodedJson,
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) =>
|
{ValueSerializer serializer}) =>
|
||||||
EMail.fromJson(DataClass.parseJson(encodedJson) as Map<String, dynamic>,
|
EMail.fromJson(DataClass.parseJson(encodedJson) as Map<String, dynamic>,
|
||||||
serializer: serializer);
|
serializer: serializer);
|
||||||
@override
|
@override
|
||||||
Map<String, dynamic> toJson(
|
Map<String, dynamic> toJson({ValueSerializer serializer}) {
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
serializer ??= moorRuntimeOptions.defaultSerializer;
|
||||||
return <String, dynamic>{
|
return <String, dynamic>{
|
||||||
'sender': serializer.toJson<String>(sender),
|
'sender': serializer.toJson<String>(sender),
|
||||||
'title': serializer.toJson<String>(title),
|
'title': serializer.toJson<String>(title),
|
||||||
|
|
|
@ -38,7 +38,8 @@ class TodoEntry extends DataClass implements Insertable<TodoEntry> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
factory TodoEntry.fromJson(Map<String, dynamic> json,
|
factory TodoEntry.fromJson(Map<String, dynamic> json,
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
{ValueSerializer serializer}) {
|
||||||
|
serializer ??= moorRuntimeOptions.defaultSerializer;
|
||||||
return TodoEntry(
|
return TodoEntry(
|
||||||
id: serializer.fromJson<int>(json['id']),
|
id: serializer.fromJson<int>(json['id']),
|
||||||
title: serializer.fromJson<String>(json['title']),
|
title: serializer.fromJson<String>(json['title']),
|
||||||
|
@ -48,13 +49,13 @@ class TodoEntry extends DataClass implements Insertable<TodoEntry> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
factory TodoEntry.fromJsonString(String encodedJson,
|
factory TodoEntry.fromJsonString(String encodedJson,
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) =>
|
{ValueSerializer serializer}) =>
|
||||||
TodoEntry.fromJson(
|
TodoEntry.fromJson(
|
||||||
DataClass.parseJson(encodedJson) as Map<String, dynamic>,
|
DataClass.parseJson(encodedJson) as Map<String, dynamic>,
|
||||||
serializer: serializer);
|
serializer: serializer);
|
||||||
@override
|
@override
|
||||||
Map<String, dynamic> toJson(
|
Map<String, dynamic> toJson({ValueSerializer serializer}) {
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
serializer ??= moorRuntimeOptions.defaultSerializer;
|
||||||
return <String, dynamic>{
|
return <String, dynamic>{
|
||||||
'id': serializer.toJson<int>(id),
|
'id': serializer.toJson<int>(id),
|
||||||
'title': serializer.toJson<String>(title),
|
'title': serializer.toJson<String>(title),
|
||||||
|
@ -309,20 +310,21 @@ class Category extends DataClass implements Insertable<Category> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
factory Category.fromJson(Map<String, dynamic> json,
|
factory Category.fromJson(Map<String, dynamic> json,
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
{ValueSerializer serializer}) {
|
||||||
|
serializer ??= moorRuntimeOptions.defaultSerializer;
|
||||||
return Category(
|
return Category(
|
||||||
id: serializer.fromJson<int>(json['id']),
|
id: serializer.fromJson<int>(json['id']),
|
||||||
description: serializer.fromJson<String>(json['description']),
|
description: serializer.fromJson<String>(json['description']),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
factory Category.fromJsonString(String encodedJson,
|
factory Category.fromJsonString(String encodedJson,
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) =>
|
{ValueSerializer serializer}) =>
|
||||||
Category.fromJson(
|
Category.fromJson(
|
||||||
DataClass.parseJson(encodedJson) as Map<String, dynamic>,
|
DataClass.parseJson(encodedJson) as Map<String, dynamic>,
|
||||||
serializer: serializer);
|
serializer: serializer);
|
||||||
@override
|
@override
|
||||||
Map<String, dynamic> toJson(
|
Map<String, dynamic> toJson({ValueSerializer serializer}) {
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
serializer ??= moorRuntimeOptions.defaultSerializer;
|
||||||
return <String, dynamic>{
|
return <String, dynamic>{
|
||||||
'id': serializer.toJson<int>(id),
|
'id': serializer.toJson<int>(id),
|
||||||
'description': serializer.toJson<String>(description),
|
'description': serializer.toJson<String>(description),
|
||||||
|
@ -488,7 +490,8 @@ class User extends DataClass implements Insertable<User> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
factory User.fromJson(Map<String, dynamic> json,
|
factory User.fromJson(Map<String, dynamic> json,
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
{ValueSerializer serializer}) {
|
||||||
|
serializer ??= moorRuntimeOptions.defaultSerializer;
|
||||||
return User(
|
return User(
|
||||||
id: serializer.fromJson<int>(json['id']),
|
id: serializer.fromJson<int>(json['id']),
|
||||||
name: serializer.fromJson<String>(json['name']),
|
name: serializer.fromJson<String>(json['name']),
|
||||||
|
@ -498,12 +501,12 @@ class User extends DataClass implements Insertable<User> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
factory User.fromJsonString(String encodedJson,
|
factory User.fromJsonString(String encodedJson,
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) =>
|
{ValueSerializer serializer}) =>
|
||||||
User.fromJson(DataClass.parseJson(encodedJson) as Map<String, dynamic>,
|
User.fromJson(DataClass.parseJson(encodedJson) as Map<String, dynamic>,
|
||||||
serializer: serializer);
|
serializer: serializer);
|
||||||
@override
|
@override
|
||||||
Map<String, dynamic> toJson(
|
Map<String, dynamic> toJson({ValueSerializer serializer}) {
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
serializer ??= moorRuntimeOptions.defaultSerializer;
|
||||||
return <String, dynamic>{
|
return <String, dynamic>{
|
||||||
'id': serializer.toJson<int>(id),
|
'id': serializer.toJson<int>(id),
|
||||||
'name': serializer.toJson<String>(name),
|
'name': serializer.toJson<String>(name),
|
||||||
|
@ -760,20 +763,21 @@ class SharedTodo extends DataClass implements Insertable<SharedTodo> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
factory SharedTodo.fromJson(Map<String, dynamic> json,
|
factory SharedTodo.fromJson(Map<String, dynamic> json,
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
{ValueSerializer serializer}) {
|
||||||
|
serializer ??= moorRuntimeOptions.defaultSerializer;
|
||||||
return SharedTodo(
|
return SharedTodo(
|
||||||
todo: serializer.fromJson<int>(json['todo']),
|
todo: serializer.fromJson<int>(json['todo']),
|
||||||
user: serializer.fromJson<int>(json['user']),
|
user: serializer.fromJson<int>(json['user']),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
factory SharedTodo.fromJsonString(String encodedJson,
|
factory SharedTodo.fromJsonString(String encodedJson,
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) =>
|
{ValueSerializer serializer}) =>
|
||||||
SharedTodo.fromJson(
|
SharedTodo.fromJson(
|
||||||
DataClass.parseJson(encodedJson) as Map<String, dynamic>,
|
DataClass.parseJson(encodedJson) as Map<String, dynamic>,
|
||||||
serializer: serializer);
|
serializer: serializer);
|
||||||
@override
|
@override
|
||||||
Map<String, dynamic> toJson(
|
Map<String, dynamic> toJson({ValueSerializer serializer}) {
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
serializer ??= moorRuntimeOptions.defaultSerializer;
|
||||||
return <String, dynamic>{
|
return <String, dynamic>{
|
||||||
'todo': serializer.toJson<int>(todo),
|
'todo': serializer.toJson<int>(todo),
|
||||||
'user': serializer.toJson<int>(user),
|
'user': serializer.toJson<int>(user),
|
||||||
|
@ -939,7 +943,8 @@ class TableWithoutPKData extends DataClass
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
factory TableWithoutPKData.fromJson(Map<String, dynamic> json,
|
factory TableWithoutPKData.fromJson(Map<String, dynamic> json,
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
{ValueSerializer serializer}) {
|
||||||
|
serializer ??= moorRuntimeOptions.defaultSerializer;
|
||||||
return TableWithoutPKData(
|
return TableWithoutPKData(
|
||||||
notReallyAnId: serializer.fromJson<int>(json['notReallyAnId']),
|
notReallyAnId: serializer.fromJson<int>(json['notReallyAnId']),
|
||||||
someFloat: serializer.fromJson<double>(json['someFloat']),
|
someFloat: serializer.fromJson<double>(json['someFloat']),
|
||||||
|
@ -947,13 +952,13 @@ class TableWithoutPKData extends DataClass
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
factory TableWithoutPKData.fromJsonString(String encodedJson,
|
factory TableWithoutPKData.fromJsonString(String encodedJson,
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) =>
|
{ValueSerializer serializer}) =>
|
||||||
TableWithoutPKData.fromJson(
|
TableWithoutPKData.fromJson(
|
||||||
DataClass.parseJson(encodedJson) as Map<String, dynamic>,
|
DataClass.parseJson(encodedJson) as Map<String, dynamic>,
|
||||||
serializer: serializer);
|
serializer: serializer);
|
||||||
@override
|
@override
|
||||||
Map<String, dynamic> toJson(
|
Map<String, dynamic> toJson({ValueSerializer serializer}) {
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
serializer ??= moorRuntimeOptions.defaultSerializer;
|
||||||
return <String, dynamic>{
|
return <String, dynamic>{
|
||||||
'notReallyAnId': serializer.toJson<int>(notReallyAnId),
|
'notReallyAnId': serializer.toJson<int>(notReallyAnId),
|
||||||
'someFloat': serializer.toJson<double>(someFloat),
|
'someFloat': serializer.toJson<double>(someFloat),
|
||||||
|
@ -1152,20 +1157,21 @@ class PureDefault extends DataClass implements Insertable<PureDefault> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
factory PureDefault.fromJson(Map<String, dynamic> json,
|
factory PureDefault.fromJson(Map<String, dynamic> json,
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
{ValueSerializer serializer}) {
|
||||||
|
serializer ??= moorRuntimeOptions.defaultSerializer;
|
||||||
return PureDefault(
|
return PureDefault(
|
||||||
id: serializer.fromJson<int>(json['id']),
|
id: serializer.fromJson<int>(json['id']),
|
||||||
txt: serializer.fromJson<String>(json['txt']),
|
txt: serializer.fromJson<String>(json['txt']),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
factory PureDefault.fromJsonString(String encodedJson,
|
factory PureDefault.fromJsonString(String encodedJson,
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) =>
|
{ValueSerializer serializer}) =>
|
||||||
PureDefault.fromJson(
|
PureDefault.fromJson(
|
||||||
DataClass.parseJson(encodedJson) as Map<String, dynamic>,
|
DataClass.parseJson(encodedJson) as Map<String, dynamic>,
|
||||||
serializer: serializer);
|
serializer: serializer);
|
||||||
@override
|
@override
|
||||||
Map<String, dynamic> toJson(
|
Map<String, dynamic> toJson({ValueSerializer serializer}) {
|
||||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
serializer ??= moorRuntimeOptions.defaultSerializer;
|
||||||
return <String, dynamic>{
|
return <String, dynamic>{
|
||||||
'id': serializer.toJson<int>(id),
|
'id': serializer.toJson<int>(id),
|
||||||
'txt': serializer.toJson<String>(txt),
|
'txt': serializer.toJson<String>(txt),
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import 'package:moor/moor.dart';
|
||||||
import 'package:test/test.dart';
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
import 'data/tables/todos.dart';
|
import 'data/tables/todos.dart';
|
||||||
|
@ -16,4 +17,41 @@ void main() {
|
||||||
|
|
||||||
expect(deserialized, equals(deserialized));
|
expect(deserialized, equals(deserialized));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('default serializer can be overridden globally', () {
|
||||||
|
final old = moorRuntimeOptions.defaultSerializer;
|
||||||
|
moorRuntimeOptions.defaultSerializer = _MySerializer();
|
||||||
|
|
||||||
|
final entry = TodoEntry(
|
||||||
|
id: 13,
|
||||||
|
title: 'Title',
|
||||||
|
content: 'Content',
|
||||||
|
category: 3,
|
||||||
|
targetDate: DateTime.now(),
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
entry.toJson(),
|
||||||
|
{
|
||||||
|
'id': 'foo',
|
||||||
|
'title': 'foo',
|
||||||
|
'content': 'foo',
|
||||||
|
'category': 'foo',
|
||||||
|
'target_date': 'foo',
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
moorRuntimeOptions.defaultSerializer = old;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
class _MySerializer extends ValueSerializer {
|
||||||
|
@override
|
||||||
|
T fromJson<T>(dynamic json) {
|
||||||
|
throw StateError('Should not be called');
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
dynamic toJson<T>(T value) {
|
||||||
|
return 'foo';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -108,9 +108,9 @@ class DataClassWriter {
|
||||||
|
|
||||||
_buffer
|
_buffer
|
||||||
..write('factory $dataClassName.fromJson('
|
..write('factory $dataClassName.fromJson('
|
||||||
'Map<String, dynamic> json,'
|
'Map<String, dynamic> json, {ValueSerializer serializer}'
|
||||||
'{ValueSerializer serializer = const ValueSerializer.defaults()}'
|
|
||||||
') {\n')
|
') {\n')
|
||||||
|
..write('serializer ??= moorRuntimeOptions.defaultSerializer;\n')
|
||||||
..write('return $dataClassName(');
|
..write('return $dataClassName(');
|
||||||
|
|
||||||
for (final column in table.columns) {
|
for (final column in table.columns) {
|
||||||
|
@ -126,7 +126,7 @@ class DataClassWriter {
|
||||||
if (scope.writer.options.generateFromJsonStringConstructor) {
|
if (scope.writer.options.generateFromJsonStringConstructor) {
|
||||||
// also generate a constructor that only takes a json string
|
// also generate a constructor that only takes a json string
|
||||||
_buffer.write('factory $dataClassName.fromJsonString(String encodedJson, '
|
_buffer.write('factory $dataClassName.fromJsonString(String encodedJson, '
|
||||||
'{ValueSerializer serializer = const ValueSerializer.defaults()}) => '
|
'{ValueSerializer serializer}) => '
|
||||||
'$dataClassName.fromJson('
|
'$dataClassName.fromJson('
|
||||||
'DataClass.parseJson(encodedJson) as Map<String, dynamic>, '
|
'DataClass.parseJson(encodedJson) as Map<String, dynamic>, '
|
||||||
'serializer: serializer);');
|
'serializer: serializer);');
|
||||||
|
@ -135,8 +135,9 @@ class DataClassWriter {
|
||||||
|
|
||||||
void _writeToJson() {
|
void _writeToJson() {
|
||||||
_buffer.write('@override Map<String, dynamic> toJson('
|
_buffer.write('@override Map<String, dynamic> toJson('
|
||||||
'{ValueSerializer serializer = const ValueSerializer.defaults()}) {'
|
'{ValueSerializer serializer}) {\n'
|
||||||
'\n return <String, dynamic>{');
|
'serializer ??= moorRuntimeOptions.defaultSerializer;\n'
|
||||||
|
'return <String, dynamic>{\n');
|
||||||
|
|
||||||
for (final column in table.columns) {
|
for (final column in table.columns) {
|
||||||
final name = column.getJsonKey(scope.options);
|
final name = column.getJsonKey(scope.options);
|
||||||
|
|
Loading…
Reference in New Issue