Skip to content

Commit

Permalink
acceptnull parameter added
Browse files Browse the repository at this point in the history
  • Loading branch information
tibrewalpratik17 committed Mar 9, 2024
1 parent d20e0a8 commit 06b3db7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -840,12 +840,16 @@ public static boolean like(String inputStr, String likePatternStr) {
* want to treat that column value as valid.
*
* @param inputStr Input string to test for valid json
* @param acceptNull boolean value to decide whether null is accepted as a valid json or not
* @return in case of null value, it returns whatever acceptNull parameter value is. In case of non-null,
* it returns true in case of valid json parsing else false
*
*/
@ScalarFunction(nullableParameters = true, names = {"isJson", "is_json"})
public static boolean isJson(@Nullable String inputStr) {
public static boolean isJson(@Nullable String inputStr, boolean acceptNull) {
try {
if (inputStr == null) {
return true;
return acceptNull;
}
JsonUtils.stringToJsonNode(inputStr);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,21 @@ public class StringFunctionsTest {
@DataProvider(name = "isJson")
public static Object[][] isJsonTestCases() {
return new Object[][]{
{null, true},
{"", true},
{"{\"key\": \"value\"}", true},
{"{\"key\": \"value\", }", false},
{"{\"key\": \"va", false},
{null, true, true},
{null, false, false},
{"", true, true},
{"{\"key\": \"value\"}", true, true},
{"{\"key\": \"value\", }", true, false},
{"{\"key\": \"va", true, false},
{"", false, true},
{"{\"key\": \"value\"}", false, true},
{"{\"key\": \"value\", }", false, false},
{"{\"key\": \"va", false, false},
};
}

@Test(dataProvider = "isJson")
public void testIsJson(String input, boolean expectedValue) {
assertEquals(StringFunctions.isJson(input), expectedValue);
public void testIsJson(String input, boolean acceptNull, boolean expectedValue) {
assertEquals(StringFunctions.isJson(input, acceptNull), expectedValue);
}
}

0 comments on commit 06b3db7

Please sign in to comment.