Ejemplo n.º 1
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            JObject o      = JObject.Load(reader);
            var     result = existingValue as Dictionary <string, AnalyzerBase> ?? new Dictionary <string, AnalyzerBase>();

            foreach (var childProperty in o.Children <JProperty>())
            {
                var propertyName = childProperty.Name;
                var typeProperty = ((JObject)childProperty.Value).Property("type");
                typeProperty.Remove();

                var      typePropertyValue = typeProperty.Value.ToString();
                Language language;
                if (Enum.TryParse(typePropertyValue, true, out language))
                {
                    typePropertyValue = "Language";
                }

                var          itemType = Type.GetType("Nest." + typePropertyValue + "Analyzer", false, true);
                AnalyzerBase item;
                if (itemType == typeof(LanguageAnalyzer))
                {
                    item = new LanguageAnalyzer(language);
                    serializer.Populate(childProperty.Value.CreateReader(), item);
                }
                else if (itemType != null)
                {
                    item = serializer.Deserialize(childProperty.Value.CreateReader(), itemType) as AnalyzerBase;
                }
                else
                {
                    continue;
                }

                result[propertyName] = item;
            }
            return(result);
        }
		public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
		{
			JObject o = JObject.Load(reader);
			var result = existingValue as Dictionary<string, AnalyzerBase> ?? new Dictionary<string, AnalyzerBase>();

			foreach (var childProperty in o.Children<JProperty>())
			{
				var propertyName = childProperty.Name;
				var typeProperty = ((JObject)childProperty.Value).Property("type");
				typeProperty.Remove();

				var typePropertyValue = typeProperty.Value.ToString();
				Language language;
				if (Enum.TryParse(typePropertyValue, true, out language))
				{
					typePropertyValue = "Language";
				}

				var itemType = Type.GetType("Nest." + typePropertyValue + "Analyzer", false, true);
				AnalyzerBase item;
				if (itemType == typeof(LanguageAnalyzer))
				{
					item = new LanguageAnalyzer(language);
					serializer.Populate(childProperty.Value.CreateReader(), item);
				}
				else if (itemType != null)
				{
					item = serializer.Deserialize(childProperty.Value.CreateReader(), itemType) as AnalyzerBase;
				}
				else
				{
					continue;
				}

				result[propertyName] = item;
			}
			return result;
		}