Skip to content

Commit

Permalink
Merge pull request #41340 from HindujaB/fix-json-parser-5.x
Browse files Browse the repository at this point in the history
[2201.5.x] Add JSON parser support for exponential values
  • Loading branch information
warunalakshitha authored Sep 7, 2023
2 parents 885a8bf + 29ac7af commit 8970787
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,8 @@ private void processNonStringValue(ValueType type) throws JsonParserException {
default:
if (isNegativeZero(str)) {
setValueToJsonType(type, Double.parseDouble(str));
} else if (isExponential(str)) {
setValueToJsonType(type, new DecimalValue(str));
} else {
setValueToJsonType(type, Long.parseLong(str));
}
Expand All @@ -936,6 +938,10 @@ private void processNonStringValue(ValueType type) throws JsonParserException {
}
}

private boolean isExponential(String str) {
return str.contains("e") || str.contains("E");
}

private void setValueToJsonType(ValueType type, Object value) {
switch (type) {
case ARRAY_ELEMENT:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import io.ballerina.runtime.api.types.StructureType;
import io.ballerina.runtime.api.types.Type;
import io.ballerina.runtime.api.utils.JsonUtils;
import io.ballerina.runtime.api.utils.StringUtils;
import io.ballerina.runtime.api.values.BError;
import io.ballerina.runtime.api.values.BMap;
Expand Down Expand Up @@ -54,4 +55,8 @@ public static Object testConvertJSON(Object sourceVal, BTypedesc t) {
public static BString convertJSONToString(Object jValue) {
return StringUtils.fromString(StringUtils.getJsonString(jValue));
}

public static Object convertStringToJson(BString str) {
return JsonUtils.parse(str);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ import testorg/utils.jsons;
public function main() {
jsons:validateAPI();
jsons:validateStringAPI();
jsons:validateJsonAPI();
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,21 @@ function testConvertJSONToRecord() {
}

type numUnion1 int|decimal|float;

type singleton1 1|"1"|true|"one";

type singleton2 2|"2"|true|"two";

type singletonunion singleton1|singleton2|false;

type union1 string|byte;

type union2 int|int:Signed16;

type union3 singletonunion|union1|union2;

type intArray int[];

type stringMap map<string>;

function testConvertJSON() {
Expand Down Expand Up @@ -99,9 +107,9 @@ function testConvertJSON() {
jval_result = trap convertJSON(jval, json);
test:assertEquals(jval_result, {"a": true});

jval = [12,-1,0,15];
jval = [12, -1, 0, 15];
jval_result = trap convertJSON(jval, intArray);
test:assertEquals(jval_result, [12,-1,0,15]);
test:assertEquals(jval_result, [12, -1, 0, 15]);

jval = {"a": "true"};
jval_result = trap convertJSON(jval, stringMap);
Expand Down Expand Up @@ -149,9 +157,9 @@ public function validateStringAPI() {
"\"map\":{\"value\":\"a\"b\\c\td\"},\"arr_map\":[\"foo\",{\"value\":\"a\"b\\c\td\"},87]}");

anydata tab = table [
{a: 1, b: " 2"},
{c: "3", d: "4"}
];
{a: 1, b: " 2"},
{c: "3", d: "4"}
];
string|error res = convertJSONToString(tab);
test:assertTrue(res is string);
test:assertEquals(res, "[{\"a\":1, \"b\":\" 2\"}, {\"c\":\"3\", \"d\":\"4\"}]");
Expand Down Expand Up @@ -180,3 +188,52 @@ function convertJSONToString(any|error v) returns string = @java:Method {
name: "convertJSONToString"
} external;

public function validateJsonAPI() {
string s = "0e-18";
json j = convertStringToJson(s);
test:assertEquals(j, 0e-18d);

s = "{\"val\" : 0e-19}";
j = convertStringToJson(s);
test:assertEquals(j, {"val": 0e-19d});

s = "99.9E+6143";
j = convertStringToJson(s);
test:assertEquals(j, 9.99E+6144d);

s = "999E6142";
j = convertStringToJson(s);
test:assertEquals(j, 9.99E+6144d);

s = "4.9e-325";
j = convertStringToJson(s);
test:assertEquals(j, 4.9E-325d);

s = "{\r\n\"factMap\":{\r\n\"105!T\":{\r\n\"aggregates\":[\r\n{\r\n\"label\":\"USD 0.00\"," +
"\r\n\"value\":0e-18\r\n}\r\n]\r\n}\r\n}\r\n}";
j = convertStringToJson(s);
test:assertEquals(j, {"factMap": {"105!T": {"aggregates": [{"label": "USD 0.00", "value": 0e-18d}]}}});

s = "0x0.0p1";
json|error result = trap convertStringToJson(s);
test:assertTrue(result is error);
error e = <error>result;
test:assertEquals(e.message(), "unrecognized token '0x0.0p1' at line: 1 column: 9");

s = "999E6143";
result = trap convertStringToJson(s);
test:assertTrue(result is error);
e = <error>result;
test:assertEquals(e.message(), "{ballerina}NumberOverflow");

s = "99.9E+6144";
result = trap convertStringToJson(s);
test:assertTrue(result is error);
e = <error>result;
test:assertEquals(e.message(), "{ballerina}NumberOverflow");
}

function convertStringToJson(string v) returns json = @java:Method {
'class: "org.ballerinalang.nativeimpl.jvm.runtime.api.tests.JsonValues",
name: "convertStringToJson"
} external;

0 comments on commit 8970787

Please sign in to comment.