|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using Microsoft.AspNetCore.Mvc; |
| 6 | +using Microsoft.AspNetCore.OData.Query.Container; |
| 7 | +using Microsoft.Extensions.DependencyInjection; |
| 8 | +using Microsoft.OData.Edm; |
| 9 | + |
| 10 | +namespace Microsoft.AspNetCore.OData.NewtonsoftJson |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Extension methods for adding OData Json converter to Newtonsoft.Json to <see cref="IMvcBuilder"/> and <see cref="IMvcCoreBuilder"/>. |
| 14 | + /// </summary> |
| 15 | + public static class ODataNewtonsoftJsonMvcBuilderExtensions |
| 16 | + { |
| 17 | + #region IMvcBuilder |
| 18 | + /// <summary> |
| 19 | + /// Configures Newtonsoft.Json using OData Json converter. |
| 20 | + /// </summary> |
| 21 | + /// <param name="builder">The Mvc builder.</param> |
| 22 | + /// <returns>The <see cref="IMvcBuilder"/>.</returns> |
| 23 | + public static IMvcBuilder AddODataNewtonsoftJson(this IMvcBuilder builder) |
| 24 | + { |
| 25 | + return builder.AddODataNewtonsoftJson(null); |
| 26 | + } |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Configures Newtonsoft.Json using OData Json converter. |
| 30 | + /// </summary> |
| 31 | + /// <param name="builder">The Mvc builder.</param> |
| 32 | + /// <param name="mapperProvider">The mapper provider.</param> |
| 33 | + /// <returns>The <see cref="IMvcBuilder"/>.</returns> |
| 34 | + public static IMvcBuilder AddODataNewtonsoftJson(this IMvcBuilder builder, |
| 35 | + Func<IEdmModel, IEdmStructuredType, IPropertyMapper> mapperProvider) |
| 36 | + { |
| 37 | + if (builder is null) |
| 38 | + { |
| 39 | + throw new ArgumentNullException(nameof(builder)); |
| 40 | + } |
| 41 | + |
| 42 | + return builder.AddNewtonsoftJson(BuildSetupAction(mapperProvider)); |
| 43 | + } |
| 44 | + #endregion |
| 45 | + |
| 46 | + #region IMvcCoreBuilder |
| 47 | + /// <summary> |
| 48 | + /// Configures Newtonsoft.Json using OData Json converter. |
| 49 | + /// </summary> |
| 50 | + /// <param name="builder">The Mvc core builder.</param> |
| 51 | + /// <returns>The <see cref="IMvcCoreBuilder"/>.</returns> |
| 52 | + public static IMvcCoreBuilder AddODataNewtonsoftJson(this IMvcCoreBuilder builder) |
| 53 | + { |
| 54 | + return builder.AddNewtonsoftJson(null); |
| 55 | + } |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// Configures Newtonsoft.Json using OData Json converter. |
| 59 | + /// </summary> |
| 60 | + /// <param name="builder">The Mvc core builder.</param> |
| 61 | + /// <param name="mapperProvider">The mapper provider.</param> |
| 62 | + /// <returns>The <see cref="IMvcCoreBuilder"/>.</returns> |
| 63 | + public static IMvcCoreBuilder AddODataNewtonsoftJson(this IMvcCoreBuilder builder, |
| 64 | + Func<IEdmModel, IEdmStructuredType, IPropertyMapper> mapperProvider) |
| 65 | + { |
| 66 | + if (builder is null) |
| 67 | + { |
| 68 | + throw new ArgumentNullException(nameof(builder)); |
| 69 | + } |
| 70 | + |
| 71 | + return builder.AddNewtonsoftJson(BuildSetupAction(mapperProvider)); |
| 72 | + } |
| 73 | + #endregion |
| 74 | + |
| 75 | + private static Action<MvcNewtonsoftJsonOptions> BuildSetupAction(Func<IEdmModel, IEdmStructuredType, IPropertyMapper> mapperProvider) |
| 76 | + { |
| 77 | + Action<MvcNewtonsoftJsonOptions> odataSetupAction = opt => |
| 78 | + { |
| 79 | + if (mapperProvider is null) |
| 80 | + { |
| 81 | + opt.SerializerSettings.Converters.Add(new JSelectExpandWrapperConverter()); |
| 82 | + } |
| 83 | + else |
| 84 | + { |
| 85 | + opt.SerializerSettings.Converters.Add(new JSelectExpandWrapperConverter(mapperProvider)); |
| 86 | + } |
| 87 | + |
| 88 | + opt.SerializerSettings.Converters.Add(new JDynamicTypeWrapperConverter()); |
| 89 | + opt.SerializerSettings.Converters.Add(new JPageResultValueConverter()); |
| 90 | + opt.SerializerSettings.Converters.Add(new JSingleResultValueConverter()); |
| 91 | + }; |
| 92 | + |
| 93 | + return odataSetupAction; |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments