ing
This commit is contained in:
parent
668e31ed66
commit
7525b0b8fe
6
pom.xml
6
pom.xml
|
@ -18,6 +18,7 @@
|
|||
|
||||
<properties>
|
||||
<gson.version>2.8.2</gson.version>
|
||||
<jackson.mapper.version>1.9.13</jackson.mapper.version>
|
||||
<protobuf-java.version>3.5.1</protobuf-java.version>
|
||||
</properties>
|
||||
|
||||
|
@ -27,6 +28,11 @@
|
|||
<artifactId>gson</artifactId>
|
||||
<version>${gson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.jackson</groupId>
|
||||
<artifactId>jackson-mapper-asl</artifactId>
|
||||
<version>${jackson.mapper.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.protobuf</groupId>
|
||||
|
|
|
@ -1,40 +1,46 @@
|
|||
package com.loafle.commons.rpc.protocol.json;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import java.io.IOException;
|
||||
|
||||
import com.loafle.commons.rpc.RPCException;
|
||||
import com.loafle.commons.rpc.protocol.RPCServerCodec;
|
||||
import com.loafle.commons.rpc.protocol.RPCServerRequestCodec;
|
||||
|
||||
import org.codehaus.jackson.map.DeserializationConfig;
|
||||
import org.codehaus.jackson.map.ObjectMapper;
|
||||
|
||||
/**
|
||||
* JSONRPCServerCodec
|
||||
*/
|
||||
public class JSONRPCServerCodec implements RPCServerCodec {
|
||||
private Gson gson;
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
public JSONRPCServerCodec() {
|
||||
this.gson = new Gson();
|
||||
}
|
||||
public JSONRPCServerCodec(Gson gson) {
|
||||
this.gson = gson;
|
||||
this.objectMapper = new ObjectMapper();
|
||||
this.objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
}
|
||||
|
||||
public void setGson(Gson gson) {
|
||||
this.gson = gson;
|
||||
public JSONRPCServerCodec(ObjectMapper objectMapper) {
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
public Gson getGson() {
|
||||
return this.gson;
|
||||
public void setObjectMapper(ObjectMapper objectMapper) {
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
public RPCServerRequestCodec request(byte[] buff) throws RPCException {
|
||||
return new JSONRPCServerRequestCodec(this.gson, buff);
|
||||
return new JSONRPCServerRequestCodec(this.objectMapper, buff);
|
||||
}
|
||||
|
||||
public byte[] notification(String method, Object... params) throws RPCException {
|
||||
JSONServerNotification notification = new JSONServerNotification(method, params);
|
||||
JSONServerResponse response = new JSONServerResponse(JSONRPC.version, notification, null);
|
||||
|
||||
String json = this.gson.toJson(response);
|
||||
try {
|
||||
String json = this.objectMapper.writeValueAsString(response);
|
||||
return json.getBytes();
|
||||
} catch (IOException e) {
|
||||
throw new RPCException("cannot marsharling", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,27 +1,29 @@
|
|||
package com.loafle.commons.rpc.protocol.json;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
import com.google.gson.internal.Primitives;
|
||||
import com.loafle.commons.rpc.RPCException;
|
||||
import com.loafle.commons.rpc.protocol.RPCError;
|
||||
import com.loafle.commons.rpc.protocol.RPCServerRequestCodec;
|
||||
|
||||
import org.codehaus.jackson.map.ObjectMapper;
|
||||
import org.codehaus.jackson.type.JavaType;
|
||||
|
||||
/**
|
||||
* JSONRPCServerRequestCodec
|
||||
*/
|
||||
class JSONRPCServerRequestCodec implements RPCServerRequestCodec {
|
||||
private Gson gson;
|
||||
private ObjectMapper objectMapper;
|
||||
private JSONServerRequest request;
|
||||
|
||||
public JSONRPCServerRequestCodec(Gson gson, byte[] buff) throws RPCException {
|
||||
this.gson = gson;
|
||||
String json = new String(buff);
|
||||
public JSONRPCServerRequestCodec(ObjectMapper objectMapper, byte[] buff) throws RPCException {
|
||||
this.objectMapper = objectMapper;
|
||||
try {
|
||||
this.request = this.gson.fromJson(json, JSONServerRequest.class);
|
||||
} catch (JsonSyntaxException e) {
|
||||
throw new RPCException("request is not valied", json);
|
||||
this.request = this.objectMapper.readValue(buff, JSONServerRequest.class);
|
||||
} catch (IOException e) {
|
||||
throw new RPCException("cannot unmarsharling", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -39,7 +41,8 @@ class JSONRPCServerRequestCodec implements RPCServerRequestCodec {
|
|||
}
|
||||
|
||||
if (paramTypes.length != this.request.params.size()) {
|
||||
throw new RPCException(String.format("The size of params[%d] and types[%d] is not same", this.request.params.size(), paramTypes.length));
|
||||
throw new RPCException(String.format("The size of params[%d] and types[%d] is not same",
|
||||
this.request.params.size(), paramTypes.length));
|
||||
}
|
||||
|
||||
Object[] result = new Object[paramTypes.length];
|
||||
|
@ -48,7 +51,15 @@ class JSONRPCServerRequestCodec implements RPCServerRequestCodec {
|
|||
Type paramType = paramTypes[i];
|
||||
String param = this.request.params.get(i);
|
||||
|
||||
result[i] = this.gson.fromJson(param, paramType);
|
||||
JavaType targetType = objectMapper.getTypeFactory().constructType(paramType);
|
||||
if (!Primitives.isPrimitive(paramType) && !paramType.getTypeName().equals(String.class.getName())) {
|
||||
try {
|
||||
result[i] = objectMapper.readValue(param, targetType);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
}
|
||||
result[i] = objectMapper.convertValue(param, targetType);
|
||||
}
|
||||
|
||||
return result;
|
||||
|
@ -66,7 +77,11 @@ class JSONRPCServerRequestCodec implements RPCServerRequestCodec {
|
|||
JSONServerResponse response = new JSONServerResponse(JSONRPC.version, reply, rpcError);
|
||||
response.id = this.request.id;
|
||||
|
||||
String json = this.gson.toJson(response);
|
||||
try {
|
||||
String json = this.objectMapper.writeValueAsString(response);
|
||||
return json.getBytes();
|
||||
} catch (IOException e) {
|
||||
throw new RPCException("cannot marsharling", e);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,7 +1,5 @@
|
|||
package com.loafle.commons.rpc.protocol.json;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
/**
|
||||
* JSONServerNotification
|
||||
*/
|
||||
|
|
|
@ -2,13 +2,13 @@ package com.loafle.commons.rpc.protocol.json;
|
|||
|
||||
import java.util.List;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import org.codehaus.jackson.annotate.JsonProperty;
|
||||
|
||||
/**
|
||||
* JSONServerRequest
|
||||
*/
|
||||
class JSONServerRequest {
|
||||
@SerializedName("jsonrpc")
|
||||
@JsonProperty("jsonrpc")
|
||||
public String version;
|
||||
public String method;
|
||||
public List<String> params;
|
||||
|
|
|
@ -1,13 +1,14 @@
|
|||
package com.loafle.commons.rpc.protocol.json;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import com.loafle.commons.rpc.protocol.RPCError;
|
||||
|
||||
import org.codehaus.jackson.annotate.JsonProperty;
|
||||
|
||||
/**
|
||||
* JSONServerResponse
|
||||
*/
|
||||
class JSONServerResponse {
|
||||
@SerializedName("jsonrpc")
|
||||
@JsonProperty("jsonrpc")
|
||||
public String version;
|
||||
|
||||
public Object result;
|
||||
|
|
Loading…
Reference in New Issue
Block a user