Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2020 Robert Bosch GmbH |
| 3 | * Author: Constantin Ziesche (constantin.ziesche@bosch.com) |
| 4 | * |
| 5 | * This program and the accompanying materials are made available under the |
| 6 | * terms of the Eclipse Public License 2.0 which is available at |
| 7 | * http://www.eclipse.org/legal/epl-2.0 |
| 8 | * |
| 9 | * SPDX-License-Identifier: EPL-2.0 |
| 10 | *******************************************************************************/ |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 11 | using Microsoft.Extensions.DependencyInjection; |
| 12 | using Newtonsoft.Json; |
| 13 | using Newtonsoft.Json.Converters; |
| 14 | using System; |
| 15 | |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 16 | namespace BaSyx.Utils.DependencyInjection |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 17 | { |
| 18 | public class JsonStandardSettings : JsonSerializerSettings |
| 19 | { |
| 20 | public IServiceProvider ServicePovider { get; } |
| 21 | public IServiceCollection Services { get; } |
| 22 | public JsonStandardSettings() : base() |
| 23 | { |
| 24 | Services = new ServiceCollection(); |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 25 | Services.AddStandardImplementation(); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 26 | |
| 27 | var serviceProviderFactory = new DefaultServiceProviderFactory(); |
| 28 | ServicePovider = serviceProviderFactory.CreateServiceProvider(Services); |
| 29 | |
| 30 | NullValueHandling = NullValueHandling.Include; |
| 31 | DefaultValueHandling = DefaultValueHandling.Include; |
| 32 | |
| 33 | Formatting = Formatting.Indented; |
| 34 | Converters.Add(new StringEnumConverter()); |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 35 | ContractResolver = new DependencyInjectionContractResolver(new DependencyInjectionExtension(Services)); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | public JsonStandardSettings(IServiceCollection services) : base() |
| 39 | { |
| 40 | Services = services; |
| 41 | |
| 42 | var serviceProviderFactory = new DefaultServiceProviderFactory(); |
| 43 | ServicePovider = serviceProviderFactory.CreateServiceProvider(Services); |
| 44 | |
| 45 | NullValueHandling = NullValueHandling.Include; |
| 46 | DefaultValueHandling = DefaultValueHandling.Include; |
| 47 | |
| 48 | Formatting = Formatting.Indented; |
| 49 | Converters.Add(new StringEnumConverter()); |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 50 | ContractResolver = new DependencyInjectionContractResolver(new DependencyInjectionExtension(Services)); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | public static IServiceCollection GetStandardServiceCollection() |
| 54 | { |
| 55 | IServiceCollection services = new ServiceCollection(); |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 56 | services.AddStandardImplementation(); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 57 | return services; |
| 58 | } |
| 59 | } |
| 60 | } |