filterHashMap = new HashMap<>();
diff --git a/fili-core/src/main/java/com/yahoo/bard/webservice/web/MetricParser.java b/fili-core/src/main/java/com/yahoo/bard/webservice/web/MetricParser.java
index 8af5d5139d..06039c9287 100644
--- a/fili-core/src/main/java/com/yahoo/bard/webservice/web/MetricParser.java
+++ b/fili-core/src/main/java/com/yahoo/bard/webservice/web/MetricParser.java
@@ -2,14 +2,14 @@
// Licensed under the terms of the Apache license. Please see LICENSE.md file distributed with this work for terms.
package com.yahoo.bard.webservice.web;
-import org.json.JSONArray;
-import org.json.JSONException;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.JsonNodeFactory;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import java.util.ArrayList;
import java.util.HashMap;
-import java.util.List;
import java.util.Map;
import java.util.Stack;
@@ -21,7 +21,7 @@ public class MetricParser {
private static final Logger LOG = LoggerFactory.getLogger(MetricParser.class);
/**
- * This function converts the string of metrics extracted from the url into JSONArray.
+ * This function converts the string of metrics extracted from the url into ArrayNode.
*
* @param metricString An Api metric string eg:
* {@code metricString = metric1(AND(dim1|id-in[a,b],dim2|id-in[c,d])),metric2 }
@@ -30,10 +30,9 @@ public class MetricParser {
* {@code {[{"filter":{"AND":{"dim2|id-in":["abc","xyz"],"dim3|id-in":["mobile","tablet"]}}, "name":"metric1"},
* {"filter":{},"name":"metric2"}]}}
- * @throws JSONException if a JSON cannot be constructed successfully from the given metricString
* @throws IllegalArgumentException if metricString is empty or the metricString has unbalanced brackets
*/
- public static JSONArray generateMetricFilterJsonArray(String metricString) throws JSONException {
+ public static ArrayNode generateMetricFilterJsonArray(String metricString) {
if (metricString.length() == 0 || !(isBracketsBalanced(metricString))) {
LOG.error("Metrics parameter values are invalid. The string is: " + metricString);
throw new IllegalArgumentException("Metrics parameter values are invalid. The string is: " + metricString);
@@ -45,7 +44,7 @@ public static JSONArray generateMetricFilterJsonArray(String metricString) throw
String[] metrics = encodeMetricFilters(metricString, mapper).split(",");
//looping each metric in a metrics array
- List metricList = new ArrayList<>();
+ ArrayNode arrayNode = JsonNodeFactory.instance.arrayNode();
for (String metric : metrics) {
//Separating metric and '-' separated filter encode metricFilterArray[0] will be metric name
//and metricFilterArray[1] will be key of the mapper contains respective filter string of the metric
@@ -53,34 +52,32 @@ public static JSONArray generateMetricFilterJsonArray(String metricString) throw
//Retrieving the filter string from the mapper and creating a metric set with metric as key
//and filter as value
String metricFilter = metricFilterArray.length > 1 ? mapper.get("-" + metricFilterArray[1]) : "";
- metricList.add(getJsonString(metricFilterArray[0], metricFilter));
- }
- StringBuilder finalJson = new StringBuilder();
- //preparing complete json string by appending brackets to metric name and metric filter
- for (String jsonToken : metricList) {
- finalJson.append("{" + jsonToken + "},");
+
+ ObjectNode objectNode = arrayNode.objectNode();
+ addJsonFilter(objectNode, metricFilterArray[0], metricFilter);
+ arrayNode.add(objectNode);
}
- return new JSONArray("[" + finalJson.toString().substring(0, finalJson.toString().length() - 1) + "]");
+
+ return arrayNode;
}
/**
- * Returns a String that is formatted to a JSON string.
+ * Add the name and filter of a metric to an ObjectNode.
+ * For eg: {"filter":{"AND":"property|id-in:[14,125],country|id-in:[US,IN]},"name":"foo"}
*
+ * @param metricNode the object node to fill with the name and filter on a metric
* @param metricName the name of the metric
* @param metricFilter the filter associated with the metric. Could be empty or have a value
- *
- * @return A String in JSON format for a given metric and filter.
- * For eg: "filter":{"AND":"property|id-in:[14,125],country|id-in:[US,IN]},"name":"foo"
*/
- private static String getJsonString(String metricName, String metricFilter) {
- if (metricFilter.isEmpty()) {
- metricFilter = "\"filter\": {}";
- } else {
- metricFilter = "\"filter\":{\"AND\":\"" +
- metricFilter.replace("AND", "").replace("(", "").replace(")", "") + "\" }";
-
+ private static void addJsonFilter(ObjectNode metricNode, String metricName, String metricFilter) {
+ metricNode.put("name", metricName);
+ ObjectNode filterNode = metricNode.putObject("filter");
+ if (!metricFilter.isEmpty()) {
+ metricFilter = metricFilter.replace("AND", "")
+ .replace("(", "")
+ .replace(")", "");
+ filterNode.put("AND", metricFilter);
}
- return "\"name\": \"" + metricName + "\"," + metricFilter;
}
/**
diff --git a/fili-core/src/main/java/com/yahoo/bard/webservice/web/MetricsFilterSetBuilder.java b/fili-core/src/main/java/com/yahoo/bard/webservice/web/MetricsFilterSetBuilder.java
index 6882f5ac78..03d038842b 100644
--- a/fili-core/src/main/java/com/yahoo/bard/webservice/web/MetricsFilterSetBuilder.java
+++ b/fili-core/src/main/java/com/yahoo/bard/webservice/web/MetricsFilterSetBuilder.java
@@ -14,8 +14,8 @@
import com.yahoo.bard.webservice.druid.model.postaggregation.SketchSetOperationPostAggFunction;
import com.yahoo.bard.webservice.table.LogicalTable;
-import org.json.JSONArray;
-import org.json.JSONObject;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ArrayNode;
import java.util.Collection;
import java.util.List;
@@ -38,7 +38,7 @@ public interface MetricsFilterSetBuilder {
* @throws BadApiRequestException Invalid metric query if the metric query has
* duplicate metrics
*/
- void validateDuplicateMetrics(JSONArray metricsJsonArray) throws BadApiRequestException;
+ void validateDuplicateMetrics(ArrayNode metricsJsonArray) throws BadApiRequestException;
/**
* Provides filter wrapped logical metric for the given logical metric.
@@ -56,7 +56,7 @@ public interface MetricsFilterSetBuilder {
*/
LogicalMetric getFilteredLogicalMetric(
LogicalMetric logicalMetric,
- JSONObject metricFilterObject,
+ JsonNode metricFilterObject,
DimensionDictionary dimensionDictionary,
LogicalTable table,
DataApiRequest apiRequest
@@ -125,7 +125,7 @@ Collection updateNestedQueryPostAggs(
*/
TemplateDruidQuery updateTemplateDruidQuery(
TemplateDruidQuery query,
- JSONObject metricFilterObject,
+ JsonNode metricFilterObject,
DimensionDictionary dimensionDictionary,
LogicalTable table,
DataApiRequest apiRequest
@@ -177,7 +177,7 @@ PostAggregation replacePostAggWithPostAggFromMap(
* filter is not found.
*/
Set getFilteredAggregation(
- JSONObject filter,
+ JsonNode filter,
Aggregation aggregation,
DimensionDictionary dimensionDictionary,
LogicalTable table,
diff --git a/fili-core/src/test/groovy/com/yahoo/bard/webservice/web/MetricParserSpec.groovy b/fili-core/src/test/groovy/com/yahoo/bard/webservice/web/MetricParserSpec.groovy
index 100bebe388..2308b2945b 100644
--- a/fili-core/src/test/groovy/com/yahoo/bard/webservice/web/MetricParserSpec.groovy
+++ b/fili-core/src/test/groovy/com/yahoo/bard/webservice/web/MetricParserSpec.groovy
@@ -2,26 +2,29 @@
// Licensed under the terms of the Apache license. Please see LICENSE.md file distributed with this work for terms.
package com.yahoo.bard.webservice.web
-import org.json.JSONArray
+import com.yahoo.bard.webservice.application.ObjectMappersSuite
+
+import com.fasterxml.jackson.databind.ObjectMapper
+import com.fasterxml.jackson.databind.node.ArrayNode
import spock.lang.Specification
import spock.lang.Unroll
class MetricParserSpec extends Specification {
-
- JSONArray expectedJsonObj1
- JSONArray expectedJsonObj2
- JSONArray expectedJsonObj3
+ ObjectMapper mapper = new ObjectMappersSuite().mapper
+ ArrayNode expectedJsonObj1
+ ArrayNode expectedJsonObj2
+ ArrayNode expectedJsonObj3
def setup() {
- expectedJsonObj1 = new JSONArray("[{\"filter\":{\"AND\":\"app3|id-in[mobile,tablet],app2|id-in[abc,xyz]\"},\"name\":\"foo\"},{\"filter\":{},\"name\":\"pageviews\"}]")
- expectedJsonObj2 = new JSONArray("[{\"name\":\"pageviews\",\"filter\":{}},{\"name\":\"foo\",\"filter\":{}}]")
- expectedJsonObj3 = new JSONArray("[{\"filter\":{\"AND\":\"app3|id-in[mobile,tablet],app2|id-in[abc,xyz]\"},\"name\":\"foo\"},{\"filter\":{\"AND\":\"app3|id-in[mobile,tablet],app2|id-in[abc,xyz]\"},\"name\":\"xcookie\"}]")
+ expectedJsonObj1 = mapper.readTree("[{\"filter\":{\"AND\":\"app3|id-in[mobile,tablet],app2|id-in[abc,xyz]\"},\"name\":\"foo\"},{\"filter\":{},\"name\":\"pageviews\"}]");
+ expectedJsonObj2 = mapper.readTree("[{\"name\":\"pageviews\",\"filter\":{}},{\"name\":\"foo\",\"filter\":{}}]")
+ expectedJsonObj3 = mapper.readTree("[{\"filter\":{\"AND\":\"app3|id-in[mobile,tablet],app2|id-in[abc,xyz]\"},\"name\":\"foo\"},{\"filter\":{\"AND\":\"app3|id-in[mobile,tablet],app2|id-in[abc,xyz]\"},\"name\":\"xcookie\"}]")
}
def "Test for wrong format of the metric filter with misaligned brackets"() {
when:
- MetricParser.generateMetricFilterJsonArray("foo(AND(app3|id-in[mobile,tablet]app2|id-in[abc,xyz])").similar(expectedJsonObj1)
+ MetricParser.generateMetricFilterJsonArray("foo(AND(app3|id-in[mobile,tablet]app2|id-in[abc,xyz])").equals(expectedJsonObj1)
then:
String expectedMessage = "Metrics parameter values are invalid. The string is: foo(AND(app3|id-in[mobile,tablet]app2|id-in[abc,xyz])"
@@ -31,17 +34,17 @@ class MetricParserSpec extends Specification {
def "Validate the json Array Object when metric string is a combination of filtered and non-filtered metrics"() {
expect:
- MetricParser.generateMetricFilterJsonArray("foo(AND(app3|id-in[mobile,tablet],app2|id-in[abc,xyz])),pageviews").similar(expectedJsonObj1)
+ MetricParser.generateMetricFilterJsonArray("foo(AND(app3|id-in[mobile,tablet],app2|id-in[abc,xyz])),pageviews").equals(expectedJsonObj1)
}
def "Validate the json Array Object when metric string contains only non-filtered metrics"() {
expect:
- MetricParser.generateMetricFilterJsonArray("pageviews,foo").similar(expectedJsonObj2)
+ MetricParser.generateMetricFilterJsonArray("pageviews,foo").equals(expectedJsonObj2)
}
def "Validate the json Array Object when metric string contains only filtered metrics"() {
expect:
- MetricParser.generateMetricFilterJsonArray("foo(AND(app3|id-in[mobile,tablet],app2|id-in[abc,xyz])),xcookie(AND(app3|id-in[mobile,tablet],app2|id-in[abc,xyz]))").similar(expectedJsonObj3)
+ MetricParser.generateMetricFilterJsonArray("foo(AND(app3|id-in[mobile,tablet],app2|id-in[abc,xyz])),xcookie(AND(app3|id-in[mobile,tablet],app2|id-in[abc,xyz]))").equals(expectedJsonObj3)
}
def "Test for isBracketsBalanced validation"() {
@@ -55,7 +58,7 @@ class MetricParserSpec extends Specification {
@Unroll
def "Create map using encodeMetricFilters, filter #filter returns values #filteredMetrics"() {
setup:
- Map metricMap = [:]
+ Map metricMap = [:]
MetricParser.encodeMetricFilters(filter, metricMap)
expect:
diff --git a/fili-core/src/test/groovy/com/yahoo/bard/webservice/web/SketchIntersectionReportingResources.groovy b/fili-core/src/test/groovy/com/yahoo/bard/webservice/web/SketchIntersectionReportingResources.groovy
index b6f2b74184..ba38fa110c 100644
--- a/fili-core/src/test/groovy/com/yahoo/bard/webservice/web/SketchIntersectionReportingResources.groovy
+++ b/fili-core/src/test/groovy/com/yahoo/bard/webservice/web/SketchIntersectionReportingResources.groovy
@@ -41,8 +41,9 @@ import com.yahoo.bard.webservice.table.LogicalTable
import com.yahoo.bard.webservice.table.PhysicalTable
import com.yahoo.bard.webservice.table.TableGroup
-import org.json.JSONArray
-import org.json.JSONObject
+import com.fasterxml.jackson.databind.JsonNode
+import com.fasterxml.jackson.databind.ObjectMapper
+import com.fasterxml.jackson.databind.node.ArrayNode
import spock.lang.Specification
/**
@@ -56,7 +57,7 @@ class SketchIntersectionReportingResources extends Specification {
public DimensionDictionary dimensionDict
public MetricDictionary metricDict
public LogicalTable table
- public JSONObject filterObj
+ public JsonNode filterObj
public PostAggregation fooPostAggregation;
public Filter filter;
public Set fooNoBarFilteredAggregationSet
@@ -68,8 +69,11 @@ class SketchIntersectionReportingResources extends Specification {
public TemplateDruidQuery dayAvgFoosTdq
public Dimension propertyDim
public Dimension countryDim
+ public ObjectMapper mapper
SketchIntersectionReportingResources init() {
+ mapper = new ObjectMapper()
+
LinkedHashSet dimensionFields = new LinkedHashSet<>()
dimensionFields.add(BardDimensionField.ID)
dimensionFields.add(BardDimensionField.DESC)
@@ -168,9 +172,9 @@ class SketchIntersectionReportingResources extends Specification {
TableGroup tableGroup = new TableGroup([physicalTable] as LinkedHashSet, metrics, physicalTable.dimensions)
table = new LogicalTable("NETWORK", DAY, tableGroup, metricDict)
- JSONArray metricJsonObjArray = new JSONArray("[{\"filter\":{\"AND\":\"country|id-in[US,IN],property|id-in[114,125]\"},\"name\":\"foo\"},{\"filter\":{},\"name\":\"pageviews\"}]")
- JSONObject jsonobject = metricJsonObjArray.getJSONObject(0)
- filterObj = jsonobject.getJSONObject("filter")
+ ArrayNode metricJsonObjArray = mapper.readTree("[{\"filter\":{\"AND\":\"country|id-in[US,IN],property|id-in[114,125]\"},\"name\":\"foo\"},{\"filter\":{},\"name\":\"pageviews\"}]")
+ JsonNode jsonobject = metricJsonObjArray.get(0)
+ filterObj = jsonobject.get("filter")
fooPostAggregation = foos.make().templateDruidQuery.getPostAggregations().first()
diff --git a/fili-core/src/test/groovy/com/yahoo/bard/webservice/web/ThetaSketchIntersectionReportingResources.groovy b/fili-core/src/test/groovy/com/yahoo/bard/webservice/web/ThetaSketchIntersectionReportingResources.groovy
index 687c8aee7a..1e8739b666 100644
--- a/fili-core/src/test/groovy/com/yahoo/bard/webservice/web/ThetaSketchIntersectionReportingResources.groovy
+++ b/fili-core/src/test/groovy/com/yahoo/bard/webservice/web/ThetaSketchIntersectionReportingResources.groovy
@@ -42,8 +42,9 @@ import com.yahoo.bard.webservice.table.LogicalTable
import com.yahoo.bard.webservice.table.PhysicalTable
import com.yahoo.bard.webservice.table.TableGroup
-import org.json.JSONArray
-import org.json.JSONObject
+import com.fasterxml.jackson.databind.JsonNode
+import com.fasterxml.jackson.databind.ObjectMapper
+import com.fasterxml.jackson.databind.node.ArrayNode
import spock.lang.Specification
@@ -54,9 +55,9 @@ class ThetaSketchIntersectionReportingResources extends Specification {
public DimensionDictionary dimensionDict
public MetricDictionary metricDict
public LogicalTable table
- public JSONObject filterObj
public PostAggregation fooPostAggregation
public Filter filter
+ public JsonNode filterObj
public Set fooNoBarFilteredAggregationSet
public Set fooRegFoosFilteredAggregationSet
public ThetaSketchSetOperationPostAggregation fooNoBarPostAggregationInterim
@@ -66,8 +67,11 @@ class ThetaSketchIntersectionReportingResources extends Specification {
public TemplateDruidQuery dayAvgFoosTdq
public Dimension propertyDim
public Dimension countryDim
+ public ObjectMapper mapper
ThetaSketchIntersectionReportingResources init() {
+ mapper = new ObjectMapper()
+
LinkedHashSet dimensionFields = new LinkedHashSet<>()
dimensionFields.add(BardDimensionField.ID)
dimensionFields.add(BardDimensionField.DESC)
@@ -167,9 +171,9 @@ class ThetaSketchIntersectionReportingResources extends Specification {
table = new LogicalTable("NETWORK", DAY, tableGroup, metricDict)
- JSONArray metricJsonObjArray = new JSONArray("[{\"filter\":{\"AND\":\"country|id-in[US,IN],property|id-in[114,125]\"},\"name\":\"foo\"},{\"filter\":{},\"name\":\"pageviews\"}]")
- JSONObject jsonobject = metricJsonObjArray.getJSONObject(0)
- filterObj = jsonobject.getJSONObject("filter")
+ ArrayNode metricJsonObjArray = mapper.readTree("[{\"filter\":{\"AND\":\"country|id-in[US,IN],property|id-in[114,125]\"},\"name\":\"foo\"},{\"filter\":{},\"name\":\"pageviews\"}]")
+ JsonNode jsonobject = metricJsonObjArray.get(0)
+ filterObj = jsonobject.get("filter")
fooPostAggregation = foos.make().templateDruidQuery.getPostAggregations().first()
diff --git a/pom.xml b/pom.xml
index 00c5dc5a18..42b3520fb6 100644
--- a/pom.xml
+++ b/pom.xml
@@ -371,12 +371,6 @@
jackson-dataformat-csv
${version.jackson}