/**
 * 
 */
package org.intermine.webservice.server.output;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.intermine.webservice.server.core.ResultProcessor;
import org.intermine.api.results.ExportResultsIterator;
import org.intermine.api.results.ResultElement;
import org.intermine.webservice.server.output.Output;
import org.json.JSONObject;

/**
 * @author Alexis Kalderimis
 *
 */
public class JSONObjResultProcessor extends ResultProcessor {

	
	/**
	 * Constructor.
	 */
	public JSONObjResultProcessor() {
		// Empty constructor
	}
	
	@Override
	public void write(Iterator<List<ResultElement>> resultIt, Output output) {
		if (! (resultIt instanceof ExportResultsIterator)) {
			throw new IllegalArgumentException("The iterator must be an ExportResultsIterator");
		}
		ExportResultsIterator exportIter = (ExportResultsIterator) resultIt;
		JSONResultsIterator jsonIter = new JSONResultsIterator(exportIter);
		while (jsonIter.hasNext()) {
			JSONObject next = jsonIter.next();
			List<String> outputLine = new ArrayList<String>(
					Arrays.asList(next.toString()));
			if (jsonIter.hasNext()) { outputLine.add(""); };
			output.addResultItem(outputLine);
		}
	}
}
@@ -1,16 +1,4 @@
-/**%0A * %0A */%0A
 pack
@@ -46,219 +46,323 @@
 t;%0A%0A
-import java.util.ArrayList;%0Aimport java.util.Arrays;%0Aimport java.util.Iterator;%0Aimport java.util.List;%0A%0Aimport org.intermine.webservice.server.core.ResultProcessor;%0Aimport org.intermine.api.results.ExportResults
+/*%0A * Copyright (C) 2002-2011 FlyMine%0A *%0A * This code may be freely distributed and modified under the%0A * terms of the GNU Lesser General Public Licence.  This should%0A * be distributed with the code.  See the LICENSE file for more%0A * information or http://www.gnu.org/copyleft/lesser.html.%0A *%0A */%0A%0Aimport java.util.
 Iter
@@ -367,16 +367,17 @@
 erator;%0A
+%0A
 import o
@@ -405,103 +405,29 @@
 lts.
-ResultElement;%0Aimport org.intermine.webservice.server.output.Output;%0Aimport org.json.JSONObject
+ExportResultsIterator
 ;%0A%0A/
@@ -509,16 +509,20 @@
 extends 
+JSON
 ResultPr
@@ -532,20 +532,24 @@
 ssor
- %7B%0A%0A%09%0A%09/**%0A%09
+%0A%7B%0A%0A    /**%0A    
  * C
@@ -564,14 +564,20 @@
 or.%0A
-%09 */%0A%09
+     */%0A    
 publ
@@ -606,18 +606,24 @@
 sor() %7B%0A
-%09%09
+        
 // Empty
@@ -639,223 +639,72 @@
 tor%0A
-%09%7D%0A%09%0A%09@Override%0A%09public void write(Iterator%3CList%3CResultElement%3E%3E resultIt, Output output) %7B%0A%09%09if (! (resultIt instanceof ExportResultsIterator)) %7B%0A%09%09%09throw new IllegalArgumentException(%22The iterator must be an Expor
+    %7D%0A%0A    @Override%0A    protected Iterator%3C? extends Object%3E ge
 tRes
@@ -719,18 +719,9 @@
 ator
-%22);%0A%09%09%7D%0A%09%09
+(
 Expo
@@ -742,57 +742,22 @@
 tor 
-exportIter = (ExportResultsIterator) resultIt;%0A%09%09
+it) %7B%0A        
 JSON
@@ -811,273 +811,43 @@
 tor(
-exportIter);%0A%09%09while (jsonIter.hasNext()) %7B%0A%09%09%09JSONObject next = jsonIter.next();%0A%09%09%09List%3CString%3E outputLine = new ArrayList%3CString%3E(%0A%09%09%09%09%09Arrays.asList(next.toString()));%0A%09%09%09if (jsonIter.hasNext()) %7B outputLine.add(%22%22); %7D;%0A%09%09%09output.addResultItem(outputLine);%0A%09%09%7D%0A%09%7D%0A%7D
+it);%0A        return jsonIter;%0A    %7D%0A%0A%7D%0A

Uses the abstract parent class