public void PASS_Create()
 {
     DateHistogramFacet facet = new DateHistogramFacet("histo1", "field", DateIntervalEnum.Hour);
     Assert.IsNotNull(facet);
     Assert.AreEqual("histo1", facet.FacetName);
     Assert.AreEqual("field", facet.Field);
     Assert.AreEqual(DateIntervalEnum.Hour, facet.ConstantInterval);
 }
        public void PASS_Serialize()
        {
            DateHistogramFacet facet = new DateHistogramFacet("histo1", "field", DateIntervalEnum.Hour);
            string json = JsonConvert.SerializeObject(facet);
            Assert.IsNotNull(json);

            string expectedJson = "{\"histo1\":{\"date_histogram\":{\"field\":\"field\",\"interval\":\"hour\"}}}";
            Assert.AreEqual(expectedJson, json);
        }
        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> histoDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(facetDict.GetString(FacetTypeEnum.DateHistogram.ToString()));

            DateHistogramFacet facet = null;

            string facetName = wholeDict.First().Key;
            string field = histoDict.GetStringOrDefault(_FIELD);

            DateIntervalEnum intervalType = DateIntervalEnum.Day;
            string intervalValue = histoDict.GetString(_INTERVAL);
            intervalType = DateIntervalEnum.Find(intervalValue);
            if (intervalType != null)
            {
                if (string.IsNullOrWhiteSpace(field))
                    facet = new DateHistogramFacet(facetName, intervalType);
                else
                    facet = new DateHistogramFacet(facetName, field, intervalType);
            }
            else
            {
                TimeValue timeValue = new TimeValue(intervalValue);
                if (string.IsNullOrWhiteSpace(field))
                    facet = new DateHistogramFacet(facetName, timeValue);
                else
                    facet = new DateHistogramFacet(facetName, field, timeValue);
            }

            FacetSerializer.DeserializeFacetInfo(facet, histoDict);
            facet.KeyField = histoDict.GetStringOrDefault(_KEY_FIELD);
            if (histoDict.ContainsKey(_POST_OFFSET))
                facet.PostOffset = JsonConvert.DeserializeObject<TimeValue>(histoDict.GetString(_POST_OFFSET));
            if(histoDict.ContainsKey(_PRE_OFFSET))
                facet.PreOffset = JsonConvert.DeserializeObject<TimeValue>(histoDict.GetString(_PRE_OFFSET));
            facet.PostZone = histoDict.GetTimeSpan(_POST_ZONE, _TIMESPAN_DEFAULT);
            facet.PreZone = histoDict.GetTimeSpan(_PRE_ZONE, _TIMESPAN_DEFAULT);
            facet.PreZoneAdjustLargeInterval = histoDict.GetBool(_PRE_ZONE_ADJUST_LARGE_INTERVAL, _PRE_ZONE_ADJUST_LARGE_INTERVAL_DEFAULT);
            if (histoDict.ContainsKey(_PARAMETERS))
                facet.ScriptParameters = JsonConvert.DeserializeObject<ScriptParameterCollection>(histoDict.GetString(_PARAMETERS));
            facet.TimeZone = histoDict.GetTimeSpan(_TIME_ZONE, _TIMESPAN_DEFAULT);
            facet.ValueField = histoDict.GetStringOrDefault(_VALUE_FIELD);
            facet.ValueScript = histoDict.GetStringOrDefault(_VALUE_SCRIPT);
            facet.ScriptLanguage = histoDict.GetStringOrDefault(_LANGUAGE);

            return facet;
        }