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:at.tugraz.kmi.medokyservice.fca.util.ImportExport.java

License:Open Source License

private Map<Long, FCAItemMetadata> json2Metadata(JsonObject json, Map<Long, LearningObject> learningObjects) {
    Map<Long, FCAItemMetadata> result = new HashMap<Long, FCAItemMetadata>();
    JsonObject jsM = json.get(SECTION_M).getAsJsonObject();
    for (Entry<String, JsonElement> entry : jsM.entrySet()) {
        JsonObject val = entry.getValue().getAsJsonObject();
        JsonArray arr = val.get(SECTION_LO).getAsJsonArray();
        LinkedHashSet<LearningObject> l_objsByLearner = new LinkedHashSet<LearningObject>();
        try {/*  w  ww.ja  v a 2  s . c  om*/
            JsonArray arrByLearner = val.get(SECTION_LO_L).getAsJsonArray();
            for (JsonElement loID : arrByLearner)
                l_objsByLearner.add(learningObjects.get(loID.getAsLong()));
        } catch (Exception notAnError) {

        }
        LinkedHashSet<LearningObject> l_objs = new LinkedHashSet<LearningObject>();
        for (JsonElement loID : arr)
            l_objs.add(learningObjects.get(loID.getAsLong()));
        clean(l_objs);
        clean(l_objsByLearner);
        result.put(Long.parseLong(entry.getKey()), new FCAItemMetadata(val.get(DESCRIPTION).getAsString(),
                val.get(O_ID).getAsLong(), l_objs, l_objsByLearner));
    }
    return result;
}

From source file:brooklyn.event.feed.http.JsonFunctions.java

License:Apache License

@SuppressWarnings("unchecked")
public static <T> Function<JsonElement, T> cast(final Class<T> expected) {
    return new Function<JsonElement, T>() {
        @Override/*from  ww  w.j  a  v a 2  s.com*/
        public T apply(JsonElement input) {
            if (input == null) {
                return (T) null;
            } else if (input.isJsonNull()) {
                return (T) null;
            } else if (expected == boolean.class || expected == Boolean.class) {
                return (T) (Boolean) input.getAsBoolean();
            } else if (expected == char.class || expected == Character.class) {
                return (T) (Character) input.getAsCharacter();
            } else if (expected == byte.class || expected == Byte.class) {
                return (T) (Byte) input.getAsByte();
            } else if (expected == short.class || expected == Short.class) {
                return (T) (Short) input.getAsShort();
            } else if (expected == int.class || expected == Integer.class) {
                return (T) (Integer) input.getAsInt();
            } else if (expected == long.class || expected == Long.class) {
                return (T) (Long) input.getAsLong();
            } else if (expected == float.class || expected == Float.class) {
                return (T) (Float) input.getAsFloat();
            } else if (expected == double.class || expected == Double.class) {
                return (T) (Double) input.getAsDouble();
            } else if (expected == BigDecimal.class) {
                return (T) input.getAsBigDecimal();
            } else if (expected == BigInteger.class) {
                return (T) input.getAsBigInteger();
            } else if (Number.class.isAssignableFrom(expected)) {
                // TODO Will result in a class-cast if it's an unexpected sub-type of Number not handled above
                return (T) input.getAsNumber();
            } else if (expected == String.class) {
                return (T) input.getAsString();
            } else if (expected.isArray()) {
                JsonArray array = input.getAsJsonArray();
                Class<?> componentType = expected.getComponentType();
                if (JsonElement.class.isAssignableFrom(componentType)) {
                    JsonElement[] result = new JsonElement[array.size()];
                    for (int i = 0; i < array.size(); i++) {
                        result[i] = array.get(i);
                    }
                    return (T) result;
                } else {
                    Object[] result = (Object[]) Array.newInstance(componentType, array.size());
                    for (int i = 0; i < array.size(); i++) {
                        result[i] = cast(componentType).apply(array.get(i));
                    }
                    return (T) result;
                }
            } else {
                throw new IllegalArgumentException("Cannot cast json element to type " + expected);
            }
        }
    };
}

From source file:ch.cern.db.flume.sink.kite.parser.JSONtoAvroParser.java

License:GNU General Public License

private Object getElementAsType(Schema schema, JsonElement element) {
    if (element == null || element.isJsonNull())
        return null;

    switch (schema.getType()) {
    case BOOLEAN:
        return element.getAsBoolean();
    case DOUBLE:/*from   w  w w  .  j av  a2  s .  co  m*/
        return element.getAsDouble();
    case FLOAT:
        return element.getAsFloat();
    case INT:
        return element.getAsInt();
    case LONG:
        return element.getAsLong();
    case NULL:
        return null;
    case UNION:
        return getElementAsType(schema.getTypes().get(0), element);
    //      case FIXED:
    //      case ARRAY:
    //      case BYTES:
    //      case ENUM:
    //      case MAP:
    //      case RECORD:
    //      case STRING:
    default:
        return element.getAsString();
    }
}

From source file:co.aurasphere.botmill.telegram.internal.util.json.CalendarFromTimestampJsonDeserializer.java

License:Open Source License

public Calendar deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    long timestamp = json.getAsLong();
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(timestamp);
    return calendar;
}

From source file:com.asakusafw.testdriver.json.JsonObjectDriver.java

License:Apache License

@Override
public void longProperty(PropertyName name, JsonObject context) throws IOException {
    JsonElement prop = property(context, name);
    if (prop == null) {
        return;/*from   ww  w .j  a  va 2s .c o m*/
    }
    builder.add(name, prop.getAsLong());
}

From source file:com.balajeetm.mystique.util.gson.lever.JsonLever.java

License:Open Source License

/**
 * Returns the source json as a long if possible. Else returns the default long
 *
 * @param source the source json element
 * @param defaultLong the default long// w w w  .  j av  a  2s .c o m
 * @return the source json as a long
 */
public Long asLong(JsonElement source, Long defaultLong) {
    return isNumber(source) ? (Long) source.getAsLong() : defaultLong;
}

From source file:com.ccc.crest.core.cache.crest.alliance.Alliance.java

License:Open Source License

@Override
public Alliance deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    Iterator<Entry<String, JsonElement>> objectIter = ((JsonObject) json).entrySet().iterator();
    while (objectIter.hasNext()) {
        Entry<String, JsonElement> objectEntry = objectIter.next();
        String key = objectEntry.getKey();
        JsonElement value = objectEntry.getValue();
        if (IdStrKey.equals(key))
            idStr = value.getAsString();
        else if (ShortNameKey.equals(key))
            shortName = value.getAsString();
        else if (HrefKey.equals(key))
            url = value.getAsString();/* w w  w .j a  v a  2  s.c  o  m*/
        else if (IdKey.equals(key))
            id = value.getAsLong();
        else if (NameKey.equals(key))
            name = value.getAsString();
        else
            LoggerFactory.getLogger(getClass())
                    .warn(key + " has a field not currently being handled: \n" + objectEntry.toString());
    }
    return this;
}

From source file:com.ccc.crest.core.cache.crest.corporation.Corporation.java

License:Open Source License

@Override
public Corporation deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    Iterator<Entry<String, JsonElement>> objectIter = ((JsonObject) json).entrySet().iterator();
    while (objectIter.hasNext()) {
        Entry<String, JsonElement> objectEntry = objectIter.next();
        String key = objectEntry.getKey();
        JsonElement value = objectEntry.getValue();
        if (DescriptionKey.equals(key))
            description = value.getAsString();
        else if (HeadquartersKey.equals(key)) {
            headquarters = new Headquarters();
            headquarters.deserialize(value, typeOfT, context);
        } else if (UrlKey.equals(key))
            corpUrl = value.getAsString();
        else if (IdStrKey.equals(key))
            idStr = value.getAsString();
        else if (LoyaltyKey.equals(key)) {
            ExternalRef ref = new ExternalRef();
            ref.deserialize(value, typeOfT, context);
            loyaltyUrl = ref.url;//  ww w.  jav a  2  s .c o m
        } else if (TickerKey.equals(key))
            ticker = value.getAsString();
        else if (IdStrKey.equals(key))
            idStr = value.getAsString();
        else if (IdKey.equals(key))
            id = value.getAsLong();
        else if (NameKey.equals(key))
            name = value.getAsString();
        else
            LoggerFactory.getLogger(getClass())
                    .warn(key + " has a field not currently being handled: \n" + objectEntry.toString());
    }
    return this;
}

From source file:com.ccc.crest.core.cache.crest.dogma.DogmaAttribute.java

License:Open Source License

@Override
public DogmaAttribute deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    Iterator<Entry<String, JsonElement>> objectIter = ((JsonObject) json).entrySet().iterator();
    while (objectIter.hasNext()) {
        Entry<String, JsonElement> objectEntry = objectIter.next();
        String key = objectEntry.getKey();
        JsonElement value = objectEntry.getValue();
        if (DescriptionKey.equals(key))
            description = value.getAsString();
        else if (HeadquartersKey.equals(key)) {
            ;/*www.  ja v  a  2 s.  c o  m*/
        } else if (UrlKey.equals(key))
            corpUrl = value.getAsString();
        else if (IdStrKey.equals(key))
            idStr = value.getAsString();
        else if (LoyaltyKey.equals(key)) {
            ExternalRef ref = new ExternalRef();
            ref.deserialize(value, typeOfT, context);
            loyaltyUrl = ref.url;
        } else if (TickerKey.equals(key))
            ticker = value.getAsString();
        else if (IdStrKey.equals(key))
            idStr = value.getAsString();
        else if (IdKey.equals(key))
            id = value.getAsLong();
        else if (NameKey.equals(key))
            name = value.getAsString();
        else
            LoggerFactory.getLogger(getClass())
                    .warn(key + " has a field not currently being handled: \n" + objectEntry.toString());
    }
    return this;
}

From source file:com.ccc.crest.core.cache.crest.Paging.java

License:Open Source License

private boolean pagingDeserialize(String key, JsonElement value) throws JsonParseException {
    if (TotalCountStringKey.equals(key))
        return true;
    if (PageCountKey.equals(key)) {
        pageCount = value.getAsLong();
        return true;
    }/*ww  w .  j  av  a 2  s .  co m*/
    if (NextKey.equals(key)) {
        next = new ExternalRef();
        next.deserialize(value, null, null);
        return true;
    } else if (PreviousKey.equals(key)) {
        previous = new ExternalRef();
        previous.deserialize(value, null, null);
        return true;
    }
    if (TotalCountKey.equals(key)) {
        totalCount = value.getAsLong();
        return true;
    } else if (PageCountStringKey.equals(key))
        return true;
    return false;
}