Skip to content

Commit 4a0ab9a

Browse files
Add gradle configurations for jsonpath operation
1 parent cb140d3 commit 4a0ab9a

17 files changed

+686
-333
lines changed

ballerina/build.gradle

+3
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ task updateTomlFiles {
6969
doLast {
7070
def newConfig = ballerinaTomlFilePlaceHolder.text.replace("@project.version@", project.version)
7171
newConfig = newConfig.replace("@toml.version@", tomlVersion)
72+
newConfig = newConfig.replace("@jsonpath.version@", project.javaJsonPathVersion)
73+
newConfig = newConfig.replace("@jsonsmart.version@", project.javaJsonSmartVersion)
74+
newConfig = newConfig.replace("@accessors.version@", project.javaAccessorsSmartVersion)
7275
ballerinaTomlFile.text = newConfig
7376

7477
def newCompilerPluginToml = compilerPluginTomlFilePlaceHolder.text.replace("@project.version@", project.version)

ballerina/errors.bal

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@
1414
// specific language governing permissions and limitations
1515
// under the License.
1616

17-
# Represents any error related to Ballerina JsonPath module
17+
# Represents the error type of the ballerina/data.jsondata module. This error type represents any error that can occur
18+
# during the execution of jsondata APIs.
1819
public type Error distinct error;

ballerina/init.bal

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ isolated function init() {
2121
}
2222

2323
isolated function setModule() = @java:Method {
24-
'class: "io.ballerina.lib.data.jsondata.utils.ModuleUtils"
24+
'class: "io.ballerina.lib.data.ModuleUtils"
2525
} external;

ballerina/json_api.bal

-4
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,6 @@ public type Options record {|
6666
boolean allowDataProjection = true;
6767
|};
6868

69-
# Represents the error type of the ballerina/data.jsondata module. This error type represents any error that can occur
70-
# during the execution of jsondata APIs.
71-
public type Error distinct error;
72-
7369
# Defines the name of the JSON Object key.
7470
#
7571
# + value - The name of the JSON Object key

ballerina/jsonpath.bal ballerina/read.bal

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,5 @@ public class JsonPathRawTemplateImpl {
4747
# + query - JSON path expression
4848
# + return - extracted details as JSON value, a jsonpath:Error otherwise
4949
public isolated function readJson(json 'json, JsonPathRawTemplateImpl query) returns json|Error = @java:Method {
50-
'class: "io.ballerina.stdlib.jsonpath.BJsonPath"
50+
'class: "io.ballerina.lib.data.jsonpath.BJsonPath"
5151
} external;

ballerina/tests/jsonpath_complex_tests.bal

+35-16
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,8 @@ function testSelectElementByNumericalConditionalExpression() returns error? {
516516
JsonPathRawTemplate condition7 = `$[?(@.a2 > 1.1)]`;
517517
JsonPathRawTemplate condition8 = `$[?(@.a1 == 2.34)]`;
518518
JsonPathRawTemplate condition9 = `$[?(@.a1 == 12.34)]`;
519+
JsonPathRawTemplate condition10 = `$[?(@.a1 != 12.34)]`;
520+
JsonPathRawTemplate condition11 = `$[?(@.a1 <= ${d1} && @.a1 > ${i1})]`;
519521

520522
json result = check read([decimalJson, floatJson, intJson], condition1);
521523
test:assertTrue(result is json[]);
@@ -552,6 +554,14 @@ function testSelectElementByNumericalConditionalExpression() returns error? {
552554
result = check read([decimalJson, floatJson, intJson], condition9);
553555
test:assertTrue(result is json[]);
554556
test:assertEquals(result, <json[]>[]);
557+
558+
result = check read([decimalJson, floatJson, intJson], condition10);
559+
test:assertTrue(result is json[]);
560+
test:assertEquals(result, [decimalJson, floatJson, intJson]);
561+
562+
result = check read([decimalJson, floatJson, intJson], condition11);
563+
test:assertTrue(result is json[]);
564+
test:assertEquals(result, <json[]>[decimalJson, floatJson]);
555565
}
556566

557567
@test:Config {}
@@ -638,22 +648,6 @@ function testSelectElementByPatternMatchingExpression() returns error? {
638648
test:assertEquals(result, {"a1": 2.34, "a2": 3.65});
639649
}
640650

641-
@test:Config {}
642-
function testA() returns error? {
643-
644-
json result = check read(j4, `$.a2[1][?(@.a2 in ['string', 'string2'])]`);
645-
test:assertEquals(result, <json[]>[stringJson]);
646-
647-
result = check read(j4, `$.a2[1][?(@.a2 nin ['string', 'string2'])]`);
648-
test:assertEquals(result, <json[]>[]);
649-
650-
result = check read(j4, `$.a2[1][?(@.a1 nin ['string', 'string2'])]`);
651-
test:assertEquals(result, <json[]>[stringJson]);
652-
653-
result = check read(j4, `$.a2[1][?(@.a1 in ['string', 'string2'])]`);
654-
test:assertEquals(result, <json[]>[]);
655-
}
656-
657651
@test:Config {}
658652
function testFunctionExpression() returns error? {
659653
json result = check read(j4, `$..a1.sum()`);
@@ -716,3 +710,28 @@ function testFunctionExpression() returns error? {
716710
result = check read(decimalJson, `$['a1'].keys()`);
717711
test:assertEquals(result, ());
718712
}
713+
714+
@test:Config {}
715+
function testListOperations() returns error? {
716+
717+
json result = check read(j4, `$.a2[1][?(@.a2 in ['string', 'string2'])]`);
718+
test:assertEquals(result, <json[]>[stringJson]);
719+
720+
result = check read(j4, `$.a2[1][?(@.a2 nin ['string', 'string2'])]`);
721+
test:assertEquals(result, <json[]>[]);
722+
723+
result = check read(j4, `$.a2[1][?(@.a1 nin ['string', 'string2'])]`);
724+
test:assertEquals(result, <json[]>[stringJson]);
725+
726+
result = check read(j4, `$.a2[1][?(@.a1 in ['string', 'string2'])]`);
727+
test:assertEquals(result, <json[]>[]);
728+
}
729+
730+
@test:Config {}
731+
function testGeneralJsonPathExpresions() returns error? {
732+
json result = check read(j5, `$`);
733+
test:assertEquals(result, j5);
734+
735+
result = check read(j5, `@`);
736+
test:assertEquals(result, j5);
737+
}

ballerina/tests/error_test.bal ballerina/tests/jsonpath_error_test.bal

+8
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,12 @@ function errorTest() {
5353
result = read(j1, `$.a1[1]`);
5454
test:assertTrue(result is Error);
5555
test:assertEquals((<Error> result).message(), "Unable to execute query '$.a1[1]' on the provided JSON value");
56+
57+
result = read((), `$.a1[1]`);
58+
test:assertTrue(result is Error);
59+
test:assertEquals((<Error> result).message(), "json object can not be null");
60+
61+
result = read(j1, ``);
62+
test:assertTrue(result is Error);
63+
test:assertEquals((<Error> result).message(), "path can not be null or empty");
5664
}

0 commit comments

Comments
 (0)