Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deserializing when using custom wrapper element #17

Closed
mortenoh opened this issue Feb 29, 2012 · 9 comments
Closed

Deserializing when using custom wrapper element #17

mortenoh opened this issue Feb 29, 2012 · 9 comments

Comments

@mortenoh
Copy link

I'm having some issues deserializing a class that uses a @JacksonXmlElementWrapper.

This how my class looks:

@JacksonXmlRootElement( localName = "person", namespace ="http://example.org/person" )
public class Person
{
   @JacksonXmlProperty( isAttribute = true )
   private String id;

   private String name;

   private Integer age;

   @JacksonXmlElementWrapper(localName = "notes")
   @JacksonXmlProperty( localName = "note" )
   private List<String> notes = new ArrayList<String>();

  // getters, setters, etc
}

And this gets serialized fine with:

Person p = new Person( "Name", 30 );
p.getNotes().add( "This is note #1" );
p.getNotes().add( "This is note #2" );

ObjectMapper xmlMapper = new XmlMapper();
String xml = xmlMapper.writeValueAsString( p );

This gives me something like:

<zdef-1759520061:person xmlns=""
xmlns:zdef-1759520061="http://example.org/person"
id="af47ac8e-ee8c-4e43-a87c-dbff93f28d87">
 <name>Name</name>
 <age>30</age>
 <notes>
   <note>This is note #1</note>
   <note>This is note #2</note>
 </notes>
</zdef-1759520061:person>

But, when I now try and deserialize this using:

Person p = xmlMapper.readValue( xml, Person.class );

I get:

Exception in thread "main"
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:
Unrecognized field "notes" (Class Person), not marked as ignorable
 at [Source: java.io.StringReader@5c1fe88d; line: 1, column: 169]
(through reference chain: Person["notes"])
       at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:51)
       at com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:526)
       at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:651)
       at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:771)
       at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:291)
       at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:99)
       at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2532)
       at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1774)
@cowtowncoder
Copy link
Member

Adding unit test: can reproduce the problem as described... will try to see what gives the problem.

@cowtowncoder
Copy link
Member

Looks like there is discrepancy between expected regular name, wrapper name (for test, changing local name to be 'notes' makes test pass for example).

@cowtowncoder
Copy link
Member

Managed to fix by checking and using wrapper name for deserialization, instead of property name.

@cowtowncoder
Copy link
Member

Strange. Things work ok when annotations are added on setters, but not when added on getters...

cowtowncoder added a commit that referenced this issue Mar 15, 2012
@cowtowncoder
Copy link
Member

Ok: needed to add quite a bit more code, workarounds than anticipated, but things now work.
New code should be more robust as well. Please let me know if I still managed to miss something...

@fredcooke
Copy link

I appear to still be getting this, or something similar, in 2.1.3. I've got annotations setup to correctly create this:

<Root BuildVersion="1.1.1" Description="horribleformatthati'mstuckwith" DataInterchangeType="dit" Locale="local pub">
    <Batch name="botched">
        <Documents>
            <Document DocID="hg is better" DocType="rocksml" MimeType="daddytype">
                <Tags>
                    <Tag id="hello" TagName="icknd" TagDataType="pasta">of no value at all</Tag>
                    <Tag id="hello2" TagName="icknd2" TagDataType="pasta2">of no value at all2</Tag>
                </Tags>
            </Document>
            <Document DocID="git is better" DocType="sucksml" MimeType="mummytype">
                <Tags>
                    <Tag id="hell3o" TagName="ick3nd" TagDataType="p3asta">of no v3alue at all</Tag>
                    <Tag id="hello42" TagName="icknd42" TagDataType="pas4ta2">of no value at al4l2</Tag>
                </Tags>
            </Document>
        </Documents>
    </Batch>
</Root>

However it can not read it back in, giving the following slightly sanitised error:

Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "Documents" (class com.example.bcn.tara.xml.perceptive.Batch), not marked as ignorable (2 known properties: , "name", "Document"])
 at [Source: java.io.FileInputStream@243f01d4; line: 1, column: 221] (through reference chain: com.example.bcn.tara.xml.Perceptive["Batch"]->com.example.bcn.tara.xml.perceptive.Batch["Documents"])
    at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:79)
    at com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:579)
    at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:672)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:906)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:328)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
    at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:375)
    at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:98)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:308)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2797)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1989)
    at com.example.bcn.tara.XMLMain.main(XMLMain.java:85)
    ... 6 more

The relevant lines from the Batch object:

    @JacksonXmlProperty(isAttribute=true)
    private String name;
    @JacksonXmlElementWrapper(localName="Documents")
    @JacksonXmlProperty(localName="Document")
    private List<Document> documents;

Let me know if I can help in any way. I'm happy to try snapshots if you deploy them to sonatype's OSS repo.

@fredcooke
Copy link

FYI, I could, of course, serialise and deserialise in both JSON and YAML using 2.1.3. In the end having heard nothing back from you guys, I tried simple 2.6.9, and found it to not be simple at all. Then I tried XStream and it works superbly. I'm still interested in seeing this resolved, though, as I would REALLY like to use Jackson as my catch-all serialiser/deserialiser across all of XML, JSON, YAML in a single application.

@cowtowncoder cowtowncoder reopened this Mar 12, 2013
@cowtowncoder
Copy link
Member

Sorry for slow response here. There are issues with List (de)serialization, and I hope they all come from a single root cause, to be fixed for Jackson 2.2.

@cowtowncoder
Copy link
Member

After fixing other issues (#47, #55), I think this may well have been fixed: if anyone thinks there are leftovers from this one, please file a new issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants