Skip to content

MismatchedInputException for nested repeating element name in List #393

@kaizen-horse

Description

@kaizen-horse

I want to deserialize the string contents to Testclass.class on jackson 2.9.10
but I got a Exception: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of java.lang.String out of START_OBJECT token
at [Source: (StringReader); line: 11, column: 7] (through reference chain: saas.mall.supplier.zowoyoo.TestClass["prices"]->saas.mall.supplier.zowoyoo.TestClass$Prices["price"]->java.util.ArrayList[0]->saas.mall.supplier.zowoyoo.TestClass$Price["price"])

The Class that I want to deserialize contains a list of fields with the same name as the fields of the object in the list。

Here is the test case

    @Test
    public void testXml() throws IOException {
        String content = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "\n" +
                "<result>\n" +
                "  <prices>\n" +
                "    <price>\n" +
                "      <num>100</num>\n" +
                "      <price>7.0</price>\n" +
                "    </price>\n" +
                "  </prices>\n" +
                "</result>";
        com.fasterxml.jackson.dataformat.xml.XmlMapper xmlMapper =
                new com.fasterxml.jackson.dataformat.xml.XmlMapper();
        TestClass testClass = xmlMapper.readValue(content, TestClass.class);
        System.out.println(testClass);
    }

this is the Class I want to deserialize

package test;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;

import java.util.ArrayList;
import java.util.List;


@JsonInclude(JsonInclude.Include.NON_NULL)
public class TestClass {
    /**
     *
     */
    private Prices prices = new Prices();

    public void setPrices(Prices prices) {
        this.prices = prices;
    }

    @JacksonXmlProperty(localName = "prices")
    public Prices getPrices() {
        return this.prices;
    }

    @JacksonXmlRootElement(localName = "price")
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class Price {
        /**
         * 0.01
         */
        private String price;

        /**
         * 469
         */
        private String num;

        public void setPrice(String price) {
            this.price = price;
        }

        @JacksonXmlProperty(localName = "price")
        public String getPrice() {
            return this.price;
        }

        public void setNum(String num) {
            this.num = num;
        }

        @JacksonXmlProperty(localName = "num")
        public String getNum() {
            return this.num;
        }
    }

    @JacksonXmlRootElement(localName = "prices")
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class Prices {
        /**
         *
         */
        private List<Price> price = new ArrayList<Price>();

        public void setPrice(List<Price> price) {
            this.price = price;
        }

        @JacksonXmlElementWrapper(useWrapping = false)
        @JacksonXmlProperty(localName = "price")
        public List<Price> getPrice() {
            return this.price;
        }
    }
}

PS: if I change the TestClass.Price.price‘s field type from String to Map,it works.like follwing

    @JacksonXmlRootElement(localName = "price")
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class Price {
        /**
         * 0.01
         */
        private Map price;


        /**
         * 469
         */
        private String num;


        public void setPrice(Map price) {
            this.price = price;
        }

        @JacksonXmlProperty(localName="price")
        public Map getPrice(){
            return this.price;
        }

        public void setNum(String num) {
            this.num = num;
        }

        @JacksonXmlProperty(localName="num")
        public String getNum (){
            return this.num;
        }
    }

but when I use a string that contains more objects,
I got a exception:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of java.util.LinkedHashMap (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('4.0')
at [Source: (StringReader); line: 11, column: 18] (through reference chain: test.TestClass["prices"]->test.TestClass$Prices["price"]->java.util.ArrayList[1]->test.TestClass$Price["price"])

    @Test
    public void testXml() throws IOException {
        String content = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "\n" +
            "<result>\n" + " <prices>\n" + " <price>\n" + " <num>100</num>\n" +
            " <price>7.0</price>\n" + " </price>\n" + " <price>\n" +
            " <num>100</num>\n" + " <price>4.0</price>\n" + " </price>" +
            " </prices>\n" + "</result>";
        com.fasterxml.jackson.dataformat.xml.XmlMapper xmlMapper = new com.fasterxml.jackson.dataformat.xml.XmlMapper();
        TestClass testClass = xmlMapper.readValue(content, TestClass.class);
        System.out.println(testClass);
    }

Hope to provide a solution,TKS

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions