Example usage for com.google.gson JsonElement getAsLong

List of usage examples for com.google.gson JsonElement getAsLong

Introduction

In this page you can find the example usage for com.google.gson JsonElement getAsLong.

Prototype

public long getAsLong() 

Source Link

Document

convenience method to get this element as a primitive long value.

Usage

From source file:org.mbari.vars.annotation.gson.DurationConverter.java

License:Apache License

@Override
public Duration deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    return Duration.ofMillis(json.getAsLong());
}

From source file:org.microworld.mangopay.implementation.serialization.InstantAdapter.java

License:Apache License

@Override
public Instant deserialize(final JsonElement json, final Type typeOfT,
        final JsonDeserializationContext context) {
    return Instant.ofEpochSecond(json.getAsLong());
}

From source file:org.microworld.mangopay.implementation.serialization.LocalDateAdapter.java

License:Apache License

@Override
public LocalDate deserialize(final JsonElement json, final Type typeOfT,
        final JsonDeserializationContext context) {
    return toLocalDate(json.getAsLong());
}

From source file:org.mitre.provenance.plusobject.json.JsonObjectPropertyWrapper.java

License:Apache License

public Object getProperty(String key) {
    if (!obj.has(key))
        return null;
    //JsonPrimitive p = obj.get(key).getAsJsonPrimitive();
    JsonElement elem = obj.get(key);

    if (elem.isJsonPrimitive() && ((JsonPrimitive) elem).isString())
        return elem.getAsString();
    if (elem.isJsonPrimitive() && ((JsonPrimitive) elem).isNumber())
        return elem.getAsLong();

    if (elem.isJsonArray()) {
        JsonArray arr = (JsonArray) elem;

        String[] r = new String[arr.size()];

        for (int x = 0; x < arr.size(); x++) {
            r[x] = arr.get(x).getAsString();
        }/*from   ww  w. j a  va2s  .c  o m*/

        return r;
    }

    // Default case.
    return elem.getAsString();
}

From source file:org.mitre.util.JsonUtils.java

License:Apache License

/**
 * Gets the value of the given member as a Long, null if it doesn't exist
 *//*from  ww w  .ja  v  a  2  s .com*/
public static Long getAsLong(JsonObject o, String member) {
    if (o.has(member)) {
        JsonElement e = o.get(member);
        if (e != null && e.isJsonPrimitive()) {
            return e.getAsLong();
        } else {
            return null;
        }
    } else {
        return null;
    }
}

From source file:org.netbeans.rest.application.config.GsonMessageBodyHandler.java

private Gson getGson() {
    if (gson == null) {
        JsonSerializer<Date> ser = new JsonSerializer<Date>() {

            @Override//from   w ww .  ja va 2  s  .c o m
            public JsonElement serialize(Date src, Type type, JsonSerializationContext jsc) {
                return src == null ? null : new JsonPrimitive(src.getTime());
            }

        };

        JsonDeserializer<Date> deser = new JsonDeserializer<Date>() {

            @Override
            public Date deserialize(JsonElement json, Type type, JsonDeserializationContext jdc)
                    throws JsonParseException {
                return json == null ? null : new Date(json.getAsLong());
            }
        };

        final GsonBuilder gsonBuilder = new GsonBuilder();
        gson = gsonBuilder.registerTypeAdapter(Date.class, ser).registerTypeAdapter(Date.class, deser).create();
    }
    return gson;
}

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

License:Apache License

/** Note: this comparator
 * imposes orderings that are inconsistent with equals.
 *///w w w .  ja  va 2s  . c  o m
@Override
public int compare(JsonElement o1, JsonElement o2) {
    // short-circuit in this case
    if (o1 == o2) {
        return 0;
    }

    DType typ1 = getType(o1);
    DType typ2 = getType(o2);

    // For lazily parsed numbers, check type of other operand and try and coerce
    if (typ1 == DType.DT_LAZYNUM) {
        switch (typ2) {
        case DT_LONG:
            return Long.compare(o1.getAsLong(), o2.getAsLong());
        case DT_DOUBLE:
            return Double.compare(o1.getAsDouble(), o2.getAsDouble());
        case DT_LAZYNUM:
            // Tricky here... there is no way to know what to coerce to,
            // underlying gson code relies on string equality, hence so do we
            typ1 = DType.DT_STRING;
            typ2 = DType.DT_STRING;
        }
    } else if (typ2 == DType.DT_LAZYNUM) {
        switch (typ1) {
        case DT_LONG:
            return Long.compare(o1.getAsLong(), o2.getAsLong());
        case DT_DOUBLE:
            return Double.compare(o1.getAsDouble(), o2.getAsDouble());
        }
    }

    final int typCompare = typ1.compareTo(typ2);
    if (typCompare != 0) {
        return typCompare;
    }

    switch (typ1) {
    case DT_JNULL:
    case DT_NULL:
        return 0;
    case DT_BOOL:
        return Boolean.compare(o1.getAsBoolean(), o2.getAsBoolean());
    case DT_STRING:
        String str1 = o1.getAsString();
        String str2 = o2.getAsString();
        // TODO
        // WARNING 
        // HACK
        // special hack for dates.
        // what could go wrong??? :-D
        // of course, this breaks the transitivity of compareTo
        // sigh...
        // a real solution to this is a bit challenging
        // since we need type information
        // or a wrapper around date times.
        try {
            final ZonedDateTime date1 = ZonedDateTime.parse(str1);
            final ZonedDateTime date2 = ZonedDateTime.parse(str2);
            // if they are both parseable as dates, we will compare them as dates
            return date1.toInstant().compareTo(date2.toInstant());
        } catch (DateTimeException e) {
            // If they are not both parseable as dates, just compare them as strings
            return str1.compareTo(str2);
        }
    case DT_LONG:
        return Long.compare(o1.getAsLong(), o2.getAsLong());
    case DT_DOUBLE:
        return Double.compare(o1.getAsDouble(), o2.getAsDouble());
    case DT_COLL:
        return compare(o1.getAsJsonArray(), o2.getAsJsonArray());
    case DT_REC:
        return compare(o1.getAsJsonObject(), o2.getAsJsonObject());
    default:
        // We should never get here.
        // but if we do, we can use toString to give
        // a deterministic order
        return o1.toString().compareTo(o2.toString());
    }
}

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

License:Apache License

public static long asLong(JsonElement e) {
    return e.getAsLong();
}

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

License:Apache License

public static JsonElement abs(JsonElement e) {
    return new JsonPrimitive(Math.abs(e.getAsLong()));
}

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

License:Apache License

public static JsonElement log2(JsonElement e) {
    return new JsonPrimitive(Math.log(e.getAsLong()) / Math.log(2));
}