public void PASS_Serialize() { CommonGramsTokenFilter filter = new CommonGramsTokenFilter("name", new List<string>() { "word1", "word2" }) { IgnoreCase = true, QueryMode = true }; string json = JsonConvert.SerializeObject(filter); Assert.IsNotNull(json); string expectedJson = "{\"name\":{\"type\":\"common_grams\",\"ignore_case\":true,\"query_mode\":true,\"common_words\":[\"word1\",\"word2\"]}}"; Assert.AreEqual(expectedJson, json); }
public void PASS_Create() { CommonGramsTokenFilter filter = new CommonGramsTokenFilter("name", new List<string>() { "word1", "word2" }) { IgnoreCase = true, QueryMode = true }; Assert.IsNotNull(filter); Assert.AreEqual("name", filter.Name); Assert.AreEqual("word1", filter.CommonWords.First()); Assert.AreEqual("word2", filter.CommonWords.Last()); Assert.AreEqual(true, filter.IgnoreCase); Assert.AreEqual(true, filter.QueryMode); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { Dictionary<string, object> filterDict = serializer.Deserialize<Dictionary<string, object>>(reader); Dictionary<string, object> fieldDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(filterDict.First().Value.ToString()); string filterName = filterDict.First().Key; CommonGramsTokenFilter filter = null; if (fieldDict.ContainsKey(_COMMON_WORDS)) { filter = new CommonGramsTokenFilter(filterName, JsonConvert.DeserializeObject<IEnumerable<string>>(fieldDict.GetString(_COMMON_WORDS))); } else if (fieldDict.ContainsKey(_COMMON_WORDS_PATH)) { filter = new CommonGramsTokenFilter(filterName, fieldDict.GetString(_COMMON_WORDS_PATH)); } TokenFilterBase.Deserialize(filter, fieldDict); filter.IgnoreCase = fieldDict.GetBool(_IGNORE_CASE, CommonGramsTokenFilter._IGNORE_CASE_DEFAULT); filter.QueryMode = fieldDict.GetBool(_QUERY_MODE, CommonGramsTokenFilter._QUERY_MODE_DEFAULT); return filter; }