public void PASS_Create() { DefaultMapping prop = new DefaultMapping() { Analyzer = new PropertyAnalyzer(new StandardAnalyzer("standard")), CopyTo = new List<string>() { "field1" }, DetectDates = false, DetectNumbers = true, Dynamic = DynamicSettingEnum.Strict, DateFormats = new List<DateFormat>() { new DateFormat("format1"), new DateFormat("format2") }, DynamicTemplates = new List<DynamicTemplate>() { new DynamicTemplate("template1", new StringProperty("string-prop")) } }; Assert.IsNotNull(prop); Assert.AreEqual("standard", prop.Analyzer.Analyzer.Name); Assert.AreEqual("field1", prop.CopyTo.First()); Assert.AreEqual(false, prop.DetectDates); Assert.AreEqual(true, prop.DetectNumbers); Assert.AreEqual(DynamicSettingEnum.Strict, prop.Dynamic); Assert.AreEqual("format1", prop.DateFormats.First().Format); Assert.AreEqual("format2", prop.DateFormats.Last().Format); DynamicTemplate template = prop.DynamicTemplates.First(); Assert.AreEqual("template1", template.Name); Assert.AreEqual("string-prop", template.Mapping.Name); Assert.AreEqual(PropertyTypeEnum.String, template.Mapping.PropertyType); }
public void PASS_Serialize() { DefaultMapping prop = new DefaultMapping() { Analyzer = new PropertyAnalyzer(new StandardAnalyzer("standard")), CopyTo = new List<string>() { "field1" }, DetectDates = false, DetectNumbers = true, Dynamic = DynamicSettingEnum.Strict, DateFormats = new List<DateFormat>() { new DateFormat("format1"), new DateFormat("format2") }, DynamicTemplates = new List<DynamicTemplate>() { new DynamicTemplate("template1", new StringProperty("string-prop")) } }; string json = JsonConvert.SerializeObject(prop); Assert.IsNotNull(json); string expectedJson = "{\"_default_\":{\"dynamic\":\"strict\",\"copy_to\":\"field1\",\"analyzer\":\"standard\",\"date_detection\":false,\"numeric_detection\":true,\"date_formats\":[\"format1\",\"format2\"],\"dynamic_templates\":[{\"template1\":{\"match\":\"*\",\"mapping\":{\"type\":\"string\"}}}]}}"; Assert.AreEqual(expectedJson, json); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { Dictionary<string, object> propDict = serializer.Deserialize<Dictionary<string, object>>(reader); Dictionary<string, object> fieldDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(propDict.First().Value.ToString()); DefaultMapping defaultMapping = new DefaultMapping(); if (fieldDict.ContainsKey(_COPY_TO)) { try { defaultMapping.CopyTo = JsonConvert.DeserializeObject<IEnumerable<string>>(fieldDict.GetString(_COPY_TO)); } catch { defaultMapping.CopyTo = new List<string>() { fieldDict.GetString(_COPY_TO) }; } } defaultMapping.Dynamic = DynamicSettingEnum.Find(fieldDict.GetString(_DYNAMIC, DefaultMapping._DYNAMIC_DEFAULT.ToString())); defaultMapping.IncludeInAll = fieldDict.GetBool(_INCLUDE_IN_ALL, DefaultMapping._INCLUDE_IN_ALL_DEFAULT); defaultMapping.IsEnabled = fieldDict.GetBool(_IS_ENABLED, DefaultMapping._IS_ENABLED_DEFAULT); defaultMapping.Analyzer = PropertyAnalyzer.Deserialize(fieldDict); defaultMapping.DetectDates = fieldDict.GetBool(_DETECT_DATES, TypeMapping._DETECT_DATES_DEFAULT); defaultMapping.DetectNumbers = fieldDict.GetBool(_DETECT_NUMBERS, TypeMapping._DETECT_NUMBERS_DEFAULT); if (fieldDict.ContainsKey(_DATE_FORMATS)) { defaultMapping.DateFormats = JsonConvert.DeserializeObject<IEnumerable<DateFormat>>(fieldDict.GetString(_DATE_FORMATS)); } if (fieldDict.ContainsKey(_DYNAMIC_TEMPLATES)) { defaultMapping.DynamicTemplates = JsonConvert.DeserializeObject<IEnumerable<DynamicTemplate>>(fieldDict.GetString(_DYNAMIC_TEMPLATES)); } return defaultMapping; }