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.commoncrawl.mapred.ec2.postprocess.linkCollector.LinkMergerJob.java

License:Open Source License

static long safeSetMaxLongValue(JsonObject jsonObj, String property, long newValue) {
    JsonElement element = jsonObj.get(property);
    if (element != null) {
        if (element.getAsLong() > newValue) {
            return element.getAsLong();
        }// ww  w .j  av a2  s  .c  o  m
    }
    jsonObj.addProperty(property, newValue);
    return newValue;
}

From source file:org.commoncrawl.mapred.ec2.postprocess.linkCollector.LinkMergerJob.java

License:Open Source License

static long safeSetMinLongValue(JsonObject jsonObj, String property, long newValue) {
    JsonElement element = jsonObj.get(property);
    if (element != null) {
        if (newValue > element.getAsLong()) {
            return element.getAsLong();
        }//from   w w  w.ja  v  a 2s .  c  om
    }
    jsonObj.addProperty(property, newValue);
    return newValue;
}

From source file:org.commoncrawl.mapred.ec2.postprocess.linkCollector.LinkMergerJob.java

License:Open Source License

void addMinMaxFeedItemTimes(JsonObject contentObj, JsonObject crawlStatsJSON) {
    JsonArray items = contentObj.getAsJsonArray("items");

    if (items != null) {
        long minPubDate = -1L;
        long maxPubDate = -1L;
        int itemCount = 0;

        for (JsonElement item : items) {
            long pubDateValue = -1;
            JsonElement pubDate = item.getAsJsonObject().get("published");

            if (pubDate != null) {
                pubDateValue = pubDate.getAsLong();
            }//from  w  w  w.  ja v a2  s .c  o m
            JsonElement updateDate = item.getAsJsonObject().get("updated");
            if (updateDate != null) {
                if (updateDate.getAsLong() > pubDateValue) {
                    pubDateValue = updateDate.getAsLong();
                }
            }

            if (minPubDate == -1L || pubDateValue < minPubDate) {
                minPubDate = pubDateValue;
            }
            if (maxPubDate == -1L || pubDateValue > maxPubDate) {
                maxPubDate = pubDateValue;
            }
            itemCount++;
        }
        crawlStatsJSON.addProperty("minPubDate", minPubDate);
        crawlStatsJSON.addProperty("maxPubDate", maxPubDate);
        crawlStatsJSON.addProperty("itemCount", itemCount);
    }
}

From source file:org.displaytag.model.CustomColumnData.java

License:Artistic License

private Long getLongValue(JsonObject jsonObj, String name) {
    if (jsonObj.has(name)) {
        JsonElement e = jsonObj.get(name);
        return e != null ? e.getAsLong() : null;
    }//from   w ww .j  a v  a 2  s .  c  o  m
    return 0L;
}

From source file:org.eclipse.scada.base.json.VariantJsonDeserializer.java

License:Open Source License

@Override
public Variant deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context)
        throws JsonParseException {
    if (json.isJsonNull()) {
        return null;
    }//from w w  w  .j  a v a  2s.  c o  m

    if (json instanceof JsonPrimitive) {
        return decodeFromPrimitive(json);
    }

    if (json instanceof JsonObject) {
        final JsonObject jsonObj = (JsonObject) json;
        final JsonElement type = jsonObj.get(VariantJson.FIELD_TYPE);
        final JsonElement value = jsonObj.get(VariantJson.FIELD_VALUE);

        if (type == null || type.isJsonNull()) {
            if (value == null) {
                throw new JsonParseException(String.format("Variant encoded as object must have a field '%s'",
                        VariantJson.FIELD_VALUE));
            }
            return Variant.valueOf(value.getAsString());
        }

        if (!type.isJsonPrimitive()) {
            throw new JsonParseException(
                    String.format("Variant field '%s' must be a string containing the variant type (%s)",
                            VariantJson.FIELD_TYPE, StringHelper.join(VariantType.values(), ", ")));
        }

        final String typeStr = type.getAsString();

        if (typeStr.equals("NULL")) {
            return Variant.NULL;
        }

        if (value == null || value.isJsonNull()) {
            throw new JsonParseException(String.format(
                    "The type '%s' does not support a null value. Use variant type NULL instead.", typeStr));
        }

        if (value.isJsonObject() || value.isJsonArray()) {
            throw new JsonParseException(
                    "The variant value must be a JSON primitive matching the type. Arrays and objects are not supported");
        }

        switch (type.getAsString()) {
        case "BOOLEAN":
            return Variant.valueOf(value.getAsBoolean());
        case "STRING":
            return Variant.valueOf(value.getAsString());
        case "DOUBLE":
            return Variant.valueOf(value.getAsDouble());
        case "INT32":
            return Variant.valueOf(value.getAsInt());
        case "INT64":
            return Variant.valueOf(value.getAsLong());
        default:
            throw new JsonParseException(String.format("Type '%s' is unknown (known types: %s)",
                    StringHelper.join(VariantType.values(), ", ")));
        }
    }

    throw new JsonParseException("Unknown serialization of Variant type");
}

From source file:org.graylog2.indexer.gson.GsonUtils.java

License:Open Source License

@Nullable
public static Long asLong(JsonElement jsonElement) {
    return jsonElement instanceof JsonPrimitive && ((JsonPrimitive) jsonElement).isNumber()
            ? jsonElement.getAsLong()
            : null;// ww w . j a  va  2 s  .  co m
}

From source file:org.greenrobot.eventbus.EventBus.java

License:Apache License

private Object getValue(Method.Data data, JsonObject json) {
    if (data == null)
        return null;

    String id = data.getId();// w  ww.  j a va  2 s .  c  o  m
    Class<?> clazz = data.getType();
    boolean isNull = data.getNull();

    JsonElement value = json == null ? new JsonNull() : json.get(id);
    if (!isNull && value.isJsonNull())
        throw new EventBusException(
                "Method.Data[" + data + "], the id[" + id + "] has null value to give " + data.getType());

    if (!value.isJsonNull()) {
        if (clazz == Boolean.class) {
            return value.getAsBoolean();
        } else if (clazz == Integer.class) {
            return value.getAsInt();
        } else if (clazz == Long.class) {
            return value.getAsLong();
        } else if (clazz == Float.class) {
            return value.getAsFloat();
        } else if (clazz == Double.class) {
            return value.getAsDouble();
        } else if (clazz == String.class) {
            return value.getAsString();
        } else if (clazz == Byte.class) {
            return value.getAsByte();
        } else if (clazz == char.class) {
            return value.getAsCharacter();
        } else {
            return new Gson().fromJson(value, clazz);
        }
    }
    return null;
}

From source file:org.greenrobot.eventbus.EventBus.java

License:Apache License

private Bundle getBundleByPage(Page page, JsonObject json) throws Exception {
    if (page != null && json != null && page.getBundleList().size() > 0) {
        Bundle bundle = new Bundle();
        for (Page.Bundle item : page.getBundleList()) {
            String id = item.getId();
            String key = item.getKey();
            Class<? extends Serializable> clazz = item.getType();
            boolean isNull = item.getNull();

            JsonElement value = json.get(id);
            if (!isNull && value.isJsonNull())
                throw new EventBusException(
                        "Page.Bundle[" + item + "], the id[" + id + "] has null value to give " + key);

            if (!value.isJsonNull()) {
                if (clazz == Boolean.class) {
                    bundle.putBoolean(key, value.getAsBoolean());
                } else if (clazz == Integer.class) {
                    bundle.putInt(key, value.getAsInt());
                } else if (clazz == Long.class) {
                    bundle.putLong(key, value.getAsLong());
                } else if (clazz == Float.class) {
                    bundle.putFloat(key, value.getAsFloat());
                } else if (clazz == Double.class) {
                    bundle.putDouble(key, value.getAsDouble());
                } else if (clazz == String.class) {
                    bundle.putString(key, value.getAsString());
                } else if (clazz == Byte.class) {
                    bundle.putByte(key, value.getAsByte());
                } else if (clazz == char.class) {
                    bundle.putChar(key, value.getAsCharacter());
                } else {
                    bundle.putSerializable(key, new Gson().fromJson(value, clazz));
                }//  w ww . j a va  2s.  c o m
            }
        }
        return bundle;
    }
    return null;
}

From source file:org.hibernate.search.elasticsearch.query.impl.PrimitiveProjection.java

License:LGPL

public void addDocumentField(Document tmp, JsonElement jsonValue) {
    if (jsonValue == null || jsonValue.isJsonNull()) {
        return;/* www .j a v a2s.c  o  m*/
    }
    switch (fieldType) {
    case INTEGER:
        tmp.add(new IntField(absoluteName, jsonValue.getAsInt(), Store.NO));
        break;
    case LONG:
        tmp.add(new LongField(absoluteName, jsonValue.getAsLong(), Store.NO));
        break;
    case FLOAT:
        tmp.add(new FloatField(absoluteName, jsonValue.getAsFloat(), Store.NO));
        break;
    case DOUBLE:
        tmp.add(new DoubleField(absoluteName, jsonValue.getAsDouble(), Store.NO));
        break;
    case UNKNOWN_NUMERIC:
        throw LOG.unexpectedNumericEncodingType(rootTypeMetadata.getType(), absoluteName);
    case BOOLEAN:
        tmp.add(new StringField(absoluteName, String.valueOf(jsonValue.getAsBoolean()), Store.NO));
        break;
    default:
        tmp.add(new StringField(absoluteName, jsonValue.getAsString(), Store.NO));
        break;
    }
}

From source file:org.hibernate.search.elasticsearch.query.impl.PrimitiveProjection.java

License:LGPL

@Override
public Object convertHit(JsonObject hit, ConversionContext conversionContext) {
    JsonElement jsonValue = extractFieldValue(hit.get("_source").getAsJsonObject(), absoluteName);
    if (jsonValue == null || jsonValue.isJsonNull()) {
        return null;
    }//from   w  ww  .ja v  a2  s.  com
    switch (fieldType) {
    case INTEGER:
        return jsonValue.getAsInt();
    case LONG:
        return jsonValue.getAsLong();
    case FLOAT:
        return jsonValue.getAsFloat();
    case DOUBLE:
        return jsonValue.getAsDouble();
    case UNKNOWN_NUMERIC:
        throw LOG.unexpectedNumericEncodingType(rootTypeMetadata.getType(), absoluteName);
    case BOOLEAN:
        return jsonValue.getAsBoolean();
    default:
        return jsonValue.getAsString();
    }
}