json serialization update for CQL :
- replace = with :
- conversion from string to jsonnode to prevent string escaping
diff --git a/src/main/java/de/ids_mannheim/korap/query/serialize/CQLTree.java b/src/main/java/de/ids_mannheim/korap/query/serialize/CQLTree.java
index 459c2ed..b58d3b2 100644
--- a/src/main/java/de/ids_mannheim/korap/query/serialize/CQLTree.java
+++ b/src/main/java/de/ids_mannheim/korap/query/serialize/CQLTree.java
@@ -1,5 +1,7 @@
package de.ids_mannheim.korap.query.serialize;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
import de.ids_mannheim.korap.util.QueryException;
import org.z3950.zing.cql.*;
@@ -20,8 +22,8 @@
private static final String TERM_RELATION_CQL_1_1 = "scr";
private static final String TERM_RELATION_CQL_1_2 = "=";
private static final String SUPPORTED_RELATION_EXACT = "exact"; // not in the doc
- private static final String OPERATION_OR = "operation:or";
- private static final String OPERATION_SEQUENCE = "operation:sequence";
+ private static final String OPERATION_OR = "\"operation:or\"";
+ private static final String OPERATION_SEQUENCE = "\"operation:sequence\"";
private static final String KORAP_CONTEXT = "http://ids-mannheim.de/ns/KorAP/json-ld/v0.1/context.jsonld";
private LinkedHashMap<String, Object> requestMap;
@@ -41,7 +43,7 @@
this.sb = new StringBuilder();
this.version = version;
this.isCaseSensitive = isCaseSensitive;
- this.requestMap = new LinkedHashMap<String, Object>();
+ this.requestMap = new LinkedHashMap<>();
requestMap.put("@context", KORAP_CONTEXT);
process(query);
}
@@ -56,8 +58,16 @@
public void process(String query) throws QueryException {
CQLNode cqlNode = parseQuerytoCQLNode(query);
parseCQLNode(cqlNode);
- String serializedQuery = sb.toString();
- requestMap.put("query", serializedQuery);
+ ObjectMapper mapper = new ObjectMapper();
+ JsonNode node;
+ try {
+ node = mapper.readTree(sb.toString());
+ } catch (IOException e) {
+ e.printStackTrace();
+ throw new QueryException("Something went wrong!");
+ }
+ requestMap.put("query", node);
+
}
private CQLNode parseQuerytoCQLNode(String query) throws QueryException {
@@ -104,10 +114,11 @@
private void parseAndNode(CQLAndNode node) throws QueryException {
checkBooleanModifier(node);
- sb.append("{@type=korap:group, operation=");
+ sb.append("{\"@type\":\"korap:group\", \"operation\":");
sb.append(OPERATION_SEQUENCE);
- sb.append(", distances=[{@type=korap:distance, key=t, min=0, max=0}]");
- sb.append(", operands=[");
+ sb.append(", distances:[{\"@type\":\"korap:distance\", \"key\":\"t\"," +
+ " \"min\":0, \"max\":0}]");
+ sb.append(", \"operands\":[");
parseCQLNode(node.getLeftOperand());
sb.append(", ");
parseCQLNode(node.getRightOperand());
@@ -116,9 +127,9 @@
private void parseOrNode(CQLOrNode node) throws QueryException {
checkBooleanModifier(node);
- sb.append("{@type=korap:group, operation=");
+ sb.append("{\"@type\":\"korap:group\", \"operation\":");
sb.append(OPERATION_OR);
- sb.append(", operands=[");
+ sb.append(", \"operands\":[");
parseCQLNode(node.getLeftOperand());
sb.append(", ");
parseCQLNode(node.getRightOperand());
@@ -127,9 +138,9 @@
private void writeSequence(String str) {
String[] terms = str.split(" ");
- sb.append("{@type=korap:group, operation=");
+ sb.append("{\"@type\":\"korap:group\", \"operation\":");
sb.append(OPERATION_SEQUENCE);
- sb.append(", operands=[");
+ sb.append(", \"operands\":[");
int size = terms.length;
for (int i = 0; i < size; i++) {
@@ -142,21 +153,21 @@
}
private void writeTerm(String term) {
- sb.append("{@type=korap:token, wrap={@type=korap:term");
+ sb.append("{\"@type\":\"korap:token\", \"wrap\":{\"@type\":\"korap:term\"");
if (!isCaseSensitive) {
- sb.append(", caseInsensitive=true");
+ sb.append(", \"caseInsensitive\":true");
}
- sb.append(", key=");
- sb.append(term);
- sb.append(", layer=orth, match=match:eq}}");
+ sb.append(", \"key\":");
+ sb.append("\"" + term + "\"");
+ sb.append(", \"layer\":\"orth\", \"match\":\"match:eq\"}}");
}
private void checkBooleanModifier(CQLBooleanNode node) throws QueryException {
List<Modifier> modifiers = node.getModifiers();
if ((modifiers != null) && !modifiers.isEmpty()) {
Modifier modifier = modifiers.get(0);
- throw new QueryException(20, "Relation modifier \"" +
- modifier.toCQL() + "\" is not supported.");
+ throw new QueryException(20, "Relation modifier " +
+ modifier.toCQL() + " is not supported.");
}
}
@@ -164,7 +175,7 @@
// only allow "cql.serverChoice" and "words" index
if (!(INDEX_CQL_SERVERCHOICE.equals(node.getIndex()) ||
INDEX_WORDS.equals(node.getIndex()))) {
- throw new QueryException(16, "Index \"" + node.getIndex() + "\" is not supported.");
+ throw new QueryException(16, "Index " + node.getIndex() + " is not supported.");
}
// only allow "=" relation without any modifiers
CQLRelation relation = node.getRelation();
@@ -172,14 +183,14 @@
if (!(TERM_RELATION_CQL_1_1.equals(baseRel) ||
TERM_RELATION_CQL_1_2.equals(baseRel) ||
SUPPORTED_RELATION_EXACT.equals(baseRel))) {
- throw new QueryException(19, "Relation \"" +
- relation.getBase() + "\" is not supported.");
+ throw new QueryException(19, "Relation " +
+ relation.getBase() + " is not supported.");
}
List<Modifier> modifiers = relation.getModifiers();
if ((modifiers != null) && !modifiers.isEmpty()) {
Modifier modifier = modifiers.get(0);
- throw new QueryException(20, "Relation modifier \"" +
- modifier.getValue() + "\" is not supported.");
+ throw new QueryException(20, "Relation modifier " +
+ modifier.getValue() + " is not supported.");
}
}
diff --git a/src/main/java/de/ids_mannheim/korap/query/serialize/QuerySerializer.java b/src/main/java/de/ids_mannheim/korap/query/serialize/QuerySerializer.java
index 61682bc..a6496d1 100644
--- a/src/main/java/de/ids_mannheim/korap/query/serialize/QuerySerializer.java
+++ b/src/main/java/de/ids_mannheim/korap/query/serialize/QuerySerializer.java
@@ -165,12 +165,4 @@
return "";
}
}
-
- public static String getQueryLanguageVersion() {
- return queryLanguageVersion;
- }
-
- public static void setQueryLanguageVersion(String queryLanguageVersion) {
- QuerySerializer.queryLanguageVersion = queryLanguageVersion;
- }
}