Skip to content

Commit

Permalink
Merge pull request #43751 from HindujaB/fix-json-serialize-master
Browse files Browse the repository at this point in the history
[master] Fix json serializer for tuple and record values
  • Loading branch information
warunalakshitha authored Jan 31, 2025
2 parents a6e4f99 + d6b4583 commit a67872d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ public void serialize(Object json) throws IOException {

switch (TypeUtils.getImpliedType(TypeChecker.getType(json)).getTag()) {
case TypeTags.ARRAY_TAG:
case TypeTags.TUPLE_TAG:
if (json instanceof StreamingJsonValue streamingJsonValue) {
streamingJsonValue.serialize(this);
break;
Expand Down Expand Up @@ -317,6 +318,7 @@ public void serialize(Object json) throws IOException {
break;
case TypeTags.MAP_TAG:
case TypeTags.JSON_TAG:
case TypeTags.RECORD_TYPE_TAG:
this.startObject();
for (Entry<BString, RefValue> entry : ((MapValueImpl<BString, RefValue>) json).entrySet()) {
this.writeFieldName(entry.getKey().getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@
import io.ballerina.runtime.api.values.BMap;
import io.ballerina.runtime.api.values.BString;
import io.ballerina.runtime.api.values.BTypedesc;
import io.ballerina.runtime.internal.json.JsonGenerator;
import io.ballerina.runtime.internal.values.ErrorValue;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -102,4 +105,16 @@ public static Object testParsingNullString(BString str) {
public static Object testBStringParsingWithProcessingMode(BString str) {
return JsonUtils.parse(str, JsonUtils.NonStringValueProcessingMode.FROM_JSON_STRING);
}

public static Object testJsonSerializeExtern(Object jsonObject) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try (JsonGenerator gen = new JsonGenerator(out)) {
gen.serialize(jsonObject);
gen.flush();
out.close();
} catch (IOException e) {
throw new ErrorValue(StringUtils.fromString(e.getMessage()));
}
return StringUtils.fromString(out.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,33 @@ public function main() {

result = testStringParsingWithProcessingMode(jsonStr);
test:assertTrue(result is json, "Invalid json");

testJsonSerialize();
}

const string[] CONST_ROLES = ["Admin"];

type Profile record {|
string name;
int age;
|};

function testJsonSerialize() {
Profile profile = {name: "John", age: 30};
json j = {
roles: CONST_ROLES,
ID: "User1",
profile: profile
};
string expected = "{\"roles\":[\"Admin\"], \"ID\":\"User1\", \"profile\":{\"name\":\"John\", \"age\":30}}";
string result = checkpanic testJsonSerializeExtern(j);
test:assertEquals(result, expected, "Invalid json serialization");
}

public isolated function testJsonSerializeExtern(json j) returns string|error = @java:Method {
'class: "org.ballerinalang.nativeimpl.jvm.runtime.api.tests.JsonValues"
} external;

public isolated function testParsingWrongCharset(string str) returns json|error = @java:Method {
'class: "org.ballerinalang.nativeimpl.jvm.runtime.api.tests.JsonValues"
} external;
Expand Down

0 comments on commit a67872d

Please sign in to comment.