question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`MismatchedInputException` for nested repeating element name in `List`

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
lucanus81commented, Apr 27, 2020

@cowtowncoder: I was using 2.10.2, but can reproduce the same issue on 2.11.0-RC1.

1reaction
cowtowncodercommented, May 12, 2020

Hmmmh. Technically, I think this must be due to low-level detection of “wrapper-less” arrays, which creates virtual wrapper for databind to use. And in this case use of price at inner level too gets things confused. Wasn’t caught before as names have been distinct.

Read more comments on GitHub >

github_iconTop Results From Across the Web

`MismatchedInputException` for nested repeating element ...
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.
Read more >
Find repeated element in a nested list - python
Uniqueness or repetition is determined by the first element of every sub list. For example: 'Name' , 'Weight' . If there are two...
Read more >
Solving the XML Problem with Jackson
These annotations allow us to control the XML namespace and local name for elements, including the root element, whether a field is rendered ......
Read more >
Nested JSON Parsing with Repeated nodes
Hi All, I have JSON as below : "elements": { "element": { "name": "ServiceNow" } }, "elements": {
Read more >
SyntaxError: JSON.parse: bad parsing - JavaScript | MDN
... array element SyntaxError: JSON.parse: end of data when property name was ... expected ':' after property name in object SyntaxError: JSON.parse: end...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found