Example usage for com.google.gson JsonElement isJsonPrimitive

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

Introduction

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

Prototype

public boolean isJsonPrimitive() 

Source Link

Document

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

Usage

From source file:com.adobe.acs.commons.json.JsonObjectUtil.java

License:Apache License

public static boolean isSingularElement(JsonElement elem) {
    return elem.isJsonPrimitive() || (elem.isJsonArray() && elem.getAsJsonArray().size() <= 1);
}

From source file:com.apcb.utils.utils.HtmlTemplateReader.java

private SectionMatch readJson(JsonElement jsonElement, String from, SectionMatch sectionMatch) {
    //log.info(jsonElement.toString());
    //SectionMatch sectionMatchChild = new SectionMatch(from);
    if (jsonElement.isJsonArray()) {
        //log.info("JsonArray");
        JsonArray jsonArray = jsonElement.getAsJsonArray();
        for (int i = 0; i < jsonArray.size(); i++) {
            SectionMatch sectionMatchArrayLevel = new SectionMatch(sectionMatch.getName());
            sectionMatchArrayLevel.setIndex(i);
            sectionMatch.putChildens(readJson(jsonArray.get(i), from + "[" + i + "]", sectionMatchArrayLevel));
        }// www .  j  a  v a 2  s .  com
    } else if (jsonElement.isJsonObject()) {
        //log.info("JsonObject");
        JsonObject jsonObject = jsonElement.getAsJsonObject(); //since you know it's a JsonObject
        Set<Map.Entry<String, JsonElement>> entries = jsonObject.entrySet();//will return members of your object
        for (Map.Entry<String, JsonElement> entry : entries) {
            //log.info(entry.getKey());
            sectionMatch.putChildens(readJson(jsonObject.get(entry.getKey()), from + "." + entry.getKey(),
                    new SectionMatch(entry.getKey())));
        }
    } else if (jsonElement.isJsonNull()) {
        log.info("JsonNull");
    } else if (jsonElement.isJsonPrimitive()) {
        //log.info("JsonPrimitive");
        String jsonVal = jsonElement.getAsString();
        //from+= "."+entry.getKey(); 
        sectionMatch.setValue(jsonVal);
        log.info(from + "=" + jsonVal);
    }

    return sectionMatch;
}

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 {//  w  w w  .  ja  v  a 2  s  . com
        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.MobileServiceTableBase.java

License:Open Source License

/**
 * Validates if the id property is numeric.
 * @param element/*  w  w  w.  j a v  a  2 s.c  o m*/
 * @return
 */
protected boolean isValidTypeId(JsonElement element) {
    return element.isJsonPrimitive() && element.getAsJsonPrimitive().isNumber();
}

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;/*from  w  w w.ja v a 2s .  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.balajeetm.mystique.util.gson.GsonFactory.java

License:Open Source License

/**
 * Gets the gson builder.// w  ww  .j av  a 2s  . com
 *
 * @return the gson builder
 */
public GsonBuilder getGsonBuilder() {
    if (null == gsonBuilder) {
        gsonBuilder = new GsonBuilder();
        gsonBuilder.setDateFormat(DateFormat.LONG, DateFormat.LONG);
        gsonBuilder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {

            @Override
            public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                    throws JsonParseException {
                Date date = null;
                if (null != json && json.isJsonPrimitive()) {
                    date = new Date(json.getAsJsonPrimitive().getAsLong());
                }
                return date;
            }
        });
        gsonBuilder.registerTypeAdapter(XMLGregorianCalendar.class, new JsonSerializer<XMLGregorianCalendar>() {

            @Override
            public JsonElement serialize(XMLGregorianCalendar src, Type typeOfSrc,
                    JsonSerializationContext context) {
                Date date = null;
                if (null != src) {
                    date = src.toGregorianCalendar().getTime();
                }
                return new JsonPrimitive(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").format(date));
            }
        });
    }
    return gsonBuilder;
}

From source file:com.balajeetm.mystique.util.gson.lever.JsonLever.java

License:Open Source License

/**
 * Checks if is json primitive.//w w  w .j  a  va  2s.  c  o m
 *
 * @param source the source
 * @return the boolean
 */
public Boolean isPrimitive(JsonElement source) {
    return isNotNull(source) && source.isJsonPrimitive();
}

From source file:com.balajeetm.mystique.util.gson.lever.JsonLever.java

License:Open Source License

/**
 * Checks if is number./*from w  w  w.  j av  a 2s .  c  o m*/
 *
 * @param source the source
 * @return the boolean
 */
public Boolean isNumber(JsonElement source) {
    return isNotNull(source) && source.isJsonPrimitive() && source.getAsJsonPrimitive().isNumber();
}

From source file:com.balajeetm.mystique.util.gson.lever.JsonLever.java

License:Open Source License

/**
 * Checks if is string./*from ww  w  .  j a  v  a 2 s .  c o  m*/
 *
 * @param source the source
 * @return the boolean
 */
public Boolean isString(JsonElement source) {
    return isNotNull(source) && source.isJsonPrimitive() && source.getAsJsonPrimitive().isString();
}

From source file:com.balajeetm.mystique.util.gson.lever.JsonLever.java

License:Open Source License

/**
 * Checks if is boolean./*from w  w w  .  j  a  v  a2s .  com*/
 *
 * @param source the source
 * @return the boolean
 */
public Boolean isBoolean(JsonElement source) {
    return isNotNull(source) && source.isJsonPrimitive() && source.getAsJsonPrimitive().isBoolean();
}