No built-in type adapter for Iterable
See original GitHub issueWhat 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:
Issue Analytics
- State:
- Created 9 years ago
- Comments:13
Top Results From Across the Web
Can't properly serialize POJO with Iterable fields using Gson
For the second case, Gson does not have declaration type information ... subclasses should be picked up by built-in type adapters if (...
Read more >Customizing Mobile Inbox on Android - Iterable Support Center
See IterableInboxAdapterExtension for more details. Create integer constants for every type of cell you're planning to have in your custom inbox. Return those ......
Read more >itertools — Functions creating iterators for efficient looping ...
This module implements a number of iterator building blocks inspired by constructs from APL, Haskell, and SML. Each has been recast in a...
Read more >JavaScript Iterables - W3docs
iterator. Let's try to create an iterable. For instance, there is an object, which is not an array but can be suitable for...
Read more >Counting Iterator - 1.34.0 - Boost C++ Libraries
The only iterator operation missing from builtin integer types is an operator*() ... One can use the counting iterator adaptor not only with...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@inder123 To answer your question: yes, absolutely.
From a user’s perspective this is consistent with the rest of Gson. When I ask Gson to deserialize some JSON to a
List<Integer>
, I don’t much care which specific implementation of theList<Integer>
interface Gson chooses to produce. It could be aLinkedList<Integer>
or anArrayList<Integer>
. But as long as it’s something that implementsList<Integer>
(and it’s reasonably performant) I’m happy, and my code works.In this case, Gson does exactly what you suggested: it picks an appropriate subclass of
List<Integer>
for me. It would be great if it did the same for anIterable<Integer>
as well.I haven’t dug into the Gson source code, so forgive me if this is technically impossible for some reason. But, from an outsider’s perspective, this seems totally feasible.
I know this is an older issue, but I’m still having to work around this issue in my current project, specifically when serializing an
Iterable<Integer>
. As @ear…@gmail.com pointed out in his fourth paragraph on 8 Nov 2012, the workaround requires me to iterate over myIterable<Integer>
one more time, which is less than ideal.Hi Folks. What is the intended behavior here? Do you want us to pick a random sub-class of Iterable (such as List) and deserialize the collection into it?