diff --git a/src/main/java/com/loafle/commons/rpc/protocol/json/JSONClientRequest.java b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONClientRequest.java index 875cbad..4eac3f9 100644 --- a/src/main/java/com/loafle/commons/rpc/protocol/json/JSONClientRequest.java +++ b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONClientRequest.java @@ -1,7 +1,5 @@ package com.loafle.commons.rpc.protocol.json; -import java.util.List; - import org.codehaus.jackson.annotate.JsonProperty; /** @@ -11,13 +9,13 @@ public class JSONClientRequest { @JsonProperty("jsonrpc") public String version; public String method; - public Object[] params; + public Object params; public Object id; - JSONClientRequest(String version, String method, Object[] params, Object id) { + JSONClientRequest(String version, String method, Object params, Object id) { this.version = version; this.method = method; this.params = params; this.id = id; } -} \ No newline at end of file +} diff --git a/src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPCClientCodec.java b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPCClientCodec.java index e50b06d..763b99f 100644 --- a/src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPCClientCodec.java +++ b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPCClientCodec.java @@ -1,6 +1,7 @@ package com.loafle.commons.rpc.protocol.json; import java.io.IOException; +import java.util.List; import com.loafle.commons.rpc.RPCException; import com.loafle.commons.rpc.protocol.RPCClientCodec; @@ -21,7 +22,8 @@ public class JSONRPCClientCodec extends JSONRPCCodec implements RPCClientCodec { @Override public byte[] request(String method, Object[] params, Object id) throws RPCException { - JSONClientRequest request = new JSONClientRequest(JSONRPC.version, method, params, id); + List _params = JSONUtil.convertParamsToStringArray(objectMapper, params); + JSONClientRequest request = new JSONClientRequest(JSONRPC.version, method, _params, id); try { String json = this.objectMapper.writeValueAsString(request); diff --git a/src/main/java/com/loafle/commons/rpc/protocol/json/JSONUtil.java b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONUtil.java new file mode 100644 index 0000000..27097d1 --- /dev/null +++ b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONUtil.java @@ -0,0 +1,38 @@ +package com.loafle.commons.rpc.protocol.json; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import com.google.gson.internal.Primitives; +import com.loafle.commons.rpc.RPCException; + +import org.codehaus.jackson.map.ObjectMapper; + +/** + * JSONUtil + */ +public abstract class JSONUtil { + public static List convertParamsToStringArray(ObjectMapper objectMapper, Object[] params) + throws RPCException { + List values = new ArrayList<>(); + if (null == params || 0 == params.length) { + return values; + } + + try { + for (Object param : params) { + Class clazz = param.getClass(); + if (!Primitives.isPrimitive(clazz) && !clazz.equals(String.class)) { + values.add(objectMapper.writeValueAsString(param)); + } else { + values.add(String.valueOf(param)); + } + } + } catch (IOException e) { + throw new RPCException("cannot unmarsharling", e); + } + + return values; + } +} \ No newline at end of file