Example usage for com.google.gson JsonPrimitive isNumber

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

Introduction

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

Prototype

public boolean isNumber() 

Source Link

Document

Check whether this primitive contains a Number.

Usage

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   ww  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 a  v a2  s  .com
        }
        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 . jav a  2  s .com*/
            } else if (entry.getValue().isJsonNull()) {
                unmapped.put(entry.getKey(), null);
            } else {
                unmapped.put(entry.getKey(), entry.getValue().getAsString());
            }
        }

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

From source file:org.structr.core.JsonInputGSONAdapter.java

License:Open Source License

private static Object fromPrimitive(final JsonPrimitive p) {

    if (p.isNumber()) {

        Number number = p.getAsNumber();
        if (number instanceof Integer) {
            return number.intValue();
        } else {/*from   w  w  w .j  a  v a2s.c o  m*/
            return number.doubleValue();
        }

    }

    return p.getAsString();
}

From source file:org.structr.core.rest.JsonInputGSONAdapter.java

License:Open Source License

public static Object fromPrimitive(final JsonPrimitive p) {

    if (p.isNumber()) {

        Number number = p.getAsNumber();

        // Detect if value is floating point
        if (p.getAsString().contains(".")) {

            return number.doubleValue();

        } else {/* w  ww.  ja  v  a  2  s.  c o  m*/

            return number.longValue();

        }

    } else if (p.isBoolean()) {

        return p.getAsBoolean();

    }

    return p.getAsString();
}

From source file:org.terasology.rendering.nui.widgets.models.JsonTreeConverter.java

License:Apache License

/**
 * @param name The name to be given to this node (if null, is replaced by a default name).
 * @param json The {@link JsonElement} to be converted to a {@link JsonTree}.
 * @return A tree representation of the JSON hierarchy.
 *///from  w ww. ja va 2s.  c om
private static JsonTree serialize(String name, JsonElement json) {
    if (json.isJsonPrimitive()) {
        JsonPrimitive primitive = json.getAsJsonPrimitive();
        if (primitive.isBoolean()) {
            return new JsonTree(new JsonTreeNode(name, json.getAsBoolean(),
                    name != null ? JsonTreeNode.ElementType.KEY_VALUE_PAIR : JsonTreeNode.ElementType.VALUE));
        } else if (primitive.isNumber()) {
            return new JsonTree(new JsonTreeNode(name, json.getAsNumber(),
                    name != null ? JsonTreeNode.ElementType.KEY_VALUE_PAIR : JsonTreeNode.ElementType.VALUE));
        } else if (primitive.isString()) {
            return new JsonTree(new JsonTreeNode(name, json.getAsString(),
                    name != null ? JsonTreeNode.ElementType.KEY_VALUE_PAIR : JsonTreeNode.ElementType.VALUE));
        } else {
            return new JsonTree(new JsonTreeNode(name, null,
                    name != null ? JsonTreeNode.ElementType.KEY_VALUE_PAIR : JsonTreeNode.ElementType.VALUE));
        }
    } else if (json.isJsonArray()) {
        JsonTree tree = new JsonTree(new JsonTreeNode(name, null, JsonTreeNode.ElementType.ARRAY));
        JsonArray array = json.getAsJsonArray();
        for (int i = 0; i < array.size(); i++) {
            tree.addChild(serialize(array.get(i)));
        }
        return tree;
    } else if (json.isJsonObject()) {
        JsonTree tree = new JsonTree(new JsonTreeNode(name, null, JsonTreeNode.ElementType.OBJECT));
        JsonObject object = json.getAsJsonObject();
        for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
            tree.addChild(serialize(entry.getKey(), entry.getValue()));
        }
        return tree;
    } else {
        return new JsonTree(new JsonTreeNode(name, null, JsonTreeNode.ElementType.NULL));
    }
}

From source file:org.terasology.rendering.nui.widgets.treeView.JsonTreeConverter.java

License:Apache License

/**
 * @param name The name to be given to this node (if null, is replaced by a default name).
 * @param json The {@link JsonElement} to be converted to a {@link JsonTree}.
 * @return A tree representation of the JSON hierarchy.
 *///from w  ww .  j a va 2s .  c o  m
private static JsonTree serialize(String name, JsonElement json) {
    if (json.isJsonPrimitive()) {
        JsonPrimitive primitive = json.getAsJsonPrimitive();
        if (primitive.isBoolean()) {
            return new JsonTree(new JsonTreeValue(name, json.getAsBoolean(),
                    name != null ? JsonTreeValue.Type.KEY_VALUE_PAIR : JsonTreeValue.Type.VALUE));
        } else if (primitive.isNumber()) {
            return new JsonTree(new JsonTreeValue(name, json.getAsNumber(),
                    name != null ? JsonTreeValue.Type.KEY_VALUE_PAIR : JsonTreeValue.Type.VALUE));
        } else if (primitive.isString()) {
            return new JsonTree(new JsonTreeValue(name, json.getAsString(),
                    name != null ? JsonTreeValue.Type.KEY_VALUE_PAIR : JsonTreeValue.Type.VALUE));
        } else {
            return new JsonTree(new JsonTreeValue(name, null,
                    name != null ? JsonTreeValue.Type.KEY_VALUE_PAIR : JsonTreeValue.Type.VALUE));
        }
    } else if (json.isJsonArray()) {
        JsonTree tree = new JsonTree(new JsonTreeValue(name, null, JsonTreeValue.Type.ARRAY));
        JsonArray array = json.getAsJsonArray();
        for (int i = 0; i < array.size(); i++) {
            tree.addChild(serialize(array.get(i)));
        }
        return tree;
    } else if (json.isJsonObject()) {
        JsonTree tree = new JsonTree(new JsonTreeValue(name, null, JsonTreeValue.Type.OBJECT));
        JsonObject object = json.getAsJsonObject();
        for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
            tree.addChild(serialize(entry.getKey(), entry.getValue()));
        }
        return tree;
    } else {
        return new JsonTree(new JsonTreeValue(name, null, JsonTreeValue.Type.NULL));
    }
}

From source file:org.testeditor.fixture.web.json.BrowserSetupReader.java

License:Open Source License

private BrowserSetting createBrowserSettting(Entry<String, JsonElement> browserSettingEntry) {
    BrowserSetting browserSetting = null;
    String key = browserSettingEntry.getKey();
    JsonElement value = browserSettingEntry.getValue();
    JsonPrimitive browserSettingAsJsonPrimitive = value.getAsJsonPrimitive();
    if (browserSettingAsJsonPrimitive.isString()) {
        browserSetting = new BrowserSetting(key, value.getAsString());
    } else if (browserSettingAsJsonPrimitive.isBoolean()) {
        browserSetting = new BrowserSetting(key, value.getAsBoolean());
    } else if (browserSettingAsJsonPrimitive.isNumber()) {
        browserSetting = new BrowserSetting(key, value.getAsInt());
    }/*from www  .  j a  v  a2 s .  c  o m*/
    return browserSetting;
}

From source file:org.thingsboard.server.common.transport.adaptor.JsonConverter.java

License:Apache License

private static List<KeyValueProto> parseProtoValues(JsonObject valuesObject) {
    List<KeyValueProto> result = new ArrayList<>();
    for (Entry<String, JsonElement> valueEntry : valuesObject.entrySet()) {
        JsonElement element = valueEntry.getValue();
        if (element.isJsonPrimitive()) {
            JsonPrimitive value = element.getAsJsonPrimitive();
            if (value.isString()) {
                if (maxStringValueLength > 0 && value.getAsString().length() > maxStringValueLength) {
                    String message = String.format(
                            "String value length [%d] for key [%s] is greater than maximum allowed [%d]",
                            value.getAsString().length(), valueEntry.getKey(), maxStringValueLength);
                    throw new JsonSyntaxException(message);
                }//from   www  .  j  a va 2s.  com
                if (isTypeCastEnabled && NumberUtils.isParsable(value.getAsString())) {
                    try {
                        result.add(buildNumericKeyValueProto(value, valueEntry.getKey()));
                    } catch (RuntimeException th) {
                        result.add(KeyValueProto.newBuilder().setKey(valueEntry.getKey())
                                .setType(KeyValueType.STRING_V).setStringV(value.getAsString()).build());
                    }
                } else {
                    result.add(KeyValueProto.newBuilder().setKey(valueEntry.getKey())
                            .setType(KeyValueType.STRING_V).setStringV(value.getAsString()).build());
                }
            } else if (value.isBoolean()) {
                result.add(KeyValueProto.newBuilder().setKey(valueEntry.getKey())
                        .setType(KeyValueType.BOOLEAN_V).setBoolV(value.getAsBoolean()).build());
            } else if (value.isNumber()) {
                result.add(buildNumericKeyValueProto(value, valueEntry.getKey()));
            } else {
                throw new JsonSyntaxException(CAN_T_PARSE_VALUE + value);
            }
        } else {
            throw new JsonSyntaxException(CAN_T_PARSE_VALUE + element);
        }
    }
    return result;
}

From source file:org.thingsboard.server.common.transport.adaptor.JsonConverter.java

License:Apache License

private static List<KvEntry> parseValues(JsonObject valuesObject) {
    List<KvEntry> result = new ArrayList<>();
    for (Entry<String, JsonElement> valueEntry : valuesObject.entrySet()) {
        JsonElement element = valueEntry.getValue();
        if (element.isJsonPrimitive()) {
            JsonPrimitive value = element.getAsJsonPrimitive();
            if (value.isString()) {
                if (maxStringValueLength > 0 && value.getAsString().length() > maxStringValueLength) {
                    String message = String.format(
                            "String value length [%d] for key [%s] is greater than maximum allowed [%d]",
                            value.getAsString().length(), valueEntry.getKey(), maxStringValueLength);
                    throw new JsonSyntaxException(message);
                }//w  w w  .  j av  a 2  s . c  o m
                if (isTypeCastEnabled && NumberUtils.isParsable(value.getAsString())) {
                    try {
                        parseNumericValue(result, valueEntry, value);
                    } catch (RuntimeException th) {
                        result.add(new StringDataEntry(valueEntry.getKey(), value.getAsString()));
                    }
                } else {
                    result.add(new StringDataEntry(valueEntry.getKey(), value.getAsString()));
                }
            } else if (value.isBoolean()) {
                result.add(new BooleanDataEntry(valueEntry.getKey(), value.getAsBoolean()));
            } else if (value.isNumber()) {
                parseNumericValue(result, valueEntry, value);
            } else {
                throw new JsonSyntaxException(CAN_T_PARSE_VALUE + value);
            }
        } else {
            throw new JsonSyntaxException(CAN_T_PARSE_VALUE + element);
        }
    }
    return result;
}