Example usage for com.google.gson JsonPrimitive isString

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

Introduction

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

Prototype

public boolean isString() 

Source Link

Document

Check whether this primitive contains a String value.

Usage

From source file:org.openecomp.sdc.be.model.tosca.converters.ToscaValueBaseConverter.java

License:Open Source License

public Object json2JavaPrimitive(JsonPrimitive prim) {
    if (prim.isBoolean()) {
        return prim.getAsBoolean();
    } else if (prim.isString()) {
        return prim.getAsString();
    } else if (prim.isNumber()) {
        String strRepesentation = prim.getAsString();
        if (strRepesentation.contains(".")) {
            return prim.getAsDouble();
        } else {/*  w ww. j  a  va 2 s. com*/
            return prim.getAsInt();
        }
    } else {
        throw new IllegalStateException();
    }
}

From source file:org.openqa.selenium.json.JsonToBeanConverter.java

License:Apache License

private Object convertJsonPrimitive(JsonPrimitive json) {
    if (json.isBoolean()) {
        return json.getAsBoolean();
    } else if (json.isNumber()) {
        if (json.getAsLong() == json.getAsDouble()) {
            return json.getAsLong();
        }/*from   w w  w  .j a  v  a  2s  .  co  m*/
        return json.getAsDouble();
    } else if (json.isString()) {
        return json.getAsString();
    } else {
        return null;
    }
}

From source file:org.plos.crepo.model.metadata.RepoMetadata.java

License:Open Source License

@VisibleForTesting
static Object convertJsonToImmutable(JsonElement element) {
    if (element.isJsonNull()) {
        return null;
    }//  ww  w. j av  a  2s  .  com
    if (element.isJsonPrimitive()) {
        JsonPrimitive primitive = element.getAsJsonPrimitive();
        if (primitive.isString())
            return primitive.getAsString();
        if (primitive.isNumber())
            return asNumber(primitive);
        if (primitive.isBoolean())
            return primitive.getAsBoolean();
        throw new RuntimeException("JsonPrimitive is not one of the expected primitive types");
    }
    if (element.isJsonArray()) {
        JsonArray array = element.getAsJsonArray();
        if (array.size() == 0)
            return Collections.emptyList();
        List<Object> convertedList = new ArrayList<>(array.size());
        for (JsonElement arrayElement : array) {
            Object convertedElement = convertJsonToImmutable(arrayElement);
            convertedList.add(convertedElement);
        }
        return Collections.unmodifiableList(convertedList);
    }
    if (element.isJsonObject()) {
        Set<Map.Entry<String, JsonElement>> entries = element.getAsJsonObject().entrySet();
        if (entries.size() == 0)
            return Collections.emptyMap();
        Map<String, Object> convertedMap = Maps.newHashMapWithExpectedSize(entries.size());
        for (Map.Entry<String, JsonElement> entry : entries) {
            String key = Preconditions.checkNotNull(entry.getKey());
            Object value = convertJsonToImmutable(entry.getValue());
            convertedMap.put(key, value);
        }
        return Collections.unmodifiableMap(convertedMap);
    }
    throw new RuntimeException("JsonElement is not one of the expected subtypes");
}

From source file:org.ppojo.data.JsonElementTypes.java

License:Apache License

public static JsonElementTypes getType(JsonElement json) {
    if (json.isJsonObject())
        return JsonElementTypes.JsonObject;
    if (json.isJsonArray())
        return JsonElementTypes.JsonArray;
    if (json.isJsonNull())
        return JsonElementTypes.JsonNull;
    if (json.isJsonPrimitive()) {
        JsonPrimitive jsonPrimitive = json.getAsJsonPrimitive();
        if (jsonPrimitive.isBoolean())
            return JsonElementTypes.Boolean;
        if (jsonPrimitive.isNumber())
            return JsonElementTypes.Number;
        if (jsonPrimitive.isString())
            return JsonElementTypes.String;
    }/*from w w  w .j  a  va  2s  . com*/
    return JsonElementTypes.Unknown;
}

From source file:org.qcert.camp.translator.Rule2CAMP.java

License:Open Source License

/**
 * Obtain the correct kind of value from a JsonPrimitive node.  The kinds we support are int, String, and boolean
 * @param primitive the JsonPrimitive node
 * @return the value//from w  w w  .  j  a va 2  s.co  m
 */
private static Object valueFromJson(JsonPrimitive primitive) {
    if (primitive.isString())
        return primitive.getAsString();
    if (primitive.isNumber())
        return primitive.getAsInt();
    if (primitive.isBoolean())
        return primitive.getAsBoolean();
    throw new IllegalStateException();
}

From source file:org.qcert.runtime.DataComparator.java

License:Apache License

private static DType getType(JsonElement obj) {
    if (obj == null) {
        return DType.DT_JNULL;
    } else if (obj.isJsonNull()) {
        return DType.DT_NULL;
    } else if (obj.isJsonPrimitive()) {
        final JsonPrimitive prim = obj.getAsJsonPrimitive();
        if (prim.isBoolean()) {
            return DType.DT_BOOL;
        } else if (prim.isString()) {
            return DType.DT_STRING;
        } else if (prim.isNumber()) {
            final Number num = prim.getAsNumber();
            if (num instanceof LazilyParsedNumber) {
                return DType.DT_LAZYNUM;
            } else if (num instanceof Long || num instanceof Short || num instanceof Integer) {
                return DType.DT_LONG;
            } else if (num instanceof Double || num instanceof Short || num instanceof Float) {
                return DType.DT_DOUBLE;
            } else {
                throw new RuntimeException(
                        "Unknown primitive json number type: " + num + " of type " + num.getClass());
            }/*from  w ww .  j a  v  a  2 s .c  o  m*/
        } else {
            throw new RuntimeException("Unknown primitive json type: " + prim);
        }
    } else if (obj.isJsonArray()) {
        return DType.DT_COLL;
    } else if (obj.isJsonObject()) {
        return DType.DT_REC;
    } else {
        throw new RuntimeException("Unknown json type: " + obj + " of type " + obj.getClass());
    }
}

From source file:org.qcert.runtime.UnaryOperators.java

License:Apache License

private static void tostring(StringBuilder sb, JsonPrimitive jp) {
    if (jp.isString()) {
        sb.append(jp.getAsString());// w w  w .  j a v  a 2  s.c  o  m
    } else {
        sb.append(jp.toString());
    }
}

From source file:org.runbuddy.libtomahawk.resolver.ScriptResolver.java

License:Open Source License

private void onTestConfigFinished(JsonPrimitive results) {
    int type = -1;
    String message = null;//from  w  w w .j  a  va2s. com
    if (results.isString()) {
        type = AuthenticatorManager.CONFIG_TEST_RESULT_TYPE_OTHER;
        message = results.getAsString();
    } else if (results.isNumber() && results.getAsInt() > 0 && results.getAsInt() < 8) {
        type = results.getAsInt();
    }
    Log.d(TAG, getName() + ": Config test result received. type: " + type + ", message:" + message);
    if (type == AuthenticatorManager.CONFIG_TEST_RESULT_TYPE_SUCCESS) {
        setEnabled(true);
    } else {
        setEnabled(false);
    }
    AuthenticatorManager.ConfigTestResultEvent event = new AuthenticatorManager.ConfigTestResultEvent();
    event.mComponent = ScriptResolver.this;
    event.mType = type;
    event.mMessage = message;
    EventBus.getDefault().post(event);
    AuthenticatorManager.showToast(getPrettyName(), event);
}

From source file:org.spongepowered.common.util.persistence.JsonTranslator.java

License:MIT License

private static Object convert(JsonElement element) {
    if (element.isJsonObject()) {
        MemoryDataView container = new MemoryDataContainer();
        for (Entry<String, JsonElement> entry : ((JsonObject) element).entrySet()) {
            Object value = convert(entry.getValue());
            if (value != null) {
                container.set(DataQuery.of(entry.getKey()), value);
            }/*from   w w  w .j  av  a 2s. c  o m*/
        }
        return container;
    } else if (element.isJsonArray()) {
        return Lists.newArrayList(Iterables.transform((JsonArray) element, JsonTranslator::convert));
    } else if (element.isJsonPrimitive()) {
        JsonPrimitive primitive = (JsonPrimitive) element;
        if (primitive.isString()) {
            return primitive.getAsString();
        } else if (primitive.isBoolean()) {
            return primitive.getAsBoolean();
        } else if (primitive.isNumber()) {
            return primitive.getAsNumber();
        }
    }
    return null;
}

From source file:org.springframework.data.cloudant.core.UnmappedDataAdapter.java

License:Apache License

@Override
public T deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
            .registerTypeAdapter(DateTime.class, new DateTimeDataAdapter()).create();
    T doc = gson.fromJson(json, typeOfT);
    Map<String, Object> unmapped = new HashMap<>();
    List<String> nameList = getNestedField(doc.getClass());
    JsonObject object = json.getAsJsonObject();

    //add support for annotated fields ...hack for now
    nameList.add("_id");
    nameList.add("_rev");
    nameList.add("doc_type");
    for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
        if (!nameList.contains(entry.getKey())) {
            if (entry.getValue().isJsonArray()) {
                unmapped.put(entry.getKey(), entry.getValue().getAsJsonArray());
            } else if (entry.getValue().isJsonObject()) {
                unmapped.put(entry.getKey(), entry.getValue().getAsJsonObject());
            } else if (entry.getValue().isJsonPrimitive()) {
                JsonPrimitive v = entry.getValue().getAsJsonPrimitive();
                if (v.isBoolean()) {
                    unmapped.put(entry.getKey(), v.getAsBoolean());
                } else if (v.isNumber()) {
                    unmapped.put(entry.getKey(), v.getAsNumber());
                } else if (v.isString()) {
                    unmapped.put(entry.getKey(), v.getAsString());
                } else if (v.isJsonNull()) {
                    unmapped.put(entry.getKey(), null);
                }//from  w w w . j a  v  a2  s . co m
            } else if (entry.getValue().isJsonNull()) {
                unmapped.put(entry.getKey(), null);
            } else {
                unmapped.put(entry.getKey(), entry.getValue().getAsString());
            }
        }

    }
    doc.setUnmappedFields(unmapped);
    return doc;
}