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

Cannot deserialize type with generics #25

Closed
ludekdolejsky opened this issue Dec 18, 2014 · 8 comments
Closed

Cannot deserialize type with generics #25

ludekdolejsky opened this issue Dec 18, 2014 · 8 comments
Assignees
Labels

Comments

@ludekdolejsky
Copy link

Having the following code:

public class ADouble<T> {
    private T field1;
    private T field2;

    public ADouble(T field1, T field2) {
        this.field1 = field1;
        this.field2 = field2;
    }
}

public class UseADouble {
    private ADouble<String> a;

    public UseADouble(ADouble<String> a) {
        this.a = a;
    }
}

And the following test:

    @Test
    public void testDouble() throws IOException {
        UseADouble o = new UseADouble(new ADouble<String>("1","2"));

        String json = JsonWriter.objectToJson(o);

        //this will crash on ArrayIndexOutOfBoundsException
        Object ha2 = JsonReader.jsonToJava(json);

        System.out.println(ha2);

    }

The serialization works just fine:

{"@type":"UseADouble","a":{"field1":"1","field2":"2"}}

However trying to deserialize given JSON, I get an ArrayIndexOutOfBoundsException exception:

java.io.IOException: ArrayIndexOutOfBoundsException setting field 'a' on target: UseADouble@e74e382 with value: {field1=1, field2=2}
Last read: {"@type":"UseADouble","a":{"field1":"1","field2":"2"}}
line: 1, col: 55
    at com.cedarsoftware.util.io.JsonReader.error(JsonReader.java:3296)
    at com.cedarsoftware.util.io.JsonReader.assignField(JsonReader.java:1947)
    at com.cedarsoftware.util.io.JsonReader.traverseFields(JsonReader.java:1815)
    at com.cedarsoftware.util.io.JsonReader.convertMapsToObjects(JsonReader.java:1347)
    at com.cedarsoftware.util.io.JsonReader.convertParsedMapsToJava(JsonReader.java:1286)
    at com.cedarsoftware.util.io.JsonReader.readObject(JsonReader.java:1246)
    at com.cedarsoftware.util.io.JsonReader.jsonToJava(JsonReader.java:1137)
    at HomeAwayTest.testDouble(HomeAwayTest.java:79)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
    at com.cedarsoftware.util.io.JsonReader.markUntypedObjects(JsonReader.java:2044)
    at com.cedarsoftware.util.io.JsonReader.assignField(JsonReader.java:1845)
    ... 28 more
@jdereg
Copy link
Owner

jdereg commented Dec 22, 2014

This issue has been corrected and a new json-io 2.7.4 has been released to correct it. It should be available from Maven Repos after 13:00 Eastern Standard Time (US).

@jdereg jdereg closed this as completed Dec 22, 2014
@seeingmachines-fovio
Copy link

Hi,

when I raised this issue, it was indeed about inner classes, but the issue still seems to remain for normal classes... Just make ADouble and UseADouble normal, public, non-inner classes

Cheers,
L.

@jdereg
Copy link
Owner

jdereg commented Dec 22, 2014

What is inside ADouble? Can you send me that?

On Mon, Dec 22, 2014 at 9:53 AM, seeingmachines-fovio <
notifications@github.com> wrote:

Hi,

when I raised this issue, it was indeed about inner classes, but the issue
still seems to remain for normal classes... Just make ADouble and
UseADouble normal, public, non-inner classes

Cheers,
L.


Reply to this email directly or view it on GitHub
#25 (comment).

@ludekdolejsky
Copy link
Author

Hi,

I have updated the issue many times - you probably only saw my first edit?

Have a look here: #25 #25

public class ADouble {
private T field1;
private T field2;

public ADouble(T field1, T field2) {
    this.field1 = field1;
    this.field2 = field2;
}

}

public class UseADouble {
private ADouble a;

public UseADouble(ADouble<String> a) {
    this.a = a;
}

}

Cheers,
L.

On 22 Dec 2014, at 16:06, John DeRegnaucourt notifications@github.com wrote:

What is inside ADouble? Can you send me that?

On Mon, Dec 22, 2014 at 9:53 AM, seeingmachines-fovio <
notifications@github.com> wrote:

Hi,

when I raised this issue, it was indeed about inner classes, but the issue
still seems to remain for normal classes... Just make ADouble and
UseADouble normal, public, non-inner classes

Cheers,
L.


Reply to this email directly or view it on GitHub
#25 (comment).


Reply to this email directly or view it on GitHub #25 (comment).

@jdereg
Copy link
Owner

jdereg commented Dec 22, 2014

How did you test this, did you get the source code for JsonReader.java and
directly use it? Maven Repo's don't yet have 2.7.4 (they will in about 2
more hours).

On Mon, Dec 22, 2014 at 10:06 AM, John DeRegnaucourt jdereg@gmail.com
wrote:

What is inside ADouble? Can you send me that?

On Mon, Dec 22, 2014 at 9:53 AM, seeingmachines-fovio <
notifications@github.com> wrote:

Hi,

when I raised this issue, it was indeed about inner classes, but the
issue still seems to remain for normal classes... Just make ADouble and
UseADouble normal, public, non-inner classes

Cheers,
L.


Reply to this email directly or view it on GitHub
#25 (comment).

@ludekdolejsky
Copy link
Author

Yes, I just cloned fresh from master and ran the test

L.

On 22 Dec 2014, at 16:11, John DeRegnaucourt notifications@github.com wrote:

How did you test this, did you get the source code for JsonReader.java and
directly use it? Maven Repo's don't yet have 2.7.4 (they will in about 2
more hours).

On Mon, Dec 22, 2014 at 10:06 AM, John DeRegnaucourt jdereg@gmail.com
wrote:

What is inside ADouble? Can you send me that?

On Mon, Dec 22, 2014 at 9:53 AM, seeingmachines-fovio <
notifications@github.com> wrote:

Hi,

when I raised this issue, it was indeed about inner classes, but the
issue still seems to remain for normal classes... Just make ADouble and
UseADouble normal, public, non-inner classes

Cheers,
L.


Reply to this email directly or view it on GitHub
#25 (comment).


Reply to this email directly or view it on GitHub #25 (comment).

@jdereg
Copy link
Owner

jdereg commented Dec 22, 2014

Ludek, thank you for the additional testing. I see the issue now, and plan
to create a 2.7.5 today. Adding additional tests.

On Mon, Dec 22, 2014 at 10:19 AM, Ludek Dolejsky notifications@github.com
wrote:

Yes, I just cloned fresh from master and ran the test

L.

On 22 Dec 2014, at 16:11, John DeRegnaucourt notifications@github.com
wrote:

How did you test this, did you get the source code for JsonReader.java
and
directly use it? Maven Repo's don't yet have 2.7.4 (they will in about 2
more hours).

On Mon, Dec 22, 2014 at 10:06 AM, John DeRegnaucourt jdereg@gmail.com
wrote:

What is inside ADouble? Can you send me that?

On Mon, Dec 22, 2014 at 9:53 AM, seeingmachines-fovio <
notifications@github.com> wrote:

Hi,

when I raised this issue, it was indeed about inner classes, but the
issue still seems to remain for normal classes... Just make ADouble
and
UseADouble normal, public, non-inner classes

Cheers,
L.


Reply to this email directly or view it on GitHub
#25 (comment).


Reply to this email directly or view it on GitHub <
https://github.com/jdereg/json-io/issues/25#issuecomment-67847331>.


Reply to this email directly or view it on GitHub
#25 (comment).

@jdereg
Copy link
Owner

jdereg commented Dec 23, 2014

Ludek, I have created a 2.7.5 version of json-io which fixes the
ArrayIndexOutOfBoundsException. I added test with a multi-template class
that use two fields of one template type, and one of the other. The test
now passes. The ArrayIndexOutOfBounds exception will no longer occur,
however, I still have some work to do to perfect handling the associated
template variable to the associated value. What I have works pretty good,
and works in many cases you would not expect it to, because the type can be
figured out later. Let me know if you have any further issues related to
template support.

On Mon, Dec 22, 2014 at 10:21 AM, John DeRegnaucourt jdereg@gmail.com
wrote:

Ludek, thank you for the additional testing. I see the issue now, and
plan to create a 2.7.5 today. Adding additional tests.

On Mon, Dec 22, 2014 at 10:19 AM, Ludek Dolejsky <notifications@github.com

wrote:

Yes, I just cloned fresh from master and ran the test

L.

On 22 Dec 2014, at 16:11, John DeRegnaucourt notifications@github.com
wrote:

How did you test this, did you get the source code for JsonReader.java
and
directly use it? Maven Repo's don't yet have 2.7.4 (they will in about
2
more hours).

On Mon, Dec 22, 2014 at 10:06 AM, John DeRegnaucourt jdereg@gmail.com

wrote:

What is inside ADouble? Can you send me that?

On Mon, Dec 22, 2014 at 9:53 AM, seeingmachines-fovio <
notifications@github.com> wrote:

Hi,

when I raised this issue, it was indeed about inner classes, but the
issue still seems to remain for normal classes... Just make ADouble
and
UseADouble normal, public, non-inner classes

Cheers,
L.


Reply to this email directly or view it on GitHub
#25 (comment).


Reply to this email directly or view it on GitHub <
https://github.com/jdereg/json-io/issues/25#issuecomment-67847331>.


Reply to this email directly or view it on GitHub
#25 (comment).

@jdereg jdereg added the bug label Jun 22, 2015
@jdereg jdereg self-assigned this Jun 22, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants