// ------------------------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information. // ------------------------------------------------------------------------------ namespace Microsoft.Graph { using System; using Newtonsoft.Json; /// /// Handles serialization and deserialization for TimeOfDay. /// public class TimeOfDayConverter : JsonConverter { /// /// Checks if the given type can be converted to a TimeOfDay. /// /// The object type. /// True if the object is type match of TimeOfDay. public override bool CanConvert(Type objectType) { if (objectType == typeof(TimeOfDay)) { return true; } return false; } /// /// Deserialize the JSON data into a TimeOfDay object. /// /// The to read from. /// The object type. /// The original value. /// The serializer to deserialize the object with. /// A TimeOfDay object. public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { try { var dateTime = (DateTime)serializer.Deserialize(reader, typeof(DateTime)); return new TimeOfDay(dateTime); } catch (JsonSerializationException serializationException) { throw new ServiceException( new Error { Code = ErrorConstants.Codes.GeneralException, Message = "Unable to deserialize time of day" }, serializationException); } } /// /// Writes the JSON representation of the object. /// /// The to write to. /// The value. /// The calling serializer. public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { var timeOfDay = value as TimeOfDay; if (timeOfDay != null) { writer.WriteValue(timeOfDay.ToString()); } else { throw new ServiceException( new Error { Code = ErrorConstants.Codes.GeneralException, Message = "Invalid type for time of day converter" }); } } } }