achrefB3 / google-api-java-client

Automatically exported from code.google.com/p/google-api-java-client
0 stars 0 forks source link

Ability to parse a JSON HttpResult containing an Array of objects #218

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
External references, such as a standards document, or specification?

Discussion Thread 
http://groups.google.com/group/google-api-java-client/browse_thread/thread/73945
dc17855052e

Example API
http://dev.twitter.com/doc/get/users/search

Java environments (e.g. Java 6, Android 2.3, App Engine 1.4.2, or All)?

All

Please describe the feature requested.

Currently the api client cannot parse an HttpResponse formatted as a JSON array 
of objects.

ie: [{"prop1":"val1","prop2":"val2"},{"prop1":"val1","prop2":"val2"}] 

Attempting to parse using parseAs(class) generates an IllegalArgumentException:

Exception in thread "main" java.lang.IllegalArgumentException: START_ARRAY at 
com.google.common.base.Preconditions.checkArgument(Preconditions.java: 
88) 

Note that parseAsString() does work properly but the call 
Feed resp = response.parseAs(Feed.class); 
Should properly parse and return a feed object.

Original issue reported on code.google.com by ddamo...@gmail.com on 21 May 2011 at 5:35

GoogleCodeExporter commented 9 years ago
I'm not sure that  Feed resp = response.parseAs(Feed.class)  should be expected 
to work because Feed would be an object, and the response is really an array of 
objects.  I wouldn't mind a syntax like:
         List<Item> response = response.parseAs(Item.class);
We should be able to make that work, and would be the obvious thing if you had 
used parseAs() before. OTOH, I'm not looking at the code right now.  The use of 
templating may require a different name, so the signature might be   template 
List<T> parseAsArray( class T);

Original comment by ai...@google.com on 23 May 2011 at 1:34

GoogleCodeExporter commented 9 years ago
An alternative is
  List<Item> items = new ArrayList<Item>();
  Status status =  response.parseInto(items);

This eliminates the need for a template method, so it may result in less code 
size.

Original comment by ai...@google.com on 23 May 2011 at 3:32

GoogleCodeExporter commented 9 years ago
Thanks for reporting the issue.  Porting to the new google-http-java-client:
http://code.google.com/p/google-http-java-client/issues/detail?id=9

Original comment by yan...@google.com on 23 May 2011 at 11:21