Example usage for com.google.gson JsonObject getAsJsonPrimitive

List of usage examples for com.google.gson JsonObject getAsJsonPrimitive

Introduction

In this page you can find the example usage for com.google.gson JsonObject getAsJsonPrimitive.

Prototype

public JsonPrimitive getAsJsonPrimitive(String memberName) 

Source Link

Document

Convenience method to get the specified member as a JsonPrimitive element.

Usage

From source file:club.jmint.crossing.specs.CrossingService.java

License:Apache License

protected String getParamAsString(JsonObject params, String key) throws CrossException {
    if (params != null) {
        if (!params.has(key)) {
            throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MISSING.getCode(),
                    ErrorCode.COMMON_ERR_PARAM_MISSING.getInfo());
        }/* www  .  j a  va  2 s . c  o  m*/
        return params.getAsJsonPrimitive(key).getAsString();
    }
    return null;
}

From source file:club.jmint.crossing.specs.CrossingService.java

License:Apache License

protected int getParamAsInt(JsonObject params, String key) throws CrossException {
    if (params != null) {
        if (!params.has(key)) {
            throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MISSING.getCode(),
                    ErrorCode.COMMON_ERR_PARAM_MISSING.getInfo());
        }/*from www.  java2 s  .  c  om*/
        return params.getAsJsonPrimitive(key).getAsInt();
    }
    return -1;
}

From source file:club.jmint.crossing.specs.CrossingService.java

License:Apache License

protected boolean getParamAsBoolean(JsonObject params, String key) throws CrossException {
    if (params != null) {
        if (!params.has(key)) {
            throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MISSING.getCode(),
                    ErrorCode.COMMON_ERR_PARAM_MISSING.getInfo());
        }//from   w ww  .j  a  v a  2 s.c o m
        return params.getAsJsonPrimitive(key).getAsBoolean();
    }
    return false;
}

From source file:club.jmint.crossing.specs.CrossingService.java

License:Apache License

protected long getParamAsLong(JsonObject params, String key) throws CrossException {
    if (params != null) {
        if (!params.has(key)) {
            throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MISSING.getCode(),
                    ErrorCode.COMMON_ERR_PARAM_MISSING.getInfo());
        }/*  w ww  .j a  v a  2 s  . co m*/
        return params.getAsJsonPrimitive(key).getAsLong();
    }
    return -1;
}

From source file:club.jmint.crossing.specs.ParamBuilder.java

License:Apache License

public static String getErrorInfo(String jsonParams) throws CrossException {
    JsonParser jp = new JsonParser();
    JsonObject jo;
    try {//from   www  .  j  a  v  a  2 s  .co  m
        jo = (JsonObject) jp.parse(jsonParams);
    } catch (JsonSyntaxException e) {
        throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(),
                ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo());
    }
    if (jo.has("errorInfo")) {
        String value = jo.getAsJsonPrimitive("errorInfo").getAsString();
        return value;
    } else {
        throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MISSING.getCode(),
                ErrorCode.COMMON_ERR_PARAM_MISSING.getInfo());
    }
}

From source file:club.jmint.crossing.specs.ParamBuilder.java

License:Apache License

public static int getErrorCode(String jsonParams) throws CrossException {
    JsonParser jp = new JsonParser();
    JsonObject jo;
    try {/*  w w w .j a  v a 2 s  .com*/
        jo = (JsonObject) jp.parse(jsonParams);
    } catch (JsonSyntaxException e) {
        throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(),
                ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo());
    }
    if (jo.has("errorCode")) {
        String value = jo.getAsJsonPrimitive("errorCode").getAsString();
        return Integer.parseInt(value);
    } else {
        throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MISSING.getCode(),
                ErrorCode.COMMON_ERR_PARAM_MISSING.getInfo());
    }
}

From source file:club.jmint.crossing.specs.ParamBuilder.java

License:Apache License

public static String checkSignAndRemove(String p, String signKey) throws CrossException {
    JsonParser jp = new JsonParser();
    JsonElement jop = null;/*from  w  ww  .  j  ava  2s.  c  om*/
    try {
        jop = jp.parse(p);
        if (!jop.isJsonObject()) {
            throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(),
                    ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo());
        }
    } catch (JsonSyntaxException jse) {
        //jse.printStackTrace();
        throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(),
                ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo());
    }

    JsonObject jo = (JsonObject) jop;
    String np = null;
    if (jo.has("sign") || jo.has("params")) {
        String sign = jo.getAsJsonPrimitive("sign").getAsString();
        np = jo.getAsJsonObject("params").toString();
        String signValue = Security.crossingSign(np, signKey, "");
        if (!sign.equals(signValue)) {
            throw new CrossException(ErrorCode.COMMON_ERR_SIGN_BAD.getCode(),
                    ErrorCode.COMMON_ERR_SIGN_BAD.getInfo());
        }

        //remove sign field only and return
        Iterator<Entry<String, JsonElement>> it = jo.entrySet().iterator();
        JsonObject rjo = new JsonObject();
        Entry<String, JsonElement> en;
        while (it.hasNext()) {
            en = it.next();
            if (!en.getKey().equals("sign")) {
                rjo.add(en.getKey(), en.getValue());
            }
        }
        return rjo.toString();
    }

    return p;
}

From source file:club.jmint.crossing.specs.ParamBuilder.java

License:Apache License

public static String buildDecryptedParams(String encryptParams, String decryptKey) throws CrossException {
    JsonParser jp = new JsonParser();
    JsonElement jop = null;//from   w ww .  ja  v  a2s  . com
    try {
        jop = jp.parse(encryptParams);
        if (!jop.isJsonObject()) {
            throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(),
                    ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo());
        }
    } catch (JsonSyntaxException jse) {
        //jse.printStackTrace();
        throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(),
                ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo());
    }

    JsonObject jo = (JsonObject) jop;
    String np = null;
    if (jo.has("encrypted")) {
        String encrypted = jo.getAsJsonPrimitive("encrypted").getAsString();
        String paramsValue = Security.crossingDecrypt(encrypted, decryptKey, "DES");
        int ec = jo.getAsJsonPrimitive("errorCode").getAsInt();
        String ed = jo.getAsJsonPrimitive("errorInfo").getAsString();
        JsonParser jpr = new JsonParser();
        JsonObject jor = null;
        try {
            jor = (JsonObject) jpr.parse(paramsValue);
        } catch (JsonSyntaxException je) {
            throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(),
                    ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo());
        }
        jor.addProperty("errorCode", ec);
        jor.addProperty("errorInfo", ed);
        np = jor.toString();
        return np;
    }

    return encryptParams;
}

From source file:co.cask.cdap.proto.codec.AuditMessageTypeAdapter.java

License:Apache License

@Override
public AuditMessage deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObj = json.getAsJsonObject();
    long timeMillis = jsonObj.get("time").getAsLong();
    EntityId entityId = context.deserialize(jsonObj.getAsJsonObject("entityId"), EntityId.class);
    String user = jsonObj.get("user").getAsString();
    AuditType auditType = context.deserialize(jsonObj.getAsJsonPrimitive("type"), AuditType.class);

    AuditPayload payload;/*w  ww .  j av a2  s .  co m*/
    JsonObject jsonPayload = jsonObj.getAsJsonObject("payload");
    switch (auditType) {
    case METADATA_CHANGE:
        payload = context.deserialize(jsonPayload, MetadataPayload.class);
        break;
    case ACCESS:
        payload = context.deserialize(jsonPayload, AccessPayload.class);
        break;
    default:
        payload = AuditPayload.EMPTY_PAYLOAD;
    }
    return new AuditMessage(timeMillis, entityId, user, auditType, payload);
}

From source file:com.adkdevelopment.jokesactivity.JokesActivityFragment.java

License:Open Source License

/**
 * Method to parse JSON string and return String[] with a link and a title
 *
 * @param jokesJson JSON string with info about comics, link, etc.
 * @return String[] with a link and a title
 *///  w  w w.ja v  a 2s . c o m
public static String[] getJokesInfo(String jokesJson) {

    String[] reviews = new String[2];

    JsonParser parser = new JsonParser();

    JsonElement element = parser.parse(jokesJson);

    if (element.isJsonObject()) {
        JsonObject results = element.getAsJsonObject();
        JsonElement title = results.getAsJsonPrimitive("title");
        JsonElement linkToImage = results.getAsJsonPrimitive("img");

        reviews[0] = title.getAsString();
        reviews[1] = linkToImage.getAsString();

        return reviews;
    }

    return null;
}