Skip to content
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

Add read functionality with Jsonpath expressions for Ballerina json datatypes #1

Merged
merged 29 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
ffdbb7c
Add read functionality with Jsonpath expressions
SasinduDilshara Jan 29, 2024
2dcbe50
Add formatting changes for gradle properties file
SasinduDilshara Jan 29, 2024
2a6221f
Add jsonpath expression tests for append operation
SasinduDilshara Jan 30, 2024
44e5a36
Update license headers in ballerna and native files
SasinduDilshara Jan 31, 2024
16738cb
Update native package path&remove mapping provider
SasinduDilshara Jan 31, 2024
75bd1d5
Update Javadoc comment in Utils file
SasinduDilshara Jan 31, 2024
9c343ff
Merge branch 'main' of https://github.com/ballerina-platform/module-b…
SasinduDilshara Mar 21, 2024
c5220cf
[Automated] Update the native jar versions
SasinduDilshara Mar 21, 2024
5e9ebe9
[Automated] Update the native jar versions
SasinduDilshara Mar 21, 2024
80e2a07
[Automated] Update the native jar versions
SasinduDilshara Mar 21, 2024
06a93e3
[Automated] Update the native jar versions
SasinduDilshara Mar 21, 2024
cb140d3
[Automated] Update the native jar versions
SasinduDilshara Mar 21, 2024
4a0ab9a
Add gradle configurations for jsonpath operation
SasinduDilshara Mar 21, 2024
8613a90
Optimize for loops with array stream in jsonpath
SasinduDilshara Mar 21, 2024
2afcb74
Remove unused imports in BJsonPathProvider
SasinduDilshara Mar 21, 2024
6014e2f
Add code to pull external jars from maven
SasinduDilshara Mar 21, 2024
a47efa1
[Automated] Update the native jar versions
SasinduDilshara Mar 22, 2024
7fbe262
[Automated] Update the native jar versions
SasinduDilshara Mar 22, 2024
0bad58e
[Automated] Update the native jar versions
SasinduDilshara Mar 22, 2024
950b32a
[Automated] Update the native jar versions
SasinduDilshara Mar 22, 2024
845a854
[Automated] Update the native jar versions
SasinduDilshara Mar 22, 2024
6ed0c9a
Add sample examples for read function
SasinduDilshara Mar 22, 2024
75463f1
Remove vscode file
SasinduDilshara Mar 22, 2024
56dc93b
Merge branch 'main' of https://github.com/ballerina-platform/module-b…
SasinduDilshara Mar 25, 2024
4d42060
Add Graalvm properties json file
SasinduDilshara Mar 25, 2024
4aa2ef8
[Automated] Update the native jar versions
SasinduDilshara Mar 26, 2024
4f3b6d5
[Automated] Update the native jar versions
SasinduDilshara Mar 26, 2024
b4cce5c
Update string type conversions in lists
SasinduDilshara Mar 27, 2024
81e9642
Update doc comments and Ballerina tomls
SasinduDilshara Apr 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@
*.zip
*.tar.gz
*.rar
bin

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
replay_pid*
.DS_Store
.gradle
.vscode
build
target
.idea
34 changes: 34 additions & 0 deletions ballerina/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[package]
org = "ballerina"
name = "data.jsondata"
version = "0.1.0"
authors = ["Ballerina"]
keywords = ["json", "json path", "json-transform", "json transform", "json to json", "json-convert", "json convert"]
repository = "https://github.com/ballerina-platform/module-ballerina-data.jsondata"
license = ["Apache-2.0"]
distribution = "2201.8.3"
export = ["data.jsondata"]

[[platform.java11.dependency]]
path = "../native/build/libs/jsonpath-native-1.0.2-SNAPSHOT.jar"
groupId = "io.ballerina.xlibb"
artifactId = "jsonpath-native"
version = "1.0.2"

[[platform.java11.dependency]]
SasinduDilshara marked this conversation as resolved.
Show resolved Hide resolved
path = "./lib/json-path-2.7.0.jar"
groupId = "com.jayway.jsonpath"
artifactId = "json-path"
version = "2.7.0"

[[platform.java11.dependency]]
path = "./lib/json-smart-2.4.7.jar"
groupId = "net.minidev.json"
artifactId = "json-smart"
version = "2.4.7"

[[platform.java11.dependency]]
path = "./lib/accessors-smart-2.4.7.jar"
groupId = "net.minidev.json"
artifactId = "accessors-smart"
version = "2.4.7"
SasinduDilshara marked this conversation as resolved.
Show resolved Hide resolved
98 changes: 98 additions & 0 deletions ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# AUTO-GENERATED FILE. DO NOT MODIFY.

# This file is auto-generated by Ballerina for managing dependency versions.
# It should not be modified by hand.

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201.8.3"

[[package]]
org = "ballerina"
name = "data.jsondata"
version = "0.1.0"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.float"},
{org = "ballerina", name = "lang.int"},
{org = "ballerina", name = "lang.object"},
{org = "ballerina", name = "test"}
]
modules = [
{org = "ballerina", packageName = "data.jsondata", moduleName = "data.jsondata"}
]

[[package]]
org = "ballerina"
name = "jballerina.java"
version = "0.0.0"
modules = [
{org = "ballerina", packageName = "jballerina.java", moduleName = "jballerina.java"}
]

[[package]]
org = "ballerina"
name = "lang.__internal"
version = "0.0.0"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.object"}
]

[[package]]
org = "ballerina"
name = "lang.error"
version = "0.0.0"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]

[[package]]
org = "ballerina"
name = "lang.float"
version = "0.0.0"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]
modules = [
{org = "ballerina", packageName = "lang.float", moduleName = "lang.float"}
]

[[package]]
org = "ballerina"
name = "lang.int"
version = "0.0.0"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.__internal"},
{org = "ballerina", name = "lang.object"}
]
modules = [
{org = "ballerina", packageName = "lang.int", moduleName = "lang.int"}
]

[[package]]
org = "ballerina"
name = "lang.object"
version = "0.0.0"
modules = [
{org = "ballerina", packageName = "lang.object", moduleName = "lang.object"}
]

[[package]]
org = "ballerina"
name = "test"
version = "0.0.0"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.error"}
]
modules = [
{org = "ballerina", packageName = "test", moduleName = "test"}
]

18 changes: 18 additions & 0 deletions ballerina/errors.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) 2024 WSO2 LLC (http://www.wso2.org) All Rights Reserved.
//
// WSO2 Inc. licenses this file to you under the Apache License,
SasinduDilshara marked this conversation as resolved.
Show resolved Hide resolved
// Version 2.0 (the "License"); you may not use this file except
// in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

# Represents any error related to Ballerina JsonPath module
public type Error distinct error;
25 changes: 25 additions & 0 deletions ballerina/init.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com) All Rights Reserved.
//
// WSO2 LLC. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
// in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

import ballerina/jballerina.java;

function init() {
setModule();
}

function setModule() = @java:Method {
SasinduDilshara marked this conversation as resolved.
Show resolved Hide resolved
'class: "io.ballerina.xlibb.jsonpath.Utils"
} external;
53 changes: 53 additions & 0 deletions ballerina/jsonpath.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) 2024 WSO2 LLC (http://www.wso2.org) All Rights Reserved.
//
// WSO2 Inc. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
// in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

import ballerina/jballerina.java;
import ballerina/lang.'object as obj;

public type JsonPathValue ()|boolean|int|float|decimal|string|json[]|map<json>;
SasinduDilshara marked this conversation as resolved.
Show resolved Hide resolved

public type JsonPathRawTemplate object {
*obj:RawTemplate;
public (string[] & readonly) strings;
SasinduDilshara marked this conversation as resolved.
Show resolved Hide resolved
public JsonPathValue[] insertions;
};

# Extract details from the given JSON value using the provided query template expression
# + 'json - JSON value
# + query - JSON path expression
# + return - extracted details as JSON value, a jsonpath:Error otherwise
public isolated function read(json 'json, JsonPathRawTemplate query) returns json|Error {
return readJson('json, new JsonPathRawTemplateImpl(query));
}

public class JsonPathRawTemplateImpl {
*object:RawTemplate;
public string[] & readonly strings;
public JsonPathValue[] insertions;
SasinduDilshara marked this conversation as resolved.
Show resolved Hide resolved

public isolated function init(JsonPathRawTemplate jsonPathRawTemplate) {
self.strings = jsonPathRawTemplate.strings;
self.insertions = jsonPathRawTemplate.insertions;
}
}

# Extract details from the given JSON value using the provided query expression
# + 'json - JSON value
# + query - JSON path expression
# + return - extracted details as JSON value, a jsonpath:Error otherwise
public isolated function readJson(json 'json, JsonPathRawTemplateImpl query) returns json|Error = @java:Method {
'class: "io.ballerina.xlibb.jsonpath.BJsonPath"
SasinduDilshara marked this conversation as resolved.
Show resolved Hide resolved
} external;
SasinduDilshara marked this conversation as resolved.
Show resolved Hide resolved
56 changes: 56 additions & 0 deletions ballerina/tests/error_test.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) 2024 WSO2 LLC (http://www.wso2.org) All Rights Reserved.
//
// WSO2 Inc. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
// in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

import ballerina/test;

@test:Config {}
function errorTest() {
json|Error result = read(j4, `$.a1[:]`);
test:assertTrue(result is Error);
test:assertEquals((<Error> result).message(), "Failed to parse SliceOperation: :");

result = read(j4, `#`);
test:assertTrue(result is Error);
test:assertEquals((<Error> result).message(), "Unable to execute query '#' on the provided JSON value");

result = read({name: ""}, `$.id`);
test:assertTrue(result is Error);
test:assertEquals((<Error> result).message(), "Unable to execute query '$.id' on the provided JSON value");

result = read("test", `$.id`);
test:assertTrue(result is Error);
test:assertEquals((<Error> result).message(), "Unable to execute query '$.id' on the provided JSON value");

result = read(1, `$.a1[:]`);
test:assertTrue(result is Error);
test:assertEquals((<Error> result).message(), "Failed to parse SliceOperation: :");

result = read([j4], `$.a3[-1]`);
test:assertTrue(result is Error);
test:assertEquals((<Error> result).message(), "Unable to execute query '$.a3[-1]' on the provided JSON value");

result = read(j4, `$.a1[-1].a12`);
test:assertTrue(result is Error);
test:assertEquals((<Error> result).message(), "Unable to execute query '$.a1[-1].a12' on the provided JSON value");

result = read(j4, `$.a40.sum()`);
test:assertTrue(result is Error);
test:assertEquals((<Error> result).message(), "Unable to execute query '$.a40.sum()' on the provided JSON value");

result = read(j1, `$.a1[1]`);
test:assertTrue(result is Error);
test:assertEquals((<Error> result).message(), "Unable to execute query '$.a1[1]' on the provided JSON value");
}
Loading