| | 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.Linq.Expressions; |
| | 9 | | using System.Reflection; |
| | 10 | | using Microsoft.Azure.Search.Models; |
| | 11 | | using Microsoft.Azure.Search.Serialization.Internal; |
| | 12 | | using Microsoft.Rest.Serialization; |
| | 13 | | using Newtonsoft.Json; |
| | 14 | | using Newtonsoft.Json.Serialization; |
| | 15 | | using static System.Linq.Expressions.Expression; |
| | 16 | | using Throw = Microsoft.Azure.Search.Common.Throw; |
| | 17 | |
|
| | 18 | | namespace Microsoft.Azure.Search.Serialization |
| | 19 | | { |
| | 20 | | internal static class JsonUtility |
| | 21 | | { |
| 346 | 22 | | private static IContractResolver CamelCaseResolver { get; } = new CamelCasePropertyNamesContractResolver(); |
| | 23 | |
|
| 402 | 24 | | private static IContractResolver DefaultResolver { get; } = new DefaultContractResolver(); |
| | 25 | |
|
| | 26 | | public static JsonSerializerSettings CreateTypedSerializerSettings<T>(JsonSerializerSettings baseSettings, bool |
| 406 | 27 | | CreateSerializerSettings<T>(baseSettings, useCamelCase); |
| | 28 | |
|
| | 29 | | public static JsonSerializerSettings CreateTypedDeserializerSettings<T>(JsonSerializerSettings baseSettings) => |
| 234 | 30 | | CreateDeserializerSettings<T>(baseSettings); |
| | 31 | |
|
| | 32 | | public static JsonSerializerSettings CreateDocumentSerializerSettings(JsonSerializerSettings baseSettings) => |
| 40 | 33 | | CreateSerializerSettings<Document>(baseSettings, useCamelCase: false); |
| | 34 | |
|
| | 35 | | public static JsonSerializerSettings CreateDocumentDeserializerSettings(JsonSerializerSettings baseSettings) => |
| 92 | 36 | | CreateDeserializerSettings<Document>(baseSettings); |
| | 37 | |
|
| | 38 | | private static JsonSerializerSettings CreateSerializerSettings<T>( |
| | 39 | | JsonSerializerSettings baseSettings, |
| | 40 | | bool useCamelCase) |
| | 41 | | { |
| 446 | 42 | | JsonSerializerSettings settings = CopySettings(baseSettings); |
| 446 | 43 | | settings.Converters.Add(new GeoJsonPointConverter()); |
| 446 | 44 | | settings.Converters.Add(new IndexActionConverter<T>()); |
| 446 | 45 | | settings.Converters.Add(new Iso8601DateTimeConverter()); |
| 446 | 46 | | settings.Converters.Add(new EdmDoubleConverter()); |
| 446 | 47 | | settings.NullValueHandling = NullValueHandling.Ignore; |
| | 48 | |
|
| 446 | 49 | | if (useCamelCase) |
| | 50 | | { |
| 344 | 51 | | settings.ContractResolver = CamelCaseResolver; |
| | 52 | | } |
| 102 | 53 | | else if (settings.ContractResolver is ReadOnlyJsonContractResolver) |
| | 54 | | { |
| 90 | 55 | | settings.ContractResolver = DefaultResolver; |
| | 56 | | } |
| | 57 | |
|
| 446 | 58 | | return settings; |
| | 59 | | } |
| | 60 | |
|
| | 61 | | private static JsonSerializerSettings CreateDeserializerSettings<T>(JsonSerializerSettings baseSettings) |
| | 62 | | { |
| 326 | 63 | | JsonSerializerSettings settings = CopySettings(baseSettings); |
| 326 | 64 | | settings.Converters.Add(new GeoJsonPointConverter()); |
| 326 | 65 | | settings.Converters.Add(new DocumentConverter()); |
| 326 | 66 | | settings.Converters.Add(new Iso8601DateTimeConverter()); |
| 326 | 67 | | settings.Converters.Add(new EdmDoubleConverter()); |
| 326 | 68 | | settings.Converters.Add(new SearchResultConverter<T>()); |
| 326 | 69 | | settings.Converters.Add(new SuggestResultConverter<T>()); |
| 326 | 70 | | settings.DateParseHandling = DateParseHandling.DateTimeOffset; |
| | 71 | |
|
| | 72 | | // Fail when deserializing null into a non-nullable type. This is to avoid silent data corruption issues. |
| 326 | 73 | | settings.NullValueHandling = NullValueHandling.Include; |
| | 74 | |
|
| 326 | 75 | | if (settings.ContractResolver is ReadOnlyJsonContractResolver) |
| | 76 | | { |
| 310 | 77 | | settings.ContractResolver = DefaultResolver; |
| | 78 | | } |
| | 79 | |
|
| 326 | 80 | | return settings; |
| | 81 | | } |
| | 82 | |
|
| | 83 | | private static JsonSerializerSettings CopySettings(JsonSerializerSettings baseSettings) => |
| 772 | 84 | | JsonSerializerSettingsCopier.Instance.Copy(baseSettings); |
| | 85 | |
|
| | 86 | | // Unfortunately we can't just assign the properties of JsonSerializerSettings between instances, |
| | 87 | | // because some of these properties come and go within the span of a few JSON.NET versions, and the |
| | 88 | | // Azure .NET SDKs can reference a very wide range of possible versions. Instead, we use reflection |
| | 89 | | // and dynamic compilation to create a copy method tailored to whatever version of JSON.NET we find |
| | 90 | | // ourselves using. |
| | 91 | | private class JsonSerializerSettingsCopier |
| | 92 | | { |
| | 93 | | private readonly Delegate _copy; |
| | 94 | |
|
| 2 | 95 | | private JsonSerializerSettingsCopier() |
| | 96 | | { |
| | 97 | | Type sourceType, targetType; |
| 2 | 98 | | sourceType = targetType = typeof(JsonSerializerSettings); |
| | 99 | |
|
| 2 | 100 | | ParameterExpression source = Parameter(sourceType); |
| 2 | 101 | | Expression copyExpr = MakeCopyExpr(source); |
| | 102 | |
|
| | 103 | | // Emits 'source => {copyExpr}' where copyExpr refers to source and returns a copy of it. |
| 2 | 104 | | LambdaExpression lambdaExpression = |
| 2 | 105 | | Lambda( |
| 2 | 106 | | delegateType: GetFuncType(sourceType, targetType), |
| 2 | 107 | | body: copyExpr, |
| 2 | 108 | | parameters: source); |
| | 109 | |
|
| 2 | 110 | | _copy = lambdaExpression.Compile(); |
| 2 | 111 | | } |
| | 112 | |
|
| 774 | 113 | | public static JsonSerializerSettingsCopier Instance { get; } = new JsonSerializerSettingsCopier(); |
| | 114 | |
|
| | 115 | | public JsonSerializerSettings Copy(JsonSerializerSettings source) |
| | 116 | | { |
| 772 | 117 | | Throw.IfArgumentNull(source, nameof(source)); |
| | 118 | |
|
| | 119 | | // Shallow copy all the properties first, then deep copy Converters. |
| 772 | 120 | | var newSettings = (JsonSerializerSettings)_copy.DynamicInvoke(source); |
| 772 | 121 | | newSettings.Converters = new List<JsonConverter>(source.Converters); |
| 772 | 122 | | return newSettings; |
| | 123 | | } |
| | 124 | |
|
| | 125 | | private static Expression MakeCopyExpr(ParameterExpression sourceExpr) |
| | 126 | | { |
| 2 | 127 | | ParameterExpression source = Variable(typeof(JsonSerializerSettings)); |
| 2 | 128 | | ParameterExpression target = Variable(typeof(JsonSerializerSettings)); |
| | 129 | |
|
| 2 | 130 | | Expression assignSource = Assign(left: source, right: sourceExpr); |
| 2 | 131 | | Expression assignTarget = Assign(left: target, right: New(typeof(JsonSerializerSettings))); |
| | 132 | |
|
| 2 | 133 | | IEnumerable<Expression> copy = CopyAllProperties(source, target); |
| | 134 | |
|
| 2 | 135 | | return Block( |
| 2 | 136 | | type: typeof(JsonSerializerSettings), |
| 2 | 137 | | variables: new[] { source, target }, |
| 2 | 138 | | expressions: new[] { assignSource, assignTarget }.Concat(copy)); |
| | 139 | | } |
| | 140 | |
|
| | 141 | | private static IEnumerable<Expression> CopyAllProperties( |
| | 142 | | ParameterExpression source, |
| | 143 | | ParameterExpression target) |
| | 144 | | { |
| 2 | 145 | | var settableNonDeprecatedProperties = |
| 2 | 146 | | typeof(JsonSerializerSettings).GetTypeInfo().DeclaredProperties |
| 66 | 147 | | .Where(p => p.CanRead && p.CanWrite && !IsPropertyDeprecated(p)); |
| | 148 | |
|
| 120 | 149 | | foreach (PropertyInfo property in settableNonDeprecatedProperties) |
| | 150 | | { |
| 58 | 151 | | yield return Assign( |
| 58 | 152 | | left: PropertyOrField(target, property.Name), |
| 58 | 153 | | right: PropertyOrField(source, property.Name)); |
| | 154 | | } |
| | 155 | |
|
| 2 | 156 | | yield return target; |
| 2 | 157 | | } |
| | 158 | |
|
| | 159 | | private static bool IsPropertyDeprecated(PropertyInfo property) => |
| 70 | 160 | | property.GetCustomAttributes().Any(a => a is ObsoleteAttribute); |
| | 161 | | } |
| | 162 | | } |
| | 163 | | } |