here is the class that I want to serialize. public class ItemRow<T> { private String id; private List<T> items; }
There are two variations that are allowed. `ItemRow<String>, ItemRow<ItemRow>`. In the latter case, it will be nested. eg: ItemRow item1 = new ItemRow("abc", Arrays.asList("item1", "item2", "item3" )); String result = mapper.writeValueAsString(item1); System.out.println(result); should give {"abc":["item1","item2","item3"]} Now, the latter case ItemRow item2 = new ItemRow("cde", Arrays.asList("item4, item5")); ItemRow item = new ItemRow("combined", Arrays.asList(item1,item2)); result = mapper.writeValueAsString(item); System.out.println(result); should give { "combined": { "abc": ["item1", "item2", "item3"], "cde": ["item4", "item5"] } } But I get exception while serializing the latter. The first one works as expected. so I believe the recursive serialization is failing, but I am unable to find out why Here is exception com.fasterxml.jackson.core.JsonGenerationException: Can not start an object , expecting field name (context: Object) at com.fasterxml.jackson.core.JsonGenerator._reportError(JsonGenerator. java:1961) at com.fasterxml.jackson.core.json.JsonGeneratorImpl. _reportCantWriteValueExpectName(JsonGeneratorImpl.java:244) at com.fasterxml.jackson.core.json.WriterBasedJsonGenerator. _verifyValueWrite(WriterBasedJsonGenerator.java:866) at com.fasterxml.jackson.core.json.WriterBasedJsonGenerator. writeStartObject(WriterBasedJsonGenerator.java:279) at hello.ItemRowSerializer.serialize(ItemRow.java:58) at hello.ItemRowSerializer.serialize(ItemRow.java:42) at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider. _serialize(DefaultSerializerProvider.java:480) at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider. serializeValue(DefaultSerializerProvider.java:319) at com.fasterxml.jackson.databind.ObjectMapper.writeValue(ObjectMapper. java:2655) at com.fasterxml.jackson.core.base.GeneratorBase.writeObject( GeneratorBase.java:381) at hello.ItemRowSerializer.serialize(ItemRow.java:67) at hello.ItemRowSerializer.serialize(ItemRow.java:42) Serializer implementation class ItemRowSerializer extends JsonSerializer<ItemRow> { @Override public void serialize(ItemRow itemRow, JsonGenerator jgen, SerializerProvider serializerProvider) throws IOException { String id = itemRow.getId(); List<Object> items = itemRow.getItems(); if (items.isEmpty()) { jgen.writeStartObject(); jgen.writeFieldName(id); jgen.writeStartArray(); jgen.writeEndArray(); jgen.writeEndObject(); } else { jgen.writeStartObject(); Object item = items.get(0); jgen.writeFieldName(id); if (item instanceof ItemRow){ for (Object i : items) { //ItemRow temp = (ItemRow) i; //jgen.writeObjectField(temp.getId(), temp); //jgen.writeObjectField(id, i); jgen.writeStartObject(); jgen.writeObject(i); jgen.writeEndObject(); } } else { //jgen.writeFieldName(id); jgen.writeStartArray(); for (Object arg : items) { jgen.writeString(arg.toString()); } jgen.writeEndArray(); } } jgen.writeEndObject(); } } -- 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.