예제 #1
0
        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
        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);
            }
        }