Skip to content

Commit

Permalink
Merge pull request #53 from HindujaB/java21
Browse files Browse the repository at this point in the history
Migrate runtime APIs for Java 21
  • Loading branch information
warunalakshitha authored Nov 13, 2024
2 parents 07653f7 + 73ca5a3 commit 6d332e1
Show file tree
Hide file tree
Showing 17 changed files with 791 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
# See: https://help.github.com/articles/about-codeowners/

# These owners will be the default owners for everything in the repo.
* @gimantha @MaryamZi @hasithaa
* @gimantha @MaryamZi @hasithaa @SasinduDilshara
2 changes: 1 addition & 1 deletion ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ modules = [
[[package]]
org = "ballerina"
name = "time"
version = "2.4.0"
version = "2.5.0"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
Expand Down
14 changes: 2 additions & 12 deletions ballerina/json_api.bal
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,8 @@ public isolated function parseStream(stream<byte[], error?> s, Options options =
#
# + v - Source anydata value
# + return - representation of `v` as value of type json
public isolated function toJson(anydata v) returns json {
if v is anydata[] {
return from anydata elem in v
select toJson(elem);
} else if v is map<anydata> {
return map from var [key, feild] in v.entries()
select [getNameAnnotation(v, key), toJson(feild)];
}
return v.toJson();
}

isolated function getNameAnnotation(map<anydata> data, string key) returns string = @java:Method {'class: "io.ballerina.lib.data.jsondata.json.Native"} external;
public isolated function toJson(anydata v) returns json =
@java:Method {'class: "io.ballerina.lib.data.jsondata.json.Native"} external;

# Prettifies a `json` value to print it.
#
Expand Down
55 changes: 55 additions & 0 deletions ballerina/tests/from_json_string_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,61 @@ isolated function testSimpleJsonStringToRecord() returns Error? {
test:assertEquals(recC.get("b"), 1);
}

@test:Config
isolated function testSimpleJsonStringToRecord2() returns Error? {
string user = string `{"id": 4012}`;
ReadOnlyUserRecord r = check parseString(user);
test:assertEquals(r, {id: 4012});
}

@test:Config
isolated function testSimpleJsonStringToRecord3() returns Error? {
string user = string `{"id": 4012, "name": {"firstname": "John", "lastname": "Doe"}, "age": 27}`;
ReadOnlyUserRecord2 r = check parseString(user);
test:assertEquals(r, {id: 4012, age: 27, name: {firstname: "John", lastname: "Doe"}});
}

@test:Config
isolated function testSimpleJsonStringToRecord4() returns Error? {
string user = string `{"id": 4012, "name": {"firstname": "John", "lastname": "Doe"}, "age": 27}`;
UserRecord3 r = check parseString(user);
test:assertEquals(r, {id: 4012, age: 27, name: {firstname: "John", lastname: "Doe"}});
}

@test:Config
isolated function testSimpleJsonStringToRecord5() returns Error? {
string user = string `{"id": 4012, "name": {"firstname": "John", "lastname": "Doe"}, "age": 27}`;
ReadOnlyUserRecord2 r = check parseString(user);
test:assertEquals(r, {id: 4012, age: 27, name: {firstname: "John", lastname: "Doe"}});
}

@test:Config
isolated function testSimpleJsonStringToRecord6() returns Error? {
string user = string `{"ids": [4012, 4013], "names": [{"firstname": "John", "lastname": "Doe"},
{"firstname": "Jane", "lastname": "Doe"}], "age": 27}`;
ReadOnlyUsersRecord3 r = check parseString(user);
test:assertEquals(r, {ids: [4012, 4013], names: [{firstname: "John", lastname: "Doe"},
{firstname: "Jane", lastname: "Doe"}]});
}

@test:Config
isolated function testSimpleJsonStringToRecord7() returns Error? {
string user = string `{"ids": [4012, 4013], "names": [{"firstname": "John", "lastname": "Doe"},
{"firstname": "Jane", "lastname": "Doe"}], "age": 27}`;
ReadOnlyUsersRecord4 r = check parseString(user);
test:assertEquals(r, {ids: [4012, 4013], names: [{firstname: "John", lastname: "Doe"},
{firstname: "Jane", lastname: "Doe"}]});
}

@test:Config
isolated function testSimpleJsonStringToRecord8() returns Error? {
string user = string `{"ids": [4012, 4013], "names": [{"firstname": "John", "lastname": "Doe"},
{"firstname": "Jane", "lastname": "Doe"}], "age": 27}`;
ReadOnlyUsersRecord5 r = check parseString(user);
test:assertEquals(r, {ids: [4012, 4013], names: [{firstname: "John", lastname: "Doe"},
{firstname: "Jane", lastname: "Doe"}]});
}

@test:Config
isolated function testSimpleJsonStringToRecordWithProjection() returns Error? {
string str = string `{"a": "hello", "b": 1}`;
Expand Down
Loading

0 comments on commit 6d332e1

Please sign in to comment.