This commit is contained in:
crusader 2018-05-04 20:33:36 +09:00
parent 48490696a4
commit 4dcc550cb2
3 changed files with 44 additions and 6 deletions

View File

@ -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;
}
}
}

View File

@ -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<String> _params = JSONUtil.convertParamsToStringArray(objectMapper, params);
JSONClientRequest request = new JSONClientRequest(JSONRPC.version, method, _params, id);
try {
String json = this.objectMapper.writeValueAsString(request);

View File

@ -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<String> convertParamsToStringArray(ObjectMapper objectMapper, Object[] params)
throws RPCException {
List<String> 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;
}
}