Java function with Map.Class parameter in Scala












0















I'm trying to use a Jackson's ObjectMapper() function: convertValue.



It takes 2 parameters (3 overloads):




  1. (Object, Call)

  2. (Object, TypeReference)

  3. (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?










share|improve this question





























    0















    I'm trying to use a Jackson's ObjectMapper() function: convertValue.



    It takes 2 parameters (3 overloads):




    1. (Object, Call)

    2. (Object, TypeReference)

    3. (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?










    share|improve this question



























      0












      0








      0








      I'm trying to use a Jackson's ObjectMapper() function: convertValue.



      It takes 2 parameters (3 overloads):




      1. (Object, Call)

      2. (Object, TypeReference)

      3. (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?










      share|improve this question
















      I'm trying to use a Jackson's ObjectMapper() function: convertValue.



      It takes 2 parameters (3 overloads):




      1. (Object, Call)

      2. (Object, TypeReference)

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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      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
























          2 Answers
          2






          active

          oldest

          votes


















          1














          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])





          share|improve this answer
























          • This throws me: java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to scala.runtime.Nothing$

            – Fede E.
            Nov 15 '18 at 16:55



















          0














          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)





          share|improve this answer























            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
            });


            }
            });














            draft saved

            draft discarded


















            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









            1














            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])





            share|improve this answer
























            • This throws me: java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to scala.runtime.Nothing$

              – Fede E.
              Nov 15 '18 at 16:55
















            1














            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])





            share|improve this answer
























            • This throws me: java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to scala.runtime.Nothing$

              – Fede E.
              Nov 15 '18 at 16:55














            1












            1








            1







            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])





            share|improve this answer













            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])






            share|improve this answer












            share|improve this answer



            share|improve this answer










            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



















            • 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













            0














            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)





            share|improve this answer




























              0














              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)





              share|improve this answer


























                0












                0








                0







                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)





                share|improve this answer













                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)






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 15 '18 at 16:38









                chandrachandra

                501311




                501311






























                    draft saved

                    draft discarded




















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    Bressuire

                    Vorschmack

                    Quarantine