diff --git a/built_value/lib/serializer.dart b/built_value/lib/serializer.dart index 14ab89e9..22e5163c 100644 --- a/built_value/lib/serializer.dart +++ b/built_value/lib/serializer.dart @@ -7,6 +7,7 @@ import 'package:built_collection/src/internal/hash.dart'; import 'package:built_value/src/big_int_serializer.dart'; import 'package:built_value/src/date_time_serializer.dart'; import 'package:built_value/src/duration_serializer.dart'; +import 'package:built_value/src/int32_serializer.dart'; import 'package:built_value/src/int64_serializer.dart'; import 'package:built_value/src/json_object_serializer.dart'; import 'package:built_value/src/num_serializer.dart'; @@ -67,6 +68,7 @@ abstract class Serializers { ..add(DoubleSerializer()) ..add(DurationSerializer()) ..add(IntSerializer()) + ..add(Int32Serializer()) ..add(Int64Serializer()) ..add(JsonObjectSerializer()) ..add(NullSerializer()) diff --git a/built_value/lib/src/int32_serializer.dart b/built_value/lib/src/int32_serializer.dart new file mode 100644 index 00000000..6962086b --- /dev/null +++ b/built_value/lib/src/int32_serializer.dart @@ -0,0 +1,27 @@ +// Copyright (c) 2023, Google Inc. Please see the AUTHORS file for details. +// All rights reserved. Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +import 'package:built_collection/built_collection.dart'; +import 'package:built_value/serializer.dart'; +import 'package:fixnum/fixnum.dart'; + +class Int32Serializer implements PrimitiveSerializer { + final bool structured = false; + @override + final Iterable types = BuiltList([Int32]); + @override + final String wireName = 'Int32'; + + @override + Object serialize(Serializers serializers, Int32 int32, + {FullType specifiedType = FullType.unspecified}) { + return int32.toInt(); + } + + @override + Int32 deserialize(Serializers serializers, Object? serialized, + {FullType specifiedType = FullType.unspecified}) { + return Int32(serialized as int); + } +} diff --git a/built_value/test/int32_serializer_test.dart b/built_value/test/int32_serializer_test.dart new file mode 100644 index 00000000..8cfa55a3 --- /dev/null +++ b/built_value/test/int32_serializer_test.dart @@ -0,0 +1,46 @@ +// Copyright (c) 2023, Google Inc. Please see the AUTHORS file for details. +// All rights reserved. Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +import 'dart:convert'; + +import 'package:built_value/serializer.dart'; +import 'package:fixnum/fixnum.dart'; +import 'package:test/test.dart'; + +void main() { + var serializers = Serializers(); + + group('int32 with known specifiedType', () { + var data = Int32.MAX_VALUE; + var serialized = Int32.MAX_VALUE.toInt(); + var specifiedType = const FullType(Int32); + + test('can be serialized', () { + expect(serializers.serialize(data, specifiedType: specifiedType), + serialized); + }); + + test('can be deserialized', () { + expect(serializers.deserialize(serialized, specifiedType: specifiedType), + data); + }); + }); + + group('int32 with unknown specifiedType', () { + var data = Int32.MIN_VALUE; + var serialized = + json.decode(json.encode(['Int32', Int32.MIN_VALUE.toInt()])) as Object; + var specifiedType = FullType.unspecified; + + test('can be serialized', () { + expect(serializers.serialize(data, specifiedType: specifiedType), + serialized); + }); + + test('can be deserialized', () { + expect(serializers.deserialize(serialized, specifiedType: specifiedType), + data); + }); + }); +}