public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { Dictionary<string, object> facetsDict = serializer.Deserialize<Dictionary<string, object>>(reader); List<IFacet> facetGenerators = new List<IFacet>(); FacetTypeEnum facetType = FacetTypeEnum.DateHistogram; foreach (KeyValuePair<string, object> facetKvp in facetsDict) { // dig down to get the type i am dealing with Dictionary<string, object> facetDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(facetKvp.Value.ToString()); KeyValuePair<string, object> facetTypeKvp = facetDict.First(); // create a dictionary just for this one Dictionary<string, object> internalDict = new Dictionary<string, object>(); internalDict.Add(facetKvp.Key, facetKvp.Value); Dictionary<string, object> facetTypeDict = new Dictionary<string, object>(); facetTypeDict.Add("junk", internalDict); facetType = FacetTypeEnum.Find(facetTypeKvp.Key); if (facetType == null) throw new Exception(facetTypeKvp.Key + " is not a valid type of facet."); string facetTypeJsonStr = JsonConvert.SerializeObject(facetTypeDict.First().Value); facetGenerators.Add(JsonConvert.DeserializeObject(facetTypeJsonStr, facetType.ImplementationType) as IFacet); } Facets facets = new Facets(facetGenerators); return facets; }
public void PASS_Serialize() { Facets facets = new Facets( new List<IFacet>() { new FilterFacet("filter-name", new TermFilter("field", "value")), new QueryFacet("query-name", new TermQuery("field", "value")) }); string json = JsonConvert.SerializeObject(facets); Assert.IsNotNull(json); string expectedJson = "{\"filter-name\":{\"filter\":{\"term\":{\"field\":\"value\"}}},\"query-name\":{\"query\":{\"term\":{\"field\":\"value\"}}}}"; Assert.AreEqual(expectedJson, json); }
public void PASS_Create() { Facets facets = new Facets( new List<IFacet>() { new FilterFacet("filter-name", new TermFilter("field", "value")), new QueryFacet("query-name", new TermQuery("field", "value")) }); Assert.IsNotNull(facets); Assert.AreEqual("filter-name", facets.FacetGenerators.First().FacetName); Assert.AreEqual("query-name", facets.FacetGenerators.Last().FacetName); Assert.AreEqual("field", ((facets.FacetGenerators.First() as FilterFacet).Filter as TermFilter).Field); Assert.AreEqual("value", ((facets.FacetGenerators.First() as FilterFacet).Filter as TermFilter).Value); Assert.AreEqual("field", ((facets.FacetGenerators.Last() as QueryFacet).Query as TermQuery).Field); Assert.AreEqual("value", ((facets.FacetGenerators.Last() as QueryFacet).Query as TermQuery).Value); }