blob: 490edab0158b46c467e385b191c638fb4d57c075 [file] [log] [blame]
// Copyright (c) 2015, 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';
class DoubleSerializer implements PrimitiveSerializer<double> {
// Constant names match those in [double].
// ignore_for_file: non_constant_identifier_names
static final String nan = 'NaN';
static final String infinity = 'INF';
static final String negativeInfinity = '-INF';
final bool structured = false;
@override
final Iterable<Type> types = BuiltList<Type>([double]);
@override
final String wireName = 'double';
@override
Object serialize(Serializers serializers, double aDouble,
{FullType specifiedType = FullType.unspecified}) {
if (aDouble.isNaN) {
return nan;
} else if (aDouble.isInfinite) {
return aDouble.isNegative ? negativeInfinity : infinity;
} else {
return aDouble;
}
}
@override
double deserialize(Serializers serializers, Object? serialized,
{FullType specifiedType = FullType.unspecified}) {
if (serialized == nan) {
return double.nan;
} else if (serialized == negativeInfinity) {
return double.negativeInfinity;
} else if (serialized == infinity) {
return double.infinity;
} else {
return (serialized as num).toDouble();
}
}
}