Co-authored-by: Antonin Delpeuch <antonin@delpeuch.eu>
This commit is contained in:
parent
a1c190a5dc
commit
f4833beb91
@ -36,7 +36,10 @@ package com.google.refine.expr.functions;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Properties;
|
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.Function;
|
||||||
|
import com.google.refine.grel.ControlFunctionRegistry;
|
||||||
import com.google.refine.util.ParsingUtilities;
|
import com.google.refine.util.ParsingUtilities;
|
||||||
|
|
||||||
public class Jsonize implements Function {
|
public class Jsonize implements Function {
|
||||||
@ -45,12 +48,13 @@ 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) {
|
||||||
try {
|
try {
|
||||||
return ParsingUtilities.mapper.writeValueAsString(args[0]);
|
return ParsingUtilities.mapper.writerWithDefaultPrettyPrinter().writeValueAsString(args[0]);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
String errorMessage = " accepts a single argument";
|
||||||
|
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + errorMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -26,10 +26,33 @@
|
|||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
package com.google.refine.expr.functions;
|
package com.google.refine.expr.functions;
|
||||||
|
|
||||||
|
import static org.testng.Assert.assertEquals;
|
||||||
|
import static org.testng.Assert.assertTrue;
|
||||||
|
|
||||||
import org.testng.annotations.Test;
|
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;
|
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\" ] ]");
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -109,7 +109,7 @@ public class FunctionTests extends RefineTest {
|
|||||||
Set<String> valid0args = new HashSet<>(Arrays.asList("now")); // valid 0-arg returns datetype
|
Set<String> 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
|
// Not sure which, if any, of these are intended, but fixing them may break existing scripts
|
||||||
Set<String> returnsNull = new HashSet<>(Arrays.asList("chomp", "contains", "escape", "unescape", "exp",
|
Set<String> 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
|
"slice", "substring", // synonyms for Slice
|
||||||
"unicode", "unicodeType"
|
"unicode", "unicodeType"
|
||||||
));
|
));
|
||||||
|
Loading…
Reference in New Issue
Block a user