< Summary

Class:Microsoft.Azure.Search.Serialization.Iso8601DateTimeConverter
Assembly:Microsoft.Azure.Search.Data
File(s):C:\Git\azure-sdk-for-net\sdk\search\Microsoft.Azure.Search.Data\src\Customizations\Serialization\Iso8601DateTimeConverter.cs
Covered lines:15
Uncovered lines:0
Coverable lines:15
Total lines:59
Line coverage:100% (15 of 15)
Covered branches:7
Total branches:8
Branch coverage:87.5% (7 of 8)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
CanConvert(...)-100%100%
ReadJson(...)-100%75%
WriteJson(...)-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\search\Microsoft.Azure.Search.Data\src\Customizations\Serialization\Iso8601DateTimeConverter.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License. See License.txt in the project root for
 3// license information.
 4
 5using System;
 6using System.Reflection;
 7using Newtonsoft.Json;
 8
 9namespace Microsoft.Azure.Search.Serialization
 10{
 11    /// <summary>
 12    /// Converts between dates serialized in ISO 8601 format in JSON strings and <c cref="System.DateTime">System.DateTi
 13    /// </summary>
 14    /// <remarks>
 15    /// This JSON converter ensures that <c cref="System.DateTime">System.DateTime</c> instances are serialized to have 
 16    /// explicitly included in the JSON. It also ensures that any time zone information in the JSON is taken into accoun
 17    /// deserializing to a new <c cref="System.DateTime">System.DateTime</c> instance. For example, if the JSON value's 
 18    /// is noon and its time zone is UTC-8, the deserialized <c cref="System.DateTime">System.DateTime</c> instance's ti
 19    /// and its <c cref="System.DateTime.Kind">Kind</c> will be <c cref="System.DateTimeKind.Utc">DateTimeKind.Utc</c>.
 20    /// </remarks>
 21    internal class Iso8601DateTimeConverter : JsonConverter
 22    {
 23        public override bool CanConvert(Type objectType)
 24        {
 26543825            TypeInfo objectTypeInfo = objectType.GetTypeInfo();
 26543826            return
 26543827                typeof(DateTime).GetTypeInfo().IsAssignableFrom(objectTypeInfo) ||
 26543828                typeof(DateTime?).GetTypeInfo().IsAssignableFrom(objectTypeInfo);
 29        }
 30
 31        public override object ReadJson(
 32            JsonReader reader,
 33            Type objectType,
 34            object existingValue,
 35            JsonSerializer serializer)
 36        {
 37            // Check for null first.
 5638            if (reader.TokenType == JsonToken.Null)
 39            {
 840                return null;
 41            }
 42
 4843            DateTimeOffset? dateTimeOffset = reader.Expect<DateTimeOffset?>(JsonToken.Date);
 4844            return dateTimeOffset.HasValue ? dateTimeOffset.Value.UtcDateTime : (DateTime?)null;
 45        }
 46
 47        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
 48        {
 5649            var dateTime = (DateTime)value;
 50
 5651            var dateTimeOffset =
 5652                dateTime.Kind == DateTimeKind.Unspecified ?
 5653                    new DateTimeOffset(dateTime, TimeSpan.Zero) :
 5654                    new DateTimeOffset(dateTime);
 55
 5656            serializer.Serialize(writer, dateTimeOffset);
 5657        }
 58    }
 59}