I didn't see a unit test covering lists in the serialize, so I wrote one 
and have attached. Would appreciate someone committing it. It passes, so 
this stuff works, but there's something about my case that's throwing it a 
curve.

The bug exists in 2.14 and 2.15 as well as 2.16.



On Sunday, November 26, 2023 at 8:24:24 PM UTC-5 Thom Hehl wrote:

> I'm a newb with this project, although I've used jackson as a user for 
> years, but never for XML before.
>
> I have XML that looks like this:
>
>     <div type="book" osisID="Obad">
>       <chapter osisID="Obad.1">
>         <verse osisID="Obad.1.1">
>           <w lemma="2377" n="1.0" morph="HNcmsc" id="31xeN">חֲז֖וֹן</w>
>
> I am deserializing into my Java class that looks like this:
>
> public class OsisVerse extends DocumentVerse {
>     @JacksonXmlElementWrapper(useWrapping = false)
>     @JacksonXmlProperty(localName = "note")
>     private List<OsisNote> osisNotes;
>     @JacksonXmlProperty(localName = "osisID")
>     private String uniqueId;
>     @JacksonXmlProperty(localName = "seg")
>     private String segment;
>     @JacksonXmlElementWrapper(useWrapping = false)
>     @JacksonXmlProperty(localName = "w")
>     private List<OsisWord> osisWords;
>
> and it de serializes great. (Thanks for a great product, BTW!)
>
> Then I make some changes and serialize it back using the same object and 
> here's the output I'm getting.
>
>       <chapters>
>         <chapters>
>           <verse>
>             <words>
>               <words id="31yNB" lemma="l/4421" morph="HRd/Ncfsa" n="0" 
> x-source-word="לַ/מִּלְחָמָֽה">(...)</words>
>             </words>
>             <notes/>
>
> Note that in addition to not changing the tags back to "w", it also 
> wrapped the list in a tag called words and then only output 1 word tag.
>
> I have stepped through the code and watched it buffer the "w" without the 
> list header, so I'm not sure what happened after that, but I"m pulling my 
> hair out.
>
> If anyone wants to have a look, you can pull 
> https://github.com/ThomHehl/xlate and run SourceTextConverterTest.groovy. 
> (Spock test under gradle.)
>
> Thanks!
>

-- 
You received this message because you are subscribed to the Google Groups 
"jackson-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to jackson-dev+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jackson-dev/1c454ebc-9ffd-48e8-8cf1-b2ae991758c7n%40googlegroups.com.
package com.fasterxml.jackson.dataformat.xml.ser;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlCData;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlText;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings("serial")
public class TestSerializationList extends XmlTestBase
{
    static class Sentence
    {
        @JacksonXmlProperty(isAttribute=true)
        public int numWords;

        @JacksonXmlElementWrapper(useWrapping = false)
        @JacksonXmlProperty(isAttribute = false, localName = "w")
        public List<Word> wordList;

        public Sentence(String sentenceText) {
            wordList = new ArrayList<>();

            boolean boldFlag = false;
            String[] words = sentenceText.split(" ");
            for (String myWord : words) {
                Word word = new Word();
                word.bodyText = myWord;

                word.bold  = boldFlag;
                boldFlag = !boldFlag;

                wordList.add(word);
            }

            numWords = wordList.size();;
        }
    }

    static class Word
    {
        @JacksonXmlProperty(isAttribute=true, localName="boldface")
        public boolean bold;

        @JacksonXmlText
        public String bodyText;
    }

    /*
    /**********************************************************
    /* Unit tests
    /**********************************************************
     */

    private final XmlMapper _xmlMapper = new XmlMapper();

    public void testSentence() throws IOException
    {
        String xml = _xmlMapper.writeValueAsString(new Sentence("Don't Panic!"));
        xml = removeSjsxpNamespace(xml);
        assertEquals("<Sentence numWords=\"2\"><w boldface=\"false\">Don't</w><w boldface=\"true\">Panic!</w></Sentence>", xml);
    }
}

Reply via email to