On Wed, Mar 22, 2017 at 10:24 PM, Zsolt Balanyi <zsolt.bala...@gmail.com> wrote:
> Hi!
>
> Of course my code is related to XML:

Right, I just meant it regarding general data-binding functionality.
Your code can certainly use its knowledge as necessary.

>
>         ObjectMapper om = new XmlMapper();
>         om.enable(SerializationFeature.INDENT_OUTPUT);
>         om.registerModule(new JacksonXmlModule() {
>             @Override
>             public void setupModule(SetupContext context) {
>                 super.setupModule(context);
>                 context.addBeanSerializerModifier(new
> UIElementSerializerModifier());
>             }
>         });
>
> public class UIElementSerializerModifier extends XmlBeanSerializerModifier {
>     @Override
>     public JsonSerializer<?> modifySerializer(SerializationConfig config,
> BeanDescription beanDesc, JsonSerializer<?> serializer) {
>         if (UIElement.class.isAssignableFrom(beanDesc.getBeanClass())) {
>             System.out.println(serializer.getClass().getName());
>             return new BSerializer((BeanSerializerBase) serializer);
>         }
>         return serializer;
>     }
> }
>
> public class BSerializer extends XmlBeanSerializer {
>
>     public BSerializer(BeanSerializerBase src) {
>         super(src);
>     }
>
>     @Override
>     public void serialize(Object bean, JsonGenerator jgen,
> SerializerProvider provider) throws IOException {
>       // The breakpoint on next line is never hit!!!
>       super.serialize(bean, jgen, provider);
>     }
> }
>
> I create an object that extends UIElement, the appropriate BSerializer is
> created, but never called.
> Is this a bug in jackson-dataformat-xml, or I'm doing wrong something?

One possibility is that if the type uses polymorphic handling, method
called is actually

     public void serializeWithType(T value, JsonGenerator g,
SerializerProvider p,
          TypeSerializer ts);

and you may need to override that as well.

-+ Tatu +-

>
> BR, Zsolt
>
> 2017. március 22., szerda 23:21:03 UTC+1 időpontban Tatu Saloranta a
> következőt írta:
>>
>> On Wed, Mar 22, 2017 at 1:31 AM, Zsolt Balanyi <zsolt....@gmail.com>
>> wrote:
>> > Hi!
>> >
>> > The tag name can be easily renamed, by
>> >
>> >         if (gen instanceof ToXmlGenerator) {
>> >             ((ToXmlGenerator)gen).setNextName(new
>> > QName(bean.getClass().getSimpleName()));
>> >         }
>> >         doNormalSerialization();
>>
>> Yes, but that does not mean much when it's `jackson-databind` and its
>> type handling code which can not call such method
>> (it is XML specific and not needed for other formats).
>>
>> > The only problem is, that I need to serialize all other fields, which I
>> > dont
>> > know how... That is why I would need to get the method to perform the
>> > serialization, as the default serializer would.
>> > Also, the BeanSerializerModifier approach did not work.
>> > The new serializer is only created, but not called during the
>> > serialization.
>> > Maybe it has to something with the fact that I register it for an
>> > abstract
>> > class, and the concrete implementors still use their defaults?
>>
>> BeanSerializerModifier is a callback, but yes, result would be bound
>> for whatever type it was called for.
>>
>> -+ Tatu +-
>>
>> >
>> > BR Zsolt
>> >
>> >
>> >
>> > 2017. március 22., szerda 5:38:56 UTC+1 időpontban Tatu Saloranta a
>> > következőt írta:
>> >>
>> >> On Tue, Mar 21, 2017 at 12:16 AM, Zsolt Balanyi <zsolt....@gmail.com>
>> >> wrote:
>> >> > Hi!
>> >> >
>> >> > That was one of my first attempts :)
>> >> >
>> >> > <Page>
>> >> >   <uiElement>
>> >> >     <StackLayout>
>> >> >       <uiElements>
>> >> >         <Label text="some text"/>
>> >> >       </uiElements>
>> >> >       <uiElements>
>> >> >         <Image url="mypageUrl"/>
>> >> >       </uiElements>
>> >> >     </StackLayout>
>> >> >   </uiElement>
>> >> > </Page>
>> >> >
>> >> > The question is how could I ommit the uiElement nodes?
>> >>
>> >> <uiElement> itself matches property `uiElement` so that can not be
>> >> removed.
>> >> Or, if expectation was that property name would be replaced by type id
>> >> that is not something Jackson supports or will be able to support --
>> >> it may make sense from XML viewpoint, but would not work with JSON or
>> >> any other supported dataformat. XML is bit of an outlier as its
>> >> structural model is the most difficult one to support, of formats
>> >> supported.
>> >>
>> >> -+ Tatu +-
>> >>
>> >> >
>> >> > BR, Zsolt
>> >> >
>> >> > 2017. március 20., hétfő 20:16:38 UTC+1 időpontban Tatu Saloranta a
>> >> > következőt írta:
>> >> >>
>> >> >> Have you tried choice of `JsonTypeInfo.As.WRAPPER_OBJECT`?
>> >> >> That does add type id as property-name "wrapper" in JSON, and should
>> >> >> work quite similarly with XML.
>> >> >> The main problem with XML is often, however, that the root value
>> >> >> requires additional extra XML element.
>> >> >>
>> >> >> -+ Tatu +-
>> >> >>
>> >> >> On Mon, Mar 20, 2017 at 11:42 AM, Zsolt Balanyi
>> >> >> <zsolt....@gmail.com>
>> >> >> wrote:
>> >> >> > Hi!
>> >> >> >
>> >> >> > public class Page {
>> >> >> >    public UIElement uiElement;
>> >> >> > }
>> >> >> >
>> >> >> > @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include =
>> >> >> > JsonTypeInfo.As.PROPERTY, property = "type")
>> >> >> > public abstract class UIElement {
>> >> >> > }
>> >> >> >
>> >> >> > public class StackLayout extends UIElement {
>> >> >> >     public List<UIElement> uiElements = new ArrayList<>();
>> >> >> > }
>> >> >> >
>> >> >> > public class Label extends UIElement {
>> >> >> >     @JacksonXmlProperty(isAttribute=true)
>> >> >> >     public String text;
>> >> >> > }
>> >> >> >
>> >> >> > So basically I have  a Page, that contains a  StackLayout that
>> >> >> > contains
>> >> >> > a
>> >> >> > Label and an Image.
>> >> >> > It would solve the problem, if the JsonTypeInfo could put the type
>> >> >> > name
>> >> >> > to
>> >> >> > the element name...
>> >> >> >
>> >> >> > BR, Zsolt
>> >> >> >
>> >> >> > 2017. március 20., hétfő 19:22:10 UTC+1 időpontban Tatu Saloranta
>> >> >> > a
>> >> >> > következőt írta:
>> >> >> >>
>> >> >> >> What is the POJO you are serializing?
>> >> >> >>
>> >> >> >> -+ Tatu +-
>> >> >> >>
>> >> >> >>
>> >> >> >> On Mon, Mar 20, 2017 at 5:11 AM, Zsolt Balanyi
>> >> >> >> <zsolt....@gmail.com>
>> >> >> >> wrote:
>> >> >> >> > Hi!
>> >> >> >> >
>> >> >> >> > I have a structure that outputs this:
>> >> >> >> >
>> >> >> >> > <Page>
>> >> >> >> >   <uiElement type="StackLayout">
>> >> >> >> >     <uiElements type="Label" text="some text"/>
>> >> >> >> >     <uiElements type="Image" url="mypageUrl"/>
>> >> >> >> >   </uiElement>
>> >> >> >> > </Page>
>> >> >> >> >
>> >> >> >> > I use @JsonTypeInfo(use=JsonTypeInfo.Id.NAME,
>> >> >> >> > include=As.PROPERTY,
>> >> >> >> > property="type") on the common base class.
>> >> >> >> > How could I get the following output:
>> >> >> >> >
>> >> >> >> > <Page>
>> >> >> >> >   <StackLayout>
>> >> >> >> >     <Label text="some text"/>
>> >> >> >> >     <Image url="mypageUrl"/>
>> >> >> >> >   </StackLayout>
>> >> >> >> > </Page>
>> >> >> >> >
>> >> >> >> > It does not matter, if it can not be deserialized by Jackson,
>> >> >> >> > it
>> >> >> >> > is
>> >> >> >> > read
>> >> >> >> > by
>> >> >> >> > another system, that requires this structure.
>> >> >> >> >
>> >> >> >> > BR, Zsolt
>> >> >> >> >
>> >> >> >> > --
>> >> >> >> > 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...@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...@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...@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.

Reply via email to