0

I defined a class like this:

public class Record1 extends AbstractList<Record1> {
    int field1;
    int field2;

    public int size() {
        return 1;
    }
    public Record1 get(int index){
        if(index == 0){
           return this;
        }else{
           throw exception;
        }
    }
}

As above definition showing, the Record1 class is a list with only one item, and the only item is it self.

When I serialize a instance if Record1 class using jackson, it treat that object as a list, and try to serialize the first item in list, but the first item of list is also the list itself, and thus lead a StackOverflow:

Caused by: java.lang.StackOverflowError
    at com.fasterxml.jackson.databind.SerializerProvider.getAttribute(SerializerProvider.java:398)
    at com.fasterxml.jackson.databind.ser.std.StdSerializer.findContextualConvertingSerializer(StdSerializer.java:364)
    at com.fasterxml.jackson.databind.ser.std.AsArraySerializerBase.createContextual(AsArraySerializerBase.java:194)
    at com.fasterxml.jackson.databind.SerializerProvider.handleSecondaryContextualization(SerializerProvider.java:1004)
    at com.fasterxml.jackson.databind.SerializerProvider.findValueSerializer(SerializerProvider.java:524)
    at com.fasterxml.jackson.databind.ser.impl.PropertySerializerMap.findAndAddSecondarySerializer(PropertySerializerMap.java:90)
    at com.fasterxml.jackson.databind.ser.std.AsArraySerializerBase._findAndAddDynamic(AsArraySerializerBase.java:305)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:140)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:107)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:145)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:107)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:145)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:107)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:145)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:107)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:145)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:107)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:145)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:107)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:145)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:107)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:145)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:107)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:145)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:107)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:145)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:107)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:145)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:107)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:145)

I want to tell jackson to treat Record1 as a pojo instead of a collection, but I failed to find a correct solution. Can someone tell me the right way?

ps. I don't need a custom Serializer. In real case, Record1 has 10+ fields, and I will define Record2/Record3/Record4....RecordN. Write Serializer by hand is a heavy work. I just want tell jackson to Serialize Record1 as bean instead of list.

  • Possible duplicate of [How do I use a custom Serializer with Jackson?](https://stackoverflow.com/questions/7161638/how-do-i-use-a-custom-serializer-with-jackson) – Torben Sep 07 '18 at 07:44
  • I don't need a custom Serializer. In real case, Record1 has 10+ fields, and I will define Record2/Record3/Record4....RecordN. Write Serializer by hand is a heavy work. I just want tell jackson to Serialize Record1 as bean instead of list – Expressway Retrograding Sep 07 '18 at 08:49
  • Why do you want to use a fake list at all? – Lukas Körfer Sep 07 '18 at 08:58
  • @lu.koerfer for memory saving. I have a data structure like this: Map>. the number of List instance in map is about 30 millions, and almost all List's size is 1. Put Record1 into list wast much memory. – Expressway Retrograding Sep 07 '18 at 09:14
  • I found the answer. [link](https://github.com/FasterXML/jackson-databind/issues/40) – Expressway Retrograding Sep 07 '18 at 09:15

0 Answers0