| | 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 | |
|
| | 5 | | namespace Microsoft.Azure.Search.Serialization |
| | 6 | | { |
| | 7 | | using System; |
| | 8 | | using System.Linq; |
| | 9 | | using System.Reflection; |
| | 10 | | using Common; |
| | 11 | | using Newtonsoft.Json; |
| | 12 | |
|
| | 13 | | /// <summary> |
| | 14 | | /// Serializes and deserializes "extensible enums" to and from JSON. Extensible enums are like enumerations in |
| | 15 | | /// that they have well-known values, but they are extensible with new values and the values are based on strings |
| | 16 | | /// instead of integers. |
| | 17 | | /// </summary> |
| | 18 | | public class ExtensibleEnumConverter<T> : JsonConverter |
| | 19 | | { |
| | 20 | | private readonly Func<string, T> _enumValueFactory; |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// Initializes a new instance of the ExtensibleEnumConverter class. |
| | 24 | | /// </summary> |
| 2470 | 25 | | public ExtensibleEnumConverter() |
| | 26 | | { |
| | 27 | | bool TakesSingleStringParameter(ConstructorInfo ctor) |
| | 28 | | { |
| 2470 | 29 | | ParameterInfo[] parameters = ctor.GetParameters(); |
| 2470 | 30 | | if (parameters.Length == 1) |
| | 31 | | { |
| 2470 | 32 | | return parameters[0].ParameterType == typeof(string); |
| | 33 | | } |
| | 34 | |
|
| 0 | 35 | | return false; |
| | 36 | | } |
| | 37 | |
|
| 2470 | 38 | | ConstructorInfo fromStringCtor = typeof(T).GetTypeInfo().DeclaredConstructors.FirstOrDefault(TakesSingleStri |
| | 39 | |
|
| 2470 | 40 | | Throw.IfArgument( |
| 2470 | 41 | | fromStringCtor == null, |
| 2470 | 42 | | typeof(T).Name, |
| 2470 | 43 | | $"No constructor taking a string parameter could be found for type '{typeof(T)}'."); |
| | 44 | |
|
| 19338 | 45 | | _enumValueFactory = str => (T)fromStringCtor.Invoke(new[] { str }); |
| 2470 | 46 | | } |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// Indicates whether this converter can serialize or deserialize objects of the given type. |
| | 50 | | /// </summary> |
| | 51 | | /// <param name="objectType">The type to test against.</param> |
| | 52 | | /// <returns>true if objectType derives from ExtensibleEnum, false otherwise.</returns> |
| | 53 | | public override bool CanConvert(Type objectType) |
| | 54 | | { |
| 0 | 55 | | return typeof(T).GetTypeInfo().IsAssignableFrom(objectType.GetTypeInfo()); |
| | 56 | | } |
| | 57 | |
|
| | 58 | | /// <summary> |
| | 59 | | /// Deserializes a string into an ExtensibleEnum. |
| | 60 | | /// </summary> |
| | 61 | | /// <param name="reader">The JSON reader.</param> |
| | 62 | | /// <param name="objectType">Ignored by this method.</param> |
| | 63 | | /// <param name="existingValue">Ignored by this method.</param> |
| | 64 | | /// <param name="serializer">Ignored by this method.</param> |
| | 65 | | /// <returns>An instance of type T, or null if the current JSON token is null.</returns> |
| | 66 | | public override object ReadJson( |
| | 67 | | JsonReader reader, |
| | 68 | | Type objectType, |
| | 69 | | object existingValue, |
| | 70 | | JsonSerializer serializer) |
| | 71 | | { |
| | 72 | | // Check for null first. |
| 16874 | 73 | | if (reader.TokenType == JsonToken.Null) |
| | 74 | | { |
| 6 | 75 | | return null; |
| | 76 | | } |
| | 77 | |
|
| 16868 | 78 | | return _enumValueFactory(reader.Expect<string>(JsonToken.String)); |
| | 79 | | } |
| | 80 | |
|
| | 81 | | /// <summary> |
| | 82 | | /// Serializes an ExtensibleEnum to a JSON string. |
| | 83 | | /// </summary> |
| | 84 | | /// <param name="writer">The JSON writer.</param> |
| | 85 | | /// <param name="value">The value to serialize.</param> |
| | 86 | | /// <param name="serializer">Ignored by this method.</param> |
| | 87 | | public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) |
| | 88 | | { |
| 16880 | 89 | | writer.WriteValue(value.ToString()); |
| 16880 | 90 | | } |
| | 91 | | } |
| | 92 | | } |