guochaiqi / google-gson

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

java.lang.IllegalArgumentException on toJson() for: new Object[] { new Object() } #249

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
Run the attached source code.

What is the expected output? What do you see instead?
Expected legal json.
Throws exception instead.

Also see example number two where expected output is [] yet it output's [null]

What version of the product are you using? On what operating system?
1.5

Please provide any additional information below.
Ubuntu 64bit, Sun Java 1.6 (Don't speak the O word!).

Original issue reported on code.google.com by hq4ever on 13 Oct 2010 at 7:35

Attachments:

GoogleCodeExporter commented 9 years ago
Added tests in r637

                // Works, but prints "null" (not expected, expected: "[]")
                System.out.println(new Gson().toJson(new Object[] { null }));

Gson indeed prints the correct answer of [null]. new Object[]{null} is not a 
null array. It is an array containing a single object of value null. Since 
arrays are ordered, nulls must always be printed.

                // java.lang.IllegalArgumentException
                System.out.println(new Gson().toJson(new Object[] { new Object() }));

Gson has no way to convert the Object into anything meaningful and hence you 
get the exception. 

Original comment by inder123 on 3 Nov 2010 at 12:18