diff --git a/main/src/com/google/refine/expr/functions/Jsonize.java b/main/src/com/google/refine/expr/functions/Jsonize.java index 10ea0c3ad..464d3436a 100644 --- a/main/src/com/google/refine/expr/functions/Jsonize.java +++ b/main/src/com/google/refine/expr/functions/Jsonize.java @@ -36,7 +36,10 @@ package com.google.refine.expr.functions; import java.io.IOException; import java.util.Properties; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.google.refine.expr.EvalError; import com.google.refine.grel.Function; +import com.google.refine.grel.ControlFunctionRegistry; import com.google.refine.util.ParsingUtilities; public class Jsonize implements Function { @@ -45,12 +48,13 @@ public class Jsonize implements Function { public Object call(Properties bindings, Object[] args) { if (args.length >= 1) { try { - return ParsingUtilities.mapper.writeValueAsString(args[0]); + return ParsingUtilities.mapper.writerWithDefaultPrettyPrinter().writeValueAsString(args[0]); } catch (IOException e) { throw new RuntimeException(e); } } - return null; + String errorMessage = " accepts a single argument"; + return new EvalError(ControlFunctionRegistry.getFunctionName(this) + errorMessage); } diff --git a/main/tests/server/src/com/google/refine/expr/functions/JsonizeTests.java b/main/tests/server/src/com/google/refine/expr/functions/JsonizeTests.java index aa51f3c78..9e7ba61ac 100644 --- a/main/tests/server/src/com/google/refine/expr/functions/JsonizeTests.java +++ b/main/tests/server/src/com/google/refine/expr/functions/JsonizeTests.java @@ -26,10 +26,33 @@ ******************************************************************************/ package com.google.refine.expr.functions; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; + import org.testng.annotations.Test; +import com.google.refine.RefineTest; +import com.google.refine.expr.EvalError; +import com.google.refine.expr.util.CalendarParser; +import com.google.refine.expr.util.CalendarParserException; import com.google.refine.util.TestUtils; -public class JsonizeTests { +public class JsonizeTests extends RefineTest { + + @Test + public void testToString() throws CalendarParserException { + + assertTrue(invoke("jsonize") instanceof EvalError); + + Object[] emptyArray = {}; + assertEquals(invoke("jsonize", (Object) emptyArray), "[ ]"); + + Object[] objArray = {4, "hello", true, 0.01, null}; + assertEquals(invoke("jsonize", (Object) objArray), "[ 4, \"hello\", true, 0.01, null ]"); + + Object[][] multiArray = {{"OpenRefine", 12}, {13, 4.6}, {"data", "mining"}}; + assertEquals(invoke("jsonize", (Object) multiArray), "[ [ \"OpenRefine\", 12 ], [ 13, 4.6 ], [ \"data\", \"mining\" ] ]"); + + } } diff --git a/main/tests/server/src/com/google/refine/grel/FunctionTests.java b/main/tests/server/src/com/google/refine/grel/FunctionTests.java index 1bf04b96e..481cc5429 100644 --- a/main/tests/server/src/com/google/refine/grel/FunctionTests.java +++ b/main/tests/server/src/com/google/refine/grel/FunctionTests.java @@ -109,7 +109,7 @@ public class FunctionTests extends RefineTest { Set valid0args = new HashSet<>(Arrays.asList("now")); // valid 0-arg returns datetype // Not sure which, if any, of these are intended, but fixing them may break existing scripts Set returnsNull = new HashSet<>(Arrays.asList("chomp", "contains", "escape", "unescape", "exp", - "fingerprint", "get", "jsonize", "parseJson", "partition", "pow", "rpartition", + "fingerprint", "get", "parseJson", "partition", "pow", "rpartition", "slice", "substring", // synonyms for Slice "unicode", "unicodeType" ));