| | 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 | | using System; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.Linq; |
| | 8 | | using System.Reflection; |
| | 9 | | using Microsoft.Azure.Search.Models; |
| | 10 | | using Microsoft.Spatial; |
| | 11 | | using Newtonsoft.Json; |
| | 12 | | using Newtonsoft.Json.Linq; |
| | 13 | |
|
| | 14 | | namespace Microsoft.Azure.Search.Serialization |
| | 15 | | { |
| | 16 | | /// <summary> |
| | 17 | | /// Deserializes JSON objects and arrays to .NET types instead of JObject and JArray. |
| | 18 | | /// </summary> |
| | 19 | | /// <remarks> |
| | 20 | | /// This JSON converter supports reading only. When deserializing JSON to an instance of type <c cref="Document">Doc |
| | 21 | | /// recursively deserialize JSON objects to <c cref="Document">Document</c> instances as well. This includes object |
| | 22 | | /// as arrays of objects. It also makes a best-effort attempt to deserialize JSON arrays to a specific .NET array ty |
| | 23 | | /// arrays are deserialized to arrays of <c cref="System.Object">System.Object</c>. |
| | 24 | | /// </remarks> |
| | 25 | | internal class DocumentConverter : JsonConverter |
| | 26 | | { |
| 2 | 27 | | private static readonly object[] EmptyObjectArray = new object[0]; |
| | 28 | |
|
| 16304 | 29 | | public override bool CanRead => true; |
| | 30 | |
|
| 0 | 31 | | public override bool CanWrite => false; |
| | 32 | |
|
| | 33 | | public override bool CanConvert(Type objectType) => |
| 184156 | 34 | | typeof(Document).GetTypeInfo().IsAssignableFrom(objectType.GetTypeInfo()); |
| | 35 | |
|
| | 36 | | public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer seriali |
| | 37 | | { |
| 16304 | 38 | | var result = new Document(); |
| 16304 | 39 | | JObject bag = serializer.Deserialize<JObject>(reader); |
| | 40 | |
|
| 293336 | 41 | | foreach (JProperty field in bag.Properties()) |
| | 42 | | { |
| | 43 | | // Skip OData @search annotations. These are deserialized separately. |
| 130364 | 44 | | if (field.Name.StartsWith("@search.", StringComparison.Ordinal)) |
| | 45 | | { |
| | 46 | | continue; |
| | 47 | | } |
| | 48 | |
|
| 114262 | 49 | | object value = |
| 114262 | 50 | | (field.Value is JArray array) ? |
| 114262 | 51 | | ConvertArray(array, serializer) : |
| 114262 | 52 | | ConvertToken(field.Value, serializer); |
| | 53 | |
|
| 114262 | 54 | | result[field.Name] = value; |
| | 55 | | } |
| | 56 | |
|
| 16304 | 57 | | return result; |
| | 58 | | } |
| | 59 | |
|
| 0 | 60 | | public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) => throw new NotImple |
| | 61 | |
|
| | 62 | | private static object ConvertToken(JToken token, JsonSerializer serializer) |
| | 63 | | { |
| | 64 | | switch (token) |
| | 65 | | { |
| | 66 | | case JObject obj: |
| 244 | 67 | | var tokenReader = new JTokenReader(obj); |
| 244 | 68 | | return obj.IsGeoJsonPoint() ? |
| 244 | 69 | | serializer.Deserialize<GeographyPoint>(tokenReader) : |
| 244 | 70 | | (object)serializer.Deserialize<Document>(tokenReader); |
| | 71 | |
|
| | 72 | | default: |
| 98226 | 73 | | return token.ToObject(typeof(object), serializer); |
| | 74 | | } |
| | 75 | | } |
| | 76 | |
|
| | 77 | | private static Array ConvertArray(JArray array, JsonSerializer serializer) |
| | 78 | | { |
| 16346 | 79 | | if (array.Count < 1) |
| | 80 | | { |
| | 81 | | // With no type information, assume type object. |
| 16094 | 82 | | return EmptyObjectArray; |
| | 83 | | } |
| | 84 | |
|
| | 85 | | Tuple<bool, T> ConvertToReferenceType<T>(JToken token, bool allowNull) where T : class |
| | 86 | | { |
| 478 | 87 | | switch (ConvertToken(token, serializer)) |
| | 88 | | { |
| | 89 | | case T typedValue: |
| 456 | 90 | | return Tuple.Create(true, typedValue); |
| | 91 | |
|
| 10 | 92 | | case null when allowNull: |
| 10 | 93 | | return Tuple.Create(true, (T)null); |
| | 94 | |
|
| | 95 | | default: |
| 12 | 96 | | return Tuple.Create(false, (T)null); |
| | 97 | | } |
| | 98 | | } |
| | 99 | |
|
| | 100 | | Tuple<bool, T> ConvertToValueType<T>(JToken token) where T : struct |
| | 101 | | { |
| 46 | 102 | | switch (ConvertToken(token, serializer)) |
| | 103 | | { |
| | 104 | | case T typedValue: |
| 46 | 105 | | return Tuple.Create(true, typedValue); |
| | 106 | |
|
| | 107 | | default: |
| 0 | 108 | | return Tuple.Create(false, default(T)); |
| | 109 | | } |
| | 110 | | } |
| | 111 | |
|
| | 112 | | Array ConvertToArrayOfType<T>(Func<JToken, Tuple<bool, T>> convert) |
| | 113 | | { |
| 252 | 114 | | var typedValues = new T[array.Count]; |
| | 115 | |
|
| | 116 | | // Explicit loop ensures only one pass through the array. |
| 1528 | 117 | | for (int i = 0; i < typedValues.Length; i++) |
| | 118 | | { |
| 524 | 119 | | JToken token = array[i]; |
| | 120 | |
|
| | 121 | | // Avoiding ValueTuple for now to avoid taking an extra dependency. |
| 524 | 122 | | Tuple<bool, T> convertResult = convert(token); |
| 524 | 123 | | bool wasConverted = convertResult.Item1; |
| 524 | 124 | | T typedValue = convertResult.Item2; |
| | 125 | |
|
| 524 | 126 | | if (wasConverted) |
| | 127 | | { |
| 512 | 128 | | typedValues[i] = typedValue; |
| | 129 | | } |
| | 130 | | else |
| | 131 | | { |
| | 132 | | // As soon as we see something other than the expected type, give up on the typed array and buil |
| 12 | 133 | | IEnumerable<JToken> remainingTokens = array.Skip(i); |
| 42 | 134 | | IEnumerable<object> remainingObjects = remainingTokens.Select(t => ConvertToken(t, serializer)); |
| 12 | 135 | | return typedValues.Take(i).Cast<object>().Concat(remainingObjects).ToArray(); |
| | 136 | | } |
| | 137 | | } |
| | 138 | |
|
| 240 | 139 | | return typedValues; |
| | 140 | | } |
| | 141 | |
|
| | 142 | | Array ConvertToArrayOfReferenceType<T>(bool allowNull = false) where T : class => |
| 712 | 143 | | ConvertToArrayOfType<T>(t => ConvertToReferenceType<T>(t, allowNull)); |
| | 144 | |
|
| 18 | 145 | | Array ConvertToArrayOfValueType<T>() where T : struct => ConvertToArrayOfType<T>(ConvertToValueType<T>); |
| | 146 | |
|
| | 147 | | // We need to figure out the best-fit type for the array based on the data that's been deserialized so far. |
| | 148 | | // We can do this by checking the first element. Note that null as a first element is a special case for |
| | 149 | | // backward compatibility. |
| 252 | 150 | | switch (array[0].Type) |
| | 151 | | { |
| | 152 | | case JTokenType.Null: |
| | 153 | | case JTokenType.String: |
| | 154 | | // Arrays of all nulls or a mixture of strings and nulls are treated as string arrays for backward c |
| 186 | 155 | | return ConvertToArrayOfReferenceType<string>(allowNull: true); |
| | 156 | |
|
| | 157 | | case JTokenType.Boolean: |
| 4 | 158 | | return ConvertToArrayOfValueType<bool>(); |
| | 159 | |
|
| | 160 | | case JTokenType.Integer: |
| 8 | 161 | | return ConvertToArrayOfValueType<long>(); |
| | 162 | |
|
| | 163 | | case JTokenType.Float: |
| 2 | 164 | | return ConvertToArrayOfValueType<double>(); |
| | 165 | |
|
| | 166 | | case JTokenType.Date: |
| 4 | 167 | | return ConvertToArrayOfValueType<DateTimeOffset>(); |
| | 168 | |
|
| | 169 | | case JTokenType.Object: |
| 48 | 170 | | return ((JObject)array[0]).IsGeoJsonPoint() ? |
| 48 | 171 | | ConvertToArrayOfReferenceType<GeographyPoint>() : |
| 48 | 172 | | ConvertToArrayOfReferenceType<Document>(); |
| | 173 | |
|
| | 174 | | default: |
| 0 | 175 | | return array.Select(t => ConvertToken(t, serializer)).ToArray(); |
| | 176 | | } |
| | 177 | | } |
| | 178 | | } |
| | 179 | | } |