コード例 #1
0
ファイル: JsonLayout.cs プロジェクト: xtrmstep/ContextLogger
        private void InitializeSerialization(JsonSerializerSettings settingsOverride = null)
        {
            if (_initialized)
            {
                return;
            }
            lock (_initializeLock)
            {
                if (_initialized)
                {
                    return;
                }

                _initialized = true;
                if (settingsOverride != null)
                {
                    _settings = settingsOverride;
                    return;
                }

                // build from parameters of layout

                _settings = new JsonSerializerSettings
                {
                    ReferenceLoopHandling = ReferenceLoopHandling
                };

                // add converters
                var jsonConverters = CreateInstances <JsonConverter>(TypeConverters);
                if (jsonConverters.Any())
                {
                    foreach (var jsonConverter in jsonConverters)
                    {
                        _settings.Converters.Add(jsonConverter);
                    }
                }
                // add format
                var dateTimeConverter = new IsoDateTimeConverter
                {
                    DateTimeFormat = DateTimeFormat ?? JsonLayoutSettings.DefaultDateTimeFormat
                };
                _settings.Converters.Add(dateTimeConverter);

                // add resolvers
                var compositeResolver = new CompositeContractResolver();
                if (HasSkippedProperties())
                {
                    var skippedProperties = GetSkippedProperties();
                    compositeResolver.Add(new ShouldSerializeContractResolver(skippedProperties));
                }
                else
                {
                    compositeResolver.Add(new ShouldSerializeContractResolver(JsonLayoutSettings.AdvancedPropertyFilter));
                }

                var jsonResolvers = CreateInstances <IContractResolver>(ContractResolvers);
                AddContractResolvers(compositeResolver, jsonResolvers);
                _settings.ContractResolver = compositeResolver;
            }
        }
コード例 #2
0
ファイル: JsonLayout.cs プロジェクト: xtrmstep/ContextLogger
        protected virtual void AddContractResolvers(CompositeContractResolver compositeResolver, IList <IContractResolver> contractResolvers)
        {
            if (compositeResolver == null)
            {
                throw new ArgumentNullException(nameof(compositeResolver));
            }

            if (contractResolvers == null || contractResolvers.Count == 0)
            {
                return;
            }

            foreach (var contractResolver in contractResolvers)
            {
                compositeResolver.Add(contractResolver);
            }
        }