Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[master] Fix json serializer for tuple and record values #43751

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading