// ------------------------------------------------------------------------------ // 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; using Newtonsoft.Json.Converters; using Newtonsoft.Json.Serialization; /// /// Handles resolving interfaces to the correct derived class during serialization/deserialization. /// public class EnumConverter : StringEnumConverter { /// /// Constructs a new EnumConverter. /// public EnumConverter() : base() { NamingStrategy = new CamelCaseNamingStrategy(); } /// /// Checks if the given type can be converted into an enum. All types /// can be converted. /// /// The object type. /// True. public override bool CanConvert(Type objectType) { return true; } /// /// Whether the object can be serialized to a request body. /// public override bool CanWrite { get { return true; } } /// /// Deserializes the object to the correct type. /// /// The to read from. /// The interface type. /// The existing value of the object being read. /// The for deserialization. /// public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { try { return base.ReadJson(reader, objectType, existingValue, serializer); } catch (JsonSerializationException) { // The StringEnumConverter will throw a JsonSerializationException if the enum value doesn't exist. // Swallow the exception and return null in this case. The actual value will be in the additional // data property bag after deserialization. } return null; } /// /// Serializes the object into a JSON string. /// /// The to write with. /// The object to write. /// The for serialization. public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { base.WriteJson(writer, value, serializer); } } }