Fix fields serialization

Change-Id: I717c7ccb7b9a58050636cae3c8a91bfcbd64e4fb
diff --git a/Changes b/Changes
index 5732d43..2dda6e1 100644
--- a/Changes
+++ b/Changes
@@ -17,6 +17,7 @@
 	- [bugfix] Fixed treatment of several collection types (diewald)
 	- [bugfix] Remove legacy error handling in Match (diewald)
 	- [bugfix] JSON-serialization bug in match in case of error messages (diewald)
+	- [bugfix] 'fields' serialization (diewald)
 
 0.51 2015-03-17
         - This is a major version (prepared for the GitHub release)
diff --git a/src/main/java/de/ids_mannheim/korap/Krill.java b/src/main/java/de/ids_mannheim/korap/Krill.java
index 8a5789a..6dfed6b 100644
--- a/src/main/java/de/ids_mannheim/korap/Krill.java
+++ b/src/main/java/de/ids_mannheim/korap/Krill.java
@@ -55,7 +55,8 @@
  * @see KrillMeta
  * @see KrillIndex
  */
-// Todo: Use a krill.properties configuration file
+// TODO: Use a krill.properties configuration file
+// TODO: Reuse passed JSON object instead of creating a new response!
 public class Krill extends Response {
     private KrillIndex index;
     private SpanQuery spanQuery;
@@ -311,9 +312,9 @@
             kr.copyNotificationsFrom(this);
         };
 
-        // kr.setRequest(this.request);
         kr.setQuery(this.getQuery());
         kr.setCollection(this.getCollection());
+        kr.setMeta(this.getMeta());
 
         return kr;
     };
diff --git a/src/main/java/de/ids_mannheim/korap/KrillMeta.java b/src/main/java/de/ids_mannheim/korap/KrillMeta.java
index 743b450..7be6a9f 100644
--- a/src/main/java/de/ids_mannheim/korap/KrillMeta.java
+++ b/src/main/java/de/ids_mannheim/korap/KrillMeta.java
@@ -323,9 +323,6 @@
         ObjectNode json = mapper.createObjectNode();
         // json.put("@type", "koral:meta");
 
-        ArrayNode fieldNode = mapper.createArrayNode();
-        fieldNode.addPOJO(this.fields);
-
         // <legacy>
         // Add cutOff attribute
         if (this.cutOff)
@@ -349,6 +346,10 @@
         json.put("context", this.getContext().toJsonNode());
 
         // Add fields attribute
+        ArrayNode fieldNode = mapper.createArrayNode();
+        Iterator<String> field = this.fields.iterator();
+        while (field.hasNext())
+            fieldNode.add(field.next());
         json.put("fields", fieldNode);
 
         // Add itemsPerResource attribute
diff --git a/src/main/java/de/ids_mannheim/korap/response/Result.java b/src/main/java/de/ids_mannheim/korap/response/Result.java
index bc57674..067d0f6 100644
--- a/src/main/java/de/ids_mannheim/korap/response/Result.java
+++ b/src/main/java/de/ids_mannheim/korap/response/Result.java
@@ -306,8 +306,22 @@
     @Deprecated
     public String toTokenListJsonString () {
         ObjectNode json = (ObjectNode) mapper.valueToTree(super.toJsonNode());
-        // ObjectNode json = (ObjectNode) mapper.valueToTree(this);
-        this._addMeta(json);
+
+        // Meta data for paging
+        ObjectNode meta = json.has("meta") ? (ObjectNode) json.get("meta")
+                : (ObjectNode) json.putObject("meta");
+
+        if (this.context != null)
+            meta.put("context", this.getContext().toJsonNode());
+
+        meta.put("itemsPerPage", this.itemsPerPage);
+        meta.put("startIndex", this.startIndex);
+
+        if (this.itemsPerResource > 0)
+            meta.put("itemsPerResource", this.itemsPerResource);
+
+        if (this.serialQuery != null)
+            meta.put("serialQuery", this.serialQuery);
 
         ArrayNode array = json.putArray("matches");
 
@@ -327,31 +341,20 @@
 
 
     private void _addMeta (ObjectNode json) {
-        ObjectNode meta = json.has("meta") ? (ObjectNode) json.get("meta")
-                : (ObjectNode) json.putObject("meta");
+        ObjectNode meta = (ObjectNode) this.getMeta().toJsonNode();
 
-
-        // Relevant context setting
-        if (this.context != null)
-            meta.put("context", this.getContext().toJsonNode());
-
-        // ItemsPerPage
-        meta.put("itemsPerPage", this.itemsPerPage);
-
-        // Relevant itemsPerResource setting
-        if (this.itemsPerResource > 0)
-            meta.put("itemsPerResource", this.itemsPerResource);
-
-        // TODO: <test>
-        /*
-        if (this.request != null)
-            json.put("request", this.request);
-        */
+        // Lucene query for debugging purposes
         if (this.serialQuery != null)
             meta.put("serialQuery", this.serialQuery);
-        // </test>
 
+        // This may override count
+        meta.put("itemsPerPage", this.itemsPerPage);
 
-        meta.put("startIndex", this.startIndex);
+        if (json.has("meta")) {
+            ((ObjectNode) json.get("meta")).putAll(meta);
+        }
+        else {
+            json.put("meta", meta);
+        };
     };
 };
diff --git a/src/test/java/de/ids_mannheim/korap/search/TestMetaFields.java b/src/test/java/de/ids_mannheim/korap/search/TestMetaFields.java
index b48263c..3b774ac 100644
--- a/src/test/java/de/ids_mannheim/korap/search/TestMetaFields.java
+++ b/src/test/java/de/ids_mannheim/korap/search/TestMetaFields.java
@@ -54,6 +54,19 @@
 
         ObjectMapper mapper = new ObjectMapper();
         JsonNode res = mapper.readTree(kr.toJsonString());
+
+        // System.err.println(res.toString());
+        // mirror fields
+        assertEquals(9, res.at("/meta/count").asInt());
+
+        if (res.at("/meta/fields/0").asText().equals("UID")) {
+            assertEquals("corpusID", res.at("/meta/fields/1").asText());
+        }
+        else {
+            assertEquals("corpusID", res.at("/meta/fields/0").asText());
+            assertEquals("UID", res.at("/meta/fields/1").asText());
+        };
+
         assertEquals(0, res.at("/matches/0/UID").asInt());
         assertEquals("WPD", res.at("/matches/0/corpusID").asText());
         assertTrue(res.at("/matches/0/docID").isMissingNode());
diff --git a/src/test/java/de/ids_mannheim/korap/search/TestResult.java b/src/test/java/de/ids_mannheim/korap/search/TestResult.java
index 6910ffb..53a42f1 100644
--- a/src/test/java/de/ids_mannheim/korap/search/TestResult.java
+++ b/src/test/java/de/ids_mannheim/korap/search/TestResult.java
@@ -162,14 +162,12 @@
         assertEquals(0, res.at("/meta/startIndex").asInt());
 
         // Request meta
-        // assertEquals(1, res.at("/request/meta/startPage").asInt());
-        // assertEquals(5, res.at("/request/meta/count").asInt());
-        /*
-        assertEquals("token", res.at("/request/meta/context/left/0").asText());
-        assertEquals(3, res.at("/request/meta/context/left/1").asInt());
-        assertEquals("char", res.at("/request/meta/context/right/0").asText());
-        assertEquals(6, res.at("/request/meta/context/right/1").asInt());
-        */
+        // assertEquals(1, res.at("/meta/startPage").asInt());
+        assertEquals(5, res.at("/meta/count").asInt());
+        assertEquals("token", res.at("/meta/context/left/0").asText());
+        assertEquals(3, res.at("/meta/context/left/1").asInt());
+        assertEquals("char", res.at("/meta/context/right/0").asText());
+        assertEquals(6, res.at("/meta/context/right/1").asInt());
         assertEquals("token", res.at("/meta/context/left/0").asText());
         assertEquals(3, res.at("/meta/context/left/1").asInt());
         assertEquals("char", res.at("/meta/context/right/0").asText());