Example #1
0
        protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
        {
            var property = base.CreateProperty(member, memberSerialization);

            // Skip serialization of empty collections that has DefaultValueHandling set to Ignore.
            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;
                    return(collection == null || collection.Count != 0);
                };
                property.ShouldSerialize = property.ShouldSerialize == null ? shouldSerialize : (o => property.ShouldSerialize(o) && shouldSerialize(o));
            }

            var att = ElasticAttributes.Property(member, this.ConnectionSettings);

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

            property.Ignored = att.OptOut;
            return(property);
        }
Example #2
0
        public string GetTypeNameFor(Type type)
        {
            if (type == null)
            {
                return(null);
            }
            string typeName;

            if (_connectionSettings.DefaultTypeNames.TryGetValue(type, out typeName))
            {
                return(typeName);
            }

            var att = ElasticAttributes.Type(type);

            if (att != null && !att.Name.IsNullOrEmpty())
            {
                typeName = att.Name;
            }
            else
            {
                typeName = _connectionSettings.DefaultTypeNameInferrer(type);
            }
            return(typeName);
        }
Example #3
0
        public string Resolve(MemberInfo info)
        {
            if (info == null)
            {
                return(null);
            }

            var name = info.Name;

            var att = ElasticAttributes.Property(info, _settings);

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

            return(_settings.DefaultPropertyNameInferrer(name));
        }
Example #4
0
        public string Resolve(MemberInfo info)
        {
            if (info == null)
            {
                return(null);
            }

            var name         = info.Name;
            var resolvedName = name.ToCamelCase();
            var att          = ElasticAttributes.Property(info);

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

            return(resolvedName);
        }
Example #5
0
        protected override Expression VisitMemberAccess(MemberExpression expression, Stack <string> stack, Stack <IElasticPropertyAttribute> properties)
        {
            if (stack != null)
            {
                var name         = expression.Member.Name;
                var resolvedName = this._settings.DefaultPropertyNameInferrer(name);

                var att = ElasticAttributes.Property(expression.Member);
                if (att != null)
                {
                    properties.Push(att);
                }
                if (att != null && !att.Name.IsNullOrEmpty())
                {
                    resolvedName = att.Name;
                }
                stack.Push(resolvedName);
            }
            return(base.VisitMemberAccess(expression, stack, properties));
        }
Example #6
0
        private PropertyInfo GetInferredId(Type type)
        {
            // if the type specifies through ElasticAttribute what the id prop is
            // use that no matter what
            var esTypeAtt = ElasticAttributes.Type(type);

            if (esTypeAtt != null && !string.IsNullOrWhiteSpace(esTypeAtt.IdProperty))
            {
                return(GetPropertyCaseInsensitive(type, esTypeAtt.IdProperty));
            }

            var propertyName = "Id";

            //Try Id on its own case insensitive
            var idProperty = GetPropertyCaseInsensitive(type, propertyName);

            if (idProperty != null)
            {
                return(idProperty);
            }

            //Try TypeNameId case insensitive
            idProperty = GetPropertyCaseInsensitive(type, type.Name + propertyName);
            if (idProperty != null)
            {
                return(idProperty);
            }

            //Try TypeName_Id case insensitive
            idProperty = GetPropertyCaseInsensitive(type, type.Name + "_" + propertyName);
            if (idProperty != null)
            {
                return(idProperty);
            }

            return(idProperty);
        }