diff --git a/native/src/main/java/io/ballerina/stdlib/websocket/WebSocketResourceDispatcher.java b/native/src/main/java/io/ballerina/stdlib/websocket/WebSocketResourceDispatcher.java index b5ce7c112..b9616c7fb 100644 --- a/native/src/main/java/io/ballerina/stdlib/websocket/WebSocketResourceDispatcher.java +++ b/native/src/main/java/io/ballerina/stdlib/websocket/WebSocketResourceDispatcher.java @@ -36,6 +36,7 @@ import io.ballerina.runtime.api.utils.JsonUtils; import io.ballerina.runtime.api.utils.StringUtils; import io.ballerina.runtime.api.utils.TypeUtils; +import io.ballerina.runtime.api.utils.ValueUtils; import io.ballerina.runtime.api.utils.XmlUtils; import io.ballerina.runtime.api.values.BArray; import io.ballerina.runtime.api.values.BError; @@ -69,7 +70,6 @@ import io.netty.handler.codec.CorruptedFrameException; import io.netty.handler.codec.http.HttpHeaders; import org.ballerinalang.langlib.value.CloneReadOnly; -import org.ballerinalang.langlib.value.CloneWithType; import org.ballerinalang.langlib.value.FromJsonString; import org.ballerinalang.langlib.value.FromJsonStringWithType; import org.slf4j.Logger; @@ -474,13 +474,13 @@ public static void dispatchOnText(WebSocketConnectionInfo connectionInfo, WebSoc bValue = XmlUtils.parse(stringAggregator.getAggregateString()); break; case TypeTags.RECORD_TYPE_TAG: - bValue = CloneWithType.convert(param, JsonUtils.parse( - stringAggregator.getAggregateString())); + bValue = ValueUtils.convert(JsonUtils.parse(stringAggregator.getAggregateString()), + param); break; case TypeTags.UNION_TAG: if (WebSocketUtil.hasStringType(param)) { - bValue = CloneWithType.convert(param, - StringUtils.fromString(stringAggregator.getAggregateString())); + bValue = ValueUtils.convert( + StringUtils.fromString(stringAggregator.getAggregateString()), param); break; } // fall through @@ -706,11 +706,11 @@ private static void createBvaluesForBinary(MethodType onBinaryMessageResource, B bValue = XmlUtils.parse(getBString(byteArray));; break; case TypeTags.RECORD_TYPE_TAG: - bValue = CloneWithType.convert(param, JsonUtils.parse(getBString(byteArray))); + bValue = ValueUtils.convert(JsonUtils.parse(getBString(byteArray)), param); break; case TypeTags.UNION_TAG: if (hasByteArrayType(param)) { - bValue = CloneWithType.convert(param, ValueCreator.createArrayValue(byteArray)); + bValue = ValueUtils.convert(ValueCreator.createArrayValue(byteArray), param); break; } // fall through diff --git a/native/src/main/java/io/ballerina/stdlib/websocket/client/listener/SyncClientConnectorListener.java b/native/src/main/java/io/ballerina/stdlib/websocket/client/listener/SyncClientConnectorListener.java index a46ba8237..07be49251 100644 --- a/native/src/main/java/io/ballerina/stdlib/websocket/client/listener/SyncClientConnectorListener.java +++ b/native/src/main/java/io/ballerina/stdlib/websocket/client/listener/SyncClientConnectorListener.java @@ -24,6 +24,7 @@ import io.ballerina.runtime.api.utils.JsonUtils; import io.ballerina.runtime.api.utils.StringUtils; import io.ballerina.runtime.api.utils.TypeUtils; +import io.ballerina.runtime.api.utils.ValueUtils; import io.ballerina.runtime.api.utils.XmlUtils; import io.ballerina.runtime.api.values.BError; import io.ballerina.runtime.api.values.BObject; @@ -41,7 +42,6 @@ import io.ballerina.stdlib.websocket.WebSocketUtil; import io.ballerina.stdlib.websocket.observability.WebSocketObservabilityUtil; import io.ballerina.stdlib.websocket.server.WebSocketConnectionInfo; -import org.ballerinalang.langlib.value.CloneWithType; import org.ballerinalang.langlib.value.FromJsonStringWithType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -94,12 +94,12 @@ public void onMessage(WebSocketTextMessage webSocketTextMessage) { message = XmlUtils.parse(stringAggregator.getAggregateString()); break; case TypeTags.RECORD_TYPE_TAG: - message = CloneWithType.convert(targetType, JsonUtils.parse( + message = cloneWithType(targetType, JsonUtils.parse( stringAggregator.getAggregateString())); break; case TypeTags.UNION_TAG: if (WebSocketUtil.hasStringType(targetType)) { - message = CloneWithType.convert(targetType, + message = cloneWithType(targetType, StringUtils.fromString(stringAggregator.getAggregateString())); break; } @@ -188,11 +188,11 @@ public void onMessage(WebSocketBinaryMessage webSocketBinaryMessage) { message = XmlUtils.parse(getBString(binMsg));; break; case TypeTags.RECORD_TYPE_TAG: - message = CloneWithType.convert(targetType, JsonUtils.parse(getBString(binMsg))); + message = cloneWithType(targetType, JsonUtils.parse(getBString(binMsg))); break; case TypeTags.UNION_TAG: if (WebSocketUtil.hasByteArrayType(targetType)) { - message = CloneWithType.convert(targetType, ValueCreator.createArrayValue(binMsg)); + message = cloneWithType(targetType, ValueCreator.createArrayValue(binMsg)); break; } // fall through @@ -318,4 +318,12 @@ public void setTargetType(BTypedesc targetType) { public void setFutureCompleted(AtomicBoolean futureCompleted) { this.futureCompleted = futureCompleted; } + + private Object cloneWithType(Type targetType, Object value) { + try { + return ValueUtils.convert(value, targetType); + } catch (BError e) { + return e; + } + } }