On Tue, Mar 19, 2019 at 2:47 AM Jan Bernitt <[email protected]> wrote: > > Using @JsonFormat throws an exception as well. > I think your idea merging the maps would work but creates unwanted API and > intermediate objects. > > FYI: I ended up using @JsonSerializer(using =...) (note that as does not work > when interface already implements Map) and implemented a fully custom > rendering of the maps and properties.
`as` only allows modifying of declared type for purpose of finding different (de)serializer, so `using` makes sense. -+ Tatu +- > > /Jan > > Am Dienstag, 19. März 2019 05:37:46 UTC+1 schrieb Tatu Saloranta: >> >> On Mon, Mar 18, 2019 at 7:28 PM Jan Bernitt <[email protected]> wrote: >> > >> > Hi, >> > >> > given this model: >> > >> > interface A<T> extends Map<String, T> { >> > >> > Map<String, Object> getExtensions(); >> > >> > String getX(); >> > >> > void setX(String x); >> > } >> > >> > class Foo extends A<Bar> { } >> > class Bar { } >> > >> > Filling it with data: >> > >> > Foo foo = new Foo(); >> > foo.put("bar", new Bar()); // using it as Map >> > foo.setX("y"); >> > foo.getExtension().put("ext", "ext-value"); >> > >> > I want this JSON generated: >> > >> > { >> > "bar": { >> > // bar's properties >> > }, >> > "x": "y" , >> > "ext": "ext-value" >> > } >> > >> > So the extensions Map is flattened into the same object as the extended >> > map. And properties like `x` are included as well. >> > >> > This has proven hard to configure. >> > Treating it as bean would require two usages of @JsonAnyGetter (extended >> > and extension map). >> > Treating it as map ignores the other properties. >> > Using custom serializers would need to delegate to a map and a bean >> > serializer as it would be used if I would not configure a custom >> > serializer. I failed to do this so far. >> > >> > Any suggestion is welcome. >> >> I think you covered all available options, and I can't think of >> others. About the only note I'll make is that it is not possible to >> annotate more than one accessor with `@JsonAnyGetter`: it will not >> work and may (should) throw an exception to indicate problem. >> >> What I would probably due would be to treat is as POJO (with >> @JsonFormat(shape = Shape.POJO)), and then define custom accessor to >> annotate with `@JsonAnyGetter`: one that would combine `extensions` >> with entries of `Map` itself. >> >> -+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 [email protected]. > To post to this group, send email to [email protected]. > 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 [email protected]. To post to this group, send email to [email protected]. For more options, visit https://groups.google.com/d/optout.
