2

I am mapping the linkedHashMap to my Custom Pojo class using the below code.

ObjectMapper mapper = new ObjectMapper();**mapper.registerModule(new ParameterNamesModule()).registerModule(new Jdk8Module()).registerModule(new JavaTimeModule());** mapper.findAndRegisterModules(); mapper.convertValue(wrapper.getObject(), wrapper.getClassType());

This is giving me the below exception "com.fasterxml.jackson.databind.JsonMappingException: Expected type float, integer, or string."

Previously, It was giving me a different exception(com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of java.time.Instant: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)) and after adding the highlighted code to the mapper then it started giving this exception. Could anyone help me figure out how to solve this exception?

Bhagath
  • 63
  • 8
  • What is your input JSON and the class you are trying to convert to? – Hitobat Sep 11 '18 at 20:11
  • public class CustomBean implements Serializable { /** * This is the default constructor */ public CustomBean() { } private static final long serialVersionUID = 1L; private String recordName; @JsonInclude(JsonInclude.Include.NON_DEFAULT) private Instant time; } json string is like this : {recordName=test, time={epochSecond=1345749474, nano=0}} – Bhagath Sep 11 '18 at 21:29
  • Just to give you more information in the question I asked the "wrapper.getObject()" in the code mapper.convertValue(wrapper.getObject(), wrapper.getClassType()) is of type LinkedHashMap and I am converting it my CustomBean using the ObjectMapper. – Bhagath Sep 11 '18 at 21:30
  • I ran into the same issue. In my case, it was due to the fact that in my previous run I did not add the JavaTimeModule and Jackson saved java Instant in a format that it cannot parse later. Now that my current run does have JavaTimeModule it saves the current format but cannot read the previous format, so it is complaining "Excepted type float integer or string" – Marlon Ou Mar 11 '20 at 22:39

1 Answers1

0

I have the following test which works for me on Jackson 2.8.9.

public class FooTest {

    public static class CustomBean implements Serializable {
        private static final long serialVersionUID = 1L;
        @JsonInclude(JsonInclude.Include.NON_DEFAULT)
        public Instant time;
        public String recordName;

        public CustomBean() {
        }

        @Override
        public String toString() {
            return String.format("name:%s time:%s", recordName, time);
        }
    }

    @Test
    public void test_me() throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        mapper.findAndRegisterModules();

        Map<String, Object> data = new LinkedHashMap<>();
        data.put("recordName", "test");
        data.put("time", Instant.now());

        String json = mapper.writeValueAsString(data);
        System.out.println(json);

        CustomBean bean = mapper.convertValue(data, CustomBean.class);
        System.out.println(bean);
    }

}

The output I get is:

{"recordName":"test","time":1536738977.085000000}
name:test time:2018-09-12T07:56:17.085Z

Comparing this to your JSON output in the comment, it feels like your Instant is not being serialised correctly. Even though you are loading the JavaTimeModule so I don't really know why that is happening.

Hitobat
  • 2,847
  • 1
  • 16
  • 12
  • I am not creating the Instant using Instant.now(), I am converting the timestamp to Instant using toInstant() method. Do you think this would make any difference? – Bhagath Sep 12 '18 at 13:56
  • Also I am saving the CustomBean Object to MongoDB and when I am getting the Object from MongoDB I am getting the Object as LinkedHashMap that looks like this "{recordName=test, time={epochSecond=1345749474, nano=0}}". I printed the map object from your test It looks like this "{recordName=test, time=2018-09-12T14:00:43.040Z}" why this difference? – Bhagath Sep 12 '18 at 14:05