I see.

I could get what I want with such ways.

Thanks for quick response.

2016年10月8日土曜日 2時52分18秒 UTC+9 Tatu Saloranta:
>
> So, what happens is that while you think you are giving a 
> List<ExampleContent> (or List<AbstractContent>), what runtimes sees is a 
> List<? extends Object>. Since `Object` has no `@JsonTypeInfo`, no type is 
> included.
>
> You have been tripped by Java Type Erasure. Also, my general suggestion is 
> to NEVER EVER use generic type as the root type, but always use a 
> non-generic type. There are multiple ways to do that. Like:
>
> 1. Always use a POJO to contain Lists, Maps (wrapper)
> 2. Sub-class Lists, Maps:
>
>     public class ExampleList extends ArrayList<AbstractContent> { }
>
>    because this does retain type information
>
> But there is also one way to make generic root type to work (although with 
> caveats)
>
> 1. Use `ObjectWriter`, specifying actual type, without erasure:
>
>    mapper.writerFor(new TypeReference<List<AbstractContent>>() { 
> }).writeValueAsString(list);
>
> in this case you may hit other problems, since type is now fixed for 
> content as well, so this may fail for some type definitions.
>
> -+Tatu +-
>
>
> On Thu, Oct 6, 2016 at 5:53 PM, krrrr38 <[email protected] <javascript:>> 
> wrote:
>
>> Hi. I use jackson-databind 2.8.3.
>>
>> I want to serialize value with `@JsonTypeName`. When serialize with 
>> `List`, the property value is not shown.
>>
>> public class Main {
>>    public static void main(String[] args) throws Exception {
>>        ObjectMapper om = new ObjectMapper();
>>        System.out.println(om.writeValueAsString(new ExampleContent()));
>>        System.out.println(om.writeValueAsString(Arrays.asList(new 
>> ExampleContent())));
>>    }
>>
>>     @JsonTypeInfo(
>>            use = JsonTypeInfo.Id.NAME,
>>            include = JsonTypeInfo.As.PROPERTY,
>>            property = "type",
>>            visible = true
>>    )
>>    @JsonSubTypes({
>>            @JsonSubTypes.Type(ExampleContent.class)
>>    })
>>    abstract static public class AbstractContent {
>>    }
>>
>>     @JsonTypeName("text")
>>    public static class ExampleContent extends AbstractContent {
>>    }
>> }
>>
>>
>> result
>>
>> {"type":"text"}
>> [{}]
>>
>>
>> expected?
>>
>> {"type":"text"}
>> [{"type":"text"}]
>>
>> Do I need to add more annotation or something?
>>
>> Regargs.
>>
>> -- 
>> 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] <javascript:>.
>> To post to this group, send email to [email protected] 
>> <javascript:>.
>> 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