On Wed, May 16, 2018 at 11:35 PM,  <ta...@transdata.net> wrote:
> Using 2.7.4 I tried to use a BeanSerializerModifier to avoid serialization
> of beans meeting some condition.

First things first: as usual, it is good idea to always verify with
the last patch version of the minor version (2.7.9 in this case),
to ensure there are no further fixes. And if possible, later minor versions.

But in this case I suspect problem is that you are not forwarding calls to

   `createContextual()` (for `ContextualSerializer`s)
   `resolve()` (for `ResolvableSerializer`s)

in your case, you need to forward these to `defaultSerializer`:
"resolve()" does not return anything, it just needs to be called.
But `createContextual()` may create modified instance (as per property
definition overrides), and you may need to construct
a new `AbstractSkippableSerializer`.

-+ Tatu +-


> This works fine. But using also identity references I run into a
> NullPointerException:
>
> at
> com.fasterxml.jackson.databind.ser.impl.WritableObjectId.writeAsField(WritableObjectId.java:64)
> at
> com.fasterxml.jackson.databind.ser.std.BeanSerializerBase._serializeWithObjectId(BeanSerializerBase.java:596)
> at
> com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:148)
>
>
> Code to reproduce:
>
> public class SerializationTest {
>
>     private static final String REFID_PROPERTY = "ref-id";
>
>     public static abstract class AbstractSkippable {
>
>         private String someData;
>
>         public abstract boolean hasData();
>
>         public String getSomeData() {
>             return someData;
>         }
>
>         public void setSomeData(String someData) {
>             this.someData = someData;
>         }
>
>     }
>
>     @JsonIdentityReference
>     @JsonIdentityInfo(property = REFID_PROPERTY, generator =
> ObjectIdGenerators.UUIDGenerator.class)
>     public static class Data extends AbstractSkippable {
>
>         @Override
>         public boolean hasData() {
>             return true;
>         }
>
>     }
>
>     public static class MyData {
>
>         private Data data1;
>         private Data data2;
>
>         public Data getData1() {
>             return data1;
>         }
>
>         public void setData1(Data data1) {
>             this.data1 = data1;
>         }
>
>         public Data getData2() {
>             return data2;
>         }
>
>         public void setData2(Data data2) {
>             this.data2 = data2;
>         }
>
>     }
>
>     public static class AbstractSkippableSerializer extends
> JsonSerializer<AbstractSkippable> {
>
>         private final JsonSerializer<Object> defaultSerializer;
>
>         public 
> AbstractSkippableSerializer(JsonSerializer<Object>defaultSerializer
> defaultSerializer) {
>             if (defaultSerializer == null) {
>                 throw new IllegalArgumentException("defaultSerializer must
> not be null");
>             }
>             this.defaultSerializer = defaultSerializer;
>         }
>
>         @Override
>         public void serialize(AbstractSkippable value, JsonGenerator gen,
> SerializerProvider serializers) throws IOException {
>             defaultSerializer.serialize(value, gen, serializers);
>         }
>
>         @Override
>         public boolean isEmpty(SerializerProvider provider,
> AbstractSkippable value) {
>             return value == null || !value.hasData();
>         }
>
>     }
>
>     public static class SkippableSerializerModifier extends
> BeanSerializerModifier {
>
>         @Override
>         public JsonSerializer<?> modifySerializer(SerializationConfig
> config, BeanDescription beanDesc, JsonSerializer<?> serializer) {
>             Class<?> beanClass = beanDesc.getBeanClass();
>             if (AbstractSkippable.class.isAssignableFrom(beanClass)) {
>                 @SuppressWarnings("unchecked")
>                 JsonSerializer<Object> defaultSerializer =
> (JsonSerializer<Object>) serializer;
>                 return new AbstractSkippableSerializer(defaultSerializer);
>             }
>             return serializer;
>         }
>
>     }
>
>     @Test
>     public void test() throws JsonProcessingException {
>         JacksonXmlModule mod = new JacksonXmlModule();
>         mod.setSerializerModifier(new SkippableSerializerModifier());
>         XmlMapper mapper = new XmlMapper(mod);
>         MyData myData = new MyData();
>         Data data = new Data();
>         data.setSomeData("data");
>         myData.setData1(data);
>         myData.setData2(data);
>         String xml = mapper.writeValueAsString(data);
>         System.out.println(xml);
>     }
>
> }
>
>
> Any idea?
>
> --Heiko
>
> --
> 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