stanfy / gson-xml

Java library for XML deserialization
Apache License 2.0
199 stars 41 forks source link

Deserializing with setSameNameLists(true) fails with JsonSyntaxException #25

Open gaul opened 8 years ago

gaul commented 8 years ago

Consider the following test case:

  private static final String TEST_ACL_XML =
    "<Class1>" +
    "    <Class2>" +
    "        <Class3/>" +
    "    </Class2>" +
    "</Class1>";

  @Test
  public void testAclXml() {
    Class1 acl = new GsonXmlBuilder()
        .setXmlParserCreator(SimpleXmlReaderTest.PARSER_CREATOR)
        .setSameNameLists(true)
        .create()
        .fromXml(TEST_ACL_XML, Class1.class);
  }

  static final class Class1 { 
    @SerializedName("Class2")
    List<Class2> class2;

    static final class Class2 { 
      @SerializedName("Class3")
      Class3 class3;

      static final class Class3 {
      }
    }
  }

which fails with:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: BEGIN_OBJECT expected, but met STRING
Scopes: INSIDE_OBJECT>NAME>INSIDE_OBJECT>INSIDE_EMBEDDED_ARRAY>INSIDE_OBJECT
Closed tags: 'Class3'/3
Token: null
Tokens queue: null
Values queue: , null

    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:81)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
    at com.google.gson.Gson.fromJson(Gson.java:795)
    at com.stanfy.gsonxml.GsonXml.fromXml(GsonXml.java:96)
    at com.stanfy.gsonxml.GsonXml.fromXml(GsonXml.java:66)
    at com.stanfy.gsonxml.GsonXml.fromXml(GsonXml.java:52)
    at com.stanfy.gsonxml.GsonXml.fromXml(GsonXml.java:42)
    at com.stanfy.gsonxml.test.ListsTest.testAclXml(ListsTest.java:303)

Removing setSameNameLists(true) allows the test to pass although my application requires this behavior.

TeemuStenhammar commented 8 years ago

I am having this same issue with nested object. Is there any plan to look in to this?

gaul commented 8 years ago

@TeemuStenhammar I moved to jackson-dataformat-xml since this issue blocked me.

TeemuStenhammar commented 8 years ago

@andrewgaul Thank you for pointing to that project! Just switched to it and after a bit configuration I got forward from this issue.