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> aggDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(wholeDict.First().Value.ToString());
            Dictionary<string, object> percentDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(aggDict.GetString(AggregationTypeEnum.Percentiles.ToString()));

            PercentilesAggregate agg = null;
            if (percentDict.ContainsKey(_FIELD))
            {
                agg = new PercentilesAggregate(wholeDict.First().Key, percentDict.GetString(_FIELD));
            }
            else if (percentDict.ContainsKey(Script.SCRIPT))
            {
                Script script = percentDict.DeserializeObject<Script>();
                agg = new PercentilesAggregate(wholeDict.First().Key, script);
            }
            else
            {
                throw new RequiredPropertyMissingException(_FIELD + "/" + Script.SCRIPT);
            }

            if (percentDict.ContainsKey(_PERCENTS))
            {
                agg.PercentBuckets = JsonConvert.DeserializeObject<IEnumerable<Double>>(percentDict.GetString(_PERCENTS));
            }

            agg.Compression = percentDict.GetInt32(_COMPRESSION, PercentilesAggregate._COMPRESSION_DEFAULT); 
            agg.SubAggregations = BucketAggregationBase.DeserializeSubAggregations(aggDict);
            return agg;
        }
 public void PASS_Create()
 {
     PercentilesAggregate agg = new PercentilesAggregate("percent-name", "field");
     Assert.IsNotNull(agg);
     Assert.AreEqual("percent-name", agg.Name);
     Assert.AreEqual("field", agg.Field);
 }
        public void PASS_Serialize()
        {
            PercentilesAggregate agg = new PercentilesAggregate("percent-name", "field")
            {
                PercentBuckets = new List<Double>() { 95, 99, 99.9 },
                Compression = 200
            };
            string json = JsonConvert.SerializeObject(agg);
            Assert.IsNotNull(json);

            string expectedJson = "{\"percent-name\":{\"percentiles\":{\"field\":\"field\",\"percents\":[95.0,99.0,99.9],\"compression\":200}}}";
            Assert.AreEqual(expectedJson, json);
        }
        public void PASS_Serialize_Aggs()
        {
            PercentilesAggregate agg = new PercentilesAggregate("percent-name", "field")
            {
                SubAggregations = new List<IAggregation>() 
                { 
                    new SumAggregate("sum-name", "field")
                }
            };
            string json = JsonConvert.SerializeObject(agg);
            Assert.IsNotNull(json);

            string expectedJson = "{\"percent-name\":{\"percentiles\":{\"field\":\"field\"},\"aggregations\":{\"sum-name\":{\"sum\":{\"field\":\"field\"}}}}}";
            Assert.AreEqual(expectedJson, json);
        }