Skip to content

Commit

Permalink
Merge pull request #35 from lnash94/toJson-Name
Browse files Browse the repository at this point in the history
Add support for overwriting field name in ToJson method
  • Loading branch information
SasinduDilshara authored Sep 18, 2024
2 parents dbbd83d + 15ba8dc commit 4a8ec18
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 11 deletions.
6 changes: 3 additions & 3 deletions ballerina/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
org = "ballerina"
name = "data.jsondata"
version = "0.2.0"
version = "0.3.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"
Expand All @@ -15,8 +15,8 @@ graalvmCompatible = true
[[platform.java17.dependency]]
groupId = "io.ballerina.lib"
artifactId = "jsondata-native"
version = "0.2.0"
path = "../native/build/libs/data.jsondata-native-0.2.0.jar"
version = "0.3.0"
path = "../native/build/libs/data.jsondata-native-0.3.0-SNAPSHOT.jar"

[[platform.java17.dependency]]
path = "./lib/json-path-2.9.0.jar"
Expand Down
2 changes: 1 addition & 1 deletion ballerina/CompilerPlugin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ id = "constraint-compiler-plugin"
class = "io.ballerina.lib.data.jsondata.compiler.JsondataCompilerPlugin"

[[dependency]]
path = "../compiler-plugin/build/libs/data.jsondata-compiler-plugin-0.2.0.jar"
path = "../compiler-plugin/build/libs/data.jsondata-compiler-plugin-0.3.0-SNAPSHOT.jar"
2 changes: 1 addition & 1 deletion ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ modules = [
[[package]]
org = "ballerina"
name = "data.jsondata"
version = "0.2.0"
version = "0.3.0"
dependencies = [
{org = "ballerina", name = "constraint"},
{org = "ballerina", name = "file"},
Expand Down
14 changes: 12 additions & 2 deletions ballerina/json_api.bal
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,18 @@ 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|Error = @java:Method {'class: "io.ballerina.lib.data.jsondata.json.Native"} external;
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;

# Prettifies a `json` value to print it.
#
Expand Down
93 changes: 93 additions & 0 deletions ballerina/tests/to_json_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,96 @@ function testToJsonWithXML() {
test:assertTrue(j is json);
test:assertEquals(j, x1.toString());
}

type TestRecord3 record {|
@Name {
value: "a-o"
}
string a;
@Name {
value: "b-o"
}
string b;
int c;
|};

type NestedRecord3 record {
@Name {
value: "d-o"
}
int d\-name;
@Name {
value: "e-o"
}
string e;
NestedRecord3 f?;
record {
@Name {
value: "i-o"
}
int i;
@Name {
value: "k-o"
}
NestedRecord3 k?;
} j;
};

@test:Config
function testToJsonWithNameANnotation() {
TestRecord3 r = {
a: "name",
b: "b name",
c: 1
};
json out = {
"a-o": "name",
"b-o": "b name",
"c": 1
};
json|Error j = toJson(r);
test:assertTrue(j is json);
test:assertEquals(j, out);

NestedRecord3 n = {
d\-name: 2,
e: "test-e",
f: {
d\-name: 45,
e: "nested-e",
j: {
i: 1000,
k: {
d\-name: 4,
e: "nest-nest-e",
j: {
i: 10000
}
}
}
},
j: { i: 100}
};
json out2 = {
"d-o": 2,
"e-o": "test-e",
f: {
"d-o": 45,
e\-o: "nested-e",
j: {
"i-o": 1000,
"k-o": {
d\-o: 4,
"e-o": "nest-nest-e",
j: {
"i-o": 10000
}
}
}
},
j: { "i-o": 100}
};
json|Error j2 = toJson(n);
test:assertTrue(j2 is json);
test:assertEquals(j2, out2);
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
org.gradle.caching=true
group=io.ballerina.lib
version=0.2.1-SNAPSHOT
version=0.3.0-SNAPSHOT
ballerinaLangVersion=2201.10.0

checkstyleToolVersion=10.12.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@

import io.ballerina.lib.data.jsondata.io.DataReaderTask;
import io.ballerina.lib.data.jsondata.io.DataReaderThreadPool;
import io.ballerina.lib.data.jsondata.utils.Constants;
import io.ballerina.runtime.api.Environment;
import io.ballerina.runtime.api.Future;
import io.ballerina.runtime.api.utils.JsonUtils;
import io.ballerina.runtime.api.types.RecordType;
import io.ballerina.runtime.api.utils.StringUtils;
import io.ballerina.runtime.api.values.BArray;
import io.ballerina.runtime.api.values.BError;
import io.ballerina.runtime.api.values.BMap;
Expand All @@ -34,6 +36,11 @@
import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.Map;

import static io.ballerina.lib.data.jsondata.json.JsonCreator.getModifiedName;
import static io.ballerina.lib.data.jsondata.utils.DataUtils.unescapeIdentifier;


/**
* Json conversions.
Expand Down Expand Up @@ -75,7 +82,23 @@ public static Object parseStream(Environment env, BStream json, BMap<BString, Ob
return null;
}

public static Object toJson(Object value) {
return JsonUtils.convertToJson(value);
public static BString getNameAnnotation(BMap<BString, Object> value, BString key) {
if (!(value.getType() instanceof RecordType recordType)) {
return key;
}
BMap<BString, Object> annotations = recordType.getAnnotations();
for (BString keyV: annotations.getKeys()) {
String keyStr = keyV.toString();
if (!keyStr.contains(Constants.FIELD)) {
continue;
}
String fieldName = unescapeIdentifier(keyStr.split(Constants.FIELD_REGEX)[1]);
if (fieldName.equals(key.getValue())) {
Map<BString, Object> fieldAnnotation = (Map<BString, Object>) annotations.get(keyV);
String modifiedName = getModifiedName(fieldAnnotation, fieldName);
return StringUtils.fromString(modifiedName);
}
}
return key;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,6 @@ public class Constants {

public static final MapType JSON_MAP_TYPE = TypeCreator.createMapType(PredefinedTypes.TYPE_JSON);
public static final MapType ANYDATA_MAP_TYPE = TypeCreator.createMapType(PredefinedTypes.TYPE_ANYDATA);
public static final String ESCAPE_SLASH = "\\\\";
public static final String EMPTY = "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import io.ballerina.runtime.api.values.BTypedesc;
import io.ballerina.stdlib.constraint.Constraints;

import static io.ballerina.lib.data.jsondata.utils.Constants.EMPTY;
import static io.ballerina.lib.data.jsondata.utils.Constants.ESCAPE_SLASH;

public class DataUtils {
public static Object validateConstraints(Object convertedValue, BTypedesc typed, boolean requireValidation) {
if (!requireValidation) {
Expand All @@ -25,4 +28,8 @@ private static String getPrintableErrorMsg(BError err) {
}
return errorMsg;
}

public static String unescapeIdentifier(String parameterName) {
return parameterName.replaceAll(ESCAPE_SLASH, EMPTY);
}
}

0 comments on commit 4a8ec18

Please sign in to comment.