-
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.
Merge pull request #429 from davidmorgan/iso-datetime
Add Iso8601DateTimeSerializer.
- Loading branch information
Showing
4 changed files
with
95 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,38 @@ | ||
// Copyright (c) 2018, 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'; | ||
|
||
/// Alternative serializer for [DateTime]. | ||
/// | ||
/// Install this to use ISO8601 format instead of the default (microseconds | ||
/// since epoch). Use [SerializersBuilder.add] to install it. | ||
/// | ||
/// An exception will be thrown on attempt to serialize local DateTime | ||
/// instances; you must use UTC. | ||
class Iso8601DateTimeSerializer implements PrimitiveSerializer<DateTime> { | ||
final bool structured = false; | ||
@override | ||
final Iterable<Type> types = new BuiltList<Type>([DateTime]); | ||
@override | ||
final String wireName = 'DateTime'; | ||
|
||
@override | ||
Object serialize(Serializers serializers, DateTime dateTime, | ||
{FullType specifiedType: FullType.unspecified}) { | ||
if (!dateTime.isUtc) { | ||
throw new ArgumentError.value( | ||
dateTime, 'dateTime', 'Must be in utc for serialization.'); | ||
} | ||
|
||
return dateTime.toIso8601String(); | ||
} | ||
|
||
@override | ||
DateTime deserialize(Serializers serializers, Object serialized, | ||
{FullType specifiedType: FullType.unspecified}) { | ||
return DateTime.parse(serialized as String).toUtc(); | ||
} | ||
} |
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,50 @@ | ||
// Copyright (c) 2018, 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_value/iso_8601_date_time_serializer.dart'; | ||
import 'package:built_value/serializer.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
final serializers = (new Serializers().toBuilder() | ||
..add(new Iso8601DateTimeSerializer())) | ||
.build(); | ||
|
||
group('DateTime with known specifiedType', () { | ||
final data = new DateTime.utc(1980, 1, 2, 3, 4, 5, 6, 7); | ||
final serialized = '1980-01-02T03:04:05.006007Z'; | ||
final specifiedType = const FullType(DateTime); | ||
|
||
test('can be serialized', () { | ||
expect(serializers.serialize(data, specifiedType: specifiedType), | ||
serialized); | ||
}); | ||
|
||
test('can be deserialized', () { | ||
expect(serializers.deserialize(serialized, specifiedType: specifiedType), | ||
data); | ||
}); | ||
|
||
test('serialize throws if not UTC', () { | ||
expect(() => serializers.serialize(new DateTime.now()), | ||
throwsA(new isInstanceOf<ArgumentError>())); | ||
}); | ||
}); | ||
|
||
group('DateTime with unknown specifiedType', () { | ||
final data = new DateTime.utc(1980, 1, 2, 3, 4, 5, 6, 7); | ||
final serialized = ['DateTime', '1980-01-02T03:04:05.006007Z']; | ||
final 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); | ||
}); | ||
}); | ||
} |