abdulrehmanz / google-gson

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

Gson doesn't serialize collection with object of derived collection #465

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
Please look at the following code:
<code language="java">
public class SerializeTest {

    public static class AssetMetadata extends ArrayList<String> {}
    public static class AssetRequest {
        public ArrayList<AssetMetadata> styles;
        public ArrayList<AssetMetadata> scripts;
    }

    private static final AssetMetadata SCRIPT_METADATA = new AssetMetadata() {{
        add("path/to/script.js");
    }};
    private static final AssetMetadata STYLE_METADATA = new AssetMetadata() {{
        add("path/to/style.js");
    }};

    @Test
    public void testSerialization() {

        AssetRequest metadata = new AssetRequest();
        metadata.styles = new ArrayList<AssetMetadata>() {{ add(STYLE_METADATA); }};
        metadata.scripts = new ArrayList<AssetMetadata>() {{ add(SCRIPT_METADATA); }};

        String json = new Gson().toJson(metadata, AssetRequest.class);

        System.out.println(json);
    }
}
</code>

What is the expected output? What do you see instead?
Expected output: 
{"styles":[["path/to/style.js"]],"scripts":[["path/to/script.js"]]}
Actual output: {"styles":[null],"scripts":[null]}

What version of the product are you using? On what operating system?
I use version 2.2.2. With version 1.7.1 serialization and deserialization works 
as expected.

Please provide any additional information below.
If I use the ArrayList<String> collection instead of the AssetMetadata class 
then it works as expected.

Original issue reported on code.google.com by kaps.chr...@gmail.com on 16 Aug 2012 at 2:11

GoogleCodeExporter commented 9 years ago
Don't use double brace initialization. This should work if you avoid double 
brace initialization.

Original comment by limpbizkit on 2 Sep 2012 at 9:27