public void PASS_Serialize() { PatternAnalyzer analyzer = new PatternAnalyzer("name", "pattern") { Version = 4.6, Lowercase = false, Flags = new List<RegexFlagEnum>() { RegexFlagEnum.CaseInsensitive, RegexFlagEnum.CannonEq }, Stopwords = new List<string>() { "stop1", "stop2" }, Aliases = new List<string>() { "alias1", "alias2" } }; string json = JsonConvert.SerializeObject(analyzer); Assert.IsNotNull(json); string expectedJson = "{\"name\":{\"type\":\"pattern\",\"version\":4.6,\"aliases\":[\"alias1\",\"alias2\"],\"lowercase\":false,\"pattern\":\"pattern\",\"flags\":\"CASE_INSENSITIVE|CANNON_EQ\",\"stopwords\":[\"stop1\",\"stop2\"]}}"; Assert.AreEqual(expectedJson, json); }
public void PASS_Create() { PatternAnalyzer analyzer = new PatternAnalyzer("name", "pattern") { Version = 4.6, Lowercase = false, Flags = new List<RegexFlagEnum>(){ RegexFlagEnum.CaseInsensitive, RegexFlagEnum.CannonEq }, Stopwords = new List<string>() { "stop1", "stop2" }, Aliases = new List<string>() { "alias1", "alias2" } }; Assert.IsNotNull(analyzer); Assert.AreEqual("name", analyzer.Name); Assert.AreEqual(4.6, analyzer.Version); Assert.AreEqual("pattern", analyzer.Pattern); Assert.AreEqual("stop1", analyzer.Stopwords.First()); Assert.AreEqual("stop2", analyzer.Stopwords.Last()); Assert.AreEqual("alias1", analyzer.Aliases.First()); Assert.AreEqual("alias2", analyzer.Aliases.Last()); Assert.AreEqual(RegexFlagEnum.CaseInsensitive, analyzer.Flags.First()); Assert.AreEqual(RegexFlagEnum.CannonEq, analyzer.Flags.Last()); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { Dictionary<string, object> analyzerDict = serializer.Deserialize<Dictionary<string, object>>(reader); Dictionary<string, object> fieldDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(analyzerDict.First().Value.ToString()); PatternAnalyzer analyzer = null; if (fieldDict.ContainsKey(_PATTERN)) { analyzer = new PatternAnalyzer(analyzerDict.First().Key, fieldDict.GetString(_PATTERN)); } else { analyzer = new PatternAnalyzer(analyzerDict.First().Key); } AnalyzerBase.Deserialize(analyzer, fieldDict); analyzer.Lowercase = fieldDict.GetBool(_LOWERCASE, PatternAnalyzer._LOWERCASE_DEFAULT); if (fieldDict.ContainsKey(_FLAGS)) { List<RegexFlagEnum> flagList = new List<RegexFlagEnum>(); string flagsStr = fieldDict.GetString(_FLAGS); foreach (string flagValue in flagsStr.Split(_FLAG_DELIMITERS.ToArray(), StringSplitOptions.RemoveEmptyEntries)) { flagList.Add(RegexFlagEnum.Find(flagValue)); } if (flagList.Any()) analyzer.Flags = flagList; } if (fieldDict.ContainsKey(_STOPWORDS)) { analyzer.Stopwords = JsonConvert.DeserializeObject<IEnumerable<string>>(fieldDict.GetString(_STOPWORDS)); } return analyzer; }