Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Int32 serializer #1271

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions built_value/lib/serializer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -67,6 +68,7 @@ abstract class Serializers {
..add(DoubleSerializer())
..add(DurationSerializer())
..add(IntSerializer())
..add(Int32Serializer())
..add(Int64Serializer())
..add(JsonObjectSerializer())
..add(NullSerializer())
Expand Down
27 changes: 27 additions & 0 deletions built_value/lib/src/int32_serializer.dart
Original file line number Diff line number Diff line change
@@ -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<Int32> {
final bool structured = false;
@override
final Iterable<Type> types = BuiltList<Type>([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);
}
}
46 changes: 46 additions & 0 deletions built_value/test/int32_serializer_test.dart
Original file line number Diff line number Diff line change
@@ -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);
});
});
}