On Mon, May 15, 2017 at 8:52 AM, Rob Sargent <[email protected]> wrote: > I missed the need for @JsonCreator. I'll try that now. Does having a > no-argument constructor remove the need for any of the annotations I'm using > (JsonTypeInfo, JsonTypeName)
It depends on exactly what you are trying to do (only serializing? No need for that; deserializing, may still need _iff_ heterogenous contents). But if contents are homogenous (same value class) for deserialization, there are simpler ways to achieve this than forcing inclusion of (polymorphic) type information. > > Here's the full error message/hint: > Can not construct instance of persist.seg.tables.pojos.Segment: no suitable > constructor found, can not deserialize from Object value (missing default > constructor or creator, or perhaps need to add/enable type information?) > at [Source: payload.json; line: 5, column: 5] (through reference chain: > edu.utah.camplab.app.SGSPayload["segmentList"]->java.util.ArrayList[0]) Right, so you are trying to deserialize, and without specifying actual content type. I think what is needed here is piece of code you use to invoke deserialization; that would show what is needed beyond constructor. I don't think that is the only problem here... Actually, also, would need to see result type (`SGSPayload`), possibly in simplified form. -+ Tatu +- > > On Friday, May 12, 2017 at 5:57:12 PM UTC-6, Tatu Saloranta wrote: >> >> I am not sure I understand the question wrt requiring no-arguments >> constructor: yes, any object that Jackson is to create must be >> constructed somehow: and choice is usually either via zero-arguments >> constructor, or a constructor with `@JsonCreator` annotation. >> >> What exactly is the exception you get? And what does the class being >> deserialized look like wrt consructors it has? >> >> -+ Tatu +- >> >> >> On Fri, May 12, 2017 at 4:28 PM, Rob Sargent <[email protected]> wrote: >> > I'm generating classes with these annotations >> > @JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, >> > include=JsonTypeInfo.As.PROPERTY, >> > property="class") >> > @JsonTypeName("persist.seg.tables.pojos.Segment") >> > and I generate json such as >> > >> > { >> > "userName" : "rob", >> > "segmentList" : [ { >> > "class" : "persist.seg.tables.pojos.Segment", >> > "id" : "d4fca0dc-c20f-436d-b2d5-45950c148357", >> > "chrom" : 1, >> > "markersetId" : "668ea0fd-e028-408f-bc46-58691aaf194e", >> > "probandsetId" : "48f7be44-87b7-4aec-8a8f-6eff0ff21da2", >> > "startbase" : 1123456, >> > "endbase" : 1223456, >> > "eventsGreater" : 299000, >> > "eventsEqual" : 300, >> > "eventsLess" : 700 >> > }, { >> > "class" : "persist.seg.tables.pojos.Segment", >> > "id" : "52a8116d-8f68-4950-8641-730b4d99b34c", >> > "chrom" : 1, >> > "markersetId" : "668ea0fd-e028-408f-bc46-58691aaf194e", >> > "probandsetId" : "48f7be44-87b7-4aec-8a8f-6eff0ff21da2", >> > "startbase" : 1123456, >> > "endbase" : 1223456, >> > "eventsGreater" : 299900, >> > "eventsEqual" : 30, >> > "eventsLess" : 70 >> > }, { >> > "class" : "persist.seg.tables.pojos.Segment", >> > "id" : "c021517a-5b3e-48b8-928c-7790c0fe9a73", >> > "chrom" : 1, >> > "markersetId" : "668ea0fd-e028-408f-bc46-58691aaf194e", >> > "probandsetId" : "48f7be44-87b7-4aec-8a8f-6eff0ff21da2", >> > "startbase" : 1123456, >> > "endbase" : 1223456, >> > "eventsGreater" : 299990, >> > "eventsEqual" : 3, >> > "eventsLess" : 7 >> > } ] >> > } >> > >> > But without a no-argument constructor the deserialization fails. I >> > thought >> > the annotations would answer the suggestion in the error message >> > "perhaps >> > need to add/enable type information?" >> > >> > On Thursday, October 6, 2016 at 9:57:18 PM UTC-6, krrrr38 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]. >> > 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. -- 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.
