Giter VIP home page Giter VIP logo

Comments (4)

briandilley avatar briandilley commented on September 7, 2024

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.

allComputableThings avatar allComputableThings commented on September 7, 2024

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.

allComputableThings avatar allComputableThings commented on September 7, 2024

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.

allComputableThings avatar allComputableThings commented on September 7, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.