On Fri, Dec 21, 2018 at 1:52 PM matt Smith <matt.smith.11...@gmail.com> wrote: > > even moving annotation to the field did not work. > Note I have annotated class ItemRow class which ofcourse i can't since it is > part of legacy code.
Except that you should be able to do that with mix-in annotations. See for example https://dzone.com/articles/jackson-mix-in-annotations -+ Tatu +- > > On Friday, December 21, 2018 at 12:49:21 PM UTC-8, Tatu Saloranta wrote: >> >> On Fri, Dec 21, 2018 at 12:06 AM matt Smith <matt.smi...@gmail.com> wrote: >> > >> > so I have made this wrapper and annotated getter with @JsonSerialize. >> > however, I do not see it being applied. what is wrong here? >> >> That looks like it should work. If not, a unit test & issue would make sense. >> >> -+ Tatu +- >> >> > >> > public class Wrapper { >> > >> > >> > private Map<RecordDomain, ItemRow> foo; >> > >> > >> > public Wrapper(Map<RecordDomain, ItemRow> fooz) { >> > foo = new HashMap<>(fooz); >> > } >> > >> > >> > @JsonSerialize( contentUsing = ItemRowSerializer.class) >> > public Map<RecordDomain, ItemRow> getFoo() { >> > return foo; >> > } >> > >> > >> > public void setFoo(Map<RecordDomain, ItemRow> foo) { >> > this.foo = foo; >> > } >> > } >> > >> > >> > On Thursday, December 20, 2018 at 1:25:59 PM UTC-8, Tatu Saloranta wrote: >> >> >> >> On Thu, Dec 20, 2018 at 11:22 AM matt Smith <matt.smi...@gmail.com> wrote: >> >> > >> >> > I encountered a case where class level JsonSerializable annotation does >> >> > not require registration with Simple Module but method level does. >> >> >> >> Hmmh. This seems odd, let's see. >> >> >> >> > Here is the example I tried >> >> > >> >> > @JsonSerialize(using = ItemRowSerializer.class ) >> >> > private ItemRow tes >> >> > tItem(){ >> >> > return new ItemRow("abc", Arrays.asList("item1", "item2", >> >> > "item3")); >> >> > } >> >> > >> >> > >> >> > I test it out like this >> >> > >> >> > >> >> > String jsonResult = new ObjectMapper().writeValueAsString(testItem()); >> >> > System.out.println(jsonResult); >> >> > >> >> > I noticed that jsonString did not serialize using ItemRowSerializer. >> >> >> >> Ah. No, it won't. Jackson does not see you calling the method; Jackson >> >> sees the object you pass, which was returned by the method. There is >> >> no linkage there: Jackson can only introspects classes you pass to it. >> >> >> >> > However, when I did this, it gave the expected jsonString >> >> > >> >> > >> >> > SimpleModule module = new SimpleModule() >> >> > module.addSerializer(ItemRowSerializer.class) >> >> > mapper.register(module); >> >> > String jsonResult = new ObjectMapper().writeValueAsString(testItem()); >> >> > System.out.println(jsonResult); >> >> > >> >> > >> >> > On the otherhand, if I had my ItemRow class annoated with >> >> > @JsonSerializable, there was no need with module at all. >> >> >> >> I think you mean `@JsonSerialize` here (there is interface type >> >> `JsonSerializable`, but no annotation). >> >> >> >> > i.e >> >> > >> >> > @JsonSerializable(using = ItemRowSerailizer >> >> > public class ItemRow<T> {...} >> >> > >> >> > >> >> > Since I am passing this serialized object over the wire, I do not >> >> > prefer to write to mapper and send string representation across. >> >> > >> >> > how would I go about getting method level annotation working without >> >> > module registration? >> >> >> >> Method level annotations only work when property is accessed via >> >> method (or field) in question. >> >> >> >> So if you had something like: >> >> >> >> public class Rows >> >> @JsonSerialize(using = .... ) >> >> public ItemRow<Type> getRows() { ... } >> >> } >> >> >> >> and serialized an instance of `Rows`, serializer would be found. But >> >> for root values, there is no such reference. >> >> >> >> On related note, I strongly recommend always using a wrapper type >> >> around generic values, so that root value is never of generic type. >> >> Reason for this is that Java Type Erasure will often cause problems if >> >> attempting to pass an instance of generic type -- so Jackson can not >> >> detect intended type (all it sees is type variable from class >> >> declaration). >> >> >> >> So, attempts to serialize, say, Row<T>, will be seen as `Row<?>`, and >> >> possibly (although not always) cause issues in finding handlers for >> >> the type -- for example, if value type has annotations, they may not >> >> be visible. >> >> >> >> -+ Tatu +- >> > >> > -- >> > 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 post to this group, send email to jackso...@googlegroups.com. >> > For more options, visit https://groups.google.com/d/optout. > > -- > 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+unsubscr...@googlegroups.com. > To post to this group, send email to jackson-user@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. -- 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+unsubscr...@googlegroups.com. To post to this group, send email to jackson-user@googlegroups.com. For more options, visit https://groups.google.com/d/optout.