is that clear Tatu?

On Thursday, December 20, 2018 at 1:51:04 PM UTC-8, matt Smith wrote:
>
> I am serializing `i` which would be "abc" : ["item1", "item2", item3"].
> `i` is object here and I have defined in same serializer how to serialize 
> "this" object
>
> do you want the example checked into github for quick run?
>
>
> On Thursday, December 20, 2018 at 1:44:45 PM UTC-8, Tatu Saloranta wrote:
>>
>> On Thu, Dec 20, 2018 at 1:28 PM matt Smith <[email protected]> 
>> wrote: 
>> > 
>> > I added as comments beside each line in the else block below. that is 
>> what I expect it to work. is my expectation in any of those lines? 
>>
>> Almost right, with one incorrect one: 
>>
>> > 
>> >  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);                   
>>  //"combined" 
>> >                  if (item instanceof  ItemRow){ 
>> >                      for (Object i : items) { 
>> >                          jgen.writeStartObject();              // "{" 
>> >                          jgen.writeObject(i);                     // 
>> "abc" :["item1","item2", "item2"] 
>>
>> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^'
>>  
>>
>>
>> "abc" is not output -- you are asking for value `i` to be serialized as a 
>> value. 
>> Not as property-name/value pair. 
>> So if you expect "abc" in there, you need to call `writeFieldName("abc")` 
>>
>> -+ Tatu +- 
>>
>> >                         jgen.writeEndObject();  // "}" 
>> > 
>> > 
>> > 
>> > On Thursday, December 20, 2018 at 11:32:55 AM UTC-8, matt Smith wrote: 
>> >> 
>> >> 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 [email protected]. 
>> > To post to this group, send email to [email protected]. 
>> > 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 [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to