Example usage for com.google.gson JsonPrimitive getAsLong

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

Introduction

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

Prototype

@Override
public long getAsLong() 

Source Link

Document

convenience method to get this element as a primitive long.

Usage

From source file:com.indragie.cmput301as1.DateJSONDeserializer.java

License:Open Source License

@Override
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (json.isJsonPrimitive()) {
        JsonPrimitive primitive = json.getAsJsonPrimitive();
        if (primitive.isNumber()) {
            return new Date(primitive.getAsLong());
        }/*from  w  ww .  java2s .c om*/
    }
    return null;
}

From source file:com.indragie.cmput301as1.JSONHelpers.java

License:Open Source License

/**
 * Gets the value of the JSON element as a long if possible.
 * @param element The JSON element./*  w w  w  . j a  v a 2  s .c  o  m*/
 * @return A string if the JSON element was a long, or
 * 0 otherwise.
 */
public static long getLongIfPossible(JsonElement element) {
    if (element == null)
        return 0;

    if (element.isJsonPrimitive()) {
        JsonPrimitive primitive = element.getAsJsonPrimitive();
        if (primitive.isNumber()) {
            return primitive.getAsLong();
        }
    }
    return 0;
}

From source file:com.jayway.jsonpath.internal.spi.mapper.GsonMapper.java

License:Apache License

@Override
public Object convert(Object src, Class<?> srcType, Class<?> targetType, Configuration conf) {

    assertValidConversion(src, srcType, targetType);

    if (src == null || src.getClass().equals(JsonNull.class)) {
        return null;
    }//from   www  .  ja v a 2s.  co m

    if (JsonPrimitive.class.isAssignableFrom(srcType)) {

        JsonPrimitive primitive = (JsonPrimitive) src;
        if (targetType.equals(Long.class)) {
            return primitive.getAsLong();
        } else if (targetType.equals(Integer.class)) {
            return primitive.getAsInt();
        } else if (targetType.equals(BigInteger.class)) {
            return primitive.getAsBigInteger();
        } else if (targetType.equals(Byte.class)) {
            return primitive.getAsByte();
        } else if (targetType.equals(BigDecimal.class)) {
            return primitive.getAsBigDecimal();
        } else if (targetType.equals(Double.class)) {
            return primitive.getAsDouble();
        } else if (targetType.equals(Float.class)) {
            return primitive.getAsFloat();
        } else if (targetType.equals(String.class)) {
            return primitive.getAsString();
        } else if (targetType.equals(Boolean.class)) {
            return primitive.getAsBoolean();
        } else if (targetType.equals(Date.class)) {

            if (primitive.isNumber()) {
                return new Date(primitive.getAsLong());
            } else if (primitive.isString()) {
                try {
                    return DateFormat.getInstance().parse(primitive.getAsString());
                } catch (ParseException e) {
                    throw new MappingException(e);
                }
            }
        }

    } else if (JsonObject.class.isAssignableFrom(srcType)) {
        JsonObject srcObject = (JsonObject) src;
        if (targetType.equals(Map.class)) {
            Map<String, Object> targetMap = new LinkedHashMap<String, Object>();
            for (Map.Entry<String, JsonElement> entry : srcObject.entrySet()) {
                Object val = null;
                JsonElement element = entry.getValue();
                if (element.isJsonPrimitive()) {
                    val = GsonJsonProvider.unwrap(element);
                } else if (element.isJsonArray()) {
                    val = convert(element, element.getClass(), List.class, conf);
                } else if (element.isJsonObject()) {
                    val = convert(element, element.getClass(), Map.class, conf);
                } else if (element.isJsonNull()) {
                    val = null;
                }
                targetMap.put(entry.getKey(), val);
            }
            return targetMap;
        }

    } else if (JsonArray.class.isAssignableFrom(srcType)) {
        JsonArray srcArray = (JsonArray) src;
        if (targetType.equals(List.class)) {
            List<Object> targetList = new ArrayList<Object>();
            for (JsonElement element : srcArray) {
                if (element.isJsonPrimitive()) {
                    targetList.add(GsonJsonProvider.unwrap(element));
                } else if (element.isJsonArray()) {
                    targetList.add(convert(element, element.getClass(), List.class, conf));
                } else if (element.isJsonObject()) {
                    targetList.add(convert(element, element.getClass(), Map.class, conf));
                } else if (element.isJsonNull()) {
                    targetList.add(null);
                }
            }
            return targetList;
        }
    }

    throw new MappingException("Can not map: " + srcType.getName() + " to: " + targetType.getName());
}

From source file:com.liferay.document.library.document.conversion.internal.ImageRequestTokenUtil.java

License:Open Source License

public static long getUserId(String tokenString) {
    try {//from   w  w w . j  a  v a 2 s. c  o  m
        JsonTokenParser jsonTokenParser = getJsonTokenParser();

        JsonToken jsonToken = jsonTokenParser.verifyAndDeserialize(tokenString);

        JsonPrimitive userIdJsonPrimitive = jsonToken.getParamAsPrimitive("userId");

        if (userIdJsonPrimitive == null) {
            if (_log.isDebugEnabled()) {
                _log.debug("Unable to find \"userId\" parameter in payload " + tokenString);
            }

            return 0;
        }

        long userId = userIdJsonPrimitive.getAsLong();

        User user = UserLocalServiceUtil.fetchUser(userId);

        Date passwordModifiedDate = user.getPasswordModifiedDate();

        if (passwordModifiedDate != null) {
            Instant instant = jsonToken.getIssuedAt();

            if (instant.isBefore(passwordModifiedDate.getTime())) {
                if (_log.isDebugEnabled()) {
                    _log.debug("Unable to accept token because the password was " + "changed");
                }

                return 0;
            }
        }

        return userId;
    } catch (Exception e) {
        if (_log.isDebugEnabled()) {
            _log.debug("Unable to parse and verify token " + tokenString, e);
        }

        return 0;
    }
}

From source file:com.liferay.sync.hook.security.auth.verifier.SyncAuthVerifier.java

License:Open Source License

public String getUserId(String tokenString) {
    try {/*from  w  w w .  ja  v  a  2  s .  co m*/
        JsonTokenParser jsonTokenParser = getJsonTokenParser();

        JsonToken jsonToken = jsonTokenParser.verifyAndDeserialize(tokenString);

        JsonPrimitive userIdJsonPrimitive = jsonToken.getParamAsPrimitive("userId");

        if (userIdJsonPrimitive == null) {
            return null;
        }

        long userId = userIdJsonPrimitive.getAsLong();

        User user = UserLocalServiceUtil.fetchUser(userId);

        Date passwordModifiedDate = user.getPasswordModifiedDate();

        if (passwordModifiedDate != null) {
            Instant instant = jsonToken.getIssuedAt();

            if (instant.isBefore(passwordModifiedDate.getTime())) {
                return null;
            }
        }

        return String.valueOf(userId);
    } catch (Exception e) {
        return null;
    }
}

From source file:com.liferay.sync.security.auth.verifier.SyncAuthVerifier.java

License:Open Source License

public String getUserId(String tokenString) {
    try {/* w  w  w .  j a  v  a  2 s. co m*/
        JsonTokenParser jsonTokenParser = getJsonTokenParser();

        JsonToken jsonToken = jsonTokenParser.verifyAndDeserialize(tokenString);

        JsonPrimitive userIdJsonPrimitive = jsonToken.getParamAsPrimitive("userId");

        if (userIdJsonPrimitive == null) {
            return null;
        }

        long userId = userIdJsonPrimitive.getAsLong();

        User user = _userLocalService.fetchUser(userId);

        Date passwordModifiedDate = user.getPasswordModifiedDate();

        if (passwordModifiedDate != null) {
            Instant instant = jsonToken.getIssuedAt();

            if (instant.isBefore(passwordModifiedDate.getTime())) {
                return null;
            }
        }

        return String.valueOf(userId);
    } catch (Exception e) {
        return null;
    }
}

From source file:com.microsoft.windowsazure.mobileservices.table.MobileServiceTableBase.java

License:Open Source License

/**
 * Updates the JsonObject to have an id property
 *
 * @param json the element to evaluate/*from w  w w .  ja v a 2  s.  c o m*/
 */
protected void updateIdProperty(final JsonObject json) throws IllegalArgumentException {
    for (Entry<String, JsonElement> entry : json.entrySet()) {
        String key = entry.getKey();

        if (key.equalsIgnoreCase("id")) {
            JsonElement element = entry.getValue();

            if (isValidTypeId(element)) {
                if (!key.equals("id")) {
                    // force the id name to 'id', no matter the casing
                    json.remove(key);
                    // Create a new id property using the given property
                    // name

                    JsonPrimitive value = entry.getValue().getAsJsonPrimitive();
                    if (value.isNumber()) {
                        json.addProperty("id", value.getAsLong());
                    } else {
                        json.addProperty("id", value.getAsString());
                    }
                }

                return;
            } else {
                throw new IllegalArgumentException("The id must be numeric or string");
            }
        }
    }
}

From source file:com.microsoft.windowsazure.mobileservices.table.MobileServiceTableBase.java

License:Open Source License

/**
 * Returns the numeric value represented by the object.
 *
 * @param o//w  w w .j a v  a 2 s .c o m
 * @return
 */
protected long getNumericValue(Object o) {
    long result;

    if (o instanceof Integer) {
        result = (Integer) o;
    } else if (o instanceof Long) {
        result = (Long) o;
    } else if (o instanceof JsonElement) {
        JsonElement json = (JsonElement) o;

        if (json.isJsonPrimitive()) {
            JsonPrimitive primitive = json.getAsJsonPrimitive();

            if (primitive.isNumber()) {
                result = primitive.getAsLong();
            } else {
                throw new IllegalArgumentException("Object does not represent a string value.");
            }
        } else {
            throw new IllegalArgumentException("Object does not represent a string value.");
        }
    } else {
        throw new IllegalArgumentException("Object does not represent a string value.");
    }

    return result;
}

From source file:com.pinterest.deployservice.rodimus.RodimusManagerImpl.java

License:Apache License

@Override
public Long getClusterInstanceLaunchGracePeriod(String clusterName) throws Exception {
    String url = String.format("%s/v1/groups/%s/config", rodimusUrl, clusterName);
    String res = httpClient.get(url, null, null, headers, RETRIES);
    JsonObject jsonObject = gson.fromJson(res, JsonObject.class);
    if (jsonObject == null || jsonObject.isJsonNull()) {
        return null;
    }/*from ww  w  . ja va  2  s  .  co m*/

    JsonPrimitive launchGracePeriod = jsonObject.getAsJsonPrimitive("launchLatencyTh");
    if (launchGracePeriod == null || launchGracePeriod.isJsonNull()) {
        return null;
    }

    return launchGracePeriod.getAsLong();
}

From source file:com.qmetry.qaf.automation.gson.GsonObjectDeserializer.java

License:Open Source License

public static Object read(JsonElement in) {

    if (in.isJsonArray()) {
        List<Object> list = new ArrayList<Object>();
        JsonArray arr = in.getAsJsonArray();
        for (JsonElement anArr : arr) {
            list.add(read(anArr));/*from w w  w.  ja v a 2  s .  co  m*/
        }
        return list;
    } else if (in.isJsonObject()) {
        Map<String, Object> map = new LinkedTreeMap<String, Object>();
        JsonObject obj = in.getAsJsonObject();
        Set<Map.Entry<String, JsonElement>> entitySet = obj.entrySet();
        for (Map.Entry<String, JsonElement> entry : entitySet) {
            map.put(entry.getKey(), read(entry.getValue()));
        }
        return map;
    } else if (in.isJsonPrimitive()) {
        JsonPrimitive prim = in.getAsJsonPrimitive();
        if (prim.isBoolean()) {
            return prim.getAsBoolean();
        } else if (prim.isString()) {
            return prim.getAsString();
        } else if (prim.isNumber()) {
            if (prim.getAsString().contains("."))
                return prim.getAsDouble();
            else {
                return prim.getAsLong();
            }
        }
    }
    return null;
}