Fix #4194: pretty printing and tests for the jsonize function (#4307)

Co-authored-by: Antonin Delpeuch <antonin@delpeuch.eu>
This commit is contained in:
Mengfan Zhu 2021-12-05 01:41:56 -06:00 committed by GitHub
parent a1c190a5dc
commit f4833beb91
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 4 deletions

View File

@ -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);
}

View File

@ -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\" ] ]");
}
}

View File

@ -109,7 +109,7 @@ public class FunctionTests extends RefineTest {
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
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
"unicode", "unicodeType"
));