コード例 #1
0
        /// <summary>
        /// this constructor is only here for stateful (de)serialization
        /// </summary>
        protected internal JsonNetSerializer(
            IConnectionSettingsValues settings,
            JsonConverter statefulConverter
            )
        {
            this.Settings = settings;

            var piggyBackState = statefulConverter == null ? null : new JsonConverterPiggyBackState {
                ActualJsonConverter = statefulConverter
            };

            // ReSharper disable once VirtualMemberCallInContructor
            this.ContractResolver = new ElasticContractResolver(this.Settings, this.ContractConverters)
            {
                PiggyBackState = piggyBackState
            };

            this._defaultSerializer = JsonSerializer.Create(this.CreateSettings(SerializationFormatting.None));
            var indentedSerializer = JsonSerializer.Create(this.CreateSettings(SerializationFormatting.Indented));

            this._defaultSerializers = new Dictionary <SerializationFormatting, JsonSerializer>
            {
                { SerializationFormatting.None, this._defaultSerializer },
                { SerializationFormatting.Indented, indentedSerializer }
            };
        }
コード例 #2
0
        protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
        {
            var property = base.CreateProperty(member, memberSerialization);

            if (property.PropertyType == typeof(QueryContainer))
            {
                property.ShouldSerialize = o => ElasticContractResolver.ShouldSerializeQueryContainer(o, property);
            }
            else if (property.PropertyType == typeof(IEnumerable <QueryContainer>))
            {
                property.ShouldSerialize = o => ElasticContractResolver.ShouldSerializeQueryContainers(o, property);
            }

            // Skip serialization of empty collections that have DefaultValueHandling set to Ignore.
            else if (property.DefaultValueHandling.HasValue &&
                     property.DefaultValueHandling.Value == DefaultValueHandling.Ignore &&
                     !typeof(string).IsAssignableFrom(property.PropertyType) &&
                     typeof(IEnumerable).IsAssignableFrom(property.PropertyType))
            {
                Predicate <object> shouldSerialize = obj =>
                {
                    var collection = property.ValueProvider.GetValue(obj) as ICollection;
                    if (collection == null)
                    {
                        return(true);
                    }
                    return(collection.Count != 0 && collection.Cast <object>().Any(item => item != null));
                };
                property.ShouldSerialize = property.ShouldSerialize == null ? shouldSerialize : (o => property.ShouldSerialize(o) && shouldSerialize(o));
            }

            IPropertyMapping propertyMapping = null;

            if (!this.ConnectionSettings.PropertyMappings.TryGetValue(member, out propertyMapping))
            {
                propertyMapping = ElasticsearchPropertyAttributeBase.From(member);
            }

            var serializerMapping = this.ConnectionSettings.Serializer?.CreatePropertyMapping(member);

            var nameOverride = propertyMapping?.Name ?? serializerMapping?.Name;

            if (!nameOverride.IsNullOrEmpty())
            {
                property.PropertyName = nameOverride;
            }

            var overrideIgnore = propertyMapping?.Ignore ?? serializerMapping?.Ignore;

            if (overrideIgnore.HasValue)
            {
                property.Ignored = overrideIgnore.Value;
            }

            return(property);
        }
コード例 #3
0
        /// <summary>
        /// this constructor is only here for stateful (de)serialization
        /// </summary>
        protected internal JsonNetSerializer(IConnectionSettingsValues settings, JsonConverter statefulConverter)
        {
            this.Settings = settings;
            var piggyBackState = statefulConverter == null ? null : new JsonConverterPiggyBackState {
                ActualJsonConverter = statefulConverter
            };

            this.ContractResolver = new ElasticContractResolver(this.Settings)
            {
                PiggyBackState = piggyBackState
            };

            var collapsed = this.CreateSettings(SerializationFormatting.None);
            var indented  = this.CreateSettings(SerializationFormatting.Indented);

            this._defaultSerializer  = JsonSerializer.Create(collapsed);
            this._indentedSerializer = JsonSerializer.Create(indented);
        }
コード例 #4
0
        /// <summary>
        /// this constructor is only here for stateful (de)serialization
        /// </summary>
        protected internal JsonNetSerializer(
            IConnectionSettingsValues settings,
            JsonConverter statefulConverter,
            Action <JsonSerializerSettings, IConnectionSettingsValues> settingsModifier = null
            )
        {
            this.Settings = settings;
            var piggyBackState = statefulConverter == null ? null : new JsonConverterPiggyBackState {
                ActualJsonConverter = statefulConverter
            };

            // ReSharper disable once VirtualMemberCallInContructor
            this.ContractResolver = new ElasticContractResolver(this.Settings, this.ContractConverters)
            {
                PiggyBackState = piggyBackState
            };

            OverwriteDefaultSerializers(settingsModifier ?? ((s, csv) => { }));
        }