Ejemplo n.º 1
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            Dictionary<string, object> fieldDict = serializer.Deserialize<Dictionary<string, object>>(reader);
            if (fieldDict.ContainsKey(SearchPieceTypeEnum.Highlighter.ToString()))
                fieldDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(fieldDict.First().Value.ToString());

            List<FieldHighlighter> fieldHighlighters = new List<FieldHighlighter>();
            foreach (KeyValuePair<string, object> fieldKvp in fieldDict.Where(x => !_KnownFields.Contains(x.Key, StringComparer.OrdinalIgnoreCase)))
            {
                Dictionary<string, object> internalDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(fieldKvp.Value.ToString());
                FieldHighlighter fieldHighlighter = new FieldHighlighter(fieldKvp.Key);
                DeserializeHighlighterOptions(fieldHighlighter, internalDict);

                if (internalDict.ContainsKey(_HIGHLIGHT_QUERY))
                    fieldHighlighter.HighlightQuery = JsonConvert.DeserializeObject<IQuery>(internalDict.GetString(_HIGHLIGHT_QUERY));
                if (internalDict.ContainsKey(_MATCHED_FIELDS))
                    fieldHighlighter.MatchedFields = JsonConvert.DeserializeObject<IEnumerable<string>>(internalDict.GetString(_MATCHED_FIELDS));

                fieldHighlighters.Add(fieldHighlighter);
            }

            if (!fieldHighlighters.Any())
                throw new RequiredPropertyMissingException("highlight fields");

            Highlighter highlighter = new Highlighter(fieldHighlighters);
            DeserializeHighlighterOptions(highlighter, fieldDict);

            return highlighter;
        }
Ejemplo n.º 2
0
        public void PASS_Create()
        {
            Highlighter highlighter = new Highlighter(new List<FieldHighlighter>()
                                                        {
                                                            new FieldHighlighter("field")
                                                        });

            Assert.IsNotNull(highlighter);
            Assert.AreEqual("field", highlighter.FieldHighlighters.First().Field);
        }
Ejemplo n.º 3
0
 public void PASS_Serializer()
 {
     Highlighter highlighter = new Highlighter(new List<FieldHighlighter>()
                                                 {
                                                     new FieldHighlighter("field")
                                                 });
     string json = JsonConvert.SerializeObject(highlighter);
     Assert.IsNotNull(json);
     string expectedJson = "{\"field\":{}}";
     Assert.AreEqual(expectedJson, json);
 }
Ejemplo n.º 4
0
 public void FAIL_Create_Fields()
 {
     try
     {
         Highlighter highlighter = new Highlighter(null);
         Assert.Fail();
     }
     catch (ArgumentNullException ex)
     {
         Assert.AreEqual("fieldHighlighters", ex.ParamName);
     }
 }