0

Have a custom Serializer and want to access default Serializer inside that.

public class HibernateLazySerializer extends JsonSerializer<PersistentCollection> {

    @Override
    public void serialize(PersistentCollection value, JsonGenerator gen, SerializerProvider provider) throws IOException {

    if (!value.wasInitialized()) {
        gen.writeNull();
        return;
    } else {
        //gen.writeStartObject();
        provider.defaultSerializeValue(value,gen);
        // gen.writeEndObject();
        //return;
   }
}

in else block want to call default Serializer to avoid stack overflow error as in same Serializer it is geting called again and again.

Or is there any other way i can handle my Serializer in else block.

Mike
  • 4,722
  • 1
  • 27
  • 40
Nayak
  • 99
  • 1
  • 7
  • Why you do not use `com.fasterxml.jackson.datatype.hibernate4.PersistentCollectionSerializer` class from [jackson-datatype-hibernate](https://github.com/FasterXML/jackson-datatype-hibernate) module? See also: [jackson - do not serialize lazy objects](https://stackoverflow.com/questions/25906985/jackson-do-not-serialize-lazy-objects) – Michał Ziober Mar 08 '19 at 22:40
  • hm.configure(Hibernate4Module.Feature.FORCE_LAZY_LOADING this is not working and loading the lazy objects which I don't want. Any idea how to access default serializer inside custom serializer – Nayak Mar 09 '19 at 07:23
  • Which version of `Hibernate` and `Jackson` do you use? – Michał Ziober Mar 09 '19 at 10:09
  • Hibernate 5 , Jackson 2.9.8 and spring boot latest. Am doing hm.configure(Hibernate5Module.Feature.FORCE_LAZY_LOADING.but it seems like it is loading lazy objects.have tried a lot to get rid of this but no luck.So I thought of writing my own serializer and if it is persistence collection and write as null.this seems to be fine but it's going in a loop and getting stack overflow error.so need to access default serializer if the proxy is loaded for egar loading else write as null.plz check my custom serializer code u will understand.have gone through all the possible answers in net but no luck. – Nayak Mar 10 '19 at 07:35

0 Answers0