parispurchase / json-io

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

assumes private fields are unique in class hierarchy #6

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
affects at least 2.0.1

Create a class hierarchy containing more than one private field of the same 
name.

Defect:
Only one of the field is saved.

Expected:
Both fields should be saved.

Original issue reported on code.google.com by taylor_t...@hotmail.com on 6 Feb 2013 at 3:03

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
The test case below works just fine.  If you can show me a case where there is 
an issue, please send it.

static class Outer
    {
        String name;
        class Inner
        {
            String name;
        };
        Inner foo;
    }

    public void testNestedWithSameMemberName() throws Exception
    {
        Outer outer = new Outer();
        outer.name = "Joe Outer";
        // TestJsonReaderWriter.A.B b = a.new B();
        TestJsonReaderWriter.Outer.Inner inner = outer.new Inner();
        inner.name = "Jane Inner";
        outer.foo = inner;

        String json = getJsonString(outer);
        println("json = " + json);

        Outer x = (Outer) readJsonObject(json);
        assertEquals(x.name, "Joe Outer");
        assertEquals(x.foo.name, "Jane Inner");
    }

Original comment by jdereg@gmail.com on 29 Sep 2013 at 3:46

GoogleCodeExporter commented 9 years ago
public class Parent {
    private String name;

    public String getParentName() {
        return name;
    }

    public void setParentName(String name) {
        this.name = name;
    }
}

public class Child extends Parent {
    private String name;

    public String getChildName() {
        return name;
    }

    public void setChildName(String name) {
        this.name = name;
    }
}

public class ChildTest {
    @Test
    public void test() throws IOException {
        Child child = new Child();
        child.setChildName("child");
        child.setParentName("parent");

        String json = JsonWriter.objectToJson(child);
        Child roundTrip = (Child) JsonReader.jsonToJava(json);

        System.out.println(json); //note missing Child name

        assertEquals(child.getParentName(), roundTrip.getParentName());
        assertEquals(child.getChildName(), roundTrip.getChildName());
    }
}

Output:
{"@type":"Child","name":"parent"}
java.lang.AssertionError: expected:<child> but was:<null>

It is assumed that no 2 private fields will have the same name which is not a 
safe assumption.

Original comment by taylor_t...@hotmail.com on 30 Sep 2013 at 8:13

GoogleCodeExporter commented 9 years ago
Looking into this now.

Original comment by jdereg@gmail.com on 1 Oct 2013 at 5:37

GoogleCodeExporter commented 9 years ago
This issue has been fixed and is available in the latest source code on GitHub. 
 The next release (either 2.2.32 or 2.3.0) will include this fix.

Original comment by jdereg@gmail.com on 1 Oct 2013 at 8:04

GoogleCodeExporter commented 9 years ago
I should add that the way this is addressed, is that the first field 
encountered retains it's short name, in your example, the word 'name.'  If 
another field in the class hierarchy has the name, it's is written into the 
JSON stream as 'fully.qualified.class.fieldName'.  All copies then get written 
out, and the reader, reads these in just fine.  This is support for both 
jsonToJava, and jsonToMaps, both read in, and written out.

Original comment by jdereg@gmail.com on 1 Oct 2013 at 10:36

GoogleCodeExporter commented 9 years ago
This issue was officially corrected in the 2.2.32 json-io release.

Original comment by jdereg@gmail.com on 13 Nov 2013 at 1:20