/// <summary>
        /// internal constructor by TypeMappingWriter itself when it recurses, passes seenTypes as safeguard agains maxRecursion
        /// </summary>
        internal TypeMappingWriter(Type t, string typeName, IConnectionSettings connectionSettings, int maxRecursion, ConcurrentDictionary <Type, int> seenTypes)
        {
            this._type = GetUnderlyingType(t);
            this._connectionSettings = connectionSettings;

            this.TypeName     = typeName;
            this.MaxRecursion = maxRecursion;
            this.SeenTypes    = seenTypes;

            this._elasticSerializer = new ElasticSerializer(this._connectionSettings);
            this.Infer = new ElasticInferrer(this._connectionSettings);
        }
Пример #2
0
        /// <summary>
        /// internal constructor by TypeMappingWriter itself when it recurses, passes seenTypes as safeguard agains maxRecursion
        /// </summary>
        internal TypeMappingWriter(Type t, string typeName, IConnectionSettings connectionSettings, int maxRecursion, ConcurrentDictionary<Type, int> seenTypes)
        {
            this._type = GetUnderlyingType(t);
            this._connectionSettings = connectionSettings;

            this.TypeName = typeName;
            this.MaxRecursion = maxRecursion;
            this.SeenTypes = seenTypes;

            this._elasticSerializer = new ElasticSerializer(this._connectionSettings);
            this.Infer = new ElasticInferrer(this._connectionSettings);
        }
        public TypeMappingWriter(Type t, TypeNameMarker typeName, IConnectionSettings connectionSettings, int maxRecursion)
        {
            this._type = t;
            this._connectionSettings = connectionSettings;

            this.TypeName     = typeName;
            this.MaxRecursion = maxRecursion;

            this.SeenTypes = new ConcurrentDictionary <Type, int>();
            this.SeenTypes.TryAdd(t, 0);

            this._elasticSerializer = new ElasticSerializer(this._connectionSettings);
            this.Infer = new ElasticInferrer(this._connectionSettings);
        }
Пример #4
0
        public TypeMappingWriter(Type t, TypeNameMarker typeName, IConnectionSettings connectionSettings, int maxRecursion)
        {
            this._type = t;
            this._connectionSettings = connectionSettings;

            this.TypeName = typeName;
            this.MaxRecursion = maxRecursion;

            this.SeenTypes = new ConcurrentDictionary<Type, int>();
            this.SeenTypes.TryAdd(t, 0);

            this._elasticSerializer = new ElasticSerializer(this._connectionSettings);
            this.Infer = new ElasticInferrer(this._connectionSettings);
        }
Пример #5
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (this._settings == null)
            {
                var elasticContractResolver = serializer.ContractResolver as ElasticContractResolver;
                if (elasticContractResolver == null)
                {
                    return new MultiSearchResponse {
                               IsValid = false
                    }
                }
                ;
                var piggyBackState = elasticContractResolver.PiggyBackState;
                if (piggyBackState == null || piggyBackState.ActualJsonConverter == null)
                {
                    return new MultiSearchResponse {
                               IsValid = false
                    }
                }
                ;

                var realConverter = piggyBackState.ActualJsonConverter as MultiSearchConverter;
                if (realConverter == null)
                {
                    return new MultiSearchResponse {
                               IsValid = false
                    }
                }
                ;

                return(realConverter.ReadJson(reader, objectType, existingValue, serializer));
            }


            var response   = new MultiSearchResponse();
            var jsonObject = JObject.Load(reader);

            var docsJarray = (JArray)jsonObject["responses"];

            if (docsJarray == null)
            {
                return(response);
            }
            var multiSearchDescriptor = this._descriptor;

            if (this._descriptor == null)
            {
                return(multiSearchDescriptor);
            }

            var withMeta = docsJarray.Zip(this._descriptor._Operations, (doc, desc) => new MultiHitTuple {
                Hit = doc, Descriptor = desc
            });
            var originalResolver = serializer.ContractResolver;

            foreach (var m in withMeta)
            {
                var descriptor           = m.Descriptor.Value;
                var concreteTypeSelector = descriptor._ConcreteTypeSelector;
                var baseType             = m.Descriptor.Value._ClrType;
                var types = m.Descriptor.Value._Types.EmptyIfNull().ToList();

                //if we dont already have a concrete type converter but we have selected more types then
                //just the base return type automagically create our own concrete type converter
                if (concreteTypeSelector == null &&
                    types.HasAny() &&
                    types.Count() > types.Count(x => x.Type == baseType))
                {
                    var typeDict = types.ToDictionary(t => t.Resolve(this._settings), t => t.Type);

                    concreteTypeSelector = (o, h) =>
                    {
                        Type t;
                        if (!typeDict.TryGetValue(h.Type, out t))
                        {
                            return(baseType);
                        }
                        return(t);
                    };
                }
                var generic = MakeDelegateMethodInfo.MakeGenericMethod(baseType);

                if (concreteTypeSelector != null)
                {
                    var elasticSerializer = new ElasticSerializer(this._settings);
                    var state             = typeof(ConcreteTypeConverter <>).CreateGenericInstance(baseType, concreteTypeSelector) as JsonConverter;
                    if (state != null)
                    {
                        var settings = elasticSerializer.CreateSettings(piggyBackJsonConverter: state);

                        var jsonSerializer = new JsonSerializer()
                        {
                            NullValueHandling    = settings.NullValueHandling,
                            DefaultValueHandling = settings.DefaultValueHandling,
                            ContractResolver     = settings.ContractResolver,
                        };
                        foreach (var converter in settings.Converters.EmptyIfNull())
                        {
                            jsonSerializer.Converters.Add(converter);
                        }
                        generic.Invoke(null, new object[] { m, jsonSerializer, response._Responses, this._settings });
                        continue;
                    }
                }
                generic.Invoke(null, new object[] { m, serializer, response._Responses, this._settings });
            }

            return(response);
        }