Ejemplo n.º 1
0
        private static dynamic GetConcreteTypeUsingSelector(
            ConcreteTypeConverter realConcreteConverter,
            JObject jObject)
        {
            var baseType = realConcreteConverter._baseType;
            var selector = realConcreteConverter._concreteTypeSelector;

            Hit <dynamic> hitDynamic = new Hit <dynamic>();
            dynamic       d          = jObject;

            //favor manual mapping over doing Populate twice.
            hitDynamic.Fields      = d.fields;
            hitDynamic.Source      = d._source;
            hitDynamic.Index       = d._index;
            hitDynamic.Score       = (d._score is double) ? d._score : default(double);
            hitDynamic.Type        = d._type;
            hitDynamic.Version     = d._version;
            hitDynamic.Id          = d._id;
            hitDynamic.Sorts       = d.sort;
            hitDynamic.Highlight   = d.highlight is Dictionary <string, List <string> >?d.highlight : null;
            hitDynamic.Explanation = d._explanation is Explanation ? d._explanation : null;

            var concreteType = selector(hitDynamic.Source, hitDynamic);

            return(concreteType);
        }
Ejemplo n.º 2
0
        private static void AppendPartialFields(JsonSerializer serializer, 
			ConcreteTypeConverter realConcreteConverter,
		    Type concreteType, dynamic hit, JObject jObject)
        {
            if (realConcreteConverter == null)
                return;
            dynamic d = jObject;
            var partialFields = realConcreteConverter._partialFields;
            if (partialFields.Any())
            {
                var itemType = typeof (CovariantItem<>).MakeGenericType(concreteType);
                var listType = typeof (List<>).MakeGenericType(itemType);
                var dictType = typeof (CovariantDictionary<>).MakeGenericType(concreteType);

                dynamic items = Activator.CreateInstance(listType);
                foreach (var pf in partialFields)
                {
                    dynamic partial = Activator.CreateInstance(concreteType);

                    serializer.Populate(d.fields[pf].CreateReader(), partial);

                    dynamic dictItem = Activator.CreateInstance(itemType);
                    dictItem.Key = pf;
                    dictItem.Value = partial;
                    items.Add(dictItem);
                }

                dynamic dict = Activator.CreateInstance(dictType);
                dict.Items = items;
                hit.PartialFields = dict;
            }
        }
Ejemplo n.º 3
0
        private static void AppendPartialFields(JsonSerializer serializer,
                                                ConcreteTypeConverter realConcreteConverter,
                                                Type concreteType, dynamic hit, JObject jObject)
        {
            if (realConcreteConverter == null)
            {
                return;
            }
            dynamic d             = jObject;
            var     partialFields = realConcreteConverter._partialFields;

            if (partialFields.Any())
            {
                var itemType = typeof(CovariantItem <>).MakeGenericType(concreteType);
                var listType = typeof(List <>).MakeGenericType(itemType);
                var dictType = typeof(CovariantDictionary <>).MakeGenericType(concreteType);

                dynamic items = Activator.CreateInstance(listType);
                foreach (var pf in partialFields)
                {
                    dynamic partial = Activator.CreateInstance(concreteType);

                    serializer.Populate(d.fields[pf].CreateReader(), partial);

                    dynamic dictItem = Activator.CreateInstance(itemType);
                    dictItem.Key   = pf;
                    dictItem.Value = partial;
                    items.Add(dictItem);
                }

                dynamic dict = Activator.CreateInstance(dictType);
                dict.Items        = items;
                hit.PartialFields = dict;
            }
        }
Ejemplo n.º 4
0
        private static dynamic GetConcreteTypeUsingSelector(
            JsonSerializer serializer,
            ConcreteTypeConverter <T> realConcreteConverter,
            JObject jObject)
        {
            var elasticContractResolver = serializer.ContractResolver as ElasticContractResolver;
            var baseType = realConcreteConverter._baseType;
            var selector = realConcreteConverter._concreteTypeSelector;

            Hit <dynamic> hitDynamic     = new Hit <dynamic>();
            dynamic       d              = jObject;
            var           fields         = jObject["fields"];
            var           fieldSelection = fields != null?fields.ToObject <IDictionary <string, object> >() : null;

            //favor manual mapping over doing Populate twice.
            hitDynamic._fields     = fieldSelection;
            hitDynamic.Fields      = new FieldSelection <dynamic>(elasticContractResolver.Infer, fieldSelection);
            hitDynamic.Source      = d._source;
            hitDynamic.Index       = d._index;
            hitDynamic.Score       = (d._score is double) ? d._score : default(double);
            hitDynamic.Type        = d._type;
            hitDynamic.Version     = d._version;
            hitDynamic.Id          = d._id;
            hitDynamic.Sorts       = d.sort;
            hitDynamic._Highlight  = d.highlight is Dictionary <string, List <string> >?d.highlight : null;
            hitDynamic.Explanation = d._explanation is Explanation ? d._explanation : null;
            object o            = hitDynamic.Source ?? ElasticsearchResponse.Create(fieldSelection) ?? new object {};
            var    concreteType = selector(o, hitDynamic);

            return(concreteType);
        }
Ejemplo n.º 5
0
        private static object GetUsingConcreteTypeConverter(JsonReader reader, JsonSerializer serializer,
                                                            ConcreteTypeConverter realConcreteConverter)
        {
            var jObject      = CreateIntermediateJObject(reader);
            var concreteType = GetConcreteTypeUsingSelector(realConcreteConverter, jObject);
            var hit          = GetHitTypeInstance(concreteType);

            PopulateHit(serializer, jObject.CreateReader(), hit);

            AppendPartialFields(serializer, realConcreteConverter, concreteType, hit, jObject);

            return(hit);
        }
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var realConverter = serializer.GetStatefulConverter <ConcreteTypeConverter <TDocument> >();

            if (realConverter != null)
            {
                return(ConcreteTypeConverter.GetUsingConcreteTypeConverter <TDocument>(reader, serializer, realConverter));
            }

            var instance = (Hit <TDocument>)(typeof(Hit <TDocument>).CreateInstance());

            serializer.Populate(reader, instance);
            instance.Fields = new FieldValues(serializer.GetConnectionSettings().Inferrer, instance.Fields);
            return(instance);
        }
Ejemplo n.º 7
0
        private static dynamic GetConcreteTypeUsingSelector(
            JsonSerializer serializer,
            ConcreteTypeConverter <T> realConcreteConverter,
            JObject jObject, out object selection)
        {
            var elasticContractResolver = serializer.ContractResolver as ElasticContractResolver;
            var baseType = realConcreteConverter._baseType;
            var selector = realConcreteConverter._concreteTypeSelector;

            //Hit<dynamic> hitDynamic = new Hit<dynamic>();
            dynamic d                  = jObject;
            var     fields             = jObject["fields"];
            var     fieldSelectionData = fields != null?fields.ToObject <IDictionary <string, object> >() : null;

            var sel        = new FieldSelection <T>(elasticContractResolver.ConnectionSettings, fieldSelectionData);
            var hitDynamic = new Hit <dynamic>();

            //favor manual mapping over doing Populate twice.
            hitDynamic._fields     = fieldSelectionData;
            hitDynamic.Fields      = sel;
            hitDynamic.Source      = d._source;
            hitDynamic.Index       = d._index;
            hitDynamic.Score       = (d._score is double) ? d._score : default(double);
            hitDynamic.Type        = d._type;
            hitDynamic.Version     = d._version;
            hitDynamic.Id          = d._id;
            hitDynamic.Sorts       = d.sort;
            hitDynamic._Highlight  = d.highlight is Dictionary <string, List <string> >?d.highlight : null;
            hitDynamic.Explanation = d._explanation is Explanation ? d._explanation : null;
            object o            = d._source ?? DynamicDictionary.Create(fieldSelectionData) ?? new object {};
            var    concreteType = selector(o, hitDynamic);

            Type fieldSelectionType;

            if (!TypeToFieldTypes.TryGetValue(concreteType, out fieldSelectionType))
            {
                fieldSelectionType = typeof(FieldSelection <>).MakeGenericType(concreteType);
                TypeToFieldTypes.TryAdd(concreteType, fieldSelectionType);
            }
            selection = fieldSelectionType.CreateInstance(elasticContractResolver.ConnectionSettings, fieldSelectionData);
            return(concreteType);
        }
Ejemplo n.º 8
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var elasticContractResolver = serializer.ContractResolver as ElasticContractResolver;

            if (elasticContractResolver != null && elasticContractResolver.PiggyBackState != null &&
                elasticContractResolver.PiggyBackState.ActualJsonConverter != null)
            {
                var realConcreteConverter = elasticContractResolver.PiggyBackState.ActualJsonConverter as ConcreteTypeConverter <T>;
                if (realConcreteConverter != null)
                {
                    return(ConcreteTypeConverter.GetUsingConcreteTypeConverter <T>(reader, serializer, realConcreteConverter));
                }
            }

            var instance = (Hit <T>)(typeof(Hit <T>).CreateInstance());

            serializer.Populate(reader, instance);
            instance.Fields = new FieldSelection <T>(elasticContractResolver.ConnectionSettings, instance._fields);
            return(instance);
        }
Ejemplo n.º 9
0
        private static object GetUsingConcreteTypeConverter(JsonReader reader, JsonSerializer serializer,
		                                                    ConcreteTypeConverter realConcreteConverter)
        {
            var jObject = CreateIntermediateJObject(reader);
            var concreteType = GetConcreteTypeUsingSelector(realConcreteConverter, jObject);
            var hit = GetHitTypeInstance(concreteType);
            PopulateHit(serializer, jObject.CreateReader(), hit);

            AppendPartialFields(serializer, realConcreteConverter, concreteType, hit, jObject);

            return hit;
        }
Ejemplo n.º 10
0
        private static dynamic GetConcreteTypeUsingSelector(
			ConcreteTypeConverter realConcreteConverter,
			JObject jObject)
        {
            var baseType = realConcreteConverter._baseType;
            var selector = realConcreteConverter._concreteTypeSelector;

            Hit<dynamic> hitDynamic = new Hit<dynamic>();
            dynamic d = jObject;

            //favor manual mapping over doing Populate twice.
            hitDynamic.Fields = d.fields;
            hitDynamic.Source = d._source;
            hitDynamic.Index = d._index;
            hitDynamic.Score = (d._score is double) ? d._score : default(double);
            hitDynamic.Type = d._type;
            hitDynamic.Version = d._version;
            hitDynamic.Id = d._id;
            hitDynamic.Sorts = d.sort;
            hitDynamic._Highlight = d.highlight is Dictionary<string, List<string>> ? d.highlight : null;
            hitDynamic.Explanation = d._explanation is Explanation ? d._explanation : null;

            var concreteType = selector(hitDynamic.Source, hitDynamic);
            return concreteType;
        }
Ejemplo n.º 11
0
        private static object GetUsingConcreteTypeConverter(JsonReader reader, JsonSerializer serializer, ConcreteTypeConverter <T> realConcreteConverter)
        {
            var    jObject = CreateIntermediateJObject(reader);
            object fieldSelection;
            var    concreteType = GetConcreteTypeUsingSelector(serializer, realConcreteConverter, jObject, out fieldSelection);
            var    hit          = GetHitTypeInstance(concreteType);

            PopulateHit(serializer, jObject.CreateReader(), hit);

            Action <object, object> cachedLookup;

            if (FieldDelegates.TryGetValue(concreteType, out cachedLookup))
            {
                cachedLookup(hit, fieldSelection);
                return(hit);
            }

            var generic = MakeDelegateMethodInfo.MakeGenericMethod(concreteType);

            cachedLookup = (h, f) => generic.Invoke(null, new[] { h, f });
            cachedLookup(hit, fieldSelection);
            FieldDelegates.TryAdd(concreteType, cachedLookup);
            return(hit);
        }