Example usage for com.google.gson JsonPrimitive isBoolean

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

Introduction

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

Prototype

public boolean isBoolean() 

Source Link

Document

Check whether this primitive contains a boolean value.

Usage

From source file:com.canoo.dolphin.impl.codec.ValueEncoder.java

License:Apache License

static Object decodeValue(JsonElement jsonElement) {
    if (jsonElement == null || jsonElement.isJsonNull()) {
        return null;
    }/*  w w  w .  ja v  a 2  s  .  c  o  m*/
    if (!jsonElement.isJsonPrimitive()) {
        throw new JsonParseException("Illegal JSON detected");
    }
    final JsonPrimitive value = (JsonPrimitive) jsonElement;

    if (value.isString()) {
        return value.getAsString();
    } else if (value.isBoolean()) {
        return value.getAsBoolean();
    } else if (value.isNumber()) {
        return value.getAsNumber();
    }
    throw new JsonParseException("Currently only String, Boolean, or Number are allowed as primitives");
}

From source file:com.cloudbase.CBNaturalDeserializer.java

License:Open Source License

private Object handlePrimitive(JsonPrimitive json) {
    if (json.isBoolean())
        return json.getAsBoolean();
    else if (json.isString())
        return json.getAsString();
    else {//w  w w. ja  v a2  s  . co m
        BigDecimal bigDec = json.getAsBigDecimal();
        // Find out if it is an int type
        try {
            bigDec.toBigIntegerExact();
            try {
                return bigDec.intValueExact();
            } catch (ArithmeticException e) {
            }
            return bigDec.longValue();
        } catch (ArithmeticException e) {
        }
        // Just return it as a double
        return bigDec.doubleValue();
    }
}

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
 *//*from w  ww . ja  va  2  s  . c  om*/
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.facebook.buck.json.RawParser.java

License:Apache License

/**
 * @return One of: String, Boolean, Long, Number, List<Object>, Map<String, Object>.
 *///  w  ww.  j  a va  2s.  c  o  m
@Nullable
@VisibleForTesting
static Object toRawTypes(JsonElement json) {
    // Cases are ordered from most common to least common.
    if (json.isJsonPrimitive()) {
        JsonPrimitive primitive = json.getAsJsonPrimitive();
        if (primitive.isString()) {
            return interner.intern(primitive.getAsString());
        } else if (primitive.isBoolean()) {
            return primitive.getAsBoolean();
        } else if (primitive.isNumber()) {
            Number number = primitive.getAsNumber();
            // Number is likely an instance of class com.google.gson.internal.LazilyParsedNumber.
            if (number.longValue() == number.doubleValue()) {
                return number.longValue();
            } else {
                return number;
            }
        } else {
            throw new IllegalStateException("Unknown primitive type: " + primitive);
        }
    } else if (json.isJsonArray()) {
        JsonArray array = json.getAsJsonArray();
        List<Object> out = Lists.newArrayListWithCapacity(array.size());
        for (JsonElement item : array) {
            out.add(toRawTypes(item));
        }
        return out;
    } else if (json.isJsonObject()) {
        Map<String, Object> out = new LinkedHashMap<>(json.getAsJsonObject().entrySet().size());
        for (Map.Entry<String, JsonElement> entry : json.getAsJsonObject().entrySet()) {
            // On a large project, without invoking intern(), we have seen `buck targets` OOM. When this
            // happened, according to the .hprof file generated using -XX:+HeapDumpOnOutOfMemoryError,
            // 39.6% of the memory was spent on char[] objects while 14.5% was spent on Strings.
            // (Another 10.5% was spent on java.util.HashMap$Entry.) Introducing intern() stopped the
            // OOM from happening.
            out.put(interner.intern(entry.getKey()), toRawTypes(entry.getValue()));
        }
        return out;
    } else if (json.isJsonNull()) {
        return null;
    } else {
        throw new IllegalStateException("Unknown type: " + json);
    }
}

From source file:com.flipkart.polyguice.config.JsonConfiguration.java

License:Apache License

private void flatten(String prefix, JsonElement element) {
    if (element.isJsonPrimitive()) {
        JsonPrimitive jsonPrim = element.getAsJsonPrimitive();
        if (jsonPrim.isBoolean()) {
            configTab.put(prefix, jsonPrim.getAsBoolean());
        } else if (jsonPrim.isNumber()) {
            configTab.put(prefix, jsonPrim.getAsNumber());
        } else if (jsonPrim.isString()) {
            configTab.put(prefix, jsonPrim.getAsString());
        }//  ww  w .j  a v a  2 s .c  o  m
    } else if (element.isJsonObject()) {
        JsonObject jsonObj = element.getAsJsonObject();
        for (Map.Entry<String, JsonElement> entry : jsonObj.entrySet()) {
            String prefix1 = ((prefix != null) ? prefix + "." : "") + entry.getKey();
            flatten(prefix1, entry.getValue());
        }
    }
}

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 .j  a  v a  2s  .  com
            return jp.getAsLong();
        }
    } else if (jp.isString()) {
        return jp.getAsString();
    }
    return obj;
}

From source file:com.getperka.flatpack.codexes.DynamicCodex.java

License:Apache License

/**
 * Attempt to infer the type from the JsonElement presented.
 * <ul>/*ww  w .  ja v a  2s  .c o m*/
 * <li>boolean -> {@link Boolean}
 * <li>number -> {@link BigDecimal}
 * <li>string -> {@link HasUuid} or {@link String}
 * <li>array -> {@link ListCodex}
 * <li>object -> {@link StringMapCodex}
 * </ul>
 */
@Override
public Object readNotNull(JsonElement element, DeserializationContext context) throws Exception {
    if (element.isJsonPrimitive()) {
        JsonPrimitive primitive = element.getAsJsonPrimitive();
        if (primitive.isBoolean()) {
            return primitive.getAsBoolean();
        } else if (primitive.isNumber()) {
            // Always return numbers as BigDecimals for consistency
            return primitive.getAsBigDecimal();
        } else {
            String value = primitive.getAsString();

            // Interpret UUIDs as entity references
            if (UUID_PATTERN.matcher(value).matches()) {
                UUID uuid = UUID.fromString(value);
                HasUuid entity = context.getEntity(uuid);
                if (entity != null) {
                    return entity;
                }
            }

            return value;
        }
    } else if (element.isJsonArray()) {
        return listCodex.get().readNotNull(element, context);
    } else if (element.isJsonObject()) {
        return mapCodex.get().readNotNull(element, context);
    }
    context.fail(new UnsupportedOperationException("Cannot infer data type for " + element.toString()));
    return null;
}

From source file:com.github.strawberry.util.Json.java

License:Open Source License

public static Object parsePrimitive(JsonElement e) {
    JsonPrimitive p = e.getAsJsonPrimitive();
    if (p.isString()) {
        return e.getAsString();
    }//  w w  w  . j  a va2 s .  co  m
    if (p.isBoolean()) {
        return e.getAsBoolean();
    }
    if (p.isNumber()) {
        return e.getAsInt();
    }
    return p.getAsString();
}

From source file:com.github.zhizheng.json.JsonValueTypes.java

License:Apache License

/**
 *  Json //w  w  w .  j av a 2  s.  c om
 * 
 * @param jsonElement
 * @return
 */
public static String getJsonValueType(JsonElement jsonElement) {
    if (jsonElement.isJsonObject()) {
        return OBJECT.toString();
    }
    if (jsonElement.isJsonArray()) {
        return ARRAY.toString();
    }
    if (jsonElement.isJsonPrimitive()) {
        JsonPrimitive asJsonPrimitive = jsonElement.getAsJsonPrimitive();
        if (asJsonPrimitive.isBoolean()) {
            return BOOLEAN.toString();
        }
        if (asJsonPrimitive.isNumber()) {
            return NUMBER.toString();
        }
        return STRING.toString();
    }
    return NULL.toString();
}

From source file:com.google.iosched.model.DataExtractor.java

License:Open Source License

private boolean isHiddenSession(JsonObject sessionObj) {
    JsonPrimitive hide = getMapValue(get(sessionObj, VendorAPISource.Topics.info),
            InputJsonKeys.VendorAPISource.Topics.INFO_HIDDEN_SESSION, Converters.BOOLEAN, null);
    if (hide != null && hide.isBoolean() && hide.getAsBoolean()) {
        return true;
    }/*w  w  w  . j a  v a 2s . com*/
    return false;
}