Example usage for com.google.gson JsonElement isJsonObject

List of usage examples for com.google.gson JsonElement isJsonObject

Introduction

In this page you can find the example usage for com.google.gson JsonElement isJsonObject.

Prototype

public boolean isJsonObject() 

Source Link

Document

provides check for verifying if this element is a Json object or not.

Usage

From source file:com.google.wave.api.RobotSerializer.java

License:Apache License

/**
 * Validates that the incoming JSON is a JSON object that represents a
 * JSON-RPC request./*from   w  ww . j  a v  a  2  s.  c  o  m*/
 *
 * @param jsonElement the incoming JSON.
 * @throws InvalidRequestException if the incoming JSON does not have the
 *     required properties.
 */
private static void validate(JsonElement jsonElement) throws InvalidRequestException {
    if (!jsonElement.isJsonObject()) {
        throw new InvalidRequestException("The incoming JSON is not a JSON object: " + jsonElement);
    }

    JsonObject jsonObject = jsonElement.getAsJsonObject();

    StringBuilder missingProperties = new StringBuilder();
    for (RequestProperty requestProperty : RequestProperty.values()) {
        if (!jsonObject.has(requestProperty.key())) {
            missingProperties.append(requestProperty.key());
        }
    }

    if (missingProperties.length() > 0) {
        throw new InvalidRequestException(
                "Missing required properties " + missingProperties + "operation: " + jsonObject);
    }
}

From source file:com.googleapis.ajax.services.impl.BaseGoogleSearchApiQuery.java

License:Apache License

@Override
public PagedList<T> list() {
    InputStream jsonContent = null;
    try {//from  w w w .j  a v a 2  s .co m
        jsonContent = callApiGet(apiUrlBuilder.buildUrl());
        JsonElement response = parser.parse(new InputStreamReader(jsonContent, UTF_8_CHAR_SET));
        if (response.isJsonObject()) {
            PagedList<T> responseList = unmarshallList(response.getAsJsonObject());
            notifyObservers(responseList);
            return responseList;
        }
        throw new GoogleSearchException("Unknown content found in response:" + response.toString());
    } catch (Exception e) {
        throw new GoogleSearchException(e);
    } finally {
        closeStream(jsonContent);
    }
}

From source file:com.googleapis.ajax.services.impl.BaseGoogleSearchApiQuery.java

License:Apache License

@Override
public T singleResult() {
    InputStream jsonContent = null;
    try {//from  w w  w .j a va2 s . com
        jsonContent = callApiGet(apiUrlBuilder.buildUrl());
        JsonElement response = parser.parse(new InputStreamReader(jsonContent, UTF_8_CHAR_SET));
        if (response.isJsonObject()) {
            JsonObject json = response.getAsJsonObject();
            int status = json.get("responseStatus").getAsInt();
            if (status != 200) {
                throw new GoogleSearchException(json.get("responseDetails").getAsString());
            }
            JsonElement data = json.get("responseData");
            if (data != null) {
                return unmarshall(data);
            }
        }
        throw new GoogleSearchException("Unknown content found in response:" + response.toString());
    } catch (Exception e) {
        throw new GoogleSearchException(e);
    } finally {
        closeStream(jsonContent);
    }
}

From source file:com.googleapis.ajax.services.impl.FindFeedQueryImpl.java

License:Apache License

@Override
public FindFeedResult singleResult() {
    InputStream jsonContent = null;
    try {/*from w ww.j  av a  2 s  .c o  m*/
        jsonContent = callApiGet(apiUrlBuilder.buildUrl());
        JsonElement response = parser.parse(new InputStreamReader(jsonContent, UTF_8_CHAR_SET));
        if (response.isJsonObject()) {
            JsonObject json = response.getAsJsonObject();
            int status = json.get("responseStatus").getAsInt();
            if (status != 200) {
                throw new GoogleSearchException(json.get("responseDetails").getAsString());
            }
            JsonElement data = json.get("responseData");
            if (data != null) {
                return unmarshall(data);
            }
        }
        throw new GoogleSearchException("Unknown content found in response:" + response.toString());
    } catch (Exception e) {
        throw new GoogleSearchException(e);
    } finally {
        closeStream(jsonContent);
    }
}

From source file:com.googleapis.ajax.services.impl.LoadFeedQueryImpl.java

License:Apache License

@Override
public LoadFeedResult singleResult() {
    InputStream jsonContent = null;
    try {//from  w w w . j  a  v a2 s .c  om
        jsonContent = callApiGet(apiUrlBuilder.buildUrl());
        JsonElement response = parser.parse(new InputStreamReader(jsonContent, UTF_8_CHAR_SET));
        if (response.isJsonObject()) {
            JsonObject json = response.getAsJsonObject();
            int status = json.get("responseStatus").getAsInt();
            if (status != 200) {
                throw new GoogleSearchException(json.get("responseDetails").getAsString());
            }
            JsonElement data = json.get("responseData");
            if (data != null) {
                return unmarshall(data.getAsJsonObject().get("feed"));
            }
        }
        throw new GoogleSearchException("Unknown content found in response:" + response.toString());
    } catch (Exception e) {
        throw new GoogleSearchException(e);
    } finally {
        closeStream(jsonContent);
    }
}

From source file:com.googleapis.ajax.services.impl.LookupFeedQueryImpl.java

License:Apache License

@Override
public LookupFeedResult singleResult() {
    InputStream jsonContent = null;
    try {//from  w ww . j a v  a 2s .c  o m
        jsonContent = callApiGet(apiUrlBuilder.buildUrl());
        JsonElement response = parser.parse(new InputStreamReader(jsonContent, UTF_8_CHAR_SET));
        if (response.isJsonObject()) {
            JsonObject json = response.getAsJsonObject();
            int status = json.get("responseStatus").getAsInt();
            if (status != 200) {
                throw new GoogleSearchException(json.get("responseDetails").getAsString());
            }
            JsonElement data = json.get("responseData");
            if (data != null) {
                return unmarshall(data);
            }
        }
        throw new GoogleSearchException("Unknown content found in response:" + response.toString());
    } catch (Exception e) {
        throw new GoogleSearchException(e);
    } finally {
        closeStream(jsonContent);
    }
}

From source file:com.googleapis.ajax.services.impl.TranslateLanguageQueryImpl.java

License:Apache License

@Override
public PagedList<TranslateLanguageResult> list() {
    InputStream jsonContent = null;
    try {/* w  w w . j  a v  a 2  s  .  c  o m*/
        jsonContent = callApiPost(apiUrlBuilder.buildUrl(), parameters);
        JsonElement response = parser.parse(new InputStreamReader(jsonContent, UTF_8_CHAR_SET));
        if (response.isJsonObject()) {
            PagedList<TranslateLanguageResult> responseList = unmarshallList(response.getAsJsonObject());
            notifyObservers(responseList);
            return responseList;
        }
        throw new GoogleSearchException("Unknown content found in response:" + response.toString());
    } catch (Exception e) {
        throw new GoogleSearchException(e);
    } finally {
        closeStream(jsonContent);
    }
}

From source file:com.googleapis.ajax.services.impl.TranslateLanguageQueryImpl.java

License:Apache License

@Override
public TranslateLanguageResult singleResult() {
    InputStream jsonContent = null;
    try {// w  w w . ja v a  2s. c  o  m
        jsonContent = callApiPost(apiUrlBuilder.buildUrl(), parameters);
        JsonElement response = parser.parse(new InputStreamReader(jsonContent, UTF_8_CHAR_SET));
        if (response.isJsonObject()) {
            JsonObject json = response.getAsJsonObject();
            int status = json.get("responseStatus").getAsInt();
            if (status != 200) {
                throw new GoogleSearchException(json.get("responseDetails").getAsString());
            }
            JsonElement data = json.get("responseData");
            if (data != null) {
                return unmarshall(data);
            }
        }
        throw new GoogleSearchException("Unknown content found in response:" + response.toString());
    } catch (Exception e) {
        throw new GoogleSearchException(e);
    } finally {
        closeStream(jsonContent);
    }
}

From source file:com.googleapis.ajax.services.impl.TranslateLanguageQueryImpl.java

License:Apache License

/**
 * Unmarshall list.//from   w  w w .ja  v a 2s. c om
 * 
 * @param response the response
 * 
 * @return the paged list< t>
 */
protected PagedList<TranslateLanguageResult> unmarshallList(JsonObject response) {
    int status = response.get("responseStatus").getAsInt();
    if (status != 200) {
        throw new GoogleSearchException(String.valueOf(response.get("responseDetails").getAsString()));
    }
    JsonArray dataArray = response.get("responseData").getAsJsonArray();
    PagedArrayList<TranslateLanguageResult> list = new PagedArrayList<TranslateLanguageResult>();
    if (dataArray != null) {
        for (JsonElement element : dataArray) {
            if (element.isJsonObject()) {
                JsonObject json = element.getAsJsonObject();
                status = json.get("responseStatus").getAsInt();
                if (status != 200) {
                    throw new GoogleSearchException(json.get("responseDetails").getAsString());
                }
                JsonElement data = json.get("responseData");
                if (data != null) {
                    list.add(unmarshall(data));
                }
            }
        }
    }
    return list;
}

From source file:com.googleapis.ajax.services.impl.TransliterateLanguageQueryImpl.java

License:Apache License

@Override
public PagedList<TransliterateLanguageResult> list() {
    InputStream jsonContent = null;
    try {/*from w  w  w . java2s . c om*/
        jsonContent = callApiGet(apiUrlBuilder.buildUrl());
        JsonElement response = parser.parse(new InputStreamReader(jsonContent, UTF_8_CHAR_SET));
        if (response.isJsonObject()) {
            PagedList<TransliterateLanguageResult> responseList = unmarshallList(response.getAsJsonObject());
            notifyObservers(responseList);
            return responseList;
        }
        throw new GoogleSearchException("Unknown content found in response:" + response.toString());
    } catch (Exception e) {
        throw new GoogleSearchException(e);
    } finally {
        closeStream(jsonContent);
    }
}