fixing Issue-125

git-svn-id: http://google-refine.googlecode.com/svn/trunk@1269 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
Stefano Mazzocchi 2010-09-16 23:05:53 +00:00
parent 9c5eda343a
commit eee4514643

View File

@ -1,7 +1,10 @@
package com.google.gridworks.expr.functions; package com.google.gridworks.expr.functions;
import java.util.Collection;
import java.util.Map;
import java.util.Properties; import java.util.Properties;
import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.json.JSONWriter; import org.json.JSONWriter;
@ -12,14 +15,30 @@ public class Jsonize implements Function {
public Object call(Properties bindings, Object[] args) { public Object call(Properties bindings, Object[] args) {
if (args.length >= 1) { if (args.length >= 1) {
Object o1 = args[0]; try {
if (o1 == null) { Object o1 = args[0];
return "null"; if (o1 == null) {
} else if (o1 instanceof Number || o1 instanceof Boolean) { return "null";
return o1.toString(); } else if (o1 instanceof Number) {
} else { return JSONObject.numberToString((Number) o1);
return JSONObject.quote(o1 instanceof String ? (String) o1 : o1.toString()); } else if (o1 instanceof Boolean) {
} return o1.toString();
} else if (o1 instanceof JSONObject) {
return ((JSONObject) o1).toString();
} else if (o1 instanceof JSONArray) {
return ((JSONArray) o1).toString();
} else if (o1 instanceof Map) {
return new JSONObject((Map<?,?>) o1).toString();
} else if (o1 instanceof Collection) {
return new JSONArray((Collection<?>) o1).toString();
} else if (o1.getClass().isArray()) {
return new JSONArray(o1).toString();
} else {
return JSONObject.quote(o1.toString());
}
} catch (JSONException e) {
throw new RuntimeException(e);
}
} }
return null; return null;
} }