开发者

Jackson: Custom JSON deserializer

开发者 https://www.devze.com 2023-01-15 22:58 出处:网络
I am using Jackson library\'s ObjectMapper for deserializing JSON into Java objects. I am using Spring \'wiring\'.I have created custom deserializers to do the conversion from JSON string to POJO. How

I am using Jackson library's ObjectMapper for deserializing JSON into Java objects. I am using Spring 'wiring'. I have created custom deserializers to do the conversion from JSON string to POJO. However, when the input is bad (eg. a number is passed as "124A" - illegal character) a default deserialiser is invoked and bombs with the NumberFormatException. Is there a way to prevent default convert开发者_JAVA百科ers from being called?


Ok, given that input is not valid JSON (numbers can not start with or contain '$'), deserializer will not be called, and any change would have to apply to parser. There are no switches in Jackson to allow such content to be considered numbers.

If you can control input, just change value to a String (add double-quotes around value). Parser then passes it as JSON String, and data binding components get a chance to handle it (JsonDeserializer).

0

精彩评论

暂无评论...
验证码 换一张
取 消