Example usage for com.google.gson JsonPrimitive getAsBoolean

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

Introduction

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

Prototype

@Override
public boolean getAsBoolean() 

Source Link

Document

convenience method to get this element as a boolean value.

Usage

From source file:org.modelmapper.gson.JsonElementValueReader.java

License:Apache License

public Object get(JsonElement source, String memberName) {
    if (source.isJsonObject()) {
        JsonObject subjObj = source.getAsJsonObject();
        JsonElement propertyElement = subjObj.get(memberName);
        if (propertyElement == null)
            throw new IllegalArgumentException();

        if (propertyElement.isJsonObject())
            return propertyElement.getAsJsonObject();
        if (propertyElement.isJsonArray())
            return propertyElement.getAsJsonArray();
        if (propertyElement.isJsonPrimitive()) {
            JsonPrimitive jsonPrim = propertyElement.getAsJsonPrimitive();
            if (jsonPrim.isBoolean())
                return jsonPrim.getAsBoolean();
            if (jsonPrim.isNumber())
                return jsonPrim.getAsNumber();
            if (jsonPrim.isString())
                return jsonPrim.getAsString();
        }//from  w w  w . jav  a2  s  .co m
    }

    return null;
}

From source file:org.nordapp.web.util.GsonHashMapDeserializer.java

License:Apache License

private Object handlePrimitive(JsonPrimitive json) {
    if (json.isBoolean())
        return json.getAsBoolean();
    else if (json.isString())
        return json.getAsString();
    else {/* w  ww  .  j a  v a  2 s .  c o  m*/
        BigDecimal bigDec = json.getAsBigDecimal();
        if (json.getAsString().indexOf('.') == -1) {
            // Find out if it is an int type
            try {
                return bigDec.longValue();
            } catch (ArithmeticException e) {
            }
        }
        // Just return it as a double
        return bigDec.doubleValue();
    }
}

From source file:org.opendolphin.core.comm.JsonCodec.java

License:Apache License

private Object toValidValue(JsonElement jsonElement) {
    if (jsonElement.isJsonNull()) {
        return null;
    } else if (jsonElement.isJsonPrimitive()) {
        JsonPrimitive primitive = jsonElement.getAsJsonPrimitive();
        if (primitive.isBoolean()) {
            return primitive.getAsBoolean();
        } else if (primitive.isString()) {
            return primitive.getAsString();
        } else {//  w w  w.j av  a 2  s .  c o m
            return primitive.getAsNumber();
        }
    } else if (jsonElement.isJsonObject()) {
        JsonObject jsonObject = jsonElement.getAsJsonObject();
        if (jsonObject.has(Date.class.toString())) {
            try {
                return new SimpleDateFormat(ISO8601_FORMAT)
                        .parse(jsonObject.getAsJsonPrimitive(Date.class.toString()).getAsString());
            } catch (Exception e) {
                throw new RuntimeException("Can not converte!", e);
            }
        } else if (jsonObject.has(BigDecimal.class.toString())) {
            try {
                return new BigDecimal(jsonObject.getAsJsonPrimitive(BigDecimal.class.toString()).getAsString());
            } catch (Exception e) {
                throw new RuntimeException("Can not converte!", e);
            }
        } else if (jsonObject.has(Float.class.toString())) {
            try {
                return Float.valueOf(jsonObject.getAsJsonPrimitive(Float.class.toString()).getAsString());
            } catch (Exception e) {
                throw new RuntimeException("Can not converte!", e);
            }
        } else if (jsonObject.has(Double.class.toString())) {
            try {
                return Double.valueOf(jsonObject.getAsJsonPrimitive(Double.class.toString()).getAsString());
            } catch (Exception e) {
                throw new RuntimeException("Can not converte!", e);
            }
        }
    }
    throw new RuntimeException("Can not converte!");
}

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 va2 s.c  o  m*/
            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();
        }// ww  w  .  j  a va2s.com
        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;
    }//from  w w w  .  j  a v a2 s. c  om
    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.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// w ww . j  a  v  a  2  s . c om
 */
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.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 ww  w. ja  v a2  s  .  c om*/
        }
        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  ww . j  a  va  2 s  .  c  om*/
            } 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.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 {/* www.j a  va 2  s  .  c o m*/

            return number.longValue();

        }

    } else if (p.isBoolean()) {

        return p.getAsBoolean();

    }

    return p.getAsString();
}