Example usage for com.google.gson JsonElement getAsJsonPrimitive

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

Introduction

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

Prototype

public JsonPrimitive getAsJsonPrimitive() 

Source Link

Document

convenience method to get this element as a JsonPrimitive .

Usage

From source file:com.adobe.acs.commons.remoteassets.impl.RemoteAssetsNodeSyncImpl.java

License:Apache License

/**
 * Handler for when a JSON element represents a resource property.
 *
 * @param key String// w ww  .  j  a  v  a2s  .  c  om
 * @param json JsonObject
 * @param resource Resource
 * @throws RepositoryException exception
 */
private void setNodeProperty(final ResourceResolver remoteAssetsResolver, final String key,
        final JsonObject json, final Resource resource) throws RepositoryException {
    try {
        JsonElement value = json.get(key);

        if (":".concat(JcrConstants.JCR_DATA).equals(key)) {
            setNodeJcrDataProperty(remoteAssetsResolver, resource,
                    json.getAsJsonPrimitive(JcrConstants.JCR_LASTMODIFIED).getAsString());
        } else if (key.startsWith(":")) {
            // Skip binary properties, since they do not come across in JSON
            return;
        } else if (PROTECTED_PROPERTIES.contains(key)) {
            // Skipping due to the property being unmodifiable.
            return;
        } else if (resource.getValueMap().get(key) != null
                && resource.getValueMap().get(key, String.class).equals(value.getAsString())) {
            // Skipping due to the property already existing and being equal
            return;
        } else {
            setNodeSimpleProperty(value.getAsJsonPrimitive(), key, resource);
        }
    } catch (RepositoryException re) {
        LOG.warn("Repository exception thrown. Skipping '{}' single property for resource '{}'.", key,
                resource.getPath());
    }
}

From source file:com.arcbees.plugin.idea.wizards.createproject.FetchArchetypes.java

License:Apache License

private GsonBuilder createGsonBuilder() {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return new Date(json.getAsJsonPrimitive().getAsLong());
        }/*w w  w.j  av a2  s.c  o m*/
    });
    gsonBuilder.registerTypeAdapter(ArchetypeCollection.class, new JsonDeserializer<ArchetypeCollection>() {
        public ArchetypeCollection deserialize(JsonElement json, Type typeOft,
                JsonDeserializationContext context) throws JsonParseException {
            JsonObject parentJson = json.getAsJsonObject();

            GsonBuilder gsonBuilder = new GsonBuilder();
            gsonBuilder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
                public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                        throws JsonParseException {
                    return new Date(json.getAsJsonPrimitive().getAsLong());
                }
            });
            Gson gson = gsonBuilder.create();

            ArchetypeCollection parent = gson.fromJson(json, ArchetypeCollection.class);
            List<Archetype> archetypes = null;

            if (parentJson.get("items").isJsonArray()) {
                JsonElement itemsJson = parentJson.get("items");
                archetypes = gson.fromJson(itemsJson, new TypeToken<List<Archetype>>() {
                }.getType());
            } else {
                Archetype single = gson.fromJson(parentJson.get("items"), Archetype.class);
                archetypes = new ArrayList<Archetype>();
                archetypes.add(single);
            }
            parent.setArchetypes(archetypes);
            return parent;
        }
    });
    return gsonBuilder;
}

From source file:com.arcbees.vcs.util.GsonDateTypeAdapter.java

License:Apache License

@Override
public Date deserialize(JsonElement json, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    String value = json.getAsJsonPrimitive().getAsString();
    Matcher matcher = DATE_TIME_LONG_FORMAT_PATTERN.matcher(value);

    for (DateFormat dateFormat : dateFormats) {
        try {/*from  ww  w .j av a2 s  .  c  o m*/
            if (matcher.matches()) {
                value = value.replace(matcher.group(1), "");
            }

            return dateFormat.parse(value);
        } catch (ParseException e) {
        }
    }

    try {
        return new Date(json.getAsJsonPrimitive().getAsLong());
    } catch (NumberFormatException e) {
        throw new JsonParseException(e);
    }
}

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

License:Open Source License

/**
 * Validates if the id property is numeric.
 * @param element//from w w w. j  a  va2s . co 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. j  a v a  2  s.  co m
    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.core.module.GsonSerialiser.java

License:Open Source License

@Override
public void serialize(JsonElement value, JsonGenerator gen, SerializerProvider provider) throws IOException {
    if (jsonLever.isNull(value)) {
        gen.writeNull();/*from w  w  w . j a  v a2  s  . c  o  m*/
    } else if (jsonLever.isObject(value)) {
        gen.writeStartObject();
        JsonObject jsonObject = value.getAsJsonObject();
        for (Entry<String, JsonElement> entry : jsonObject.entrySet()) {
            gen.writeFieldName(entry.getKey());
            serialize(entry.getValue(), gen, provider);
        }
        gen.writeEndObject();
    } else if (jsonLever.isArray(value)) {
        gen.writeStartArray();
        JsonArray jsonArray = value.getAsJsonArray();
        for (JsonElement jsonElement : jsonArray) {
            serialize(jsonElement, gen, provider);
        }
        gen.writeEndArray();
    } else if (jsonLever.isPrimitive(value)) {
        JsonPrimitive jsonPrimitive = value.getAsJsonPrimitive();
        if (jsonPrimitive.isBoolean()) {
            gen.writeBoolean(jsonPrimitive.getAsBoolean());
        }
        if (jsonPrimitive.isNumber()) {
            Number nnode = jsonPrimitive.getAsNumber();
            if (nnode instanceof LazilyParsedNumber) {
                gen.writeNumber(nnode.toString());
            } else if (nnode instanceof Integer) {
                gen.writeNumber(nnode.intValue());
            } else if (nnode instanceof Short) {
                gen.writeNumber(nnode.shortValue());
            } else if (nnode instanceof BigInteger || nnode instanceof Long) {
                gen.writeNumber(nnode.longValue());
            } else if (nnode instanceof Float) {
                gen.writeNumber(nnode.floatValue());
            } else if (nnode instanceof Double || nnode instanceof BigDecimal) {
                gen.writeNumber(nnode.doubleValue());
            }
        }
        if (jsonPrimitive.isString()) {
            gen.writeString(jsonPrimitive.getAsString());
        }
    }
}

From source file:com.balajeetm.mystique.util.gson.GsonFactory.java

License:Open Source License

/**
 * Gets the gson builder.//from w w w  .ja  va  2  s. c o m
 *
 * @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 number./*from   ww w  . ja  va  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   w w w  .j  a  va 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  www.  j  av a2  s  .  c o  m*/
 *
 * @param source the source
 * @return the boolean
 */
public Boolean isBoolean(JsonElement source) {
    return isNotNull(source) && source.isJsonPrimitive() && source.getAsJsonPrimitive().isBoolean();
}