Consultation on implementation in kotlin-module.

18 views
Skip to first unread message

wrongwrong

unread,
Mar 21, 2025, 3:09:40 PMMar 21
to jackson-dev
Hello everyone, I'm looking for advice on implementation in kotlin-module.

The ObjectMapper.readValue may return null as a result.
On the other hand, in the kotlin-module extension, if the user calls readValue expecting non-null, an error should be thrown if the result is null(https://github.com/FasterXML/jackson-module-kotlin/issues/399).

I have learned how to achieve this, but am struggling with which exception should be thrown as a validation error.

1. NPE
2. some exception defined in Jackson (what would be appropriate?)
3. any proprietary exception in kotlin-module

Personally, I think 2 is better, but is there any appropriate exception?
Any advice would be appreciated.

Sandeep Gaur

unread,
Mar 21, 2025, 6:43:56 PMMar 21
to jacks...@googlegroups.com
Hi,

If I understand your problem correctly, IllegalArgumentException would be the most appropriate choice because null is a valid response from readValue. The additional restriction is being imposed by the client, making IllegalArgumentException the right fit for enforcing this constraints.

Thanks,
Sandeep gaur 



--
You received this message because you are subscribed to the Google Groups "jackson-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jackson-dev...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/jackson-dev/1dbee532-1337-46f2-b81c-563503656aa8n%40googlegroups.com.

Joo Hyuk Kim

unread,
Mar 21, 2025, 10:32:12 PMMar 21
to jacks...@googlegroups.com
Or consider InvalidNullException? 
Its doc says... 

/**
* Exception thrown if a `null` value is being encountered for a property
* designed as "fail on null" property (see {@link com.fasterxml.jackson.annotation.JsonSetter}).
*
* @since 2.9
*/
public class InvalidNullException

wrongwrong

unread,
Mar 22, 2025, 1:50:34 AMMar 22
to jackson-dev
Thank you all for the replies.

>  If I understand your problem correctly, IllegalArgumentException would be the most appropriate choice because null is a valid response from readValue.

In edge cases, the return value of readValue may not be valid.
Since ObjectMapper can be inherited, it is possible that null will be returned due to an implementation error.
It is even possible that an invalid value is returned.

Personally, I feel that IllegalArgumentException may not be the way to go in such cases.

> Or consider InvalidNullException?

I initially thought this would be better, but had the above concerns.
Would the more abstract JsonMappingException be more appropriate?

2025年3月22日土曜日 11:32:12 UTC+9 Joo Hyuk Kim:

Joo Hyuk Kim

unread,
Mar 22, 2025, 1:53:33 AMMar 22
to jacks...@googlegroups.com
I would like to suggest bring this to GH discussion for more ideas. 

2025년 3월 22일 (토) 오후 2:50, wrongwrong <boran...@gmail.com>님이 작성:

wrongwrong

unread,
Mar 22, 2025, 2:31:10 AMMar 22
to jackson-dev
OK, I have created an isssue.
https://github.com/FasterXML/jackson-module-kotlin/issues/934

Please reply to the discussion here.

2025年3月22日土曜日 14:53:33 UTC+9 Joo Hyuk Kim:
Reply all
Reply to author
Forward
0 new messages