Пример #1
0
        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());

            ObjectProperty objProp = new ObjectProperty(propDict.First().Key);
            ObjectProperty.Deserialize(objProp, fieldDict);

            TypeMapping prop = new TypeMapping(objProp);
            prop.Fields = DocumentMapping.Deserialize(fieldDict);
            prop.Analyzer = PropertyAnalyzer.Deserialize(fieldDict);
            prop.DetectDates = fieldDict.GetBool(TypeMapping.DETECT_DATES, TypeMapping._DETECT_DATES_DEFAULT);
            prop.DetectNumbers = fieldDict.GetBool(TypeMapping.DETECT_NUMBERS, TypeMapping._DETECT_NUMBERS_DEFAULT);
            if (fieldDict.ContainsKey(TypeMapping.DYNAMIC_DATE_FORMATS))
            {
                prop.DynamicDateFormats = JsonConvert.DeserializeObject<IEnumerable<DateFormat>>(fieldDict.GetString(TypeMapping.DYNAMIC_DATE_FORMATS));
            }
            if (fieldDict.ContainsKey(TypeMapping.DYNAMIC_TEMPLATES))
            {
                prop.DynamicTemplates = JsonConvert.DeserializeObject<IEnumerable<DynamicTemplate>>(fieldDict.GetString(TypeMapping.DYNAMIC_TEMPLATES));
            }
            if (fieldDict.ContainsKey(TypeMapping.META))
            {
                prop.MetaData = JsonConvert.DeserializeObject<Dictionary<string, object>>(fieldDict.GetString(TypeMapping.META));
            }
            return prop;
        }
        public void PASS_Serialize()
        {
            TypeMapping prop = new TypeMapping("entity")
            {
                Analyzer = new PropertyAnalyzer(new StandardAnalyzer("standard")),
                DetectDates = false,
                DetectNumbers = true,
                Dynamic = DynamicSettingEnum.Strict,
                DynamicDateFormats = new List<DateFormat>() { new DateFormat("format1"), new DateFormat("format2") },
                DynamicTemplates = new List<DynamicTemplate>()
                {
                    new DynamicTemplate("template1", new StringProperty("string-prop"))
                },
                Properties = new List<IDocumentProperty>()
                {
                    new StringProperty("name")
                }
            };

            IndexMapping indexMapping = new IndexMapping("1234index", new List<TypeMapping>() { prop });

            string json = JsonConvert.SerializeObject(indexMapping);
            Assert.IsNotNull(json);

            string expectedJson = "{\"1234index\":{\"entity\":{\"analyzer\":\"standard\",\"date_detection\":false,\"numeric_detection\":true,\"dynamic_date_formats\":[\"format1\",\"format2\"],\"dynamic_templates\":[{\"template1\":{\"match\":\"*\",\"mapping\":{\"type\":\"string\"}}}],\"dynamic\":\"strict\",\"properties\":{\"name\":{\"type\":\"string\"}}}}}";

            Assert.AreEqual(expectedJson, json);
        }
        public void PASS_Create()
        {
            TypeMapping prop = new TypeMapping("entity")
            {
                Analyzer = new PropertyAnalyzer(new StandardAnalyzer("standard")),
                DetectDates = false,
                DetectNumbers = true,
                Dynamic = DynamicSettingEnum.Strict,
                DynamicDateFormats = new List<DateFormat>() { new DateFormat("format1"), new DateFormat("format2") },
                DynamicTemplates = new List<DynamicTemplate>()
                {
                    new DynamicTemplate("template1", new StringProperty("string-prop"))
                },
                Properties = new List<IDocumentProperty>()
                {
                    new StringProperty("name")
                }
            };

            Assert.IsNotNull(prop);
            Assert.AreEqual("standard", prop.Analyzer.Analyzer.Name);
            Assert.AreEqual(false, prop.DetectDates);
            Assert.AreEqual(true, prop.DetectNumbers);
            Assert.AreEqual(DynamicSettingEnum.Strict, prop.Dynamic);
            Assert.AreEqual("format1", prop.DynamicDateFormats.First().Format);
            Assert.AreEqual("format2", prop.DynamicDateFormats.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);
            Assert.AreEqual("name", prop.Properties.First().Name);
            Assert.AreEqual(PropertyTypeEnum.String, prop.Properties.First().PropertyType);
        }