public IProperties GetProperties(ConcurrentDictionary <Type, int> seenTypes = null, int maxRecursion = 0)
        {
            var properties = new Properties();

            if (seenTypes != null && seenTypes.TryGetValue(_type, out var seen) && seen > maxRecursion)
            {
                return(properties);
            }

            foreach (var propertyInfo in _type.AllPropertiesCached())
            {
                var attribute = ElasticsearchPropertyAttributeBase.From(propertyInfo);
                if (attribute != null && attribute.Ignore)
                {
                    continue;
                }
                if (_visitor.SkipProperty(propertyInfo, attribute))
                {
                    continue;
                }

                var property = GetProperty(propertyInfo, attribute);
                if (property is IPropertyWithClrOrigin withCLrOrigin)
                {
                    withCLrOrigin.ClrOrigin = propertyInfo;
                }
                properties.Add(propertyInfo, property);
            }

            return(properties);
        }
Exemple #2
0
            private JsonProperty GetMapping(MemberInfo member)
            {
                // TODO: Skip calling this method for NEST and Elasticsearch.Net types, at the type level
                if (!_settings.PropertyMappings.TryGetValue(member, out var propertyMapping))
                {
                    propertyMapping = ElasticsearchPropertyAttributeBase.From(member);
                }

                var serializerMapping = _settings.PropertyMappingProvider?.CreatePropertyMapping(member);

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

                var property = new JsonProperty(nameOverride);

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

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

                if (propertyMapping != null || serializerMapping != null)
                {
                    property.AllowPrivate = true;
                }

                if (member.GetCustomAttribute <StringEnumAttribute>() != null)
                {
                    CreateEnumFormatterForProperty(member, property);
                }

                return(property);
            }
        public string Resolve(MemberInfo info)
        {
            if (info == null)
            {
                return(null);
            }

            var name = info.Name;

            IPropertyMapping propertyMapping = null;

            if (this._settings.PropertyMappings.TryGetValue(info, out propertyMapping))
            {
                return(propertyMapping.Name);
            }

            var att = ElasticsearchPropertyAttributeBase.From(info);

            if (att != null && !att.Name.IsNullOrEmpty())
            {
                return(att.Name);
            }

            return(_settings.PropertyMappingProvider?.CreatePropertyMapping(info)?.Name ?? _settings.DefaultFieldNameInferrer(name));
        }
Exemple #4
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);
        }
            private JsonProperty GetMapping(MemberInfo member)
            {
                // TODO: Skip calling this method for NEST and Elasticsearch.Net types, at the type level
                if (!_settings.PropertyMappings.TryGetValue(member, out var propertyMapping))
                {
                    propertyMapping = ElasticsearchPropertyAttributeBase.From(member);
                }

                var serializerMapping = _settings.PropertyMappingProvider?.CreatePropertyMapping(member);
                var nameOverride      = propertyMapping?.Name ?? serializerMapping?.Name;
                var property          = new JsonProperty(nameOverride);

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

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

                if (propertyMapping != null || serializerMapping != null)
                {
                    property.AllowPrivate = true;
                }

                if (member.GetCustomAttribute <StringEnumAttribute>() != null)
                {
                    CreateEnumFormatterForProperty(member, property);
                }
                else if (member.GetCustomAttribute <StringTimeSpanAttribute>() != null)
                {
                    switch (member)
                    {
                    case PropertyInfo propertyInfo:
                        property.JsonFormatter =
                            BuiltinResolver.BuiltinResolverGetFormatterHelper.GetFormatter(propertyInfo.PropertyType);
                        break;

                    case FieldInfo fieldInfo:
                        property.JsonFormatter =
                            BuiltinResolver.BuiltinResolverGetFormatterHelper.GetFormatter(fieldInfo.FieldType);
                        break;
                    }
                }
                else if (member.GetCustomAttribute <MachineLearningDateTimeAttribute>() != null)
                {
                    property.JsonFormatter = MachineLearningDateTimeFormatter.Instance;
                }

                return(property);
            }