protected override List <MemberInfo> GetSerializableMembers(Type objectType)
        {
            var source = new List <MemberInfo>();
            var bti    = BYteWareTypeInfo.GetBYteWareTypeInfo(objectType);

            foreach (var p in bti.GetTopPropertyInfos)
            {
                var props = bti.ESProperties(p.Name);
                var type  = ElasticSearchClient.GetElasticSearchType(props, p.PropertyType);
                if (props != null && !props.OptOut && type != null)
                {
                    source.Add(p);
                }
            }
            return(source);
        }
예제 #2
0
        internal void WriteProperties(JsonWriter jsonWriter)
        {
            foreach (var p in byteWareTypeInfo.GetTopPropertyInfos)
            {
                var props        = byteWareTypeInfo.ESProperties(p.Name);
                var type         = ElasticSearchClient.GetElasticSearchType(props, p.PropertyType);
                var propertyName = ElasticSearchClient.FieldName(string.IsNullOrEmpty(props?.FieldName) ? p.Name : props.FieldName);
                if ((props != null && props.OptOut) || (props == null && OptOut) || (type == null))
                {
                    continue;
                }

                jsonWriter.WritePropertyName(propertyName);
                jsonWriter.WriteStartObject();
                {
                    if (props == null)
                    {
                        // properties that follow can not be inferred from the CLR.
#pragma warning disable CC0021 // Use nameof
                        jsonWriter.WritePropertyName("type");
#pragma warning restore CC0021 // Use nameof
                        jsonWriter.WriteValue(type);
                    }
                    else
                    {
                        WriteProperties(jsonWriter, byteWareTypeInfo, p, props, propertyName, type);
                        var multiFields = BYteWareTypeInfo.Model != null ? (props as IModelMemberElasticSearchField)?.Fields : Attribute.GetCustomAttributes(p, typeof(ElasticMultiFieldAttribute), true).OfType <IElasticSearchFieldProperties>();
                        if (multiFields != null && multiFields.Any())
                        {
                            jsonWriter.WritePropertyName("fields");
                            jsonWriter.WriteStartObject();
                            foreach (var ga in multiFields.GroupBy(t => t.FieldName))
                            {
                                var a         = ga.First();
                                var fieldName = ElasticSearchClient.FieldName(ga.Key);
                                jsonWriter.WritePropertyName(fieldName);
                                jsonWriter.WriteStartObject();
                                WriteProperties(jsonWriter, byteWareTypeInfo, p, a, string.Format(CultureInfo.InvariantCulture, "{0}.{1}", propertyName, fieldName), ElasticSearchClient.GetElasticSearchType(a, p.PropertyType));
                                jsonWriter.WriteEndObject();
                            }
                            jsonWriter.WriteEndObject();
                        }
                    }
                    if (type == "object" || type == "nested")
                    {
                        var deepType  = p.PropertyType;
                        var dbti      = BYteWareTypeInfo.GetBYteWareTypeInfo(BYteWareTypeInfo.GetUnderlyingType(deepType));
                        var seenTypes = new ConcurrentDictionary <Type, int>(SeenTypes);
                        seenTypes.AddOrUpdate(deepType, 0, (t, i) => ++ i);

                        var newTypeMappingWriter = new TypeMappingWriter(elasticSearchClient, dbti, MaxRecursion, OptOut, baseBWTypeInfo, seenTypes);
                        var nestedProperties     = newTypeMappingWriter.MapProperties();

                        jsonWriter.WritePropertyName("properties");
                        nestedProperties.WriteTo(jsonWriter);
                        foreach (var pathField in byteWareTypeInfo.ESSuggestContextPathFields.Where(pi => dbti.ClassInfo.Members.Contains(pi.MemberInfo)))
                        {
                            WritePathField(jsonWriter, pathField);
                        }
                    }
                }
                jsonWriter.WriteEnd();
            }
        }