Example usage for com.google.gson JsonPrimitive getAsString

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

Introduction

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

Prototype

@Override
public String getAsString() 

Source Link

Document

convenience method to get this element as a String.

Usage

From source file:org.eclipse.recommenders.internal.coordinates.rcp.DependencyInfoJsonTypeAdapter.java

License:Open Source License

@Override
public DependencyInfo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObject = json.getAsJsonObject();

    JsonPrimitive fileElement = jsonObject.getAsJsonPrimitive("location"); //$NON-NLS-1$
    File file = new File(fileElement.getAsString());

    JsonElement typeElement = jsonObject.get("type"); //$NON-NLS-1$
    DependencyType type = context.deserialize(typeElement, DependencyType.class);

    JsonElement hintsElement = jsonObject.get("hints"); //$NON-NLS-1$
    Map<String, String> hints = context.deserialize(hintsElement, cacheType);

    return new DependencyInfo(file, type, hints);
}

From source file:org.eclipse.scada.base.json.VariantJsonDeserializer.java

License:Open Source License

private Variant decodeFromPrimitive(final JsonElement json) {
    final JsonPrimitive jsonPrim = (JsonPrimitive) json;
    if (jsonPrim.isBoolean()) {
        return Variant.valueOf(jsonPrim.getAsBoolean());
    } else if (jsonPrim.isNumber()) {
        return Variant.valueOf(jsonPrim.getAsNumber());
    } else {/*  w w w  . ja  v a2s  .c o  m*/
        return VariantEditor.toVariant(jsonPrim.getAsString());
    }
}

From source file:org.eclipse.smarthome.automation.parser.gson.internal.ConfigurationDeserializer.java

License:Open Source License

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

    Configuration configuration = new Configuration();
    JsonObject jo = (JsonObject) json;//from ww w  .  j a v  a2 s .  c om
    for (Entry<String, JsonElement> e : jo.entrySet()) {
        JsonPrimitive value = e.getValue().getAsJsonPrimitive();
        if (value.isString()) {
            configuration.put(e.getKey(), value.getAsString());
        } else if (value.isNumber()) {
            configuration.put(e.getKey(), value.getAsBigDecimal());
        } else if (value.isBoolean()) {
            configuration.put(e.getKey(), value.getAsBoolean());
        }
    }
    return configuration;
}

From source file:org.eclipse.smarthome.config.core.ConfigurationDeserializer.java

License:Open Source License

private Object deserialize(JsonPrimitive primitive) {
    if (primitive.isString()) {
        return primitive.getAsString();
    } else if (primitive.isNumber()) {
        return primitive.getAsBigDecimal();
    } else if (primitive.isBoolean()) {
        return primitive.getAsBoolean();
    } else {//from   w  w w.j a  v  a  2s. c om
        throw new IllegalArgumentException("Unsupported primitive: " + primitive);
    }
}

From source file:org.eclipse.wst.jsdt.nodejs.ide.ui.internal.views.dependencies.NpmDependenciesViewLabelProvider.java

License:Open Source License

/**
 * {@inheritDoc}/*from  ww  w .  j ava 2  s  .co  m*/
 *
 * @see org.eclipse.jface.viewers.StyledCellLabelProvider#update(org.eclipse.jface.viewers.ViewerCell)
 */
@Override
public void update(ViewerCell cell) {
    Object element = cell.getElement();
    if (element instanceof JsonObject) {
        JsonObject jsonObject = (JsonObject) element;
        JsonElement nameJsonElement = jsonObject.get("name");
        JsonElement versionJsonELement = jsonObject.get("version");
        if (nameJsonElement instanceof JsonPrimitive && versionJsonELement instanceof JsonPrimitive) {
            JsonPrimitive nameJsonPrimitive = (JsonPrimitive) nameJsonElement;
            String name = nameJsonPrimitive.getAsString();

            JsonPrimitive versionJsonPrimitive = (JsonPrimitive) versionJsonELement;
            String version = versionJsonPrimitive.getAsString();

            StyledString text = new StyledString();
            text.append(name);
            text.append(" ");
            text.append("[" + version + "]", StyledString.COUNTER_STYLER);

            cell.setText(text.toString());
            cell.setStyleRanges(text.getStyleRanges());
            cell.setImage(NodeJsIdeUiActivator.getInstance().getImage(INodeJsIcons.PROJECT_16x16));
        }
    } else if (element instanceof JsonDependencyField) {
        JsonDependencyField jsonDependencyField = (JsonDependencyField) element;
        String name = jsonDependencyField.getName();

        JsonElement versionJsonElement = jsonDependencyField.getValue().get("version");
        if (versionJsonElement instanceof JsonPrimitive) {
            JsonPrimitive versionJsonPrimitive = (JsonPrimitive) versionJsonElement;
            String version = versionJsonPrimitive.getAsString();

            StyledString text = new StyledString();
            text.append(name);
            text.append(" ");
            text.append("[" + version + "]", StyledString.COUNTER_STYLER);

            cell.setText(text.toString());
            cell.setStyleRanges(text.getStyleRanges());
            cell.setImage(NodeJsIdeUiActivator.getInstance().getImage(INodeJsIcons.FILE_16x16));
        }
    }
}

From source file:org.eclipse.wst.jsdt.nodejs.ide.ui.internal.views.outdated.NpmOutdatedView.java

License:Open Source License

private String getStringValue(JsonElement jsonElement, String fieldName) {
    String value = "";
    if (jsonElement instanceof JsonObject) {
        JsonObject jsonObject = (JsonObject) jsonElement;
        JsonPrimitive jsonPrimitive = jsonObject.getAsJsonPrimitive(fieldName);
        if (jsonPrimitive != null && jsonPrimitive.isString()) {
            value = jsonPrimitive.getAsString();
        }/*from  w  ww . jav  a2 s  .  c om*/
    }
    return value;
}

From source file:org.eel.kitchen.jsonschema.GsonProvider.java

License:Open Source License

private static JsonNode gsonToValueNode(final JsonPrimitive primitive) {
    if (primitive.isBoolean())
        return factory.booleanNode(primitive.getAsBoolean());
    if (primitive.isNumber())
        return toNumberNode(primitive.getAsBigDecimal());

    // Can only be a string,now
    return factory.textNode(primitive.getAsString());
}

From source file:org.eiichiro.bootleg.json.JSONRequest.java

License:Open Source License

/**
 * Converts the specified object to the specified type.
 * The object to be converted must be a {@code JsonPrimitive}, or this 
 * method return <code>null</code>.
 * //from  w w w  .j a va2  s .  c  o m
 * @param object The object to be converted
 * @param type The type to which the specified object be converted.
 * @return The converted object.
 */
@Override
protected Object convert(Object object, Class<?> type) {
    if (object instanceof JsonPrimitive) {
        JsonPrimitive primitive = (JsonPrimitive) object;
        return super.convert(primitive.getAsString(), type);
    } else {
        logger.warn("Parameter [" + object + "] cannot be converted to [" + type
                + "]; Converted 'object' must be a [" + JsonPrimitive.class + "]");
        return null;
    }
}

From source file:org.eiichiro.bootleg.json.JSONRequest.java

License:Open Source License

/**
 * Converts the specified object to the specified user-defined value type.
 * The object to be converted must be a {@code JsonPrimitive}, or this 
 * method return <code>null</code>.
 * /*  w  ww  . j av  a2 s  . c  o  m*/
 * @param object The object to be converted
 * @param type The user-defined value type to which the specified object be 
 * converted.
 * @return The converted object.
 */
@Override
protected Object convertUserDefinedValueType(Object object, Class<?> type) {
    if (object instanceof JsonPrimitive) {
        JsonPrimitive primitive = (JsonPrimitive) object;
        return super.convertUserDefinedValueType(primitive.getAsString(), type);
    } else {
        logger.warn("Parameter [" + object + "] cannot be converted to [" + type
                + "]; Converted 'object' must be a [" + JsonPrimitive.class + "]");
        return null;
    }
}

From source file:org.faul.jql.utils.RemoteObjectHandler.java

License:Open Source License

Object getPrimitive(JsonPrimitive p) {
    if (p.isNumber()) {
        String str = p.getAsString();
        try {/*from  w  w w.  j a  v  a  2  s. co m*/
            return Long.parseLong(str);
        } catch (Exception error) {
        }
        return Double.parseDouble(str);
    }

    if (p.isBoolean()) {
        return p.getAsBoolean();
    }

    if (p.isJsonArray()) {
        return p.getAsJsonArray();
    }

    if (p.isString()) {
        return p.getAsString();
    }

    return null;
}