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.atwelm.aezwidget.responses.openhab.OpenHABFetchLayoutResponse.java

License:Apache License

public static List<OpenHABWidget> parseWidgets(JsonElement widget, Gson gson) {
    List<OpenHABWidget> widgets = new LinkedList<OpenHABWidget>();

    if (widget != null) {
        if (widget.isJsonObject()) {
            OpenHABWidget innerWidget = gson.fromJson(widget, OpenHABWidget.class);
            widgets.add(innerWidget);//from   w w  w  .  j av  a2s .c o  m
        } else if (widget.isJsonArray()) {
            JsonArray array = widget.getAsJsonArray();

            for (JsonElement element : array) {
                if (element.isJsonObject()) {
                    OpenHABWidget innerWidget = gson.fromJson(element, OpenHABWidget.class);
                    widgets.add(innerWidget);
                } else {
                    // Log error
                }
            }
        }
    }
    return widgets;
}

From source file:com.auth0.android.lock.internal.configuration.GsonDeserializer.java

License:Open Source License

void assertJsonObject(JsonElement jsonObject) throws JsonParseException {
    if (jsonObject.isJsonNull() || !jsonObject.isJsonObject()) {
        throw new JsonParseException("Received json is not a valid json object.");
    }//from  w  w w  . ja v  a  2 s. c o m
}

From source file:com.autoclavestudios.jbower.config.internal.JsonRegistryTranslator.java

License:Apache License

@Override
public Registry deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {

    Registry registry = new Registry();
    try {//from  ww  w . ja  v a  2 s.c  o m
        if (json.isJsonObject()) {
            JsonObject jsonObject = json.getAsJsonObject();
            if (jsonObject.has("search"))
                registry.search(ConvertToArray(jsonObject.get("search").getAsJsonArray()));
            if (jsonObject.has("publish"))
                registry.publish(ConvertToArray(jsonObject.get("publish").getAsJsonArray()));
            if (jsonObject.has("register"))
                registry.register(ConvertToArray(jsonObject.get("register").getAsJsonArray()));

        } else if (json.isJsonPrimitive()) {
            registry.all(json.getAsString());

        } else {
            throw new JsonParseException("Registry object is not a Json Object or Primitive");
        }
    } catch (MalformedURLException e) {
        logger.error("Malformed URL: {}", json.getAsString());
    }
    return registry;
}

From source file:com.azure.webapi.JsonEntityParser.java

License:Open Source License

/**
 * Parses the JSON object to a typed list
 * //from www  .j a  v  a2  s.  c  o  m
 * @param results
 *            JSON results
 * @param gson
 *            Gson object used for parsing
 * @param clazz
 *            Target entity class
 * @return List of entities
 */
public static <E> List<E> parseResults(JsonElement results, Gson gson, Class<E> clazz) {
    List<E> result = new ArrayList<E>();
    String idPropertyName = getIdPropertyName(clazz);

    // Parse results
    if (results.isJsonArray()) // Query result
    {
        JsonArray elements = results.getAsJsonArray();

        for (JsonElement element : elements) {
            changeIdPropertyName(element.getAsJsonObject(), idPropertyName);
            E typedElement = gson.fromJson(element, clazz);
            result.add(typedElement);
        }
    } else { // Lookup result
        if (results.isJsonObject()) {
            changeIdPropertyName(results.getAsJsonObject(), idPropertyName);
        }
        E typedElement = gson.fromJson(results, clazz);
        result.add(typedElement);
    }
    return result;
}

From source file:com.azure.webapi.MobileServiceJsonTable.java

License:Open Source License

/**
 * Retrieves a set of rows from using the specified URL
 * //w w  w . jav  a  2s  .  co m
 * @param query
 *            The URL used to retrieve the rows
 * @param callback
 *            Callback to invoke when the operation is completed
 */
private void executeGetRecords(final String url, final TableJsonQueryCallback callback) {
    ServiceFilterRequest request = new ServiceFilterRequestImpl(new HttpGet(url));

    MobileServiceConnection conn = mClient.createConnection();
    // Create AsyncTask to execute the request and parse the results
    new RequestAsyncTask(request, conn) {
        @Override
        protected void onPostExecute(ServiceFilterResponse response) {
            if (callback != null) {
                if (mTaskException == null && response != null) {
                    JsonElement results = null;

                    int count = 0;

                    try {
                        // Parse the results using the given Entity class
                        String content = response.getContent();
                        JsonElement json = new JsonParser().parse(content);

                        if (json.isJsonObject()) {
                            JsonObject jsonObject = json.getAsJsonObject();
                            // If the response has count property, store its
                            // value
                            if (jsonObject.has("results") && jsonObject.has("count")) { // inlinecount
                                // result
                                count = jsonObject.get("count").getAsInt();
                                results = jsonObject.get("results");
                            } else {
                                results = json;
                            }
                        } else {
                            results = json;
                        }
                    } catch (Exception e) {
                        callback.onCompleted(null, 0,
                                new MobileServiceException("Error while retrieving data from response.", e),
                                response);
                        return;
                    }

                    callback.onCompleted(results, count, null, response);

                } else {
                    callback.onCompleted(null, 0, mTaskException, response);
                }
            }
        }
    }.execute();
}

From source file:com.b12kab.tmdblibrary.AccountStateDeserializer.java

License:Apache License

@Override
public AccountState deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    AccountState accountState = new AccountState();
    String convertVariable = "";
    JsonObject obj = (JsonObject) json;/*  w  w  w  .  j  av  a 2 s  .c om*/
    JsonElement element;
    JsonElement ratingElement;
    JsonPrimitive rating;
    try {
        convertVariable = "id";
        element = obj.get("id");
        if (element != null) {
            if (element.isJsonPrimitive()) {
                if (element.getAsJsonPrimitive().isNumber()) {
                    accountState.setId(element.getAsInt());
                }
            }
        }

        convertVariable = "favorite";
        element = obj.get("favorite");
        if (element != null) {
            if (element.isJsonPrimitive()) {
                if (element.getAsJsonPrimitive().isBoolean()) {
                    accountState.setFavorite(element.getAsBoolean());
                }
            }
        }

        accountState.setRated(null);
        convertVariable = "rated";
        element = obj.get("rated");
        if (element != null) {
            if (element.isJsonObject()) {
                if (element.getAsJsonObject().has("value")) {
                    ratingElement = element.getAsJsonObject().get("value");
                    if (ratingElement.isJsonPrimitive()) {
                        //                        rating
                        if (ratingElement.getAsJsonPrimitive().isNumber()) {
                            accountState.setRated(ratingElement.getAsFloat());
                        }
                    }
                }
            }
        }

        convertVariable = "watchlist";
        element = obj.get("watchlist");
        if (element != null) {
            if (element.isJsonPrimitive()) {
                if (element.getAsJsonPrimitive().isBoolean()) {
                    accountState.setWatchlist(element.getAsBoolean());
                }
            }
        }
    } catch (NullPointerException npe) {
        Log.e(TAG, "Processing " + convertVariable + " response from Tmdb, NullPointerException occurred"
                + npe.toString());
        return null;
    } catch (NumberFormatException npe) {
        Log.e(TAG, "Processing " + convertVariable + " response from Tmdb, NumberFormatException occurred"
                + npe.toString());
        return null;
    } catch (IllegalStateException ise) {
        Log.e(TAG, "Processing " + convertVariable + " response from Tmdb, IllegalStateException occurred"
                + ise.toString());
    }

    return accountState;
}

From source file:com.baidu.rigel.biplatform.ac.util.deserialize.CubeDeserialize.java

License:Open Source License

@Override
public Cube deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (json.isJsonObject()) {
        MiniCube cube = context.deserialize(json, MiniCube.class);
        return cube;
    }//from  w ww  . ja v a2  s  . c o  m
    return null;
}

From source file:com.baidu.rigel.biplatform.ac.util.deserialize.DataSourceInfoDeserialize.java

License:Open Source License

@Override
public DataSourceInfo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (json.isJsonObject()) {
        DataSourceType dataSourceType = context.deserialize(json.getAsJsonObject().get("dataSourceType"),
                DataSourceType.class);
        if (dataSourceType.equals(DataSourceType.SQL)) {
            return context.deserialize(json, SqlDataSourceInfo.class);
        }//  w w  w. j a  v a 2  s. c o  m

    }
    return null;
}

From source file:com.baidu.rigel.biplatform.ac.util.deserialize.DimensionDeserialize.java

License:Open Source License

@Override
public Dimension deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    Dimension dimension = null;/*  ww  w .j av a2  s  . co m*/
    if (json.isJsonObject()) {
        JsonObject dimensionObject = json.getAsJsonObject();

        if (dimensionObject.get("type") != null) {
            DimensionType dimensionType = DimensionType.valueOf(dimensionObject.get("type").getAsString());
            if (dimensionType.equals(DimensionType.TIME_DIMENSION)) {
                dimension = context.deserialize(json, TimeDimension.class);
            } else {
                dimension = context.deserialize(json, StandardDimension.class);
            }
            if (dimension != null && MapUtils.isNotEmpty(dimension.getLevels())) {
                for (Level level : dimension.getLevels().values()) {
                    level.setDimension(dimension);
                }
            }
        }
    }
    return dimension;
}

From source file:com.baidu.rigel.biplatform.ac.util.deserialize.LevelDeserialize.java

License:Open Source License

@Override
public Level deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (json.isJsonObject()) {
        JsonObject levelJsonObj = json.getAsJsonObject();

        if (levelJsonObj.get("type") != null) {
            LevelType levelType = LevelType.valueOf(levelJsonObj.get("type").getAsString());
            if (levelType.equals(LevelType.CALL_BACK)) {
                return context.deserialize(levelJsonObj, CallbackLevel.class);
            } else if (levelType.name().equals(LevelType.USER_CUSTOM)) {
                return context.deserialize(levelJsonObj, UserCustomLevel.class);
            } else {
                return context.deserialize(levelJsonObj, MiniCubeLevel.class);
            }/*w  w  w. j  a  v a 2  s.co  m*/

        }
    }

    return null;
}