Java function with Map.Class parameter in Scala
I'm trying to use a Jackson's ObjectMapper() function: convertValue
.
It takes 2 parameters (3 overloads):
- (Object, Call)
- (Object, TypeReference)
- (Object, JavaType)
I have the following code:
val m = new ObjectMapper()
val map: Map[String, Object] = m.convertValue(bean, classOf[Map])
which doesn't work with error Type Mismatch. Expected JavaType actual Class[Map]
.
I tested with classOf[java.util.Map]
, Map.getClass
, etc. but can't make it work.
How should I send that parameter?
scala
add a comment |
I'm trying to use a Jackson's ObjectMapper() function: convertValue
.
It takes 2 parameters (3 overloads):
- (Object, Call)
- (Object, TypeReference)
- (Object, JavaType)
I have the following code:
val m = new ObjectMapper()
val map: Map[String, Object] = m.convertValue(bean, classOf[Map])
which doesn't work with error Type Mismatch. Expected JavaType actual Class[Map]
.
I tested with classOf[java.util.Map]
, Map.getClass
, etc. but can't make it work.
How should I send that parameter?
scala
add a comment |
I'm trying to use a Jackson's ObjectMapper() function: convertValue
.
It takes 2 parameters (3 overloads):
- (Object, Call)
- (Object, TypeReference)
- (Object, JavaType)
I have the following code:
val m = new ObjectMapper()
val map: Map[String, Object] = m.convertValue(bean, classOf[Map])
which doesn't work with error Type Mismatch. Expected JavaType actual Class[Map]
.
I tested with classOf[java.util.Map]
, Map.getClass
, etc. but can't make it work.
How should I send that parameter?
scala
I'm trying to use a Jackson's ObjectMapper() function: convertValue
.
It takes 2 parameters (3 overloads):
- (Object, Call)
- (Object, TypeReference)
- (Object, JavaType)
I have the following code:
val m = new ObjectMapper()
val map: Map[String, Object] = m.convertValue(bean, classOf[Map])
which doesn't work with error Type Mismatch. Expected JavaType actual Class[Map]
.
I tested with classOf[java.util.Map]
, Map.getClass
, etc. but can't make it work.
How should I send that parameter?
scala
scala
edited Nov 15 '18 at 16:44
P3trur0
2,2091523
2,2091523
asked Nov 15 '18 at 16:19
Fede E.Fede E.
4621614
4621614
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Step 1: look at https://fasterxml.github.io/jackson-databind/javadoc/2.8/com/fasterxml/jackson/databind/JavaType.html. See
Instances can (only) be constructed by
com.fasterxml.jackson.databind.type.TypeFactory
.
Step 2: look at https://fasterxml.github.io/jackson-databind/javadoc/2.8/com/fasterxml/jackson/databind/type/TypeFactory.html.
Then you can see it can be used as e.g.
m.getTypeFactory.constructMapType(classOf[java.util.Map[_, _]], classOf[YourKey], classOf[YourValue])
This throws me: java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to scala.runtime.Nothing$
– Fede E.
Nov 15 '18 at 16:55
add a comment |
You can use the mapper to get the JavaType, for example:
val stringType:JavaType = mapper.constructType(String.class);
You can try the following for your problem:
val m = new ObjectMapper()
val mapType:JavaType = mapper.constructType(java.util.Map.class)
val map: Map[String, Object] = m.convertValue(bean, mapType)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323713%2fjava-function-with-map-class-parameter-in-scala%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Step 1: look at https://fasterxml.github.io/jackson-databind/javadoc/2.8/com/fasterxml/jackson/databind/JavaType.html. See
Instances can (only) be constructed by
com.fasterxml.jackson.databind.type.TypeFactory
.
Step 2: look at https://fasterxml.github.io/jackson-databind/javadoc/2.8/com/fasterxml/jackson/databind/type/TypeFactory.html.
Then you can see it can be used as e.g.
m.getTypeFactory.constructMapType(classOf[java.util.Map[_, _]], classOf[YourKey], classOf[YourValue])
This throws me: java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to scala.runtime.Nothing$
– Fede E.
Nov 15 '18 at 16:55
add a comment |
Step 1: look at https://fasterxml.github.io/jackson-databind/javadoc/2.8/com/fasterxml/jackson/databind/JavaType.html. See
Instances can (only) be constructed by
com.fasterxml.jackson.databind.type.TypeFactory
.
Step 2: look at https://fasterxml.github.io/jackson-databind/javadoc/2.8/com/fasterxml/jackson/databind/type/TypeFactory.html.
Then you can see it can be used as e.g.
m.getTypeFactory.constructMapType(classOf[java.util.Map[_, _]], classOf[YourKey], classOf[YourValue])
This throws me: java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to scala.runtime.Nothing$
– Fede E.
Nov 15 '18 at 16:55
add a comment |
Step 1: look at https://fasterxml.github.io/jackson-databind/javadoc/2.8/com/fasterxml/jackson/databind/JavaType.html. See
Instances can (only) be constructed by
com.fasterxml.jackson.databind.type.TypeFactory
.
Step 2: look at https://fasterxml.github.io/jackson-databind/javadoc/2.8/com/fasterxml/jackson/databind/type/TypeFactory.html.
Then you can see it can be used as e.g.
m.getTypeFactory.constructMapType(classOf[java.util.Map[_, _]], classOf[YourKey], classOf[YourValue])
Step 1: look at https://fasterxml.github.io/jackson-databind/javadoc/2.8/com/fasterxml/jackson/databind/JavaType.html. See
Instances can (only) be constructed by
com.fasterxml.jackson.databind.type.TypeFactory
.
Step 2: look at https://fasterxml.github.io/jackson-databind/javadoc/2.8/com/fasterxml/jackson/databind/type/TypeFactory.html.
Then you can see it can be used as e.g.
m.getTypeFactory.constructMapType(classOf[java.util.Map[_, _]], classOf[YourKey], classOf[YourValue])
answered Nov 15 '18 at 16:28
Alexey RomanovAlexey Romanov
109k26214355
109k26214355
This throws me: java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to scala.runtime.Nothing$
– Fede E.
Nov 15 '18 at 16:55
add a comment |
This throws me: java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to scala.runtime.Nothing$
– Fede E.
Nov 15 '18 at 16:55
This throws me: java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to scala.runtime.Nothing$
– Fede E.
Nov 15 '18 at 16:55
This throws me: java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to scala.runtime.Nothing$
– Fede E.
Nov 15 '18 at 16:55
add a comment |
You can use the mapper to get the JavaType, for example:
val stringType:JavaType = mapper.constructType(String.class);
You can try the following for your problem:
val m = new ObjectMapper()
val mapType:JavaType = mapper.constructType(java.util.Map.class)
val map: Map[String, Object] = m.convertValue(bean, mapType)
add a comment |
You can use the mapper to get the JavaType, for example:
val stringType:JavaType = mapper.constructType(String.class);
You can try the following for your problem:
val m = new ObjectMapper()
val mapType:JavaType = mapper.constructType(java.util.Map.class)
val map: Map[String, Object] = m.convertValue(bean, mapType)
add a comment |
You can use the mapper to get the JavaType, for example:
val stringType:JavaType = mapper.constructType(String.class);
You can try the following for your problem:
val m = new ObjectMapper()
val mapType:JavaType = mapper.constructType(java.util.Map.class)
val map: Map[String, Object] = m.convertValue(bean, mapType)
You can use the mapper to get the JavaType, for example:
val stringType:JavaType = mapper.constructType(String.class);
You can try the following for your problem:
val m = new ObjectMapper()
val mapType:JavaType = mapper.constructType(java.util.Map.class)
val map: Map[String, Object] = m.convertValue(bean, mapType)
answered Nov 15 '18 at 16:38
chandrachandra
501311
501311
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323713%2fjava-function-with-map-class-parameter-in-scala%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown