Comments (4)
getGenericReturnType type returns:
List
vs.:
List
so getReturnType() can’t be used.
--
Brian C. Dilley
On August 20, 2014 at 7:26:57 PM, stuz5000 ([email protected]) wrote:
I'm providing a scala-aware mapper to JSON RPC but finding that its impossible to deserialize return values (via JsonRpcHttpClient) if method returns scala collection types. I've checked and found that the same mapper object can deserialize JSON into the same scala list type.
So... I did some digging and found that using
method.getReturnType()
as the type parameter to mapper instead of
method.getGenericReturnType()
Seems to solve this issue. i.e. use
ProxyUtil::createClientProxy() ...
... Class[_] clazz = method.getReturnType()
... invoke(... clazz ...)
JsonRpcClient::readResponse() ...
...
val tokens = mapper.treeAsTokens(jsonObject.get("result"))
return mapper.readValue(tokens, clazz).asInstanceOf[Object]
instead of
ProxyUtil::createClientProxy() ...
... Type returnJavaType = method.getGenericReturnType()
... invoke(... returnJavaType ...)
JsonRpcClient::readResponse() ...
JsonParser returnJsonParser = mapper.treeAsTokens(jsonObject.get("result"));
JavaType returnJavaType = TypeFactory.defaultInstance().constructType(returnType);
return mapper.readValue(returnJsonParser, returnJavaType);
Is there any reason to prefer using getGenericReturnType? Or can a pull request be submitted to replace Type with Class<?> wherever return types are used?
NB. I thought I could fix this by simply providing my own client proxy to use method.getReturnType() (which returns a class). However, although there's a JsonRpcClient::invoke method that takes a Class<?>, this is downcast to a Type, and, I think, by then its not possible to get mapper to instantiate the scala collection class using either:
mapper.readValue(returnJsonParser, );
mapper.readValue(returnJsonParser, );
:( So... better to pass around Class than Type. Any reason not to?
—
Reply to this email directly or view it on GitHub.
from jsonrpc4j.
Hmmm... if I do:
val userListText = mapper.writeValueAsString(List[ExampleService.User](user, user))
// Method returns scala.collection.immutable.List[ExampleService.User]
val method = classOf[ExampleService.UserService].getMethod("getListUser")
val result = mapper.readValue(userListText, method.getReturnType())
the above successfully deserializes a List[User] from a method.getReturnType().
Though if print...
println(method.getReturnType())
>> class scala.collection.immutable.List
println(method.getGenericReturnType())
>> scala.collection.immutable.List<net.ailive.scratchbox.ExampleService.User>
it seems somehow that
method.getReturnType()
carries all the required information.
from jsonrpc4j.
Ahh... looks like you were right. In this case, the mapper successfully returns a List[User], but the User object data are actual Map objects -- thanks to type erasure.... :(
from jsonrpc4j.
NB. I'm still blocked on deserializing scala Lists though, even though my ObjectMapper supports this.
Here is the real culprit. In creatClientProxy, I believe
TypeFactory.defaultInstance().constructType(javaType);
ignores the supplied ObjectMapper. See the discussion here: FasterXML/jackson-module-scala#107
Should prefer to call:
mapper.getTypeFactory.constructType(javaType)
from jsonrpc4j.
Related Issues (20)
- Method parameter conversion error handling logic should preserve JSON-RPC id from the request HOT 2
- Cross language conversion of JsonRpcService
- 方法重载不支持
- 对于数值相关类型有bug,基于1.6版本
- Basic HTTP Client
- ResponseBodyAdvice not triggered
- Remove net.iharder:base64
- RemoteExporter in SpringBoot 3.0.0 HOT 4
- is jsonrpc4j dead or still supported / maintained ? HOT 2
- Deprecate `javax.portlet:portlet-api` and portlet support
- Add support for newer `jakarta.servlet.http` API into `JsonRpcServer`
- CALL FOR HELP
- Looking for someone to handle maintenance and releases of this library HOT 2
- And new annotation supports creating specially JsonRpcClient.
- AutoJsonRpcServiceImplExporter not set parallelBatchProcessingTimeout to JsonServiceExporter HOT 1
- Have any plans to release the latest version? HOT 3
- per request headers
- BaseUrl truncated when create JsonRpc bean
- ObjectMapper passed into JsonRpcMultiServer is ignored
- How to add swagger or open api to the project? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jsonrpc4j.