Example usage for com.google.gson JsonElement getAsString

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

Introduction

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

Prototype

public String getAsString() 

Source Link

Document

convenience method to get this element as a string value.

Usage

From source file:co.vaughnvernon.tradercommon.media.AbstractJSONMediaReader.java

License:Apache License

protected String stringValue(JsonObject aStartingJsonObject, String... aKeys) {
    String value = null;/*from w  w w .  j  a  va 2s.c o m*/

    JsonElement element = this.navigateTo(aStartingJsonObject, aKeys);

    if (element != null) {
        value = element.getAsString();
    }

    return value;
}

From source file:com.aaasec.sigserv.cscommon.json.DateTypeAdapter.java

License:EUPL

public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (!(json instanceof JsonPrimitive)) {
        throw new JsonParseException("The date should be a string value");
    }//from w ww .j a v  a2 s.c  om

    try {
        return format.parse(json.getAsString());
    } catch (ParseException e) {
        throw new JsonParseException(e);
    }
}

From source file:com.actimem.blog.gson.customtypes.CompanyIdAdapter.java

License:Apache License

@Override
public CompanyId deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    return new CompanyId(json.getAsString());
}

From source file:com.actimem.blog.gson.customtypes.DateTimeAdapter.java

License:Apache License

@Override
public DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    return new DateTime(json.getAsString());
}

From source file:com.addhen.voto.sdk.DateDeserializer.java

License:Apache License

@Override
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    // Determine whether date has time as well
    String[] dateTime = json.getAsString().trim().split(" ");
    try {//from w  w  w .j  a va  2s .c om
        if (dateTime.length > 1) {
            return new SimpleDateFormat(DATE_FORMATS[1], Locale.US).parse(json.getAsString());
        }
        return new SimpleDateFormat(DATE_FORMATS[0], Locale.US).parse(json.getAsString());
    } catch (ParseException e) {
        e.printStackTrace();
    }
    throw new JsonParseException("Unparseable date: \"" + json.getAsString() + "\". Supported formats: "
            + Arrays.toString(DATE_FORMATS));
}

From source file:com.adkdevelopment.jokesactivity.JokesActivityFragment.java

License:Open Source License

/**
 * Method to parse JSON string and return String[] with a link and a title
 *
 * @param jokesJson JSON string with info about comics, link, etc.
 * @return String[] with a link and a title
 *//*from   w w w.  j a  v  a 2  s.  c  o  m*/
public static String[] getJokesInfo(String jokesJson) {

    String[] reviews = new String[2];

    JsonParser parser = new JsonParser();

    JsonElement element = parser.parse(jokesJson);

    if (element.isJsonObject()) {
        JsonObject results = element.getAsJsonObject();
        JsonElement title = results.getAsJsonPrimitive("title");
        JsonElement linkToImage = results.getAsJsonPrimitive("img");

        reviews[0] = title.getAsString();
        reviews[1] = linkToImage.getAsString();

        return reviews;
    }

    return null;
}

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//from  w  ww  . j  a  va  2s  .  c o  m
 * @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.adobe.acs.commons.remoteassets.impl.RemoteAssetsNodeSyncImpl.java

License:Apache License

/**
 * Set mixins property for a resource, based on an array found in the retrieved JSON..
 *
 * @param jsonArray JsonArray/*from  ww  w  .j  a v a 2 s  . c  om*/
 * @param key String
 * @param resource Resource
 * @throws RepositoryException exception
 */
protected void setNodeMixinsProperty(final JsonArray jsonArray, final String key, final Resource resource)
        throws RepositoryException {
    Node node = resource.adaptTo(Node.class);
    for (JsonElement jsonElement : jsonArray) {
        LOG.trace("Adding mixin '{}' for resource '{}'.", jsonElement.getAsString(), resource.getPath());
        node.addMixin(jsonElement.getAsString());
    }
}

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

License:Apache License

/**
 * Set tags property for a resource, based on an array found in the retrieved JSON..
 *
 * @param jsonArray JsonArray/*from  www  .  j a  va2 s.co m*/
 * @param key String
 * @param resource Resource
 * @throws RepositoryException exception
 */
private void setNodeTagsProperty(final ResourceResolver remoteAssetsResolver, final JsonArray jsonArray,
        final Resource resource) throws RepositoryException {
    TagManager tagManager = remoteAssetsResolver.adaptTo(TagManager.class);
    ArrayList<Tag> tagList = new ArrayList<>();

    for (JsonElement jsonElement : jsonArray) {
        Tag tag = tagManager.resolve(jsonElement.getAsString());
        if (tag == null) {
            LOG.warn("Tag '{}' could not be found. Skipping tag for resource '{}'.", jsonElement.getAsString(),
                    resource.getPath());
            continue;
        }

        tagList.add(tag);
    }

    if (tagList.size() > 0) {
        tagManager.setTags(resource, tagList.toArray(new Tag[tagList.size()]));
        LOG.trace("Tags added for resource '{}'.", resource.getPath());
    }
}

From source file:com.adr.datasql.orm.KindResultsJson.java

License:Apache License

@Override
public String getString(String columnName) throws DataLinkException {
    JsonElement element = json.get(columnName);
    return element == null || element.isJsonNull() ? null : element.getAsString();
}