Example usage for com.google.gson JsonPrimitive getAsDouble

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

Introduction

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

Prototype

@Override
public double getAsDouble() 

Source Link

Document

convenience method to get this element as a primitive double.

Usage

From source file:com.addthis.hydra.task.source.bundleizer.GsonBundleizer.java

License:Apache License

public ValueObject fromGson(JsonElement gson) {
    if (gson.isJsonNull()) {
        return null;
    } else if (gson.isJsonObject()) {
        JsonObject object = gson.getAsJsonObject();
        ValueMap map = ValueFactory.createMap();
        for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
            map.put(entry.getKey(), fromGson(entry.getValue()));
        }//from ww  w .j  av a  2 s  .c  o m
        return map;
    } else if (gson.isJsonArray()) {
        JsonArray gsonArray = gson.getAsJsonArray();
        ValueArray valueArray = ValueFactory.createArray(gsonArray.size());
        for (JsonElement arrayElement : gsonArray) {
            valueArray.add(fromGson(arrayElement));
        }
        return valueArray;
    } else {
        JsonPrimitive primitive = gson.getAsJsonPrimitive();
        if (primitive.isNumber()) {
            return ValueFactory.create(primitive.getAsDouble());
        } else {
            return ValueFactory.create(primitive.getAsString());
        }
    }
}

From source file:com.continusec.client.ObjectHash.java

License:Apache License

/**
 * Calculate the objecthash for a Gson JsonElement object, with a custom redaction prefix string.
 * @param o the Gson JsonElement to calculated the objecthash for.
 * @param r the string to use as a prefix to indicate that a string should be treated as a redacted subobject.
 * @return the objecthash for this object
 * @throws ContinusecException upon error
 *//*  w w w .ja  v  a 2s. c  o  m*/
public static final byte[] objectHashWithRedaction(JsonElement o, String r) throws ContinusecException {
    if (o == null || o.isJsonNull()) {
        return hashNull();
    } else if (o.isJsonArray()) {
        return hashArray(o.getAsJsonArray(), r);
    } else if (o.isJsonObject()) {
        return hashObject(o.getAsJsonObject(), r);
    } else if (o.isJsonPrimitive()) {
        JsonPrimitive p = o.getAsJsonPrimitive();
        if (p.isBoolean()) {
            return hashBoolean(p.getAsBoolean());
        } else if (p.isNumber()) {
            return hashDouble(p.getAsDouble());
        } else if (p.isString()) {
            return hashString(p.getAsString(), r);
        } else {
            throw new InvalidObjectException();
        }
    } else {
        throw new InvalidObjectException();
    }
}

From source file:com.frontier45.flume.sink.elasticsearch2.ElasticSearchLogStashEventSerializer.java

License:Apache License

private Object getValue(JsonElement obj) {
    JsonPrimitive jp = obj.getAsJsonPrimitive();
    if (jp.isBoolean()) {
        return jp.getAsBoolean();
    } else if (jp.isNumber()) {
        String value = jp.getAsString();
        if (value.indexOf(".") > 0) {
            return jp.getAsDouble();
        } else {//ww  w .ja v a  2  s.co  m
            return jp.getAsLong();
        }
    } else if (jp.isString()) {
        return jp.getAsString();
    }
    return obj;
}

From source file:com.github.autermann.matlab.json.MatlabValueSerializer.java

License:Open Source License

private MatlabScalar parseMatlabScalar(JsonElement value) {
    JsonPrimitive p = (JsonPrimitive) value;
    if (p.isString()) {
        return new MatlabScalar(Double.valueOf(p.getAsString()));
    } else {//ww w.  j  a v  a  2  s  . c o m
        return new MatlabScalar(p.getAsDouble());
    }
}

From source file:com.hi3project.dandelion.gip.codec.json.JSONGIPCodec.java

License:Open Source License

private String jsonValueAsString(JsonElement value) throws GIPEventCodeDecodeErrorException {

    JsonPrimitive primitive = value.getAsJsonPrimitive();

    if (primitive.isString()) {
        return primitive.getAsString();
    } else if (primitive.isBoolean()) {
        return (Boolean.toString(primitive.getAsBoolean()));
    } else if (primitive.isNumber()) {
        return (Double.toString(primitive.getAsDouble()));
    } else {//from   ww w  .  j a  va  2s . co m
        throw new GIPEventCodeDecodeErrorException("", "Unable to decode JsonValue.");
    }

}

From source file:com.jayway.jsonpath.internal.spi.mapper.GsonMapper.java

License:Apache License

@Override
public Object convert(Object src, Class<?> srcType, Class<?> targetType, Configuration conf) {

    assertValidConversion(src, srcType, targetType);

    if (src == null || src.getClass().equals(JsonNull.class)) {
        return null;
    }//from w  w  w .ja  v a 2 s .  c o  m

    if (JsonPrimitive.class.isAssignableFrom(srcType)) {

        JsonPrimitive primitive = (JsonPrimitive) src;
        if (targetType.equals(Long.class)) {
            return primitive.getAsLong();
        } else if (targetType.equals(Integer.class)) {
            return primitive.getAsInt();
        } else if (targetType.equals(BigInteger.class)) {
            return primitive.getAsBigInteger();
        } else if (targetType.equals(Byte.class)) {
            return primitive.getAsByte();
        } else if (targetType.equals(BigDecimal.class)) {
            return primitive.getAsBigDecimal();
        } else if (targetType.equals(Double.class)) {
            return primitive.getAsDouble();
        } else if (targetType.equals(Float.class)) {
            return primitive.getAsFloat();
        } else if (targetType.equals(String.class)) {
            return primitive.getAsString();
        } else if (targetType.equals(Boolean.class)) {
            return primitive.getAsBoolean();
        } else if (targetType.equals(Date.class)) {

            if (primitive.isNumber()) {
                return new Date(primitive.getAsLong());
            } else if (primitive.isString()) {
                try {
                    return DateFormat.getInstance().parse(primitive.getAsString());
                } catch (ParseException e) {
                    throw new MappingException(e);
                }
            }
        }

    } else if (JsonObject.class.isAssignableFrom(srcType)) {
        JsonObject srcObject = (JsonObject) src;
        if (targetType.equals(Map.class)) {
            Map<String, Object> targetMap = new LinkedHashMap<String, Object>();
            for (Map.Entry<String, JsonElement> entry : srcObject.entrySet()) {
                Object val = null;
                JsonElement element = entry.getValue();
                if (element.isJsonPrimitive()) {
                    val = GsonJsonProvider.unwrap(element);
                } else if (element.isJsonArray()) {
                    val = convert(element, element.getClass(), List.class, conf);
                } else if (element.isJsonObject()) {
                    val = convert(element, element.getClass(), Map.class, conf);
                } else if (element.isJsonNull()) {
                    val = null;
                }
                targetMap.put(entry.getKey(), val);
            }
            return targetMap;
        }

    } else if (JsonArray.class.isAssignableFrom(srcType)) {
        JsonArray srcArray = (JsonArray) src;
        if (targetType.equals(List.class)) {
            List<Object> targetList = new ArrayList<Object>();
            for (JsonElement element : srcArray) {
                if (element.isJsonPrimitive()) {
                    targetList.add(GsonJsonProvider.unwrap(element));
                } else if (element.isJsonArray()) {
                    targetList.add(convert(element, element.getClass(), List.class, conf));
                } else if (element.isJsonObject()) {
                    targetList.add(convert(element, element.getClass(), Map.class, conf));
                } else if (element.isJsonNull()) {
                    targetList.add(null);
                }
            }
            return targetList;
        }
    }

    throw new MappingException("Can not map: " + srcType.getName() + " to: " + targetType.getName());
}

From source file:com.make.json2java.ClassField.java

License:Apache License

/**
 * Based on the seen json values, infer a type for this field.
 * Strings are mapped to String. numbers are preferred mapped to ints, then longs and finally as doubles.
 *///  ww  w. jav  a  2  s  .  c  o  m
private InferredType inferType(Iterable<JsonElement> jsonValues, String type, boolean isArrayType) {
    if (mappedType)
        return new InferredType(type, isArrayType);
    InferredType inferredType = new InferredType(type, isArrayType);
    for (JsonElement jsonValue : jsonValues) {
        if (jsonValue instanceof JsonPrimitive) {
            JsonPrimitive primitive = jsonValue.getAsJsonPrimitive();
            if (isBooleanValue(primitive)) {
                inferredType = new InferredType("boolean", false);
            } else if (primitive.isString()) {
                inferredType = new InferredType("String", false);
            } else if (primitive.isNumber()) {
                double number = primitive.getAsDouble();
                boolean isWholeNumber = number - Math.ceil(number) == 0;
                if (isWholeNumber) { // int is preferred over long so look for that
                    long longValue = (long) number;
                    boolean isLargerThanInt = longValue > Integer.MAX_VALUE || longValue < Integer.MIN_VALUE;
                    if (isLargerThanInt && !inferredType.type.equals("double")) { // some other value was a floating point
                        inferredType = new InferredType("long", false);
                    } else { // some other jsonValue was big enough to fit in long
                        if (!inferredType.equals("long") && !inferredType.equals("double")) {
                            inferredType = new InferredType("int", false);
                        }
                    }
                } else { // double is preferred over float
                    inferredType = new InferredType("double", false);
                }
            }
        } else if (jsonValue instanceof JsonArray) {
            this.isArrayType = true;
            inferredType = new InferredType(inferType(jsonValue.getAsJsonArray(), type, false).type, true);
        }
    }
    return inferredType;
}

From source file:com.qmetry.qaf.automation.gson.GsonObjectDeserializer.java

License:Open Source License

public static Object read(JsonElement in) {

    if (in.isJsonArray()) {
        List<Object> list = new ArrayList<Object>();
        JsonArray arr = in.getAsJsonArray();
        for (JsonElement anArr : arr) {
            list.add(read(anArr));//from www  . ja  v a  2s.c o  m
        }
        return list;
    } else if (in.isJsonObject()) {
        Map<String, Object> map = new LinkedTreeMap<String, Object>();
        JsonObject obj = in.getAsJsonObject();
        Set<Map.Entry<String, JsonElement>> entitySet = obj.entrySet();
        for (Map.Entry<String, JsonElement> entry : entitySet) {
            map.put(entry.getKey(), read(entry.getValue()));
        }
        return map;
    } else if (in.isJsonPrimitive()) {
        JsonPrimitive prim = in.getAsJsonPrimitive();
        if (prim.isBoolean()) {
            return prim.getAsBoolean();
        } else if (prim.isString()) {
            return prim.getAsString();
        } else if (prim.isNumber()) {
            if (prim.getAsString().contains("."))
                return prim.getAsDouble();
            else {
                return prim.getAsLong();
            }
        }
    }
    return null;
}

From source file:com.ryanantkowiak.jOptionsHouseAPI.JsonUtilities.java

License:Open Source License

/**
 * Recursively print the contents of a GSON JSON element.
 * /*from   ww  w  .  j ava  2s.  c  o  m*/
 * @param element   the GSON JSON element to be printed
 * @param prefix   output will be prefixed with this string
 */
public static void printJson(JsonElement element, String prefix) {
    if (null == prefix || prefix.isEmpty()) {
        prefix = "";
    }

    if (null == element || element.isJsonNull()) {
        System.out.println(prefix + " [null]");
        return;
    }

    else if (element.isJsonPrimitive()) {
        JsonPrimitive p = element.getAsJsonPrimitive();
        if (p.isBoolean()) {
            System.out.println(prefix + " [bool=" + p.getAsBoolean() + "]");
        } else if (p.isString()) {
            System.out.println(prefix + " [string='" + p.getAsString() + "']");
        } else if (p.isNumber()) {
            System.out.println(prefix + " [number=" + p.getAsDouble() + "]");
        }
    }

    else if (element.isJsonArray()) {
        System.out.println(prefix + " [array]");
        for (int i = 0; i < element.getAsJsonArray().size(); ++i) {
            String newPrefix = prefix + "[" + i + "]";
            printJson(element.getAsJsonArray().get(i), newPrefix);
        }
    }

    else if (element.isJsonObject()) {
        JsonObject obj = element.getAsJsonObject();

        for (Map.Entry<String, JsonElement> entry : obj.entrySet()) {
            String key = entry.getKey();
            JsonElement value = entry.getValue();
            String newPrefix = prefix + "." + key;
            printJson(value, newPrefix);
        }
    }

}

From source file:com.seleritycorp.common.base.config.ConfigUtils.java

License:Apache License

/**
 * Adds a JSON primitive to a Config.//from   www .  ja va  2  s.  c o  m
 * 
 * @param primitive The primitive to add
 * @param config The config instance to add the primitive to
 * @param key The key in the config space
 */
private static void loadJson(JsonPrimitive primitive, ConfigImpl config, String key) {
    String value = null;
    if (primitive.isBoolean()) {
        boolean bool = primitive.getAsBoolean();
        value = bool ? "true" : "false";
    } else if (primitive.isString()) {
        value = primitive.getAsString();
    } else if (primitive.isNumber()) {
        value = Double.toString(primitive.getAsDouble());
    }
    config.set(key, value);
}