artiya4u / google-http-java-client

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

HttpParser/ObjectParser should support Type and InputStream #112

Closed GoogleCodeExporter closed 9 years ago

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

http://javadoc.google-http-java-client.googlecode.com/hg/1.8.3-beta/com/google/a
pi/client/http/HttpParser.html

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

All

Please describe the feature requested.

Suppose you want to parse a response using List<A>, where A is some class using 
@Key for key/value pairs.  parse(List<A>.class) doesn't compile, so instead you 
can try this:

  List<?> result = (List<?>) HttpParser.parse(List.class, null);

But this would parse it as a List<Object> which means each element which end up 
being some GenericData instead of A.

So we need some way of specifying that we want to parse into the type 
List<String>.  TypeToken from Google Guava provides an elegant way to specify 
the Type using:

  new TypeToken<List<String>>(){}.getType()

The missing piece is a parse method that accepts Type.  We cannot change 
HttpParser because that would be a backwards-incompatible change.  So I propose 
adding a new interface like this:

public interface HttpParser2 extends HttpParser {
  Object parseType(HttpResponse response, Type dataType) throws IOException;
}

and add this to HttpResponse:

  public Object parseAsType(Type dataType) throws IOException {

Original issue reported on code.google.com by yan...@google.com on 11 May 2012 at 2:38

GoogleCodeExporter commented 9 years ago
early proof of concept: http://codereview.appspot.com/6209053/

Original comment by yan...@google.com on 11 May 2012 at 2:41

GoogleCodeExporter commented 9 years ago
Actually Ravi, can you take this please?  I think Matthias is proposing to add 
something else to HttpParser, so we should probably merge his work into 
HttpParser2 also.

Note: we need TypeToken which unfortunately is part of Guava 12, which is not 
Java 5 compatible.  So we're stuck in a position of either forking Guava to a 
Java 5 compatible build (big project), OR writing our own light-weight 
TypeToken implementation as part of google-http-client (easy).

Original comment by yan...@google.com on 11 May 2012 at 3:02

GoogleCodeExporter commented 9 years ago

Original comment by mlin...@google.com on 23 May 2012 at 3:18

GoogleCodeExporter commented 9 years ago

Original comment by mlin...@google.com on 5 Jun 2012 at 8:51

GoogleCodeExporter commented 9 years ago
Explanation for the InputStream/Reader benefit of this: HttpParser requires 
HttpResponse, whereas ObjectParser takes an InputStream or Reader.  That makes 
it more generally useful.  In particular, google-api-java-client is able to 
take advantage of this to implement parsing of batches of responses in a 
multipart response, and also for parsing push notifications.

Original comment by yan...@google.com on 17 Aug 2012 at 11:52