From eee4514643bbbfee15fcd1446ce9d0dde518ada9 Mon Sep 17 00:00:00 2001 From: Stefano Mazzocchi Date: Thu, 16 Sep 2010 23:05:53 +0000 Subject: [PATCH] fixing Issue-125 git-svn-id: http://google-refine.googlecode.com/svn/trunk@1269 7d457c2a-affb-35e4-300a-418c747d4874 --- .../gridworks/expr/functions/Jsonize.java | 35 ++++++++++++++----- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/main/src/com/google/gridworks/expr/functions/Jsonize.java b/main/src/com/google/gridworks/expr/functions/Jsonize.java index aeb12fa22..a6a0392cc 100644 --- a/main/src/com/google/gridworks/expr/functions/Jsonize.java +++ b/main/src/com/google/gridworks/expr/functions/Jsonize.java @@ -1,7 +1,10 @@ package com.google.gridworks.expr.functions; +import java.util.Collection; +import java.util.Map; import java.util.Properties; +import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.json.JSONWriter; @@ -12,14 +15,30 @@ public class Jsonize implements Function { public Object call(Properties bindings, Object[] args) { if (args.length >= 1) { - Object o1 = args[0]; - if (o1 == null) { - return "null"; - } else if (o1 instanceof Number || o1 instanceof Boolean) { - return o1.toString(); - } else { - return JSONObject.quote(o1 instanceof String ? (String) o1 : o1.toString()); - } + try { + Object o1 = args[0]; + if (o1 == null) { + return "null"; + } else if (o1 instanceof Number) { + return JSONObject.numberToString((Number) o1); + } 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; }