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