-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d15b7f9
commit 5cdc8e6
Showing
3 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
} |