-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Upgrade to JsonPath 2.7.0 #7819
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Jackie-Jiang
merged 2 commits into
apache:master
from
richardstartin:jsonpath-exceptions
Feb 1, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ | |
| import com.google.common.annotations.VisibleForTesting; | ||
| import com.jayway.jsonpath.Configuration; | ||
| import com.jayway.jsonpath.JsonPath; | ||
| import com.jayway.jsonpath.Option; | ||
| import com.jayway.jsonpath.ParseContext; | ||
| import com.jayway.jsonpath.Predicate; | ||
| import com.jayway.jsonpath.spi.cache.CacheProvider; | ||
|
|
@@ -50,10 +51,12 @@ public class JsonFunctions { | |
| private JsonFunctions() { | ||
| } | ||
|
|
||
| private static final Object[] EMPTY = new Object[0]; | ||
| private static final Predicate[] NO_PREDICATES = new Predicate[0]; | ||
| private static final ParseContext PARSE_CONTEXT = JsonPath.using( | ||
| new Configuration.ConfigurationBuilder().jsonProvider(new ArrayAwareJacksonJsonProvider()) | ||
| .mappingProvider(new JacksonMappingProvider()).build()); | ||
| .mappingProvider(new JacksonMappingProvider()).options(Option.SUPPRESS_EXCEPTIONS) | ||
| .build()); | ||
|
|
||
| static { | ||
| // Set the JsonPath cache before the cache is accessed | ||
|
|
@@ -93,8 +96,7 @@ public static Object jsonPath(Object object, String jsonPath) { | |
| * Extract object array based on Json path | ||
| */ | ||
| @ScalarFunction | ||
| public static Object[] jsonPathArray(Object object, String jsonPath) | ||
| throws JsonProcessingException { | ||
| public static Object[] jsonPathArray(Object object, String jsonPath) { | ||
| if (object instanceof String) { | ||
| return convertObjectToArray(PARSE_CONTEXT.parse((String) object).read(jsonPath, NO_PREDICATES)); | ||
| } | ||
|
|
@@ -103,18 +105,17 @@ public static Object[] jsonPathArray(Object object, String jsonPath) | |
|
|
||
| @ScalarFunction | ||
| public static Object[] jsonPathArrayDefaultEmpty(Object object, String jsonPath) { | ||
| try { | ||
| return jsonPathArray(object, jsonPath); | ||
| } catch (Exception e) { | ||
| return new Object[0]; | ||
| } | ||
| Object[] result = jsonPathArray(object, jsonPath); | ||
| return result == null ? EMPTY : result; | ||
| } | ||
|
|
||
| private static Object[] convertObjectToArray(Object arrayObject) { | ||
| if (arrayObject instanceof List) { | ||
| return ((List) arrayObject).toArray(); | ||
| } else if (arrayObject instanceof Object[]) { | ||
| return (Object[]) arrayObject; | ||
| } else if (arrayObject == null) { | ||
| return null; | ||
| } | ||
| return new Object[]{arrayObject}; | ||
| } | ||
|
|
@@ -129,17 +130,21 @@ public static String jsonPathString(Object object, String jsonPath) | |
| if (jsonValue instanceof String) { | ||
| return (String) jsonValue; | ||
| } | ||
| return JsonUtils.objectToString(jsonValue); | ||
| return jsonValue == null ? null : JsonUtils.objectToString(jsonValue); | ||
| } | ||
|
|
||
| /** | ||
| * Extract from Json with path to String | ||
| */ | ||
| @ScalarFunction | ||
| public static String jsonPathString(Object object, String jsonPath, String defaultValue) { | ||
| Object jsonValue = jsonPath(object, jsonPath); | ||
| if (jsonValue instanceof String) { | ||
| return (String) jsonValue; | ||
| } | ||
| try { | ||
| return jsonPathString(object, jsonPath); | ||
| } catch (Exception e) { | ||
| return jsonValue == null ? defaultValue : JsonUtils.objectToString(jsonValue); | ||
| } catch (JsonProcessingException e) { | ||
| return defaultValue; | ||
| } | ||
| } | ||
|
|
@@ -149,50 +154,45 @@ public static String jsonPathString(Object object, String jsonPath, String defau | |
| */ | ||
| @ScalarFunction | ||
| public static long jsonPathLong(Object object, String jsonPath) { | ||
| final Object jsonValue = jsonPath(object, jsonPath); | ||
| if (jsonValue == null) { | ||
| return Long.MIN_VALUE; | ||
| } | ||
| if (jsonValue instanceof Number) { | ||
| return ((Number) jsonValue).longValue(); | ||
| } | ||
| return Long.parseLong(jsonValue.toString()); | ||
| return jsonPathLong(object, jsonPath, Long.MIN_VALUE); | ||
| } | ||
|
|
||
| /** | ||
| * Extract from Json with path to Long | ||
| */ | ||
| @ScalarFunction | ||
| public static long jsonPathLong(Object object, String jsonPath, long defaultValue) { | ||
| try { | ||
| return jsonPathLong(object, jsonPath); | ||
| } catch (Exception e) { | ||
| Object jsonValue = jsonPath(object, jsonPath); | ||
| if (jsonValue == null) { | ||
| return defaultValue; | ||
| } | ||
| if (jsonValue instanceof Number) { | ||
| return ((Number) jsonValue).longValue(); | ||
| } | ||
| return Long.parseLong(jsonValue.toString()); | ||
| } | ||
|
|
||
| /** | ||
| * Extract from Json with path to Double | ||
| */ | ||
| @ScalarFunction | ||
| public static double jsonPathDouble(Object object, String jsonPath) { | ||
| final Object jsonValue = jsonPath(object, jsonPath); | ||
| if (jsonValue instanceof Number) { | ||
| return ((Number) jsonValue).doubleValue(); | ||
| } | ||
| return Double.parseDouble(jsonValue.toString()); | ||
| return jsonPathDouble(object, jsonPath, Double.NaN); | ||
|
||
| } | ||
|
|
||
| /** | ||
| * Extract from Json with path to Double | ||
| */ | ||
| @ScalarFunction | ||
| public static double jsonPathDouble(Object object, String jsonPath, double defaultValue) { | ||
| try { | ||
| return jsonPathDouble(object, jsonPath); | ||
| } catch (Exception e) { | ||
| Object jsonValue = jsonPath(object, jsonPath); | ||
| if (jsonValue == null) { | ||
| return defaultValue; | ||
| } | ||
| if (jsonValue instanceof Number) { | ||
| return ((Number) jsonValue).doubleValue(); | ||
| } | ||
| return Double.parseDouble(jsonValue.toString()); | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
| */ | ||
| package org.apache.pinot.common.function.scalar; | ||
|
|
||
| import com.jayway.jsonpath.JsonPathException; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.HashMap; | ||
|
|
@@ -48,15 +49,11 @@ public void testArrayLength() { | |
|
|
||
| List<Object> dataInList = Arrays.asList("abc", "efg", "hij"); | ||
| assertEquals(jp.length(dataInList), 3); | ||
| } | ||
|
|
||
| try { | ||
| jp.length(null); | ||
| fail(); | ||
| } catch (NullPointerException e) { | ||
| // It's supposed to get a JsonPathException, but JsonPath library actually | ||
| // has a bug leading to NullPointerException while creating the JsonPathException. | ||
|
||
| assertNull(e.getMessage()); | ||
| } | ||
| @Test(expectedExceptions = JsonPathException.class) | ||
| public void testArrayLengthThrowsForNullArray() { | ||
| new JsonFunctions.ArrayAwareJacksonJsonProvider().length(null); | ||
| } | ||
|
|
||
| @Test | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.