diff --git a/pom.xml b/pom.xml
index 788b75e..2ac90f7 100644
--- a/pom.xml
+++ b/pom.xml
@@ -18,6 +18,7 @@
2.8.2
+ 1.9.13
3.5.1
@@ -27,6 +28,11 @@
gson
${gson.version}
+
+ org.codehaus.jackson
+ jackson-mapper-asl
+ ${jackson.mapper.version}
+
com.google.protobuf
diff --git a/src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPCServerCodec.java b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPCServerCodec.java
index 4e50637..42b6eb8 100644
--- a/src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPCServerCodec.java
+++ b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPCServerCodec.java
@@ -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);
- return json.getBytes();
+ try {
+ String json = this.objectMapper.writeValueAsString(response);
+ return json.getBytes();
+ } catch (IOException e) {
+ throw new RPCException("cannot marsharling", e);
+ }
}
}
diff --git a/src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPCServerRequestCodec.java b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPCServerRequestCodec.java
index 891e0a2..6ab1fa2 100644
--- a/src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPCServerRequestCodec.java
+++ b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPCServerRequestCodec.java
@@ -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);
- return json.getBytes();
+ try {
+ String json = this.objectMapper.writeValueAsString(response);
+ return json.getBytes();
+ } catch (IOException e) {
+ throw new RPCException("cannot marsharling", e);
+ }
}
}
\ No newline at end of file
diff --git a/src/main/java/com/loafle/commons/rpc/protocol/json/JSONServerNotification.java b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONServerNotification.java
index b5a94e5..8bb2d72 100644
--- a/src/main/java/com/loafle/commons/rpc/protocol/json/JSONServerNotification.java
+++ b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONServerNotification.java
@@ -1,7 +1,5 @@
package com.loafle.commons.rpc.protocol.json;
-import com.google.gson.annotations.SerializedName;
-
/**
* JSONServerNotification
*/
diff --git a/src/main/java/com/loafle/commons/rpc/protocol/json/JSONServerRequest.java b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONServerRequest.java
index 963a1a7..295a152 100644
--- a/src/main/java/com/loafle/commons/rpc/protocol/json/JSONServerRequest.java
+++ b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONServerRequest.java
@@ -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 params;
diff --git a/src/main/java/com/loafle/commons/rpc/protocol/json/JSONServerResponse.java b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONServerResponse.java
index 1c13db9..f093e2e 100644
--- a/src/main/java/com/loafle/commons/rpc/protocol/json/JSONServerResponse.java
+++ b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONServerResponse.java
@@ -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;