com.google.api.client.http.json
Class JsonHttpParser

java.lang.Object
  extended by com.google.api.client.http.json.JsonHttpParser
All Implemented Interfaces:
HttpParser

public class JsonHttpParser
extends Object
implements HttpParser

Parses HTTP JSON response content into an data class of key/value pairs.

Implementation is thread-safe.

Sample usage:

 
  static void setParser(HttpRequest request) {
    request.addParser(new JsonHttpParser(new JacksonFactory()));
  }
 
 

Since:
1.0
Author:
Yaniv Inbar

Nested Class Summary
static class JsonHttpParser.Builder
          Builder for JsonHttpParser.
 
Constructor Summary
  JsonHttpParser(JsonFactory jsonFactory)
          Constructor with required parameters.
protected JsonHttpParser(JsonFactory jsonFactory, String contentType)
           
 
Method Summary
static JsonHttpParser.Builder builder(JsonFactory jsonFactory)
          Returns an instance of a new builder.
 String getContentType()
          Returns the content type.
 JsonFactory getJsonFactory()
          Returns the JSON factory.
<T> T
parse(HttpResponse response, Class<T> dataClass)
          Parses the given HTTP response into a new instance of the the given data class of key/value pairs.
static JsonParser parserForResponse(JsonFactory jsonFactory, HttpResponse response)
          Returns a JSON parser to use for parsing the given HTTP response.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JsonHttpParser

public JsonHttpParser(JsonFactory jsonFactory)
Constructor with required parameters.

Use builder(com.google.api.client.json.JsonFactory) if you need to specify any of the optional parameters.

Parameters:
jsonFactory - JSON factory
Since:
1.5

JsonHttpParser

protected JsonHttpParser(JsonFactory jsonFactory,
                         String contentType)
Parameters:
jsonFactory - JSON factory
contentType - content type or null for none
Since:
1.5
Method Detail

getContentType

public final String getContentType()
Description copied from interface: HttpParser
Returns the content type.

Specified by:
getContentType in interface HttpParser

parse

public <T> T parse(HttpResponse response,
                   Class<T> dataClass)
        throws IOException
Description copied from interface: HttpParser
Parses the given HTTP response into a new instance of the the given data class of key/value pairs.

How the parsing is performed is not restricted by this interface, and is instead defined by the concrete implementation. Implementations should check HttpResponse.isSuccessStatusCode() to know whether they are parsing a success or error response.

Specified by:
parse in interface HttpParser
Throws:
IOException

getJsonFactory

public final JsonFactory getJsonFactory()
Returns the JSON factory.

Since:
1.5

parserForResponse

public static JsonParser parserForResponse(JsonFactory jsonFactory,
                                           HttpResponse response)
                                    throws IOException
Returns a JSON parser to use for parsing the given HTTP response.

The response content will be closed if any throwable is thrown. On success, the current token will be the first top token, which is normally JsonToken.START_ARRAY or JsonToken.START_OBJECT.

Parameters:
jsonFactory - JSON factory to use
response - HTTP response
Returns:
JSON parser
Throws:
IllegalArgumentException - if content type is not Json.CONTENT_TYPE
IOException
Since:
1.3

builder

public static JsonHttpParser.Builder builder(JsonFactory jsonFactory)
Returns an instance of a new builder.

Parameters:
jsonFactory - JSON factory
Since:
1.5


Copyright © 2011 Google. All Rights Reserved.