blob: d345f5b2892d4b013921283375314eeef3daecbb [file] [log] [blame]
Constantin Ziesche857c7ab2020-02-25 11:24:51 +01001/*******************************************************************************
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 Ziesche857c7ab2020-02-25 11:24:51 +010011using Microsoft.Extensions.DependencyInjection;
12using Newtonsoft.Json;
13using Newtonsoft.Json.Converters;
14using System;
15
Constantin Zieschefa612082020-04-03 09:54:56 +020016namespace BaSyx.Utils.DependencyInjection
Constantin Ziesche857c7ab2020-02-25 11:24:51 +010017{
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 Zieschefa612082020-04-03 09:54:56 +020025 Services.AddStandardImplementation();
Constantin Ziesche857c7ab2020-02-25 11:24:51 +010026
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 Zieschefa612082020-04-03 09:54:56 +020035 ContractResolver = new DependencyInjectionContractResolver(new DependencyInjectionExtension(Services));
Constantin Ziesche857c7ab2020-02-25 11:24:51 +010036 }
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 Zieschefa612082020-04-03 09:54:56 +020050 ContractResolver = new DependencyInjectionContractResolver(new DependencyInjectionExtension(Services));
Constantin Ziesche857c7ab2020-02-25 11:24:51 +010051 }
52
53 public static IServiceCollection GetStandardServiceCollection()
54 {
55 IServiceCollection services = new ServiceCollection();
Constantin Zieschefa612082020-04-03 09:54:56 +020056 services.AddStandardImplementation();
Constantin Ziesche857c7ab2020-02-25 11:24:51 +010057 return services;
58 }
59 }
60}