sinamdar2015 / google-gson

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

No built-in type adapter for Iterable #428

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
See the attached file.

What is the expected output?
[{"a":1},{"a":2}]
[{"a":1},{"a":2}]
[1, 2]
[1, 2]

What do you see instead?
[{"a":1},{"a":2}]
{}
[1, 2]
Exception in thread "main" java.lang.RuntimeException: Unable to invoke no-args 
constructor for java.lang.Iterable<com.ash2k.test1.App$A>. Register an 
InstanceCreator with Gson for this type may fix this problem.
    at com.google.gson.internal.ConstructorConstructor$8.construct(ConstructorConstructor.java:166)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:163)
    at com.google.gson.Gson.fromJson(Gson.java:755)
    at com.google.gson.Gson.fromJson(Gson.java:721)
    at com.google.gson.Gson.fromJson(Gson.java:670)
    at com.ash2k.test1.App.main(App.java:31)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.google.gson.internal.UnsafeAllocator$1.newInstance(UnsafeAllocator.java:48)
    at com.google.gson.internal.ConstructorConstructor$8.construct(ConstructorConstructor.java:163)
    ... 5 more
Caused by: java.lang.InstantiationException: java.lang.Iterable
    at sun.misc.Unsafe.allocateInstance(Native Method)
    ... 11 more

What version of the product are you using? On what operating system?
2.1 on Ubuntu 11.10 x64

Original issue reported on code.google.com by Ash2kk@gmail.com on 1 Apr 2012 at 7:38

Attachments:

GoogleCodeExporter commented 9 years ago
Just use a Collection or a List?

Original comment by limpbizkit on 11 Apr 2012 at 8:36

GoogleCodeExporter commented 9 years ago
The problem was that I have big Iterable objects that need to be
serialized. It's not efficient to convert those into Lists or Collections
just for the serialization purpose.

Original comment by phamduc...@gmail.com on 11 Apr 2012 at 8:52

GoogleCodeExporter commented 9 years ago
I have an interface (DAO) with method that returns Iterable<SomeType> and i 
want to be able to pass the returned object and it's type to Gson. I cannot be 
sure that the object is always a Collection.

Original comment by Ash2kk@gmail.com on 12 Apr 2012 at 4:31

GoogleCodeExporter commented 9 years ago
You should be able to write a TypeAdapterFactory for Iterable. You can use the 
type adapter for collection as a guide:
http://code.google.com/p/google-gson/source/browse/trunk/gson/src/main/java/com/
google/gson/internal/bind/CollectionTypeAdapterFactory.java

The code is even simpler if the iterable always has the same type parameter.

Original comment by limpbizkit on 16 Apr 2012 at 10:36

GoogleCodeExporter commented 9 years ago
Why can't or shouldn't it be supported in Gson? Isn't it a valid usecase? 
Iterable is Java's built-in type and i expect it to be supported as well as 
Collection or Map.

Original comment by Ash2kk@gmail.com on 17 Apr 2012 at 4:07

GoogleCodeExporter commented 9 years ago
Iterable must be supported. It is a very very common & valid usecase. 

Original comment by rajkumar...@gmail.com on 25 Oct 2012 at 7:49

GoogleCodeExporter commented 9 years ago
Any suggestions to implementing a TypeAdapterFactory for Iterable? The example 
for Collection won't work since TypeAdapterRuntimeTypeWrapper is 
package-private.
Also, the Iterable interface does not have an add method so the read method 
won't work.

Original comment by jason.di...@gmail.com on 31 Oct 2012 at 5:06

GoogleCodeExporter commented 9 years ago

Hello, 

This comment isn't intended to be aggressive or demanding but I'm really 
confused by the status of this ticket.  Would somebody working on Gson please 
provide an explanation as to why this is marked WontFix?

Simply informing users that they can implement this themselves isn't really an 
answer - we could all go off and implement our own JSON libraries.  Instead I 
will have an additional dependency everywhere I use Gson, and will have to 
maintain and test this extension.  As will countless other users.

Give the increasing functional paradigm being employed in Java and Guava's 
judicious use of Iterable (in preference to ? extends Collection) this would 
seem like a very obvious use-case.

Also from a performance perspective it just seems crazy to expect me to convert 
my Iterable to a Collection just to pass it to Gson to ...  iterate over it 
again.  And rewriting my code to suit Gson is obviously absurd and out of the 
question.

If anything throw out the Collection TypeAdapterFactory etc and replace with a 
single Ierable version.  I strongly suspect this ticket will receive votes from 
every Gson user when Java 8 is released.

Please comment with the rationale behind closing this.

I would also like to thank you for providing a nice, tidy library - so thanks =)

Original comment by ear...@gmail.com on 8 Nov 2012 at 5:48

GoogleCodeExporter commented 9 years ago
Please reopen this issue.

Original comment by eirir...@gmail.com on 9 Oct 2013 at 9:53

GoogleCodeExporter commented 9 years ago
Please resolve this issue.Having the same problem. Any alternate for this?

Original comment by vivek.sr...@gmail.com on 16 Oct 2013 at 5:49

GoogleCodeExporter commented 9 years ago
From the front page:

Gson Goals:
* Extensive support of Java Generics
* Support arbitrarily complex objects (with deep inheritance hierarchies and 
extensive use of generic types)

Yet for some reason refuse to support such a fundamental, basic type such as 
Iterable.

Original comment by collinsa...@gmail.com on 23 Jan 2015 at 5:25