Can not deserialize instance of java.lang.Class out of START_OBJECT token

StackOverflow https://stackoverflow.com/questions/3156674

  •  01-10-2019
  •  | 
  •  

Pergunta

I can't understand propperly the error I get when I run this code:

InputStream is = this.getClass().getClassLoader().getResourceAsStream(filename);  
String jsonTxt = IOUtils.toString(is);  
JSONArray json = (JSONArray) JSONSerializer.toJSON(jsonTxt);  
JSONObject metadatacontent = json.getJSONObject(0);   
ObjectMapper mapper = new ObjectMapper();  
mapper.readValue(metadatacontent.toString(), MetadataContentBean.class.getClass());

Error:

org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.lang.Class out of START_OBJECT token at [Source: java.io.StringReader@e3b895; line: 1, column: 1] at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:159) at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:192) at org.codehaus.jackson.map.deser.StdDeserializer$ClassDeserializer.deserialize(StdDeserializer.java:439) at org.codehaus.jackson.map.deser.StdDeserializer$ClassDeserializer.deserialize(StdDeserializer.java:421) at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:1588) at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1116) at com.path.parser.JSon.Parser(JSon.java:65) at com.path.parser.JSon.main(JSon.java:29)

What does it mean??

Maybe if I know this, I could find out my error.

Foi útil?

Solução

Your problem is the last line:

MetadataContentBean.class.getClass()

This means "get me the Class object for the MetadataContentBean class, and then get me the Class object for that Class object".... if you see what I mean. So you're asking Jackson to deserialize your JSON onto a Class object, which it doesn't know how to do.

This should be just

MetadataContentBean.class

Outras dicas

This is probably related to the other question, but just to complete the answer, error comes from discrepancy: type "java.lang.Class" is serialized as a JSON String (class name itself), and not as JSON object like beans are. So deserializer expects to see a JSON String, instead sees a JSON Object (which starts with START_OBJECT) and throws exception.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top