diff --git a/moor/lib/src/runtime/data_class.dart b/moor/lib/src/runtime/data_class.dart index f3c450f0..47dfb69c 100644 --- a/moor/lib/src/runtime/data_class.dart +++ b/moor/lib/src/runtime/data_class.dart @@ -118,6 +118,10 @@ class _DefaultValueSerializer extends ValueSerializer { } } + if (T == double && json is int) { + return json.toDouble() as T; + } + return json as T; } diff --git a/moor/test/data_class_test.dart b/moor/test/data_class_test.dart index 1c9d1ae4..bfd6ccf9 100644 --- a/moor/test/data_class_test.dart +++ b/moor/test/data_class_test.dart @@ -18,6 +18,16 @@ void main() { expect(deserialized, equals(deserialized)); }); + test('can deserialize ints as doubles', () { + final entry = TableWithoutPKData.fromJson({ + 'notReallyAnId': 3, + 'someFloat': 4, + }); + + expect(entry, + TableWithoutPKData(notReallyAnId: 3, someFloat: 4, custom: null)); + }); + test('default serializer can be overridden globally', () { final old = moorRuntimeOptions.defaultSerializer; moorRuntimeOptions.defaultSerializer = _MySerializer();