JsonUtils class
diff --git a/src/main/java/de/ids_mannheim/korap/query/serialize/CollectionQuery.java b/src/main/java/de/ids_mannheim/korap/query/serialize/CollectionQuery.java
index 6fa6465..c758f1d 100644
--- a/src/main/java/de/ids_mannheim/korap/query/serialize/CollectionQuery.java
+++ b/src/main/java/de/ids_mannheim/korap/query/serialize/CollectionQuery.java
@@ -1,12 +1,11 @@
package de.ids_mannheim.korap.query.serialize;
-import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multiset;
import de.ids_mannheim.korap.resource.Relation;
+import de.ids_mannheim.korap.utils.JsonUtils;
import lombok.Data;
import java.io.IOException;
@@ -20,8 +19,6 @@
*/
public class CollectionQuery {
-
- private static ObjectMapper serialzer = new ObjectMapper();
private CollectionTypes types;
private List<Map> rq;
private Multimap<String, String> mfilter;
@@ -39,12 +36,11 @@
public CollectionQuery addResource(String query) {
try {
- List v = serialzer.readValue(query, LinkedList.class);
+ List v = JsonUtils.read(query, LinkedList.class);
this.rq.addAll(v);
} catch (IOException e) {
throw new IllegalArgumentException("Conversion went wrong!");
}
-
return this;
}
@@ -229,13 +225,7 @@
public String toCollections() {
Map meta = new LinkedHashMap();
meta.put("collections", join());
-
- try {
- return serialzer.writeValueAsString(meta);
- } catch (JsonProcessingException e) {
- e.printStackTrace();
- return "";
- }
+ return JsonUtils.toJSON(meta);
}
/**
@@ -244,17 +234,11 @@
* @return
*/
public JsonNode buildNode() {
- return serialzer.valueToTree(join());
+ return JsonUtils.valueToTree(join());
}
public String buildString() {
- try {
- return serialzer.writeValueAsString(join());
- } catch (JsonProcessingException e) {
- e.printStackTrace();
- return "";
- }
-
+ return JsonUtils.toJSON(join());
}
@@ -301,7 +285,6 @@
String sub = queries.substring(queries.indexOf(whole), queries.lastIndexOf(whole));
queries.replace(whole, "");
}
-
}
private void v(String queries, boolean filter) {
diff --git a/src/main/java/de/ids_mannheim/korap/query/serialize/MetaQuery.java b/src/main/java/de/ids_mannheim/korap/query/serialize/MetaQuery.java
index 5312274..0eb790d 100644
--- a/src/main/java/de/ids_mannheim/korap/query/serialize/MetaQuery.java
+++ b/src/main/java/de/ids_mannheim/korap/query/serialize/MetaQuery.java
@@ -1,8 +1,5 @@
package de.ids_mannheim.korap.query.serialize;
-import com.fasterxml.jackson.core.JsonProcessingException;
-import com.fasterxml.jackson.databind.ObjectMapper;
-
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
@@ -14,11 +11,9 @@
*/
public class MetaQuery {
- private ObjectMapper serialier;
private Map meta;
public MetaQuery() {
- this.serialier = new ObjectMapper();
this.meta = new LinkedHashMap();
}
@@ -46,11 +41,4 @@
return meta;
}
- public String toMetaString() {
- try {
- return serialier.writeValueAsString(meta);
- } catch (JsonProcessingException e) {
- return "";
- }
- }
}
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 a798968..fbe0af0 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
@@ -2,11 +2,10 @@
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
-import com.fasterxml.jackson.databind.ObjectMapper;
import de.ids_mannheim.korap.util.QueryException;
+import de.ids_mannheim.korap.utils.JsonUtils;
import org.slf4j.LoggerFactory;
-import java.io.File;
import java.io.IOException;
import java.util.Map;
@@ -17,7 +16,6 @@
public static String queryLanguageVersion;
- private ObjectMapper mapper;
private AbstractSyntaxTree ast;
private Object collection;
private Object meta;
@@ -25,7 +23,6 @@
.getLogger(QuerySerializer.class);
public QuerySerializer() {
- mapper = new ObjectMapper();
// mapper.enable(SerializationFeature.INDENT_OUTPUT);
}
@@ -124,10 +121,9 @@
throw new QueryException(queryLanguage + " is not a supported query language!");
}
Map<String, Object> requestMap = ast.getRequestMap();
- mapper.writeValue(new File(outFile), requestMap);
+// mapper.writeValue(new File(outFile), requestMap);
}
- // change page to startindex
public QuerySerializer setQuery(String query, String ql, String version)
throws QueryException {
try {
@@ -148,17 +144,13 @@
} catch (QueryException e) {
throw e;
} catch (Exception e) {
- throw new QueryException("UNKNOWN: Query could not be parsed");
+ throw new QueryException("UNKNOWN: Query could not be parsed (" + query + ")");
}
return this;
}
public final String build() {
- try {
- return mapper.writeValueAsString(raw());
- } catch (IOException e) {
- return "";
- }
+ return JsonUtils.toJSON(raw());
}
@@ -183,6 +175,11 @@
return this;
}
+ public QuerySerializer setMeta(MetaQuery meta) {
+ this.meta = meta.raw();
+ return this;
+ }
+
public QuerySerializer setCollection(String collection) {
CollectionQuery qobj = new CollectionQuery();
qobj.addResource(collection);