Skip to content

Fail to deserialize List of Objects with Enum with default value #238

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

Open
jesjobom opened this issue May 10, 2019 · 1 comment
Open

Fail to deserialize List of Objects with Enum with default value #238

jesjobom opened this issue May 10, 2019 · 1 comment

Comments

@jesjobom
Copy link

JsonIter 0.9.23
JDK 1.8.0_181

When deserializing a list of objects (size > 1) with an enum attribute with default value it fails with "Can not set br.com.test.DummyType field br.com.test.Dummy.type to br.com.test.DummyType".

package br.com.test;

import com.jsoniter.JsonIterator;
import org.junit.Assert;
import org.junit.Test;

public class JsoniterTest {

    @Test
    public void testOk() {
        String json = "[{ \"type\":\"TYPE_A\" }]";
        Dummy[] perms = JsonIterator.deserialize(json, Dummy[].class); //OK
        Assert.assertEquals(1, perms.length);
    }

    @Test
    public void testFail() {
        String json = "[{ \"type\":\"TYPE_A\" }, { \"type\":\"TYPE_A\" }]";
        Dummy[] perms = JsonIterator.deserialize(json, Dummy[].class); //FAIL
        Assert.assertEquals(2, perms.length);
    }

    @Test
    public void testOk2() {
        String json = "[{ \"type\":\"TYPE_A\" }, { \"type\":\"TYPE_A\" }]";
        Dummy2[] perms = JsonIterator.deserialize(json, Dummy2[].class); //OK
        Assert.assertEquals(2, perms.length);
    }
}

class Dummy {
    private DummyType type = DummyType.TYPE_A; //with default value
}

class Dummy2 {
    private DummyType type; //without default value
}

enum DummyType {
    TYPE_A
}

At second test...

java.lang.IllegalArgumentException: Can not set br.com.test.DummyType field br.com.test.Dummy.type to br.com.test.DummyType

	at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
	at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
	at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
	at sun.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:36)
	at java.lang.reflect.Field.get(Field.java:393)
	at com.jsoniter.ReflectionObjectDecoder.decodeBinding(ReflectionObjectDecoder.java:382)
	at com.jsoniter.ReflectionObjectDecoder.access$700(ReflectionObjectDecoder.java:10)
	at com.jsoniter.ReflectionObjectDecoder$OnlyField.decode_(ReflectionObjectDecoder.java:153)
	at com.jsoniter.ReflectionObjectDecoder$OnlyField.decode(ReflectionObjectDecoder.java:119)
	at com.jsoniter.ReflectionArrayDecoder.decode(ReflectionArrayDecoder.java:35)
	at com.jsoniter.JsonIterator.read(JsonIterator.java:385)
	at com.jsoniter.JsonIterator.read(JsonIterator.java:375)
	at com.jsoniter.JsonIterator.deserialize(JsonIterator.java:441)
	at com.jsoniter.JsonIterator.deserialize(JsonIterator.java:411)
	at br.com.test.JsoniterTest.testFail(JsoniterTest.java:19)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
@Danielyt
Copy link

I think I'm having the same problem. The issue occurs if there are no setters, if there are setters, it seems to work, but the following code throws an exception:

public class TestClass {

    public static enum Colour {
        GREEN, YELLOW, RED
     }

    public static class Fruit {
        public String name;
        public Colour colour = Colour.GREEN;

        @Override
        public String toString() {
            return "Fruit [name=" + name + ", colour=" + colour + "]";
        }

    }

    public static class FruitBasket {
        List<Fruit> fruits;

        @Override
        public String toString() {
            return "FruitBasket [fruits=" + fruits + "]";
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        String jsonString = "{\"fruits\":[{\"name\":\"apple\",\"colour\":\"RED\"},{\"name\":\"banana\",\"colour\":\"YELLOW\"}]}";
        FruitBasket basket = JsonIterator.deserialize(jsonString, FruitBasket.class);
        System.out.println(basket);
    }

}

This is the exception:

Exception in thread "main" com.jsoniter.spi.JsonException: java.lang.IllegalArgumentException: Can not set java.lang.String field com.base.json.TestClass$Fruit.name to com.base.json.TestClass$Colour
    at com.jsoniter.ReflectionCollectionDecoder.decode(ReflectionCollectionDecoder.java:32)
    at com.jsoniter.ReflectionObjectDecoder.decodeBinding(ReflectionObjectDecoder.java:376)
    at com.jsoniter.ReflectionObjectDecoder.decodeBinding(ReflectionObjectDecoder.java:384)
    at com.jsoniter.ReflectionObjectDecoder.access$700(ReflectionObjectDecoder.java:10)
    at com.jsoniter.ReflectionObjectDecoder$OnlyField.decode_(ReflectionObjectDecoder.java:153)
    at com.jsoniter.ReflectionObjectDecoder$OnlyField.decode(ReflectionObjectDecoder.java:119)
    at com.jsoniter.JsonIterator.read(JsonIterator.java:385)
    at com.jsoniter.JsonIterator.read(JsonIterator.java:375)
    at com.jsoniter.JsonIterator.deserialize(JsonIterator.java:441)
    at com.jsoniter.JsonIterator.deserialize(JsonIterator.java:411)
    at com.base.json.TestClass.main(TestClass.java:45)
Caused by: java.lang.IllegalArgumentException: Can not set java.lang.String field com.base.json.TestClass$Fruit.name to com.base.json.TestClass$Colour
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
    at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
    at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:75)
    at java.lang.reflect.Field.set(Field.java:764)
    at com.jsoniter.ReflectionObjectDecoder.setToBinding(ReflectionObjectDecoder.java:343)
    at com.jsoniter.ReflectionObjectDecoder.access$400(ReflectionObjectDecoder.java:10)
    at com.jsoniter.ReflectionObjectDecoder$OnlyField.decode_(ReflectionObjectDecoder.java:153)
    at com.jsoniter.ReflectionObjectDecoder$OnlyField.decode(ReflectionObjectDecoder.java:119)
    at com.jsoniter.ReflectionCollectionDecoder.decode_(ReflectionCollectionDecoder.java:47)
    at com.jsoniter.ReflectionCollectionDecoder.decode(ReflectionCollectionDecoder.java:28)
    ... 10 more

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants