Disabling type coercion

16 views
Skip to first unread message

Mantas Gridinas

unread,
Nov 20, 2024, 2:59:27 AM11/20/24
to jackson-user
Consider the following snippet which passes json string with ascii value 5 (0x35), and expects deserialization result to be an integer.

        ObjectMapper o = new ObjectMapper();

        String json = "\"5\""; // notice json string rather than json number

        Integer result = o.readValue(json, Integer.class); // expect throws for wrong type

        System.out.println("Hello world!");


In default configuration this does not seem to throw mismatch exception. Which of the flags should be used to prevent type coercion? For enums I found FAIL_ON_NUMBERS_AS_ENUMS, but I couldn't seem to find an equivalent for general coercion.

Mantas Gridinas

unread,
Nov 20, 2024, 5:00:22 AM11/20/24
to jackso...@googlegroups.com

Ah, the databind module contains allow scalar coercion feature which solves my problem


--
You received this message because you are subscribed to the Google Groups "jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/jackson-user/048bd9e4-cd72-4ac3-b6ea-aee5ffeb34d9n%40googlegroups.com.

Joo Hyuk Kim

unread,
Nov 21, 2024, 9:42:24 AM11/21/24
to jackso...@googlegroups.com
Oh shoot, im sorry about late reply. Glad you were able to solve your problem. 

Just one thing though. Next time try Writing inquiries on GitHub discussions? Your questions would have more visibility there

2024년 11월 20일 (수) 오후 7:00, Mantas Gridinas <mgri...@gmail.com>님이 작성:

Mantas Gridinas

unread,
Nov 21, 2024, 9:43:33 AM11/21/24
to jackso...@googlegroups.com

Yeah, i suppose i need to move there as well. I sent message here out of habit


Reply all
Reply to author
Forward
0 new messages