Is it possible to conditionally apply a CustomSerializer?

29 views
Skip to first unread message

Olive Ahn

unread,
Apr 23, 2023, 2:12:41 PM4/23/23
to jackson-user

I created a CustomSerializer for String class, is there a way to apply this to a specific class only?

 ex. 

class Response<T> {

  private T result;

}


class DefaultResponse<T> {

  private T result;

}


When there is, I want to apply that Serializer only to DefaultResponse.

Tatu Saloranta

unread,
Apr 23, 2023, 4:27:05 PM4/23/23
to jackso...@googlegroups.com
I am not 100% sure I understand the question here, given reference to
`String` type, but use of type-variable T.

But if we assume `T` would be bound to `String` for usage, the only
way I know to indicate usage within certain type is to use annotation

@JsonSerialize(using = MySerializer.class)
private T result;

and not registering serializers globally. Global serializers are
assigned only based on specific type of value, but not containment
(Value of type T within type C).

-+ Tatu +-

Joo Hyuk Kim

unread,
Apr 24, 2023, 12:32:25 PM4/24/23
to jackso...@googlegroups.com
What is the outcome of this one?
Did Tatu's suggestion work?

--
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 on the web visit https://groups.google.com/d/msgid/jackson-user/CAL4a10gR4DSi%3DvDp89xgx334P2QwuLRdaitA7GTKD6Jq4ZeG2Q%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages