Skip to content

Commit

Permalink
Merge pull request #1 from SasinduDilshara/read
Browse files Browse the repository at this point in the history
Add read functionality with Jsonpath expressions for Ballerina json datatypes
  • Loading branch information
hasithaa authored Apr 3, 2024
2 parents 65b795c + 81e9642 commit b0d3dfe
Show file tree
Hide file tree
Showing 21 changed files with 2,259 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*.zip
*.tar.gz
*.rar
bin

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
Expand Down
25 changes: 22 additions & 3 deletions ballerina/Ballerina.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ org = "ballerina"
name = "data.jsondata"
version = "0.1.0"
authors = ["Ballerina"]
keywords = ["json"]
repository = "https://github.com/ballerina-platform/module-ballerina.jsondata"
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.4"
distribution = "2201.9.0-20240326-110600-aca0cc0c"
export = ["data.jsondata"]

[platform.java17]
graalvmCompatible = true
Expand All @@ -16,3 +17,21 @@ groupId = "io.ballerina.lib"
artifactId = "jsondata-native"
version = "0.1.0"
path = "../native/build/libs/data.jsondata-native-0.1.0-SNAPSHOT.jar"

[[platform.java17.dependency]]
path = "./lib/json-path-2.9.0.jar"
groupId = "com.jayway.jsonpath"
artifactId = "json-path"
version = "2.9.0"

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

[[platform.java17.dependency]]
path = "./lib/accessors-smart-2.4.7.jar"
groupId = "net.minidev.json"
artifactId = "accessors-smart"
version = "2.4.7"
60 changes: 59 additions & 1 deletion ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201.8.4"
distribution-version = "2201.9.0-20240326-110600-aca0cc0c"

[[package]]
org = "ballerina"
Expand All @@ -14,6 +14,9 @@ version = "0.1.0"
dependencies = [
{org = "ballerina", name = "io"},
{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 = [
Expand Down Expand Up @@ -41,6 +44,26 @@ 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.array"
version = "0.0.0"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.__internal"}
]

[[package]]
org = "ballerina"
name = "lang.error"
Expand All @@ -50,6 +73,40 @@ 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 = "lang.value"
Expand All @@ -66,6 +123,7 @@ version = "0.0.0"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.array"},
{org = "ballerina", name = "lang.error"}
]
modules = [
Expand Down
21 changes: 21 additions & 0 deletions ballerina/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,36 @@ def stripBallerinaExtensionVersion(String extVersion) {
apply plugin: 'io.ballerina.plugin'

ballerina {
testCoverageParam = "--code-coverage --coverage-format=xml --includes=io.ballerina.lib.data.*:ballerina.*"
packageOrganization = packageOrg
module = packageName
langVersion = ballerinaLangVersion
}

configurations {
externalJars
}

dependencies {

externalJars(group: 'com.jayway.jsonpath', name: 'json-path', version: "${javaJsonPathVersion}") {
transitive = false
}
externalJars(group: 'net.minidev', name: 'json-smart', version: "${javaJsonSmartVersion}") {
transitive = false
}
externalJars(group: 'net.minidev', name: 'accessors-smart', version: "${javaAccessorsSmartVersion}") {
transitive = false
}
}

task updateTomlFiles {
doLast {
def newConfig = ballerinaTomlFilePlaceHolder.text.replace("@project.version@", project.version)
newConfig = newConfig.replace("@toml.version@", tomlVersion)
newConfig = newConfig.replace("@jsonpath.version@", project.javaJsonPathVersion)
newConfig = newConfig.replace("@jsonsmart.version@", project.javaJsonSmartVersion)
newConfig = newConfig.replace("@accessors.version@", project.javaAccessorsSmartVersion)
ballerinaTomlFile.text = newConfig

def newCompilerPluginToml = compilerPluginTomlFilePlaceHolder.text.replace("@project.version@", project.version)
Expand Down
19 changes: 19 additions & 0 deletions ballerina/errors.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com).
//
// 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.

# Represents the error type of the ballerina/data.jsondata module. This error type represents any error that can occur
# during the execution of jsondata APIs.
public type Error distinct error;
2 changes: 1 addition & 1 deletion ballerina/init.bal
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ isolated function init() {
}

isolated function setModule() = @java:Method {
'class: "io.ballerina.lib.data.jsondata.utils.ModuleUtils"
'class: "io.ballerina.lib.data.ModuleUtils"
} external;
4 changes: 0 additions & 4 deletions ballerina/json_api.bal
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ public type Options record {|
boolean allowDataProjection = true;
|};

# Represents the error type of the ballerina/data.jsondata module. This error type represents any error that can occur
# during the execution of jsondata APIs.
public type Error distinct error;

# Defines the name of the JSON Object key.
#
# + value - The name of the JSON Object key
Expand Down
56 changes: 56 additions & 0 deletions ballerina/read.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com).
//
// 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;
import ballerina/lang.'object as obj;

public type JsonPathValue json;

public type JsonPathRawTemplate object {
*obj:RawTemplate;
public string[] & readonly strings;
public JsonPathValue[] insertions;
};

# Extract details from the given JSON value using the provided query template expression.
#
# ```ballerina
# read({id: 1, "name": "John Doe"}, `$.name`) => "John Doe"
# ```
#
# + '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));
}

class JsonPathRawTemplateImpl {
*JsonPathRawTemplate;

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
isolated function readJson(json 'json, JsonPathRawTemplateImpl query) returns json|Error = @java:Method {
'class: "io.ballerina.lib.data.jsonpath.BJsonPath"
} external;
Loading

0 comments on commit b0d3dfe

Please sign in to comment.