Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues deserializing scala collections #38

Closed
allComputableThings opened this issue Aug 21, 2014 · 4 comments
Closed

Issues deserializing scala collections #38

allComputableThings opened this issue Aug 21, 2014 · 4 comments
Milestone

Comments

@allComputableThings
Copy link

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, <Class[] >);

:( So... better to pass around Class than Type. Any reason not to?

@briandilley
Copy link
Owner

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.

@allComputableThings
Copy link
Author

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.

@allComputableThings
Copy link
Author

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.... :(

@allComputableThings
Copy link
Author

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)

@gaborbernat gaborbernat added this to the 1.5.0 milestone Nov 10, 2015
gaborbernat pushed a commit that referenced this issue Mar 15, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants