public void PASS_Create() { RangeFacet facet = new RangeFacet("range-name", "field", new List<RangeBucket>() { new RangeBucket() { GreaterThan = 5 } }); Assert.IsNotNull(facet); Assert.AreEqual("range-name", facet.FacetName); Assert.AreEqual("field", facet.Field); Assert.AreEqual((int)5, Convert.ToInt32(facet.RangeBuckets.First().GreaterThan)); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { Dictionary<string, object> wholeDict = serializer.Deserialize<Dictionary<string, object>>(reader); Dictionary<string, object> facetDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(wholeDict.First().Value.ToString()); Dictionary<string, object> rangeDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(facetDict.GetString(FacetTypeEnum.Range.ToString())); List<RangeBucket> rangeBuckets = new List<RangeBucket>(); string rangeBucketsJson = null; if (rangeDict.ContainsKey(_RANGES)) { rangeBucketsJson = rangeDict.GetString(_RANGES); } else { KeyValuePair<string, object> rangesKvp = rangeDict.FirstOrDefault(x => !_KnownFields.Contains(x.Key, StringComparer.OrdinalIgnoreCase)); if(string.IsNullOrWhiteSpace(rangesKvp.Key)) throw new RequiredPropertyMissingException("field/ranges"); rangeBucketsJson = rangesKvp.Value.ToString(); } foreach (Dictionary<string, object> bucketDict in JsonConvert.DeserializeObject<IEnumerable<Dictionary<string, object>>>(rangeBucketsJson)) { RangeBucket bucket = new RangeBucket(); if (bucketDict.ContainsKey(_GREATER_THAN)) bucket.GreaterThan = bucketDict[_GREATER_THAN]; if (bucketDict.ContainsKey(_LESS_THAN)) bucket.LessThan = bucketDict[_LESS_THAN]; if (bucketDict.ContainsKey(_GREATER_THAN_OR_EQUAL)) bucket.GreaterThanOrEqualTo = bucketDict[_GREATER_THAN_OR_EQUAL]; if (bucketDict.ContainsKey(_LESS_THAN_OR_EQUAL)) bucket.LessThanOrEqualTo = bucketDict[_LESS_THAN_OR_EQUAL]; rangeBuckets.Add(bucket); } string facetName = wholeDict.First().Key; RangeFacet facet = null; if (rangeDict.ContainsKey(_FIELD)) facet = new RangeFacet(facetName, rangeDict.GetString(_FIELD), rangeBuckets); else { facet = new RangeFacet(facetName, rangeBuckets); } facet.KeyField = rangeDict.GetStringOrDefault(_KEY_FIELD); facet.ValueField = rangeDict.GetStringOrDefault(_VALUE_FIELD); facet.KeyScript = rangeDict.GetStringOrDefault(_KEY_SCRIPT); facet.ValueScript = rangeDict.GetStringOrDefault(_VALUE_SCRIPT); if (rangeDict.ContainsKey(_PARAMETERS)) facet.ScriptParameters = JsonConvert.DeserializeObject<ScriptParameterCollection>(rangeDict.GetString(_PARAMETERS)); facet.ScriptLanguage = rangeDict.GetStringOrDefault(_LANGUAGE); //FacetSerializer.DeserializeFacetInfo(facet, rangeDict); return facet; }
public void PASS_Serialize() { RangeFacet facet = new RangeFacet("range-name", "field", new List<RangeBucket>() { new RangeBucket() { GreaterThan = 5 } }); string json = JsonConvert.SerializeObject(facet); Assert.IsNotNull(json); string expectedJson = "{\"range-name\":{\"range\":{\"field\":\"field\",\"ranges\":[{\"gt\":5}]}}}"; Assert.AreEqual(expectedJson, json); }