paul-stinchcombe / google-gson

Automatically exported from code.google.com/p/google-gson
0 stars 0 forks source link

Gson not populating fields on Object created with InstanceCreator #634

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
Run the following test:
{code}

    @Test
    public void testParseFloat() {
        Parsed value = new GsonBuilder().registerTypeAdapter(Container.class, 
                                                             new FloatContainerInstanceCreator())
                                        .create().fromJson("{\"container\":{\"value\":0.0024}}", Parsed.class);
        Assert.assertEquals(0.0024f, value.getContainer().getValue(), 0.00001f);
    }

    private static class FloatContainerInstanceCreator implements InstanceCreator<Container> {

        @Override
        public Container createInstance(Type arg0) {
            return new FloatContainer(0f);
        }

    }

    private static class Parsed {
        private Container container;

        public Container getContainer() {
            return container;
        }
    }

    private static abstract class Container {
        public abstract float getValue();
    }

    private static class FloatContainer extends Container {
        private final float value;

        public FloatContainer(float value) {
            this.value = value;
        }

        @Override
        public float getValue() {
            return value;
        }
    }
{code}

What is the expected output? What do you see instead?
I expect the test to pass.  The test fails.

What version of the product are you using? On what operating system?
Under Gson 2.3.1 this test fails.  However in Gson 1.7.1 it will pass.  I'm on 
OSX 10.9.5 and using java 1.8.0_25

Please provide any additional information below.
The bug happens whether Container is an abstract class or an interface. It is 
definitely something around the class hierarchy though.  The code works if 
Parsed contains a FloatContainer.

Original issue reported on code.google.com by kcar...@dstillery.com on 31 Mar 2015 at 7:26